Refactor utils::getMessageDescription
This commit is contained in:
parent
b67024149c
commit
95120c602b
@ -19,12 +19,11 @@
|
||||
|
||||
#include <QDebug>
|
||||
#include <QDir>
|
||||
#include <QImage>
|
||||
#include <json.hpp>
|
||||
#include <lmdb++.h>
|
||||
#include <mtx/responses.hpp>
|
||||
|
||||
#include "Utils.h"
|
||||
|
||||
struct SearchResult
|
||||
{
|
||||
QString user_id;
|
||||
|
@ -23,7 +23,6 @@
|
||||
#include <QVBoxLayout>
|
||||
#include <QWidget>
|
||||
|
||||
#include "Cache.h"
|
||||
#include <mtx.hpp>
|
||||
|
||||
class LeaveRoomDialog;
|
||||
@ -32,7 +31,9 @@ class OverlayModal;
|
||||
class RoomInfoListItem;
|
||||
class Sync;
|
||||
class UserSettings;
|
||||
class Cache;
|
||||
struct DescInfo;
|
||||
struct RoomInfo;
|
||||
|
||||
class RoomList : public QWidget
|
||||
{
|
||||
|
@ -1,6 +1,11 @@
|
||||
#pragma once
|
||||
|
||||
#include "Cache.h"
|
||||
#include "RoomInfoListItem.h"
|
||||
#include "timeline/widgets/AudioItem.h"
|
||||
#include "timeline/widgets/FileItem.h"
|
||||
#include "timeline/widgets/ImageItem.h"
|
||||
#include "timeline/widgets/VideoItem.h"
|
||||
|
||||
#include <QDateTime>
|
||||
#include <mtx/events/collections.hpp>
|
||||
@ -27,6 +32,64 @@ firstChar(const QString &input);
|
||||
QString
|
||||
humanReadableFileSize(uint64_t bytes);
|
||||
|
||||
//! Match widgets/events with a description message.
|
||||
template<class T>
|
||||
QString
|
||||
messageDescription(const QString &username = "", const QString &body = "")
|
||||
{
|
||||
using Audio = mtx::events::RoomEvent<mtx::events::msg::Audio>;
|
||||
using Emote = mtx::events::RoomEvent<mtx::events::msg::Emote>;
|
||||
using File = mtx::events::RoomEvent<mtx::events::msg::File>;
|
||||
using Image = mtx::events::RoomEvent<mtx::events::msg::Image>;
|
||||
using Notice = mtx::events::RoomEvent<mtx::events::msg::Notice>;
|
||||
using Sticker = mtx::events::Sticker;
|
||||
using Text = mtx::events::RoomEvent<mtx::events::msg::Text>;
|
||||
using Video = mtx::events::RoomEvent<mtx::events::msg::Video>;
|
||||
|
||||
if (std::is_same<T, AudioItem>::value || std::is_same<T, Audio>::value)
|
||||
return QString("sent an audio clip");
|
||||
else if (std::is_same<T, ImageItem>::value || std::is_same<T, Image>::value)
|
||||
return QString("sent an image");
|
||||
else if (std::is_same<T, FileItem>::value || std::is_same<T, File>::value)
|
||||
return QString("sent a file");
|
||||
else if (std::is_same<T, VideoItem>::value || std::is_same<T, Video>::value)
|
||||
return QString("sent a video clip");
|
||||
else if (std::is_same<T, StickerItem>::value || std::is_same<T, Sticker>::value)
|
||||
return QString("sent a sticker");
|
||||
else if (std::is_same<T, Notice>::value)
|
||||
return QString("sent a notification");
|
||||
else if (std::is_same<T, Text>::value)
|
||||
return QString(": %1").arg(body);
|
||||
else if (std::is_same<T, Emote>::value)
|
||||
return QString("* %1 %2").arg(username).arg(body);
|
||||
}
|
||||
|
||||
template<class T, class Event>
|
||||
DescInfo
|
||||
createDescriptionInfo(const Event &event, const QString &localUser, const QString &room_id)
|
||||
{
|
||||
using Text = mtx::events::RoomEvent<mtx::events::msg::Text>;
|
||||
using Emote = mtx::events::RoomEvent<mtx::events::msg::Emote>;
|
||||
|
||||
const auto msg = mpark::get<T>(event);
|
||||
const auto sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
bool isText = std::is_same<T, Text>::value;
|
||||
bool isEmote = std::is_same<T, Emote>::value;
|
||||
|
||||
return DescInfo{
|
||||
isEmote ? "" : (sender == localUser ? "You" : username),
|
||||
sender,
|
||||
(isText || isEmote)
|
||||
? messageDescription<T>(username, QString::fromStdString(msg.content.body).trimmed())
|
||||
: QString(" %1").arg(messageDescription<T>()),
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
}
|
||||
|
||||
//! Scale down an image to fit to the given width & height limitations.
|
||||
template<class ImageType>
|
||||
ImageType
|
||||
|
@ -26,11 +26,12 @@
|
||||
#include <QStyleOption>
|
||||
|
||||
#include "AvatarProvider.h"
|
||||
#include "Cache.h"
|
||||
#include "ChatPage.h"
|
||||
#include "RoomInfoListItem.h"
|
||||
#include "Utils.h"
|
||||
|
||||
#include "Cache.h"
|
||||
|
||||
class ImageItem;
|
||||
class StickerItem;
|
||||
class AudioItem;
|
||||
@ -136,16 +137,10 @@ private:
|
||||
void addSaveImageAction(ImageItem *image);
|
||||
|
||||
template<class Widget>
|
||||
void setupLocalWidgetLayout(Widget *widget,
|
||||
const QString &userid,
|
||||
const QString &msgDescription,
|
||||
bool withSender);
|
||||
void setupLocalWidgetLayout(Widget *widget, const QString &userid, bool withSender);
|
||||
|
||||
template<class Event, class Widget>
|
||||
void setupWidgetLayout(Widget *widget,
|
||||
const Event &event,
|
||||
const QString &msgDescription,
|
||||
bool withSender);
|
||||
void setupWidgetLayout(Widget *widget, const Event &event, bool withSender);
|
||||
|
||||
void generateBody(const QString &body);
|
||||
void generateBody(const QString &userid, const QString &body);
|
||||
@ -184,17 +179,14 @@ private:
|
||||
|
||||
template<class Widget>
|
||||
void
|
||||
TimelineItem::setupLocalWidgetLayout(Widget *widget,
|
||||
const QString &userid,
|
||||
const QString &msgDescription,
|
||||
bool withSender)
|
||||
TimelineItem::setupLocalWidgetLayout(Widget *widget, const QString &userid, bool withSender)
|
||||
{
|
||||
auto displayName = Cache::displayName(room_id_, userid);
|
||||
auto timestamp = QDateTime::currentDateTime();
|
||||
|
||||
descriptionMsg_ = {"You",
|
||||
userid,
|
||||
QString(" %1").arg(msgDescription),
|
||||
QString(" %1").arg(utils::messageDescription<Widget>()),
|
||||
utils::descriptiveTime(timestamp),
|
||||
timestamp};
|
||||
|
||||
@ -227,10 +219,7 @@ TimelineItem::setupLocalWidgetLayout(Widget *widget,
|
||||
|
||||
template<class Event, class Widget>
|
||||
void
|
||||
TimelineItem::setupWidgetLayout(Widget *widget,
|
||||
const Event &event,
|
||||
const QString &msgDescription,
|
||||
bool withSender)
|
||||
TimelineItem::setupWidgetLayout(Widget *widget, const Event &event, bool withSender)
|
||||
{
|
||||
init();
|
||||
|
||||
@ -243,7 +232,7 @@ TimelineItem::setupWidgetLayout(Widget *widget,
|
||||
QSettings settings;
|
||||
descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName,
|
||||
sender,
|
||||
msgDescription,
|
||||
QString(" %1").arg(utils::messageDescription<Widget>()),
|
||||
utils::descriptiveTime(timestamp),
|
||||
timestamp};
|
||||
|
||||
|
@ -26,6 +26,7 @@
|
||||
#include <variant.hpp>
|
||||
|
||||
#include "Cache.h"
|
||||
#include "Utils.h"
|
||||
|
||||
//! Should be changed when a breaking change occurs in the cache format.
|
||||
//! This will reset client's data.
|
||||
|
@ -27,6 +27,7 @@
|
||||
#include "RoomInfoListItem.h"
|
||||
#include "RoomList.h"
|
||||
#include "UserSettingsPage.h"
|
||||
#include "Utils.h"
|
||||
|
||||
RoomList::RoomList(QSharedPointer<MatrixClient> client,
|
||||
QSharedPointer<UserSettings> userSettings,
|
||||
|
97
src/Utils.cc
97
src/Utils.cc
@ -1,7 +1,5 @@
|
||||
#include "Utils.h"
|
||||
|
||||
#include "Cache.h"
|
||||
|
||||
#include <variant.hpp>
|
||||
|
||||
using TimelineEvent = mtx::events::collections::TimelineEvents;
|
||||
@ -36,100 +34,21 @@ utils::getMessageDescription(const TimelineEvent &event,
|
||||
using Video = mtx::events::RoomEvent<mtx::events::msg::Video>;
|
||||
|
||||
if (mpark::holds_alternative<Audio>(event)) {
|
||||
const auto msg = mpark::get<Audio>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
return DescInfo{sender == localUser ? "You" : username,
|
||||
sender,
|
||||
" sent an audio clip",
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<Audio>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<Emote>(event)) {
|
||||
auto msg = mpark::get<Emote>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
const auto body = QString::fromStdString(msg.content.body).trimmed();
|
||||
|
||||
return DescInfo{"",
|
||||
sender,
|
||||
QString("* %1 %2").arg(username).arg(body),
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<Emote>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<File>(event)) {
|
||||
const auto msg = mpark::get<File>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
return DescInfo{sender == localUser ? "You" : username,
|
||||
sender,
|
||||
" sent a file",
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<File>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<Image>(event)) {
|
||||
const auto msg = mpark::get<Image>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
return DescInfo{sender == localUser ? "You" : username,
|
||||
sender,
|
||||
" sent an image",
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<Image>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<Notice>(event)) {
|
||||
const auto msg = mpark::get<Notice>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
return DescInfo{
|
||||
username, sender, " sent a notification", utils::descriptiveTime(ts), ts};
|
||||
return createDescriptionInfo<Notice>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<Text>(event)) {
|
||||
const auto msg = mpark::get<Text>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
const auto body = QString::fromStdString(msg.content.body).trimmed();
|
||||
|
||||
return DescInfo{sender == localUser ? "You" : username,
|
||||
sender,
|
||||
QString(": %1").arg(body),
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<Text>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<Video>(event)) {
|
||||
const auto msg = mpark::get<Video>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
return DescInfo{sender == localUser ? "You" : username,
|
||||
sender,
|
||||
" sent a video clip",
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<Video>(event, localUser, room_id);
|
||||
} else if (mpark::holds_alternative<mtx::events::Sticker>(event)) {
|
||||
const auto msg = mpark::get<mtx::events::Sticker>(event);
|
||||
QString sender = QString::fromStdString(msg.sender);
|
||||
|
||||
const auto username = Cache::displayName(room_id, sender);
|
||||
const auto ts = QDateTime::fromMSecsSinceEpoch(msg.origin_server_ts);
|
||||
|
||||
return DescInfo{sender == localUser ? "You" : username,
|
||||
sender,
|
||||
" sent a sticker",
|
||||
utils::descriptiveTime(ts),
|
||||
ts};
|
||||
return createDescriptionInfo<mtx::events::Sticker>(event, localUser, room_id);
|
||||
}
|
||||
|
||||
return DescInfo{};
|
||||
|
@ -152,7 +152,7 @@ TimelineItem::TimelineItem(ImageItem *image,
|
||||
{
|
||||
init();
|
||||
|
||||
setupLocalWidgetLayout<ImageItem>(image, userid, "sent an image", withSender);
|
||||
setupLocalWidgetLayout<ImageItem>(image, userid, withSender);
|
||||
|
||||
addSaveImageAction(image);
|
||||
}
|
||||
@ -167,7 +167,7 @@ TimelineItem::TimelineItem(FileItem *file,
|
||||
{
|
||||
init();
|
||||
|
||||
setupLocalWidgetLayout<FileItem>(file, userid, "sent a file", withSender);
|
||||
setupLocalWidgetLayout<FileItem>(file, userid, withSender);
|
||||
}
|
||||
|
||||
TimelineItem::TimelineItem(AudioItem *audio,
|
||||
@ -180,7 +180,7 @@ TimelineItem::TimelineItem(AudioItem *audio,
|
||||
{
|
||||
init();
|
||||
|
||||
setupLocalWidgetLayout<AudioItem>(audio, userid, "sent an audio clip", withSender);
|
||||
setupLocalWidgetLayout<AudioItem>(audio, userid, withSender);
|
||||
}
|
||||
|
||||
TimelineItem::TimelineItem(VideoItem *video,
|
||||
@ -193,7 +193,7 @@ TimelineItem::TimelineItem(VideoItem *video,
|
||||
{
|
||||
init();
|
||||
|
||||
setupLocalWidgetLayout<VideoItem>(video, userid, "sent a video clip", withSender);
|
||||
setupLocalWidgetLayout<VideoItem>(video, userid, withSender);
|
||||
}
|
||||
|
||||
TimelineItem::TimelineItem(ImageItem *image,
|
||||
@ -205,7 +205,7 @@ TimelineItem::TimelineItem(ImageItem *image,
|
||||
, room_id_{room_id}
|
||||
{
|
||||
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>(
|
||||
image, event, " sent an image", with_sender);
|
||||
image, event, with_sender);
|
||||
|
||||
addSaveImageAction(image);
|
||||
}
|
||||
@ -218,8 +218,7 @@ TimelineItem::TimelineItem(StickerItem *image,
|
||||
: QWidget(parent)
|
||||
, room_id_{room_id}
|
||||
{
|
||||
setupWidgetLayout<mtx::events::Sticker, StickerItem>(
|
||||
image, event, " sent a sticker", with_sender);
|
||||
setupWidgetLayout<mtx::events::Sticker, StickerItem>(image, event, with_sender);
|
||||
|
||||
addSaveImageAction(image);
|
||||
}
|
||||
@ -233,7 +232,7 @@ TimelineItem::TimelineItem(FileItem *file,
|
||||
, room_id_{room_id}
|
||||
{
|
||||
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::File>, FileItem>(
|
||||
file, event, " sent a file", with_sender);
|
||||
file, event, with_sender);
|
||||
}
|
||||
|
||||
TimelineItem::TimelineItem(AudioItem *audio,
|
||||
@ -245,7 +244,7 @@ TimelineItem::TimelineItem(AudioItem *audio,
|
||||
, room_id_{room_id}
|
||||
{
|
||||
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Audio>, AudioItem>(
|
||||
audio, event, " sent an audio clip", with_sender);
|
||||
audio, event, with_sender);
|
||||
}
|
||||
|
||||
TimelineItem::TimelineItem(VideoItem *video,
|
||||
@ -257,7 +256,7 @@ TimelineItem::TimelineItem(VideoItem *video,
|
||||
, room_id_{room_id}
|
||||
{
|
||||
setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Video>, VideoItem>(
|
||||
video, event, " sent a video clip", with_sender);
|
||||
video, event, with_sender);
|
||||
}
|
||||
|
||||
/*
|
||||
|
@ -19,6 +19,7 @@
|
||||
#include <QFileInfo>
|
||||
#include <QTimer>
|
||||
|
||||
#include "Cache.h"
|
||||
#include "ChatPage.h"
|
||||
#include "Config.h"
|
||||
#include "FloatingButton.h"
|
||||
@ -212,8 +213,9 @@ TimelineView::addBackwardsEvents(const QString &room_id, const mtx::responses::M
|
||||
isTimelineFinished = false;
|
||||
|
||||
// Queue incoming messages to be rendered later.
|
||||
for (auto const &e : msgs.chunk)
|
||||
topMessages_.emplace_back(e);
|
||||
topMessages_.insert(topMessages_.end(),
|
||||
std::make_move_iterator(msgs.chunk.begin()),
|
||||
std::make_move_iterator(msgs.chunk.end()));
|
||||
|
||||
// The RoomList message preview will be updated only if this
|
||||
// is the first batch of messages received through /messages
|
||||
@ -361,8 +363,9 @@ TimelineView::addEvents(const mtx::responses::Timeline &timeline)
|
||||
isInitialSync = false;
|
||||
}
|
||||
|
||||
for (const auto &e : timeline.events)
|
||||
bottomMessages_.push_back(e);
|
||||
bottomMessages_.insert(bottomMessages_.end(),
|
||||
std::make_move_iterator(timeline.events.begin()),
|
||||
std::make_move_iterator(timeline.events.end()));
|
||||
|
||||
if (!bottomMessages_.empty())
|
||||
notifyForLastEvent(findLastViewableEvent(bottomMessages_));
|
||||
|
Loading…
Reference in New Issue
Block a user