| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "RoomSettings.h" |
| 6 | |
| 7 | #include <QFileDialog> |
| 8 | #include <QImageReader> |
| 9 | #include <QMimeDatabase> |
| 10 | #include <QStandardPaths> |
| 11 | #include <mtx/events/event_type.hpp> |
| 12 | #include <mtx/responses/common.hpp> |
| 13 | #include <mtx/responses/media.hpp> |
| 14 | #include <mtxclient/http/client.hpp> |
| 15 | |
| 16 | #include "Cache.h" |
| 17 | #include "Cache_p.h" |
| 18 | #include "Logging.h" |
| 19 | #include "MatrixClient.h" |
| 20 | #include "Utils.h" |
| 21 | |
| 22 | using namespace mtx::events; |
| 23 | |
| 24 | RoomSettings::RoomSettings(QString roomid, QObject *parent) |
| 25 | : QObject(parent) |
| 26 | , roomid_{std::move(roomid)} |
| 27 | { |
| 28 | connect(this, &RoomSettings::accessJoinRulesChanged, &RoomSettings::allowedRoomsChanged); |
| 29 | retrieveRoomInfo(); |
| 30 | |
| 31 | // get room setting notifications |
| 32 | http::client()->get_pushrules( |
| 33 | "global", |
| 34 | "override", |
| 35 | roomid_.toStdString(), |
| 36 | [this](const mtx::pushrules::PushRule &rule, mtx::http::RequestErr err) { |
| 37 | if (err) { |
| 38 | if (err->status_code == 404) |
| 39 | http::client()->get_pushrules( |
| 40 | "global", |
| 41 | "room", |
| 42 | roomid_.toStdString(), |
| 43 | [this](const mtx::pushrules::PushRule &rule, mtx::http::RequestErr err) { |
| 44 | if (err) { |
| 45 | notifications_ = 2; // all messages |
| 46 | emit notificationsChanged(); |
| 47 | return; |
| 48 | } |
| 49 | |
| 50 | if (rule.enabled) { |
| 51 | notifications_ = 1; // mentions only |
| 52 | emit notificationsChanged(); |
| 53 | } |
| 54 | }); |
| 55 | return; |
| 56 | } |
| 57 | |
| 58 | if (rule.enabled) { |
| 59 | notifications_ = 0; // muted |
| 60 | emit notificationsChanged(); |
| 61 | } else { |
| 62 | notifications_ = 2; // all messages |
| 63 | emit notificationsChanged(); |
| 64 | } |
| 65 | }); |
| 66 | |
| 67 | // access rules |
| 68 | this->accessRules_ = cache::client() |
| 69 | ->getStateEvent<mtx::events::state::JoinRules>(roomid_.toStdString()) |
| 70 | .value_or(mtx::events::StateEvent<mtx::events::state::JoinRules>{}) |
| 71 | .content; |
| 72 | using mtx::events::state::AccessState; |
| 73 | guestRules_ = info_.guest_access ? AccessState::CanJoin : AccessState::Forbidden; |
| 74 | emit accessJoinRulesChanged(); |
| 75 | |
| 76 | if (auto ev = cache::client()->getStateEvent<mtx::events::state::HistoryVisibility>( |
| 77 | roomid_.toStdString())) { |
| 78 | this->historyVisibility_ = ev->content.history_visibility; |
| 79 | } |
| 80 | |
| 81 | this->allowedRoomsModel = new RoomSettingsAllowedRoomsModel(this); |
| 82 | } |
| 83 | |
| 84 | bool |
| 85 | RoomSettings::isRoomNameSet() const |
| 86 | { |
| 87 | return !cache::client() |
| 88 | ->getStateEvent<mtx::events::state::Name>(roomid_.toStdString()) |
| 89 | .value_or(mtx::events::StateEvent<mtx::events::state::Name>{}) |
| 90 | .content.name.empty(); |
| 91 | } |
| 92 | |
| 93 | QString |
| 94 | RoomSettings::roomName() const |
| 95 | { |
| 96 | return utils::replaceEmoji(QString::fromStdString(info_.name).toHtmlEscaped()); |
| 97 | } |
| 98 | |
| 99 | QString |
| 100 | RoomSettings::roomTopic() const |
| 101 | { |
| 102 | return utils::replaceEmoji( |
| 103 | utils::linkifyMessage(QString::fromStdString(info_.topic) |
| 104 | .toHtmlEscaped() |
| 105 | .replace(QLatin1String("\n"), QLatin1String("<br>")))); |
| 106 | } |
| 107 | |
| 108 | QString |
| 109 | RoomSettings::plainRoomName() const |
| 110 | { |
| 111 | return QString::fromStdString(info_.name); |
| 112 | } |
| 113 | |
| 114 | QString |
| 115 | RoomSettings::plainRoomTopic() const |
| 116 | { |
| 117 | return QString::fromStdString(info_.topic); |
| 118 | } |
| 119 | |
| 120 | QString |
| 121 | RoomSettings::roomId() const |
| 122 | { |
| 123 | return roomid_; |
| 124 | } |
| 125 | |
| 126 | QString |
| 127 | RoomSettings::roomVersion() const |
| 128 | { |
| 129 | return QString::fromStdString(info_.version); |
| 130 | } |
| 131 | |
| 132 | bool |
| 133 | RoomSettings::isLoading() const |
| 134 | { |
| 135 | return isLoading_; |
| 136 | } |
| 137 | |
| 138 | QString |
| 139 | RoomSettings::roomAvatarUrl() |
| 140 | { |
| 141 | return QString::fromStdString(info_.avatar_url); |
| 142 | } |
| 143 | |
| 144 | int |
| 145 | RoomSettings::memberCount() const |
| 146 | { |
| 147 | return static_cast<int>(info_.member_count); |
| 148 | } |
| 149 | |
| 150 | void |
| 151 | RoomSettings::retrieveRoomInfo() |
| 152 | { |
| 153 | try { |
| 154 | usesEncryption_ = cache::isRoomEncrypted(roomid_.toStdString()); |
| 155 | info_ = cache::singleRoomInfo(roomid_.toStdString()); |
| 156 | } catch (const lmdb::error &) { |
| 157 | nhlog::db()->warn("failed to retrieve room info from cache: {}", roomid_.toStdString()); |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | int |
| 162 | RoomSettings::notifications() |
| 163 | { |
| 164 | return notifications_; |
| 165 | } |
| 166 | |
| 167 | RoomSettings::Visibility |
| 168 | RoomSettings::historyVisibility() const |
| 169 | { |
| 170 | switch (this->historyVisibility_) { |
| 171 | case mtx::events::state::Visibility::WorldReadable: |
| 172 | return WorldReadable; |
| 173 | case mtx::events::state::Visibility::Joined: |
| 174 | return Joined; |
| 175 | case mtx::events::state::Visibility::Invited: |
| 176 | return Invited; |
| 177 | case mtx::events::state::Visibility::Shared: |
| 178 | return Shared; |
| 179 | } |
| 180 | return Shared; |
| 181 | } |
| 182 | |
| 183 | bool |
| 184 | RoomSettings::privateAccess() const |
| 185 | { |
| 186 | return accessRules_.join_rule != mtx::events::state::JoinRule::Public; |
| 187 | } |
| 188 | |
| 189 | bool |
| 190 | RoomSettings::guestAccess() const |
| 191 | { |
| 192 | return guestRules_ == mtx::events::state::AccessState::CanJoin; |
| 193 | } |
| 194 | bool |
| 195 | RoomSettings::knockingEnabled() const |
| 196 | { |
| 197 | return accessRules_.join_rule == mtx::events::state::JoinRule::Knock || |
| 198 | accessRules_.join_rule == mtx::events::state::JoinRule::KnockRestricted; |
| 199 | } |
| 200 | bool |
| 201 | RoomSettings::restrictedEnabled() const |
| 202 | { |
| 203 | return accessRules_.join_rule == mtx::events::state::JoinRule::Restricted || |
| 204 | accessRules_.join_rule == mtx::events::state::JoinRule::KnockRestricted; |
| 205 | } |
| 206 | |
| 207 | QStringList |
| 208 | RoomSettings::allowedRooms() const |
| 209 | { |
| 210 | QStringList rooms; |
| 211 | assert(accessRules_.allow.size() < std::numeric_limits<int>::max()); |
| 212 | rooms.reserve(static_cast<int>(accessRules_.allow.size())); |
| 213 | for (const auto &e : accessRules_.allow) { |
| 214 | if (e.type == mtx::events::state::JoinAllowanceType::RoomMembership) |
| 215 | rooms.push_back(QString::fromStdString(e.room_id)); |
| 216 | } |
| 217 | return rooms; |
| 218 | } |
| 219 | void |
| 220 | RoomSettings::setAllowedRooms(QStringList rooms) |
| 221 | { |
| 222 | accessRules_.allow.clear(); |
| 223 | for (const auto &e : rooms) { |
| 224 | accessRules_.allow.push_back( |
| 225 | {mtx::events::state::JoinAllowanceType::RoomMembership, e.toStdString()}); |
| 226 | } |
| 227 | } |
| 228 | |
| 229 | void |
| 230 | RoomSettings::enableEncryption() |
| 231 | { |
| 232 | if (usesEncryption_) |
| 233 | return; |
| 234 | |
| 235 | const auto room_id = roomid_.toStdString(); |
| 236 | http::client()->enable_encryption( |
| 237 | room_id, [room_id, this](const mtx::responses::EventId &, mtx::http::RequestErr err) { |
| 238 | if (err) { |
| 239 | int status_code = static_cast<int>(err->status_code); |
| 240 | nhlog::net()->warn("failed to enable encryption in room ({}): {} {}", |
| 241 | room_id, |
| 242 | err->matrix_error.error, |
| 243 | status_code); |
| 244 | emit displayError(tr("Failed to enable encryption: %1") |
| 245 | .arg(QString::fromStdString(err->matrix_error.error))); |
| 246 | usesEncryption_ = false; |
| 247 | emit encryptionChanged(); |
| 248 | return; |
| 249 | } |
| 250 | |
| 251 | nhlog::net()->info("enabled encryption on room ({})", room_id); |
| 252 | }); |
| 253 | |
| 254 | usesEncryption_ = true; |
| 255 | emit encryptionChanged(); |
| 256 | } |
| 257 | |
| 258 | bool |
| 259 | RoomSettings::canChangeJoinRules() const |
| 260 | { |
| 261 | try { |
| 262 | return cache::hasEnoughPowerLevel( |
| 263 | {EventType::RoomJoinRules}, roomid_.toStdString(), utils::localUser().toStdString()); |
| 264 | } catch (const lmdb::error &e) { |
| 265 | nhlog::db()->warn("lmdb error: {}", e.what()); |
| 266 | } |
| 267 | |
| 268 | return false; |
| 269 | } |
| 270 | |
| 271 | bool |
| 272 | RoomSettings::canChangeName() const |
| 273 | { |
| 274 | try { |
| 275 | return cache::hasEnoughPowerLevel( |
| 276 | {EventType::RoomName}, roomid_.toStdString(), utils::localUser().toStdString()); |
| 277 | } catch (const lmdb::error &e) { |
| 278 | nhlog::db()->warn("lmdb error: {}", e.what()); |
| 279 | } |
| 280 | |
| 281 | return false; |
| 282 | } |
| 283 | |
| 284 | bool |
| 285 | RoomSettings::canChangeTopic() const |
| 286 | { |
| 287 | try { |
| 288 | return cache::hasEnoughPowerLevel( |
| 289 | {EventType::RoomTopic}, roomid_.toStdString(), utils::localUser().toStdString()); |
| 290 | } catch (const lmdb::error &e) { |
| 291 | nhlog::db()->warn("lmdb error: {}", e.what()); |
| 292 | } |
| 293 | |
| 294 | return false; |
| 295 | } |
| 296 | |
| 297 | bool |
| 298 | RoomSettings::canChangeAvatar() const |
| 299 | { |
| 300 | try { |
| 301 | return cache::hasEnoughPowerLevel( |
| 302 | {EventType::RoomAvatar}, roomid_.toStdString(), utils::localUser().toStdString()); |
| 303 | } catch (const lmdb::error &e) { |
| 304 | nhlog::db()->warn("lmdb error: {}", e.what()); |
| 305 | } |
| 306 | |
| 307 | return false; |
| 308 | } |
| 309 | |
| 310 | bool |
| 311 | RoomSettings::canChangeHistoryVisibility() const |
| 312 | { |
| 313 | try { |
| 314 | return cache::hasEnoughPowerLevel({EventType::RoomHistoryVisibility}, |
| 315 | roomid_.toStdString(), |
| 316 | utils::localUser().toStdString()); |
| 317 | } catch (const lmdb::error &e) { |
| 318 | nhlog::db()->warn("lmdb error: {}", e.what()); |
| 319 | } |
| 320 | |
| 321 | return false; |
| 322 | } |
| 323 | |
| 324 | bool |
| 325 | RoomSettings::isEncryptionEnabled() const |
| 326 | { |
| 327 | return usesEncryption_; |
| 328 | } |
| 329 | |
| 330 | bool |
| 331 | RoomSettings::supportsKnocking() const |
| 332 | { |
| 333 | const static std::set<std::string_view> unsupported{ |
| 334 | "", |
| 335 | "1", |
| 336 | "2", |
| 337 | "3", |
| 338 | "4", |
| 339 | "5", |
| 340 | "6", |
| 341 | }; |
| 342 | return !unsupported.count(info_.version); |
| 343 | } |
| 344 | bool |
| 345 | RoomSettings::supportsRestricted() const |
| 346 | { |
| 347 | const static std::set<std::string_view> unsupported{ |
| 348 | "", |
| 349 | "1", |
| 350 | "2", |
| 351 | "3", |
| 352 | "4", |
| 353 | "5", |
| 354 | "6", |
| 355 | "7", |
| 356 | }; |
| 357 | return !unsupported.count(info_.version); |
| 358 | } |
| 359 | bool |
| 360 | RoomSettings::supportsKnockRestricted() const |
| 361 | { |
| 362 | const static std::set<std::string_view> unsupported{ |
| 363 | "", |
| 364 | "1", |
| 365 | "2", |
| 366 | "3", |
| 367 | "4", |
| 368 | "5", |
| 369 | "6", |
| 370 | "7", |
| 371 | "8", |
| 372 | "9", |
| 373 | }; |
| 374 | return !unsupported.count(info_.version); |
| 375 | } |
| 376 | |
| 377 | void |
| 378 | RoomSettings::changeNotifications(int currentIndex) |
| 379 | { |
| 380 | notifications_ = currentIndex; |
| 381 | |
| 382 | std::string room_id = roomid_.toStdString(); |
| 383 | if (notifications_ == 0) { |
| 384 | // mute room |
| 385 | // delete old rule first, then add new rule |
| 386 | mtx::pushrules::PushRule rule; |
| 387 | rule.actions = {mtx::pushrules::actions::dont_notify{}}; |
| 388 | mtx::pushrules::PushCondition condition; |
| 389 | condition.kind = "event_match"; |
| 390 | condition.key = "room_id"; |
| 391 | condition.pattern = room_id; |
| 392 | rule.conditions = {condition}; |
| 393 | |
| 394 | http::client()->put_pushrules( |
| 395 | "global", "override", room_id, rule, [room_id](mtx::http::RequestErr &err) { |
| 396 | if (err) |
| 397 | nhlog::net()->error("failed to set pushrule for room {}: {} {}", |
| 398 | room_id, |
| 399 | static_cast<int>(err->status_code), |
| 400 | err->matrix_error.error); |
| 401 | http::client()->delete_pushrules( |
| 402 | "global", "room", room_id, [room_id](mtx::http::RequestErr &) {}); |
| 403 | }); |
| 404 | } else if (notifications_ == 1) { |
| 405 | // mentions only |
| 406 | // delete old rule first, then add new rule |
| 407 | mtx::pushrules::PushRule rule; |
| 408 | rule.actions = {mtx::pushrules::actions::dont_notify{}}; |
| 409 | http::client()->put_pushrules( |
| 410 | "global", "room", room_id, rule, [room_id](mtx::http::RequestErr &err) { |
| 411 | if (err) |
| 412 | nhlog::net()->error("failed to set pushrule for room {}: {} {}", |
| 413 | room_id, |
| 414 | static_cast<int>(err->status_code), |
| 415 | err->matrix_error.error); |
| 416 | http::client()->delete_pushrules( |
| 417 | "global", "override", room_id, [room_id](mtx::http::RequestErr &) {}); |
| 418 | }); |
| 419 | } else { |
| 420 | // all messages |
| 421 | http::client()->delete_pushrules( |
| 422 | "global", "override", room_id, [room_id](mtx::http::RequestErr &) { |
| 423 | http::client()->delete_pushrules( |
| 424 | "global", "room", room_id, [room_id](mtx::http::RequestErr &) {}); |
| 425 | }); |
| 426 | } |
| 427 | } |
| 428 | |
| 429 | void |
| 430 | RoomSettings::changeAccessRules(bool private_, |
| 431 | bool guestsAllowed, |
| 432 | bool knockingAllowed, |
| 433 | bool restrictedAllowed) |
| 434 | { |
| 435 | using namespace mtx::events::state; |
| 436 | |
| 437 | auto guest_access = [guestsAllowed]() -> state::GuestAccess { |
| 438 | state::GuestAccess event; |
| 439 | |
| 440 | if (guestsAllowed) |
| 441 | event.guest_access = state::AccessState::CanJoin; |
| 442 | else |
| 443 | event.guest_access = state::AccessState::Forbidden; |
| 444 | |
| 445 | return event; |
| 446 | }(); |
| 447 | |
| 448 | auto join_rule = [this, private_, knockingAllowed, restrictedAllowed]() -> state::JoinRules { |
| 449 | state::JoinRules event = this->accessRules_; |
| 450 | |
| 451 | if (!private_) { |
| 452 | event.join_rule = state::JoinRule::Public; |
| 453 | } else if (knockingAllowed && restrictedAllowed && supportsKnockRestricted()) { |
| 454 | event.join_rule = state::JoinRule::KnockRestricted; |
| 455 | } else if (knockingAllowed && supportsKnocking()) { |
| 456 | event.join_rule = state::JoinRule::Knock; |
| 457 | } else if (restrictedAllowed && supportsRestricted()) { |
| 458 | event.join_rule = state::JoinRule::Restricted; |
| 459 | } else { |
| 460 | event.join_rule = state::JoinRule::Invite; |
| 461 | } |
| 462 | |
| 463 | return event; |
| 464 | }(); |
| 465 | |
| 466 | updateAccessRules(roomid_.toStdString(), join_rule, guest_access); |
| 467 | } |
| 468 | |
| 469 | void |
| 470 | RoomSettings::changeName(const QString &name) |
| 471 | { |
| 472 | // Check if the values are changed from the originals. |
| 473 | auto newName = name.trimmed().toStdString(); |
| 474 | |
| 475 | if (newName == info_.name) { |
| 476 | return; |
| 477 | } |
| 478 | |
| 479 | using namespace mtx::events; |
| 480 | auto proxy = std::make_shared<ThreadProxy>(); |
| 481 | connect(proxy.get(), &ThreadProxy::nameEventSent, this, [this](const QString &newRoomName) { |
| 482 | this->info_.name = newRoomName.toStdString(); |
| 483 | emit roomNameChanged(); |
| 484 | }); |
| 485 | connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError); |
| 486 | |
| 487 | state::Name body; |
| 488 | body.name = newName; |
| 489 | |
| 490 | http::client()->send_state_event( |
| 491 | roomid_.toStdString(), |
| 492 | body, |
| 493 | [proxy, newName](const mtx::responses::EventId &, mtx::http::RequestErr err) { |
| 494 | if (err) { |
| 495 | emit proxy->error(QString::fromStdString(err->matrix_error.error)); |
| 496 | return; |
| 497 | } |
| 498 | |
| 499 | emit proxy->nameEventSent(QString::fromStdString(newName)); |
| 500 | }); |
| 501 | } |
| 502 | |
| 503 | void |
| 504 | RoomSettings::changeHistoryVisibility(Visibility value) |
| 505 | { |
| 506 | auto tempVis = mtx::events::state::Visibility::Shared; |
| 507 | |
| 508 | switch (value) { |
| 509 | case WorldReadable: |
| 510 | tempVis = mtx::events::state::Visibility::WorldReadable; |
| 511 | break; |
| 512 | case Joined: |
| 513 | tempVis = mtx::events::state::Visibility::Joined; |
| 514 | break; |
| 515 | case Invited: |
| 516 | tempVis = mtx::events::state::Visibility::Invited; |
| 517 | break; |
| 518 | case Shared: |
| 519 | tempVis = mtx::events::state::Visibility::Shared; |
| 520 | break; |
| 521 | default: |
| 522 | return; |
| 523 | } |
| 524 | |
| 525 | using namespace mtx::events; |
| 526 | auto proxy = std::make_shared<ThreadProxy>(); |
| 527 | connect(proxy.get(), &ThreadProxy::eventSent, this, [this, tempVis]() { |
| 528 | this->historyVisibility_ = tempVis; |
| 529 | emit historyVisibilityChanged(); |
| 530 | }); |
| 531 | connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError); |
| 532 | |
| 533 | state::HistoryVisibility body; |
| 534 | body.history_visibility = tempVis; |
| 535 | |
| 536 | http::client()->send_state_event( |
| 537 | roomid_.toStdString(), |
| 538 | body, |
| 539 | [proxy](const mtx::responses::EventId &, mtx::http::RequestErr err) { |
| 540 | if (err) { |
| 541 | emit proxy->error(QString::fromStdString(err->matrix_error.error)); |
| 542 | return; |
| 543 | } |
| 544 | |
| 545 | emit proxy->eventSent(); |
| 546 | }); |
| 547 | } |
| 548 | |
| 549 | void |
| 550 | RoomSettings::changeTopic(const QString &topic) |
| 551 | { |
| 552 | // Check if the values are changed from the originals. |
| 553 | auto newTopic = topic.trimmed().toStdString(); |
| 554 | |
| 555 | if (newTopic == info_.topic) { |
| 556 | return; |
| 557 | } |
| 558 | |
| 559 | using namespace mtx::events; |
| 560 | auto proxy = std::make_shared<ThreadProxy>(); |
| 561 | connect(proxy.get(), &ThreadProxy::topicEventSent, this, [this](const QString &newRoomTopic) { |
| 562 | this->info_.topic = newRoomTopic.toStdString(); |
| 563 | emit roomTopicChanged(); |
| 564 | }); |
| 565 | connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError); |
| 566 | |
| 567 | state::Topic body; |
| 568 | body.topic = newTopic; |
| 569 | |
| 570 | http::client()->send_state_event( |
| 571 | roomid_.toStdString(), |
| 572 | body, |
| 573 | [proxy, newTopic](const mtx::responses::EventId &, mtx::http::RequestErr err) { |
| 574 | if (err) { |
| 575 | emit proxy->error(QString::fromStdString(err->matrix_error.error)); |
| 576 | return; |
| 577 | } |
| 578 | |
| 579 | emit proxy->topicEventSent(QString::fromStdString(newTopic)); |
| 580 | }); |
| 581 | } |
| 582 | |
| 583 | void |
| 584 | RoomSettings::updateAccessRules(const std::string &room_id, |
| 585 | const mtx::events::state::JoinRules &join_rule, |
| 586 | const mtx::events::state::GuestAccess &guest_access) |
| 587 | { |
| 588 | isLoading_ = true; |
| 589 | allowedRoomsModified_ = false; |
| 590 | emit loadingChanged(); |
| 591 | emit allowedRoomsModifiedChanged(); |
| 592 | |
| 593 | http::client()->send_state_event( |
| 594 | room_id, |
| 595 | join_rule, |
| 596 | [this, room_id, guest_access, join_rule](const mtx::responses::EventId &, |
| 597 | mtx::http::RequestErr err) { |
| 598 | if (err) { |
| 599 | nhlog::net()->warn("failed to send m.room.join_rule: {} {}", |
| 600 | static_cast<int>(err->status_code), |
| 601 | err->matrix_error.error); |
| 602 | emit displayError(QString::fromStdString(err->matrix_error.error)); |
| 603 | isLoading_ = false; |
| 604 | emit loadingChanged(); |
| 605 | return; |
| 606 | } |
| 607 | |
| 608 | http::client()->send_state_event( |
| 609 | room_id, |
| 610 | guest_access, |
| 611 | [this, join_rule](const mtx::responses::EventId &, mtx::http::RequestErr err) { |
| 612 | if (err) { |
| 613 | nhlog::net()->warn("failed to send m.room.guest_access: {} {}", |
| 614 | static_cast<int>(err->status_code), |
| 615 | err->matrix_error.error); |
| 616 | emit displayError(QString::fromStdString(err->matrix_error.error)); |
| 617 | } |
| 618 | |
| 619 | isLoading_ = false; |
| 620 | emit loadingChanged(); |
| 621 | |
| 622 | this->accessRules_ = join_rule; |
| 623 | emit accessJoinRulesChanged(); |
| 624 | }); |
| 625 | }); |
| 626 | } |
| 627 | |
| 628 | void |
| 629 | RoomSettings::stopLoading() |
| 630 | { |
| 631 | isLoading_ = false; |
| 632 | emit loadingChanged(); |
| 633 | } |
| 634 | |
| 635 | void |
| 636 | RoomSettings::avatarChanged() |
| 637 | { |
| 638 | retrieveRoomInfo(); |
| 639 | emit avatarUrlChanged(); |
| 640 | } |
| 641 | |
| 642 | void |
| 643 | RoomSettings::updateAvatar() |
| 644 | { |
| 645 | const QString picturesFolder = |
| 646 | QStandardPaths::writableLocation(QStandardPaths::PicturesLocation); |
| 647 | const QString fileName = QFileDialog::getOpenFileName( |
| 648 | nullptr, tr("Select an avatar"), picturesFolder, tr("All Files (*)")); |
| 649 | |
| 650 | if (fileName.isEmpty()) |
| 651 | return; |
| 652 | |
| 653 | QMimeDatabase db; |
| 654 | QMimeType mime = db.mimeTypeForFile(fileName, QMimeDatabase::MatchContent); |
| 655 | |
| 656 | const auto format = mime.name().split(QStringLiteral("/"))[0]; |
| 657 | |
| 658 | QFile file{fileName, this}; |
| 659 | if (format != QLatin1String("image")) { |
| 660 | emit displayError(tr("The selected file is not an image")); |
| 661 | return; |
| 662 | } |
| 663 | |
| 664 | if (!file.open(QIODevice::ReadOnly)) { |
| 665 | emit displayError(tr("Error while reading file: %1").arg(file.errorString())); |
| 666 | return; |
| 667 | } |
| 668 | |
| 669 | isLoading_ = true; |
| 670 | emit loadingChanged(); |
| 671 | |
| 672 | // Events emitted from the http callbacks (different threads) will |
| 673 | // be queued back into the UI thread through this proxy object. |
| 674 | auto proxy = std::make_shared<ThreadProxy>(); |
| 675 | connect(proxy.get(), &ThreadProxy::error, this, &RoomSettings::displayError); |
| 676 | connect(proxy.get(), &ThreadProxy::stopLoading, this, &RoomSettings::stopLoading); |
| 677 | |
| 678 | const auto bin = file.peek(file.size()); |
| 679 | const auto payload = std::string(bin.data(), bin.size()); |
| 680 | const auto dimensions = QImageReader(&file).size(); |
| 681 | |
| 682 | // First we need to create a new mxc URI |
| 683 | // (i.e upload media to the Matrix content repository) for the new avatar. |
| 684 | http::client()->upload( |
| 685 | payload, |
| 686 | mime.name().toStdString(), |
| 687 | QFileInfo(fileName).fileName().toStdString(), |
| 688 | [proxy = std::move(proxy), |
| 689 | dimensions, |
| 690 | payload, |
| 691 | mimetype = mime.name().toStdString(), |
| 692 | size = payload.size(), |
| 693 | room_id = roomid_.toStdString(), |
| 694 | content = std::move(bin)](const mtx::responses::ContentURI &res, mtx::http::RequestErr err) { |
| 695 | if (err) { |
| 696 | emit proxy->stopLoading(); |
| 697 | emit proxy->error(tr("Failed to upload image: %s") |
| 698 | .arg(QString::fromStdString(err->matrix_error.error))); |
| 699 | return; |
| 700 | } |
| 701 | |
| 702 | using namespace mtx::events; |
| 703 | state::Avatar avatar_event; |
| 704 | avatar_event.image_info.w = dimensions.width(); |
| 705 | avatar_event.image_info.h = dimensions.height(); |
| 706 | avatar_event.image_info.mimetype = mimetype; |
| 707 | avatar_event.image_info.size = size; |
| 708 | avatar_event.url = res.content_uri; |
| 709 | |
| 710 | http::client()->send_state_event( |
| 711 | room_id, |
| 712 | avatar_event, |
| 713 | [content = std::move(content), |
| 714 | proxy = std::move(proxy)](const mtx::responses::EventId &, mtx::http::RequestErr err) { |
| 715 | if (err) { |
| 716 | emit proxy->error(tr("Failed to upload image: %s") |
| 717 | .arg(QString::fromStdString(err->matrix_error.error))); |
| 718 | return; |
| 719 | } |
| 720 | |
| 721 | emit proxy->stopLoading(); |
| 722 | }); |
| 723 | }); |
| 724 | } |
| 725 | |
| 726 | RoomSettingsAllowedRoomsModel::RoomSettingsAllowedRoomsModel(RoomSettings *parent) |
| 727 | : QAbstractListModel(parent) |
| 728 | , settings(parent) |
| 729 | { |
| 730 | this->allowedRoomIds = settings->allowedRooms(); |
| 731 | |
| 732 | auto prIds = cache::client()->getParentRoomIds(settings->roomId().toStdString()); |
| 733 | for (const auto &prId : prIds) { |
| 734 | this->parentSpaces.insert(QString::fromStdString(prId)); |
| 735 | } |
| 736 | |
| 737 | this->listedRoomIds = QStringList(parentSpaces.begin(), parentSpaces.end()); |
| 738 | |
| 739 | for (const auto &e : std::as_const(this->allowedRoomIds)) { |
| 740 | if (!this->parentSpaces.count(e)) |
| 741 | this->listedRoomIds.push_back(e); |
| 742 | } |
| 743 | } |
| 744 | |
| 745 | QHash<int, QByteArray> |
| 746 | RoomSettingsAllowedRoomsModel::roleNames() const |
| 747 | { |
| 748 | return { |
| 749 | {Roles::Name, "name"}, |
| 750 | {Roles::IsAllowed, "allowed"}, |
| 751 | {Roles::IsSpaceParent, "isParent"}, |
| 752 | }; |
| 753 | } |
| 754 | |
| 755 | int |
| 756 | RoomSettingsAllowedRoomsModel::rowCount(const QModelIndex &) const |
| 757 | { |
| 758 | return listedRoomIds.size(); |
| 759 | } |
| 760 | |
| 761 | QVariant |
| 762 | RoomSettingsAllowedRoomsModel::data(const QModelIndex &index, int role) const |
| 763 | { |
| 764 | if (index.row() < 0 || index.row() > listedRoomIds.size()) |
| 765 | return {}; |
| 766 | |
| 767 | if (role == Roles::IsAllowed) { |
| 768 | return allowedRoomIds.contains(listedRoomIds.at(index.row())); |
| 769 | } else if (role == Roles::IsSpaceParent) { |
| 770 | return parentSpaces.find(listedRoomIds.at(index.row())) != parentSpaces.cend(); |
| 771 | } else if (role == Roles::Name) { |
| 772 | auto id = listedRoomIds.at(index.row()); |
| 773 | auto info = cache::client()->getRoomInfo({ |
| 774 | id.toStdString(), |
| 775 | }); |
| 776 | if (!info.empty()) |
| 777 | return QString::fromStdString(info[id].name); |
| 778 | else |
| 779 | return ""; |
| 780 | } else { |
| 781 | return {}; |
| 782 | } |
| 783 | } |
| 784 | |
| 785 | bool |
| 786 | RoomSettingsAllowedRoomsModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 787 | { |
| 788 | if (index.row() < 0 || index.row() > listedRoomIds.size()) |
| 789 | return false; |
| 790 | |
| 791 | if (role != Roles::IsAllowed) |
| 792 | return false; |
| 793 | |
| 794 | if (value.toBool()) { |
| 795 | if (!allowedRoomIds.contains(listedRoomIds.at(index.row()))) |
| 796 | allowedRoomIds.push_back(listedRoomIds.at(index.row())); |
| 797 | } else { |
| 798 | allowedRoomIds.removeAll(listedRoomIds.at(index.row())); |
| 799 | } |
| 800 | |
| 801 | return true; |
| 802 | } |
| 803 | |
| 804 | void |
| 805 | RoomSettingsAllowedRoomsModel::addRoom(QString room) |
| 806 | { |
| 807 | if (listedRoomIds.contains(room) || !room.startsWith('!')) |
| 808 | return; |
| 809 | |
| 810 | beginInsertRows(QModelIndex(), listedRoomIds.size(), listedRoomIds.size()); |
| 811 | listedRoomIds.push_back(room); |
| 812 | allowedRoomIds.push_back(room); |
| 813 | endInsertRows(); |
| 814 | } |
| 815 | |
| 816 | void |
| 817 | RoomSettings::applyAllowedFromModel() |
| 818 | { |
| 819 | this->setAllowedRooms(this->allowedRoomsModel->allowedRoomIds); |
| 820 | this->allowedRoomsModified_ = true; |
| 821 | emit allowedRoomsModifiedChanged(); |
| 822 | } |
| 823 | |
| 824 | #include "moc_RoomSettings.cpp" |