chatgui: channel sorting by name or last message time with preview

- Add last_msg_at column to channels table
- Update last_msg_at on every message insert (send/receive)
- Parameterize getChannels() with ChannelSortMode enum (Alphabetical/Recent)
- Add getChannelLastMessage() for preview text in channel list
- Add sorting combo to Profile settings page (stored in ui_state)
- Reload channels on message receive when sort is recent
This commit is contained in:
Evgeny committed 2026-07-26 11:08:59 +03:00
1 parent 332df381d1
commit 49da899964
10 files changed
+105 -12

No files matched your search

+31 -5
View File
@@ -105,13 +105,17 @@ QByteArray DbManager::colBlob(sqlite3_stmt* stmt, int col) {
/* ── channels ── */
QList<ChannelRow> DbManager::getChannels() const {
QList<ChannelRow> DbManager::getChannels(ChannelSortMode sort) const {
QList<ChannelRow> list;
sqlite3_stmt* stmt = prepareOrNull(
const char* orderBy = (sort == ChannelSortMode::Recent)
? " ORDER BY last_msg_at DESC, name COLLATE NOCASE ASC"
: " ORDER BY name COLLATE NOCASE ASC";
char sql[512]; snprintf(sql, sizeof(sql),
"SELECT channel_id, name, owner_node_id,"
" x25519_pubkey, x25519_privkey, ed25519_pubkey, ed25519_privkey, signature,"
" last_read_msg_id, last_pos_msg_id, created_at"
" FROM channels ORDER BY created_at ASC");
" last_read_msg_id, last_pos_msg_id, last_msg_at, created_at"
" FROM channels%s", orderBy);
sqlite3_stmt* stmt = prepareOrNull(sql);
if (!stmt) return list;
while (sqlite3_step(stmt) == SQLITE_ROW) {
ChannelRow r;
@@ -125,7 +129,8 @@ QList<ChannelRow> DbManager::getChannels() const {
r.signature = colBlob(stmt, 7);
r.lastReadMsgId = sqlite3_column_int64(stmt, 8);
r.lastPosMsgId = sqlite3_column_int64(stmt, 9);
r.createdAt = sqlite3_column_int64(stmt, 10);
r.lastMsgAt = sqlite3_column_int64(stmt, 10);
r.createdAt = sqlite3_column_int64(stmt, 11);
list.append(r);
}
sqlite3_finalize(stmt);
@@ -139,6 +144,27 @@ QList<ChannelRow> DbManager::getChannels() const {
return list;
}
QString DbManager::getChannelLastMessage(const QString& channelId) const {
QString sql = QStringLiteral(
"SELECT node_id, data FROM \"%1\" ORDER BY id DESC LIMIT 1")
.arg(msgTableName(channelId));
sqlite3_stmt* stmt = prepareOrNull(sql.toUtf8().constData());
if (!stmt) return {};
QString result;
if (sqlite3_step(stmt) == SQLITE_ROW) {
quint64 author = (quint64)sqlite3_column_int64(stmt, 0);
QByteArray jdata = colBlob(stmt, 1);
QString text = QString::fromUtf8(parseJsonField(jdata, "d"));
if (text.isEmpty()) text = QString::fromUtf8(parseJsonField(jdata, "ct"));
QString nick = getDisplayName(author);
QString shortText = text.left(60);
if (text.size() > 60) shortText += QStringLiteral("...");
result = nick + QStringLiteral(": ") + shortText;
}
sqlite3_finalize(stmt);
return result;
}
/* ── messages (UI) ── */
QList<MessageRow> DbManager::getMessages(const QString& chId, int limit) const {