summary refs log tree commit diff
path: root/src/timeline
diff options
context:
space:
mode:
authorKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-21 16:34:50 +0300
committerKonstantinos Sideris <sideris.konstantin@gmail.com>2018-04-21 16:35:03 +0300
commit2f00fc51bf27708a9c0ac1ce186043059f93923e (patch)
tree2e65159dd08fd9576b18f7f1570b41b56029ccda /src/timeline
parentPopup improvements (diff)
downloadnheko-2f00fc51bf27708a9c0ac1ce186043059f93923e.tar.xz
Cache refactoring
Diffstat (limited to 'src/timeline')
-rw-r--r--src/timeline/TimelineItem.cc52
-rw-r--r--src/timeline/TimelineView.cc5
-rw-r--r--src/timeline/TimelineViewManager.cc23
3 files changed, 52 insertions, 28 deletions
diff --git a/src/timeline/TimelineItem.cc b/src/timeline/TimelineItem.cc
index 0296c6cd..cd1dac67 100644
--- a/src/timeline/TimelineItem.cc
+++ b/src/timeline/TimelineItem.cc
@@ -99,12 +99,14 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
                            const QString &userid,
                            QString body,
                            bool withSender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         init();
 
-        auto displayName = TimelineViewManager::displayName(userid);
+        auto displayName = Cache::displayName(room_id_, userid);
         auto timestamp   = QDateTime::currentDateTime();
 
         if (ty == mtx::events::MessageType::Emote) {
@@ -127,7 +129,7 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
                 messageLayout_->addLayout(headerLayout_, 1);
 
                 AvatarProvider::resolve(
-                  userid, this, [this](const QImage &img) { setUserAvatar(img); });
+                  room_id_, userid, this, [this](const QImage &img) { setUserAvatar(img); });
         } else {
                 generateBody(body);
                 setupSimpleLayout();
@@ -143,8 +145,10 @@ TimelineItem::TimelineItem(mtx::events::MessageType ty,
 TimelineItem::TimelineItem(ImageItem *image,
                            const QString &userid,
                            bool withSender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget{parent}
+  , room_id_{room_id}
 {
         init();
 
@@ -153,8 +157,13 @@ TimelineItem::TimelineItem(ImageItem *image,
         addSaveImageAction(image);
 }
 
-TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSender, QWidget *parent)
+TimelineItem::TimelineItem(FileItem *file,
+                           const QString &userid,
+                           bool withSender,
+                           const QString &room_id,
+                           QWidget *parent)
   : QWidget{parent}
+  , room_id_{room_id}
 {
         init();
 
@@ -164,8 +173,10 @@ TimelineItem::TimelineItem(FileItem *file, const QString &userid, bool withSende
 TimelineItem::TimelineItem(AudioItem *audio,
                            const QString &userid,
                            bool withSender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget{parent}
+  , room_id_{room_id}
 {
         init();
 
@@ -175,8 +186,10 @@ TimelineItem::TimelineItem(AudioItem *audio,
 TimelineItem::TimelineItem(VideoItem *video,
                            const QString &userid,
                            bool withSender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget{parent}
+  , room_id_{room_id}
 {
         init();
 
@@ -186,8 +199,10 @@ TimelineItem::TimelineItem(VideoItem *video,
 TimelineItem::TimelineItem(ImageItem *image,
                            const mtx::events::RoomEvent<mtx::events::msg::Image> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Image>, ImageItem>(
           image, event, " sent an image", with_sender);
@@ -198,8 +213,10 @@ TimelineItem::TimelineItem(ImageItem *image,
 TimelineItem::TimelineItem(FileItem *file,
                            const mtx::events::RoomEvent<mtx::events::msg::File> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::File>, FileItem>(
           file, event, " sent a file", with_sender);
@@ -208,8 +225,10 @@ TimelineItem::TimelineItem(FileItem *file,
 TimelineItem::TimelineItem(AudioItem *audio,
                            const mtx::events::RoomEvent<mtx::events::msg::Audio> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Audio>, AudioItem>(
           audio, event, " sent an audio clip", with_sender);
@@ -218,8 +237,10 @@ TimelineItem::TimelineItem(AudioItem *audio,
 TimelineItem::TimelineItem(VideoItem *video,
                            const mtx::events::RoomEvent<mtx::events::msg::Video> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         setupWidgetLayout<mtx::events::RoomEvent<mtx::events::msg::Video>, VideoItem>(
           video, event, " sent a video clip", with_sender);
@@ -230,8 +251,10 @@ TimelineItem::TimelineItem(VideoItem *video,
  */
 TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         init();
 
@@ -240,7 +263,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
         const auto timestamp = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
         auto body            = QString::fromStdString(event.content.body).trimmed().toHtmlEscaped();
 
-        descriptionMsg_ = {TimelineViewManager::displayName(sender),
+        descriptionMsg_ = {Cache::displayName(room_id_, sender),
                            sender,
                            " sent a notification",
                            utils::descriptiveTime(timestamp),
@@ -253,7 +276,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
         body = "<i>" + body + "</i>";
 
         if (with_sender) {
-                auto displayName = TimelineViewManager::displayName(sender);
+                auto displayName = Cache::displayName(room_id_, sender);
 
                 generateBody(displayName, body);
                 setupAvatarLayout(displayName);
@@ -261,7 +284,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
                 messageLayout_->addLayout(headerLayout_, 1);
 
                 AvatarProvider::resolve(
-                  sender, this, [this](const QImage &img) { setUserAvatar(img); });
+                  room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
         } else {
                 generateBody(body);
                 setupSimpleLayout();
@@ -279,8 +302,10 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Notice
  */
 TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         init();
 
@@ -289,7 +314,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
 
         auto body        = QString::fromStdString(event.content.body).trimmed();
         auto timestamp   = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
-        auto displayName = TimelineViewManager::displayName(sender);
+        auto displayName = Cache::displayName(room_id_, sender);
         auto emoteMsg    = QString("* %1 %2").arg(displayName).arg(body);
 
         descriptionMsg_ = {"", sender, emoteMsg, utils::descriptiveTime(timestamp), timestamp};
@@ -306,7 +331,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
                 messageLayout_->addLayout(headerLayout_, 1);
 
                 AvatarProvider::resolve(
-                  sender, this, [this](const QImage &img) { setUserAvatar(img); });
+                  room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
         } else {
                 generateBody(emoteMsg);
                 setupSimpleLayout();
@@ -324,8 +349,10 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Emote>
  */
 TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text> &event,
                            bool with_sender,
+                           const QString &room_id,
                            QWidget *parent)
   : QWidget(parent)
+  , room_id_{room_id}
 {
         init();
 
@@ -334,7 +361,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
 
         auto body        = QString::fromStdString(event.content.body).trimmed();
         auto timestamp   = QDateTime::fromMSecsSinceEpoch(event.origin_server_ts);
-        auto displayName = TimelineViewManager::displayName(sender);
+        auto displayName = Cache::displayName(room_id_, sender);
 
         QSettings settings;
         descriptionMsg_ = {sender == settings.value("auth/user_id") ? "You" : displayName,
@@ -356,7 +383,7 @@ TimelineItem::TimelineItem(const mtx::events::RoomEvent<mtx::events::msg::Text>
                 messageLayout_->addLayout(headerLayout_, 1);
 
                 AvatarProvider::resolve(
-                  sender, this, [this](const QImage &img) { setUserAvatar(img); });
+                  room_id_, sender, this, [this](const QImage &img) { setUserAvatar(img); });
         } else {
                 generateBody(body);
                 setupSimpleLayout();
@@ -532,7 +559,7 @@ TimelineItem::addAvatar()
 
         // TODO: should be replaced with the proper event struct.
         auto userid      = descriptionMsg_.userid;
-        auto displayName = TimelineViewManager::displayName(userid);
+        auto displayName = Cache::displayName(room_id_, userid);
 
         QFontMetrics fm(usernameFont_);
         userName_ = new QLabel(this);
@@ -566,5 +593,6 @@ TimelineItem::addAvatar()
         messageLayout_->addWidget(checkmark_);
         messageLayout_->addWidget(timestamp_);
 
-        AvatarProvider::resolve(userid, this, [this](const QImage &img) { setUserAvatar(img); });
+        AvatarProvider::resolve(
+          room_id_, userid, this, [this](const QImage &img) { setUserAvatar(img); });
 }
diff --git a/src/timeline/TimelineView.cc b/src/timeline/TimelineView.cc
index d18c1cff..f7194265 100644
--- a/src/timeline/TimelineView.cc
+++ b/src/timeline/TimelineView.cc
@@ -477,8 +477,7 @@ TimelineView::addUserMessage(mtx::events::MessageType ty, const QString &body)
         auto with_sender = lastSender_ != local_user_;
 
         TimelineItem *view_item =
-          new TimelineItem(ty, local_user_, body, with_sender, scroll_widget_);
-        view_item->setRoomId(room_id_);
+          new TimelineItem(ty, local_user_, body, with_sender, room_id_, scroll_widget_);
 
         addTimelineItem(view_item);
 
@@ -538,7 +537,7 @@ TimelineView::notifyForLastEvent()
 void
 TimelineView::notifyForLastEvent(const TimelineEvent &event)
 {
-        auto descInfo = utils::getMessageDescription(event, local_user_);
+        auto descInfo = utils::getMessageDescription(event, local_user_, room_id_);
 
         if (!descInfo.timestamp.isEmpty())
                 emit updateLastTimelineMessage(room_id_, descInfo);
diff --git a/src/timeline/TimelineViewManager.cc b/src/timeline/TimelineViewManager.cc
index 55f25dfc..28f44770 100644
--- a/src/timeline/TimelineViewManager.cc
+++ b/src/timeline/TimelineViewManager.cc
@@ -172,18 +172,23 @@ TimelineViewManager::initialize(const mtx::responses::Rooms &rooms)
         for (auto it = rooms.join.cbegin(); it != rooms.join.cend(); ++it) {
                 addRoom(it->second, QString::fromStdString(it->first));
         }
+
+        sync(rooms);
 }
 
 void
-TimelineViewManager::initialize(const std::vector<QString> &rooms)
+TimelineViewManager::initialize(const std::vector<std::string> &rooms)
 {
         for (const auto &roomid : rooms)
-                addRoom(roomid);
+                addRoom(QString::fromStdString(roomid));
 }
 
 void
 TimelineViewManager::addRoom(const mtx::responses::JoinedRoom &room, const QString &room_id)
 {
+        if (timelineViewExists(room_id))
+                return;
+
         // Create a history view with the room events.
         TimelineView *view = new TimelineView(room.timeline, client_, room_id);
         views_.emplace(room_id, QSharedPointer<TimelineView>(view));
@@ -200,6 +205,9 @@ TimelineViewManager::addRoom(const mtx::responses::JoinedRoom &room, const QStri
 void
 TimelineViewManager::addRoom(const QString &room_id)
 {
+        if (timelineViewExists(room_id))
+                return;
+
         // Create a history view without any events.
         TimelineView *view = new TimelineView(client_, room_id);
         views_.emplace(room_id, QSharedPointer<TimelineView>(view));
@@ -247,8 +255,6 @@ TimelineViewManager::setHistoryView(const QString &room_id)
         view->scrollDown();
 }
 
-std::map<QString, QString> TimelineViewManager::DISPLAY_NAMES;
-
 QString
 TimelineViewManager::chooseRandomColor()
 {
@@ -307,15 +313,6 @@ TimelineViewManager::chooseRandomColor()
         return color.name();
 }
 
-QString
-TimelineViewManager::displayName(const QString &userid)
-{
-        if (DISPLAY_NAMES.find(userid) != DISPLAY_NAMES.end())
-                return DISPLAY_NAMES.at(userid);
-
-        return userid;
-}
-
 bool
 TimelineViewManager::hasLoaded() const
 {