aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKitsune Ral <Kitsune-Ral@users.sf.net>2018-08-11 15:24:41 +0900
committerKitsune Ral <Kitsune-Ral@users.sf.net>2018-08-11 15:24:41 +0900
commitb028dff6bf6074fdeea5d6f50e97df074f53b45b (patch)
treec25f3577b2f5e358a58acba5a377e32a286fd0dd
parent62e1550ed842a8a0d4c7b31d788176814a03e814 (diff)
downloadlibquotient-b028dff6bf6074fdeea5d6f50e97df074f53b45b.tar.gz
libquotient-b028dff6bf6074fdeea5d6f50e97df074f53b45b.zip
Connection::*DirectChat(): add overloads accepting User*
-rw-r--r--lib/connection.cpp28
-rw-r--r--lib/connection.h20
-rw-r--r--lib/user.cpp4
-rw-r--r--lib/user.h2
4 files changed, 47 insertions, 7 deletions
diff --git a/lib/connection.cpp b/lib/connection.cpp
index 92134675..ec29bbc1 100644
--- a/lib/connection.cpp
+++ b/lib/connection.cpp
@@ -516,15 +516,37 @@ CreateRoomJob* Connection::createRoom(RoomVisibility visibility,
void Connection::requestDirectChat(const QString& userId)
{
- doInDirectChat(userId, [this] (Room* r) { emit directChatAvailable(r); });
+ if (const auto* u = user(userId))
+ requestDirectChat(u);
+ else
+ qCCritical(MAIN)
+ << "Connection::requestDirectChat: Couldn't get a user object for"
+ << userId;
+}
+
+void Connection::requestDirectChat(const User* u)
+{
+ doInDirectChat(u, [this] (Room* r) { emit directChatAvailable(r); });
}
void Connection::doInDirectChat(const QString& userId,
- std::function<void (Room*)> operation)
+ const std::function<void(Room*)>& operation)
+{
+ if (const auto* u = user(userId))
+ doInDirectChat(u, operation);
+ else
+ qCCritical(MAIN)
+ << "Connection::doInDirectChat: Couldn't get a user object for"
+ << userId;
+}
+
+void Connection::doInDirectChat(const User* u,
+ const std::function<void(Room*)>& operation)
{
+ Q_ASSERT(u);
+ const auto& userId = u->id();
// There can be more than one DC; find the first valid, and delete invalid
// (left/forgotten) ones along the way.
- const auto* u = user(userId);
DirectChatsMap removals;
for (auto it = d->directChats.find(u);
it != d->directChats.end() && it.key() == u; ++it)
diff --git a/lib/connection.h b/lib/connection.h
index 5d817d69..e761acfd 100644
--- a/lib/connection.h
+++ b/lib/connection.h
@@ -385,6 +385,15 @@ namespace QMatrixClient
*/
void requestDirectChat(const QString& userId);
+ /** Get a direct chat with a single user
+ * This method may return synchronously or asynchoronously depending
+ * on whether a direct chat room with the respective person exists
+ * already.
+ *
+ * \sa directChatAvailable
+ */
+ void requestDirectChat(const User* u);
+
/** Run an operation in a direct chat with the user
* This method may return synchronously or asynchoronously depending
* on whether a direct chat room with the respective person exists
@@ -392,7 +401,16 @@ namespace QMatrixClient
* function object with the direct chat room as its parameter.
*/
void doInDirectChat(const QString& userId,
- std::function<void(Room*)> operation);
+ const std::function<void(Room*)>& operation);
+
+ /** Run an operation in a direct chat with the user
+ * This method may return synchronously or asynchoronously depending
+ * on whether a direct chat room with the respective person exists
+ * already. Instead of emitting a signal it executes the passed
+ * function object with the direct chat room as its parameter.
+ */
+ void doInDirectChat(const User* u,
+ const std::function<void(Room*)>& operation);
/** Create a direct chat with a single user, optional name and topic
* A room will always be created, unlike in requestDirectChat.
diff --git a/lib/user.cpp b/lib/user.cpp
index f6611309..12977b8a 100644
--- a/lib/user.cpp
+++ b/lib/user.cpp
@@ -299,9 +299,9 @@ bool User::setAvatar(QIODevice* source)
std::bind(&Private::setAvatarOnServer, d.data(), _1, this));
}
-void User::requestDirectChat()
+void User::requestDirectChat() const
{
- connection()->requestDirectChat(d->userId);
+ connection()->requestDirectChat(this);
}
void User::ignore()
diff --git a/lib/user.h b/lib/user.h
index 2eb7f78a..c33ed028 100644
--- a/lib/user.h
+++ b/lib/user.h
@@ -120,7 +120,7 @@ namespace QMatrixClient
* The resulting chat is returned asynchronously via
* Connection::directChatAvailable()
*/
- void requestDirectChat();
+ void requestDirectChat() const;
/** Add the user to the ignore list */
void ignore();
/** Remove the user from the ignore list */