| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "RoomDirectoryModel.h" |
| 6 | |
| 7 | #include <algorithm> |
| 8 | |
| 9 | #include <mtx/requests.hpp> |
| 10 | |
| 11 | #include "Cache.h" |
| 12 | #include "ChatPage.h" |
| 13 | #include "Logging.h" |
| 14 | #include "MatrixClient.h" |
| 15 | |
| 16 | RoomDirectoryModel::RoomDirectoryModel(QObject *parent, const std::string &server) |
| 17 | : QAbstractListModel(parent) |
| 18 | , server_(server) |
| 19 | { |
| 20 | connect(ChatPage::instance(), &ChatPage::newRoom, this, [this](const QString &roomid) { |
| 21 | auto roomid_ = roomid.toStdString(); |
| 22 | |
| 23 | int i = 0; |
| 24 | for (const auto &room : publicRoomsData_) { |
| 25 | if (room.room_id == roomid_) { |
| 26 | emit dataChanged(index(i), index(i), {Roles::CanJoin}); |
| 27 | break; |
| 28 | } |
| 29 | i++; |
| 30 | } |
| 31 | }); |
| 32 | } |
| 33 | |
| 34 | QHash<int, QByteArray> |
| 35 | RoomDirectoryModel::roleNames() const |
| 36 | { |
| 37 | return { |
| 38 | {Roles::Name, "name"}, |
| 39 | {Roles::Id, "roomid"}, |
| 40 | {Roles::AvatarUrl, "avatarUrl"}, |
| 41 | {Roles::Topic, "topic"}, |
| 42 | {Roles::MemberCount, "numMembers"}, |
| 43 | {Roles::Previewable, "canPreview"}, |
| 44 | {Roles::CanJoin, "canJoin"}, |
| 45 | }; |
| 46 | } |
| 47 | |
| 48 | void |
| 49 | RoomDirectoryModel::resetDisplayedData() |
| 50 | { |
| 51 | beginResetModel(); |
| 52 | |
| 53 | prevBatch_ = ""; |
| 54 | nextBatch_ = ""; |
| 55 | canFetchMore_ = true; |
| 56 | |
| 57 | publicRoomsData_.clear(); |
| 58 | |
| 59 | endResetModel(); |
| 60 | } |
| 61 | |
| 62 | void |
| 63 | RoomDirectoryModel::setMatrixServer(const QString &s) |
| 64 | { |
| 65 | server_ = s.toStdString(); |
| 66 | |
| 67 | nhlog::ui()->debug("Received matrix server: {}", server_); |
| 68 | |
| 69 | resetDisplayedData(); |
| 70 | } |
| 71 | |
| 72 | void |
| 73 | RoomDirectoryModel::setSearchTerm(const QString &f) |
| 74 | { |
| 75 | userSearchString_ = f.toStdString(); |
| 76 | |
| 77 | nhlog::ui()->debug("Received user query: {}", userSearchString_); |
| 78 | |
| 79 | resetDisplayedData(); |
| 80 | } |
| 81 | |
| 82 | bool |
| 83 | RoomDirectoryModel::canJoinRoom(const QString &room) const |
| 84 | { |
| 85 | return !room.isEmpty() && cache::getRoomInfo({room.toStdString()}).empty(); |
| 86 | } |
| 87 | |
| 88 | std::vector<std::string> |
| 89 | RoomDirectoryModel::getViasForRoom(const std::vector<std::string> &aliases) |
| 90 | { |
| 91 | std::vector<std::string> vias; |
| 92 | |
| 93 | vias.reserve(aliases.size()); |
| 94 | |
| 95 | std::transform(aliases.begin(), aliases.end(), std::back_inserter(vias), [](const auto &alias) { |
| 96 | return alias.substr(alias.find(":") + 1); |
| 97 | }); |
| 98 | |
| 99 | // When joining a room hosted on a homeserver other than the one the |
| 100 | // account has been registered on, the room's server has to be explicitly |
| 101 | // specified in the "server_name=..." URL parameter of the Matrix Join Room |
| 102 | // request. For more details consult the specs: |
| 103 | // https://matrix.org/docs/spec/client_server/r0.6.1#post-matrix-client-r0-join-roomidoralias |
| 104 | if (!server_.empty()) { |
| 105 | vias.push_back(server_); |
| 106 | } |
| 107 | |
| 108 | return vias; |
| 109 | } |
| 110 | |
| 111 | void |
| 112 | RoomDirectoryModel::joinRoom(const int &index) |
| 113 | { |
| 114 | if (index >= 0 && static_cast<size_t>(index) < publicRoomsData_.size()) { |
| 115 | const auto &chunk = publicRoomsData_[index]; |
| 116 | nhlog::ui()->debug("'Joining room {}", chunk.room_id); |
| 117 | ChatPage::instance()->joinRoomVia(chunk.room_id, getViasForRoom(chunk.aliases)); |
| 118 | } |
| 119 | } |
| 120 | |
| 121 | QVariant |
| 122 | RoomDirectoryModel::data(const QModelIndex &index, int role) const |
| 123 | { |
| 124 | if (hasIndex(index.row(), index.column(), index.parent())) { |
| 125 | const auto &room_chunk = publicRoomsData_[index.row()]; |
| 126 | switch (role) { |
| 127 | case Roles::Name: |
| 128 | return QString::fromStdString(room_chunk.name); |
| 129 | case Roles::Id: |
| 130 | return QString::fromStdString(room_chunk.room_id); |
| 131 | case Roles::AvatarUrl: |
| 132 | return QString::fromStdString(room_chunk.avatar_url); |
| 133 | case Roles::Topic: |
| 134 | return QString::fromStdString(room_chunk.topic); |
| 135 | case Roles::MemberCount: |
| 136 | return QVariant::fromValue(room_chunk.num_joined_members); |
| 137 | case Roles::Previewable: |
| 138 | return QVariant::fromValue(room_chunk.world_readable); |
| 139 | case Roles::CanJoin: |
| 140 | return canJoinRoom(QString::fromStdString(room_chunk.room_id)); |
| 141 | } |
| 142 | } |
| 143 | return {}; |
| 144 | } |
| 145 | |
| 146 | void |
| 147 | RoomDirectoryModel::fetchMore(const QModelIndex &) |
| 148 | { |
| 149 | if (!canFetchMore_) |
| 150 | return; |
| 151 | |
| 152 | nhlog::net()->debug("Fetching more rooms from mtxclient..."); |
| 153 | |
| 154 | mtx::requests::PublicRooms req; |
| 155 | req.limit = limit_; |
| 156 | req.since = prevBatch_; |
| 157 | req.filter.generic_search_term = userSearchString_; |
| 158 | // req.third_party_instance_id = third_party_instance_id; |
| 159 | auto requested_server = server_; |
| 160 | |
| 161 | reachedEndOfPagination_ = false; |
| 162 | emit reachedEndOfPaginationChanged(); |
| 163 | |
| 164 | loadingMoreRooms_ = true; |
| 165 | emit loadingMoreRoomsChanged(); |
| 166 | |
| 167 | auto job = QSharedPointer<FetchRoomsChunkFromDirectoryJob>::create(); |
| 168 | connect(job.data(), |
| 169 | &FetchRoomsChunkFromDirectoryJob::fetchedRoomsBatch, |
| 170 | this, |
| 171 | &RoomDirectoryModel::displayRooms); |
| 172 | |
| 173 | http::client()->post_public_rooms( |
| 174 | req, |
| 175 | [requested_server, job, req](const mtx::responses::PublicRooms &res, |
| 176 | mtx::http::RequestErr err) { |
| 177 | if (err) { |
| 178 | nhlog::net()->error("Failed to retrieve rooms from mtxclient - {} - {} - {}", |
| 179 | mtx::errors::to_string(err->matrix_error.errcode), |
| 180 | err->matrix_error.error, |
| 181 | err->parse_error); |
| 182 | } else { |
| 183 | nhlog::net()->debug("signalling chunk to GUI thread"); |
| 184 | emit job->fetchedRoomsBatch(res.chunk, |
| 185 | res.next_batch, |
| 186 | req.filter.generic_search_term, |
| 187 | requested_server, |
| 188 | req.since); |
| 189 | } |
| 190 | }, |
| 191 | requested_server); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | RoomDirectoryModel::displayRooms(std::vector<mtx::responses::PublicRoomsChunk> fetched_rooms, |
| 196 | const std::string &next_batch, |
| 197 | const std::string &search_term, |
| 198 | const std::string &server, |
| 199 | const std::string &since) |
| 200 | { |
| 201 | if (search_term != this->userSearchString_ || since != this->prevBatch_ || |
| 202 | server != this->server_) { |
| 203 | return; |
| 204 | } |
| 205 | |
| 206 | loadingMoreRooms_ = false; |
| 207 | emit loadingMoreRoomsChanged(); |
| 208 | |
| 209 | nhlog::net()->debug("Prev batch: {} | Next batch: {}", prevBatch_, next_batch); |
| 210 | |
| 211 | if (fetched_rooms.empty()) { |
| 212 | nhlog::net()->error("mtxclient helper thread yielded empty chunk!"); |
| 213 | return; |
| 214 | } |
| 215 | |
| 216 | beginInsertRows(QModelIndex(), |
| 217 | static_cast<int>(publicRoomsData_.size()), |
| 218 | static_cast<int>(publicRoomsData_.size() + fetched_rooms.size()) - 1); |
| 219 | this->publicRoomsData_.insert( |
| 220 | this->publicRoomsData_.end(), fetched_rooms.begin(), fetched_rooms.end()); |
| 221 | endInsertRows(); |
| 222 | |
| 223 | if (next_batch.empty()) { |
| 224 | canFetchMore_ = false; |
| 225 | reachedEndOfPagination_ = true; |
| 226 | emit reachedEndOfPaginationChanged(); |
| 227 | } |
| 228 | |
| 229 | prevBatch_ = next_batch; |
| 230 | |
| 231 | nhlog::ui()->debug("Finished loading rooms"); |
| 232 | } |
| 233 | |
| 234 | #include "moc_RoomDirectoryModel.cpp" |