aboutsummaryrefslogtreecommitdiff
path: root/room.cpp
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-27 15:19:07 +0300
committerKitsune Ral <Kitsune-Ral@users.sf.net>2017-10-27 15:19:07 +0300
commit0fb7adfcaa5dffee4efd0a34a2a4fd655fe5c709 (patch)
tree5d05f339f5fa5eea4bae44633190cbc5666558a5 /room.cpp
parentc287d335da0d2ae6609dd090d45a89c6b8974af0 (diff)
downloadlibquotient-0fb7adfcaa5dffee4efd0a34a2a4fd655fe5c709.tar.gz
libquotient-0fb7adfcaa5dffee4efd0a34a2a4fd655fe5c709.zip
Support m.room.avatar events
The events are detected in /sync output, and avatars for rooms are loaded from respective URLs. Clients can use Room::avatar() method to request a pixmap of a certain size, and react to avatarChanged() in order to update the UI when new pixmaps/avatars arrive. avatarChanged() signal is overloaded with two tasks - the first firing merely indicates that a new avatar is available (without actual pixmap yet available) while the second firing means that an actual pixmap has arrived (all this is entirely transparent for clients, they just should update their pixmaps from Room::avatar() every time when Room::avatarChanged() is emitted).
Diffstat (limited to 'room.cpp')
-rw-r--r--room.cpp57
1 files changed, 47 insertions, 10 deletions
diff --git a/room.cpp b/room.cpp
index 65cf2d2a..53a6b160 100644
--- a/room.cpp
+++ b/room.cpp
@@ -27,12 +27,14 @@
#include "events/roomaliasesevent.h"
#include "events/roomcanonicalaliasevent.h"
#include "events/roomtopicevent.h"
+#include "events/roomavatarevent.h"
#include "events/roommemberevent.h"
#include "events/typingevent.h"
#include "events/receiptevent.h"
#include "jobs/sendeventjob.h"
#include "jobs/roommessagesjob.h"
#include "jobs/postreceiptjob.h"
+#include "avatar.h"
#include "connection.h"
#include "user.h"
@@ -53,7 +55,7 @@ class Room::Private
Private(Connection* c, QString id_, JoinState initialJoinState)
: q(nullptr), connection(c), id(std::move(id_))
- , joinState(initialJoinState), unreadMessages(false)
+ , avatar(c), joinState(initialJoinState), unreadMessages(false)
, highlightCount(0), notificationCount(0), roomMessagesJob(nullptr)
{ }
@@ -73,6 +75,7 @@ class Room::Private
QString name;
QString displayname;
QString topic;
+ Avatar avatar;
JoinState joinState;
bool unreadMessages;
int highlightCount;
@@ -189,6 +192,23 @@ QString Room::topic() const
return d->topic;
}
+QPixmap Room::avatar(int width, int height)
+{
+ if (!d->avatar.url().isEmpty())
+ return d->avatar.get(width, height, [=] { emit avatarChanged(); });
+
+ // Use the other side's avatar for 1:1's
+ if (d->membersMap.size() == 2)
+ {
+ auto theOtherOneIt = d->membersMap.begin();
+ if (theOtherOneIt.value() == localUser())
+ ++theOtherOneIt;
+ return theOtherOneIt.value()->avatarObject()
+ .get(width, height, [=] { emit avatarChanged(); });
+ }
+ return {};
+}
+
JoinState Room::joinState() const
{
return d->joinState;
@@ -778,6 +798,17 @@ void Room::processStateEvents(const RoomEvents& events)
emit topicChanged();
break;
}
+ case EventType::RoomAvatar: {
+ const auto& avatarEventContent =
+ static_cast<RoomAvatarEvent*>(event)->content();
+ if (d->avatar.updateUrl(avatarEventContent.url))
+ {
+ qCDebug(MAIN) << "Room avatar URL updated:"
+ << avatarEventContent.url.toString();
+ emit avatarChanged();
+ }
+ break;
+ }
case EventType::RoomMember: {
auto memberEvent = static_cast<RoomMemberEvent*>(event);
// Can't use d->member() below because the user may be not a member (yet)
@@ -939,9 +970,13 @@ void Room::Private::updateDisplayname()
emit q->displaynameChanged(q);
}
-QJsonObject stateEventToJson(const QString& type, const QString& name,
- const QJsonValue& content)
+template <typename T>
+void appendEventJson(QJsonArray& events, const QString& type,
+ const QString& name, const T& content)
{
+ if (content.isEmpty())
+ return;
+
QJsonObject contentObj;
contentObj.insert(name, content);
@@ -949,7 +984,7 @@ QJsonObject stateEventToJson(const QString& type, const QString& name,
eventObj.insert("type", type);
eventObj.insert("content", contentObj);
- return eventObj;
+ events.append(eventObj);
}
QJsonObject Room::Private::toJson() const
@@ -958,12 +993,14 @@ QJsonObject Room::Private::toJson() const
{
QJsonArray stateEvents;
- stateEvents.append(stateEventToJson("m.room.name", "name", name));
- stateEvents.append(stateEventToJson("m.room.topic", "topic", topic));
- stateEvents.append(stateEventToJson("m.room.aliases", "aliases",
- QJsonArray::fromStringList(aliases)));
- stateEvents.append(stateEventToJson("m.room.canonical_alias", "alias",
- canonicalAlias));
+ appendEventJson(stateEvents, "m.room.name", "name", name);
+ appendEventJson(stateEvents, "m.room.topic", "topic", topic);
+ appendEventJson(stateEvents, "m.room.avatar", "avatar_url",
+ avatar.url().toString());
+ appendEventJson(stateEvents, "m.room.aliases", "aliases",
+ QJsonArray::fromStringList(aliases));
+ appendEventJson(stateEvents, "m.room.canonical_alias", "alias",
+ canonicalAlias);
for (const auto &i : membersMap)
{