chat: превью медиасообщений в списке каналов (Android + desktop)
This commit is contained in:
1 parent
07ef7d9a01
commit
ff53c735dd
2 files changed
+105
-9
No files matched your search
@@ -144,6 +144,54 @@ QList<ChannelRow> DbManager::getChannels(ChannelSortMode sort) const {
|
||||
return list;
|
||||
}
|
||||
|
||||
/* Формирует человекочитаемое тело превью последнего сообщения по content_type и "d"-полю. */
|
||||
static QString mediaPreviewBody(const QString& ct, const QByteArray& d) {
|
||||
bool isText = ct.isEmpty() || ct == QStringLiteral("text/plain") ||
|
||||
ct == QStringLiteral("voice_transcription");
|
||||
if (isText) {
|
||||
QString t = QString::fromUtf8(d);
|
||||
if (t.size() > 60) { t = t.left(60); t += QStringLiteral("..."); }
|
||||
return t;
|
||||
}
|
||||
if (ct == QStringLiteral("audio/opus")) {
|
||||
int durMs = 0;
|
||||
if (d.startsWith('*')) {
|
||||
int pipe = d.indexOf('|');
|
||||
if (pipe > 1) {
|
||||
QByteArray b64 = d.mid(1, pipe - 1);
|
||||
b64.replace('-', '+');
|
||||
b64.replace('_', '/');
|
||||
QString raw = QString::fromUtf8(QByteArray::fromBase64(b64));
|
||||
int durPos = raw.indexOf(QStringLiteral(";dur="));
|
||||
if (durPos >= 0) {
|
||||
int end = raw.indexOf(';', durPos + 5);
|
||||
QString ds = (end > 0) ? raw.mid(durPos + 5, end - durPos - 5)
|
||||
: raw.mid(durPos + 5);
|
||||
durMs = ds.toInt();
|
||||
}
|
||||
}
|
||||
}
|
||||
if (durMs > 0) {
|
||||
int s = durMs / 1000;
|
||||
return QStringLiteral("Voice message (%1:%2)")
|
||||
.arg(s / 60).arg(s % 60, 2, 10, QLatin1Char('0'));
|
||||
}
|
||||
return QStringLiteral("Voice message");
|
||||
}
|
||||
if (ct.startsWith(QStringLiteral("video/"))) return QStringLiteral("Video");
|
||||
if (ct.startsWith(QStringLiteral("image/"))) return QStringLiteral("Photo");
|
||||
/* файл и прочие нетекстовые: base64-префикс — имя файла */
|
||||
if (d.startsWith('*')) {
|
||||
int pipe = d.indexOf('|');
|
||||
QByteArray b64 = d.mid(1, (pipe > 1) ? pipe - 1 : d.size() - 1);
|
||||
b64.replace('-', '+');
|
||||
b64.replace('_', '/');
|
||||
QString fname = QString::fromUtf8(QByteArray::fromBase64(b64));
|
||||
if (!fname.isEmpty()) return fname;
|
||||
}
|
||||
return QStringLiteral("File");
|
||||
}
|
||||
|
||||
QString DbManager::getChannelLastMessage(const QString& channelId) const {
|
||||
QString sql = QStringLiteral(
|
||||
"SELECT node_id, data FROM \"%1\" ORDER BY id DESC LIMIT 1")
|
||||
@@ -154,12 +202,10 @@ QString DbManager::getChannelLastMessage(const QString& channelId) const {
|
||||
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 ct = QString::fromUtf8(parseJsonField(jdata, "ct"));
|
||||
QByteArray d = parseJsonField(jdata, "d");
|
||||
QString nick = getDisplayName(author);
|
||||
QString shortText = text.left(60);
|
||||
if (text.size() > 60) shortText += QStringLiteral("...");
|
||||
result = nick + QStringLiteral(": ") + shortText;
|
||||
result = nick + QStringLiteral(": ") + mediaPreviewBody(ct, d);
|
||||
}
|
||||
sqlite3_finalize(stmt);
|
||||
return result;
|
||||
|
||||
Reference in new issue
Block a user