fix: update or delete news on newsModel update (#2007)

* fix: update or delete news on newsModel update

* update: changed check for news editing

* update: changed news edit updating

* update: changed news model updating method

* chore: add rich text support for news page

---------

Co-authored-by: vkamn <vk@amnezia.org>
This commit is contained in:
MrMirDan
2025-12-01 14:23:14 +02:00
committed by GitHub
parent 105c42db1c
commit c81ae2b060
2 changed files with 24 additions and 18 deletions
+12 -18
View File
@@ -78,34 +78,28 @@ void NewsModel::setProcessedIndex(int index)
void NewsModel::updateModel(const QJsonArray &serverItems)
{
QSet<QString> existingIds;
for (const NewsItem &item : m_items) {
existingIds.insert(item.id);
}
QList<NewsItem> updatedItems;
QList<NewsItem> newItems;
for (const QJsonValue &value : serverItems) {
if (!value.isObject())
continue;
const QJsonObject obj = value.toObject();
QString id = obj.value("id").toString();
if (!existingIds.contains(id)) {
NewsItem item;
item.id = id;
item.title = obj.value("title").toString();
item.content = obj.value("content").toString();
item.timestamp = QDateTime::fromString(obj.value("timestamp").toString(), Qt::ISODate);
item.read = m_readIds.contains(id);
newItems.append(item);
existingIds.insert(id);
}
QJsonObject object = value.toObject();
NewsItem item;
item.id = object.value("id").toString();
item.title = object.value("title").toString();
item.content = object.value("content").toString();
item.timestamp = QDateTime::fromString(object.value("timestamp").toString(), Qt::ISODate);
item.read = m_readIds.contains(object.value("id").toString());
updatedItems.append(item);
}
beginResetModel();
m_items.append(newItems);
m_items = updatedItems;
std::sort(m_items.begin(), m_items.end(), [](const NewsItem &a, const NewsItem &b) { return a.timestamp > b.timestamp; });
endResetModel();
loadReadIds();
emit hasUnreadChanged();
}
@@ -63,6 +63,18 @@ PageType {
Layout.leftMargin: 16
Layout.rightMargin: 16
text: newsItem.content
textFormat: Text.RichText
onLinkActivated: function(link) {
Qt.openUrlExternally(link)
}
MouseArea {
anchors.fill: parent
acceptedButtons: Qt.NoButton
cursorShape: parent.hoveredLink ? Qt.PointingHandCursor : Qt.ArrowCursor
}
}
}
}