| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "SingleImagePackModel.h" |
| 6 | |
| 7 | #include <QFile> |
| 8 | #include <QFileInfo> |
| 9 | #include <QMimeDatabase> |
| 10 | |
| 11 | #include <mtx/events/mscs/image_packs.hpp> |
| 12 | #include <nlohmann/json.hpp> |
| 13 | |
| 14 | #include <unordered_set> |
| 15 | |
| 16 | #include <mtx/responses/media.hpp> |
| 17 | |
| 18 | #include "Cache_p.h" |
| 19 | #include "ChatPage.h" |
| 20 | #include "Logging.h" |
| 21 | #include "MatrixClient.h" |
| 22 | #include "Utils.h" |
| 23 | #include "timeline/Permissions.h" |
| 24 | #include "timeline/TimelineModel.h" |
| 25 | |
| 26 | SingleImagePackModel::SingleImagePackModel(ImagePackInfo pack_, QObject *parent) |
| 27 | : QAbstractListModel(parent) |
| 28 | , roomid_(std::move(pack_.source_room)) |
| 29 | , statekey_(std::move(pack_.state_key)) |
| 30 | , old_statekey_(statekey_) |
| 31 | , pack(std::move(pack_.pack)) |
| 32 | , fromSpace_(pack_.from_space) |
| 33 | { |
| 34 | if (!pack.pack) |
| 35 | pack.pack = mtx::events::msc2545::ImagePack::PackDescription{}; |
| 36 | |
| 37 | shortcodes.reserve(pack.images.size()); |
| 38 | for (const auto &e : pack.images) |
| 39 | shortcodes.push_back(e.first); |
| 40 | |
| 41 | connect(this, &SingleImagePackModel::addImage, this, &SingleImagePackModel::addImageCb); |
| 42 | connect(this, &SingleImagePackModel::avatarUploaded, this, &SingleImagePackModel::setAvatarUrl); |
| 43 | } |
| 44 | |
| 45 | int |
| 46 | SingleImagePackModel::rowCount(const QModelIndex &) const |
| 47 | { |
| 48 | return (int)shortcodes.size(); |
| 49 | } |
| 50 | |
| 51 | QHash<int, QByteArray> |
| 52 | SingleImagePackModel::roleNames() const |
| 53 | { |
| 54 | return { |
| 55 | {Roles::Url, "url"}, |
| 56 | {Roles::ShortCode, "shortCode"}, |
| 57 | {Roles::Body, "body"}, |
| 58 | {Roles::IsEmote, "isEmote"}, |
| 59 | {Roles::IsSticker, "isSticker"}, |
| 60 | }; |
| 61 | } |
| 62 | |
| 63 | QVariant |
| 64 | SingleImagePackModel::data(const QModelIndex &index, int role) const |
| 65 | { |
| 66 | if (hasIndex(index.row(), index.column(), index.parent())) { |
| 67 | const auto &img = pack.images.at(shortcodes.at(index.row())); |
| 68 | switch (role) { |
| 69 | case Url: |
| 70 | return QString::fromStdString(img.url); |
| 71 | case ShortCode: |
| 72 | return QString::fromStdString(shortcodes.at(index.row())); |
| 73 | case Body: |
| 74 | return QString::fromStdString(img.body); |
| 75 | case IsEmote: |
| 76 | return img.overrides_usage() ? img.is_emoji() : pack.pack->is_emoji(); |
| 77 | case IsSticker: |
| 78 | return img.overrides_usage() ? img.is_sticker() : pack.pack->is_sticker(); |
| 79 | default: |
| 80 | return {}; |
| 81 | } |
| 82 | } |
| 83 | return {}; |
| 84 | } |
| 85 | |
| 86 | bool |
| 87 | SingleImagePackModel::setData(const QModelIndex &index, const QVariant &value, int role) |
| 88 | { |
| 89 | using mtx::events::msc2545::PackUsage; |
| 90 | |
| 91 | if (hasIndex(index.row(), index.column(), index.parent())) { |
| 92 | auto &img = pack.images.at(shortcodes.at(index.row())); |
| 93 | switch (role) { |
| 94 | case ShortCode: { |
| 95 | auto newCode = value.toString().toStdString(); |
| 96 | |
| 97 | // otherwise we delete this by accident |
| 98 | newCode = unconflictingShortcode(newCode); |
| 99 | |
| 100 | auto tmp = img; |
| 101 | auto oldCode = shortcodes.at(index.row()); |
| 102 | pack.images.erase(oldCode); |
| 103 | shortcodes[index.row()] = newCode; |
| 104 | pack.images.insert({newCode, tmp}); |
| 105 | |
| 106 | emit dataChanged( |
| 107 | this->index(index.row()), this->index(index.row()), {Roles::ShortCode}); |
| 108 | return true; |
| 109 | } |
| 110 | case Body: |
| 111 | img.body = value.toString().toStdString(); |
| 112 | emit dataChanged(this->index(index.row()), this->index(index.row()), {Roles::Body}); |
| 113 | return true; |
| 114 | case IsEmote: { |
| 115 | bool isEmote = value.toBool(); |
| 116 | bool isSticker = img.overrides_usage() ? img.is_sticker() : pack.pack->is_sticker(); |
| 117 | |
| 118 | img.usage.set(PackUsage::Emoji, isEmote); |
| 119 | img.usage.set(PackUsage::Sticker, isSticker); |
| 120 | |
| 121 | if (img.usage == pack.pack->usage) |
| 122 | img.usage.reset(); |
| 123 | |
| 124 | emit dataChanged(this->index(index.row()), this->index(index.row()), {Roles::IsEmote}); |
| 125 | |
| 126 | return true; |
| 127 | } |
| 128 | case IsSticker: { |
| 129 | bool isEmote = img.overrides_usage() ? img.is_emoji() : pack.pack->is_emoji(); |
| 130 | bool isSticker = value.toBool(); |
| 131 | |
| 132 | img.usage.set(PackUsage::Emoji, isEmote); |
| 133 | img.usage.set(PackUsage::Sticker, isSticker); |
| 134 | |
| 135 | if (img.usage == pack.pack->usage) |
| 136 | img.usage.reset(); |
| 137 | |
| 138 | emit dataChanged( |
| 139 | this->index(index.row()), this->index(index.row()), {Roles::IsSticker}); |
| 140 | |
| 141 | return true; |
| 142 | } |
| 143 | } |
| 144 | } |
| 145 | return false; |
| 146 | } |
| 147 | |
| 148 | bool |
| 149 | SingleImagePackModel::isGloballyEnabled() const |
| 150 | { |
| 151 | if (auto roomPacks = cache::client()->getAccountData(mtx::events::EventType::ImagePackRooms)) { |
| 152 | if (auto tmp = |
| 153 | std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePackRooms>>( |
| 154 | &*roomPacks)) { |
| 155 | if (tmp->content.rooms.count(roomid_) && |
| 156 | tmp->content.rooms.at(roomid_).count(statekey_)) |
| 157 | return true; |
| 158 | } |
| 159 | } |
| 160 | return false; |
| 161 | } |
| 162 | void |
| 163 | SingleImagePackModel::setGloballyEnabled(bool enabled) |
| 164 | { |
| 165 | mtx::events::msc2545::ImagePackRooms content{}; |
| 166 | if (auto roomPacks = cache::client()->getAccountData(mtx::events::EventType::ImagePackRooms)) { |
| 167 | if (auto tmp = |
| 168 | std::get_if<mtx::events::EphemeralEvent<mtx::events::msc2545::ImagePackRooms>>( |
| 169 | &*roomPacks)) { |
| 170 | content = tmp->content; |
| 171 | } |
| 172 | } |
| 173 | |
| 174 | if (enabled) |
| 175 | content.rooms[roomid_][statekey_] = {}; |
| 176 | else |
| 177 | content.rooms[roomid_].erase(statekey_); |
| 178 | |
| 179 | http::client()->put_account_data(content, [](mtx::http::RequestErr) { |
| 180 | // emit this->globallyEnabledChanged(); |
| 181 | }); |
| 182 | } |
| 183 | |
| 184 | bool |
| 185 | SingleImagePackModel::canEdit() const |
| 186 | { |
| 187 | if (roomid_.empty()) |
| 188 | return true; |
| 189 | else |
| 190 | return Permissions(QString::fromStdString(roomid_)) |
| 191 | .canChange(qml_mtx_events::ImagePackInRoom); |
| 192 | } |
| 193 | |
| 194 | void |
| 195 | SingleImagePackModel::setPackname(QString val) |
| 196 | { |
| 197 | auto val_ = val.toStdString(); |
| 198 | if (val_ != this->pack.pack->display_name) { |
| 199 | this->pack.pack->display_name = val_; |
| 200 | emit packnameChanged(); |
| 201 | } |
| 202 | } |
| 203 | |
| 204 | void |
| 205 | SingleImagePackModel::setAttribution(QString val) |
| 206 | { |
| 207 | auto val_ = val.toStdString(); |
| 208 | if (val_ != this->pack.pack->attribution) { |
| 209 | this->pack.pack->attribution = val_; |
| 210 | emit attributionChanged(); |
| 211 | } |
| 212 | } |
| 213 | |
| 214 | void |
| 215 | SingleImagePackModel::setAvatarUrl(QString val) |
| 216 | { |
| 217 | auto val_ = val.toStdString(); |
| 218 | if (val_ != this->pack.pack->avatar_url) { |
| 219 | this->pack.pack->avatar_url = val_; |
| 220 | emit avatarUrlChanged(); |
| 221 | } |
| 222 | } |
| 223 | |
| 224 | QString |
| 225 | SingleImagePackModel::avatarUrl() const |
| 226 | { |
| 227 | if (!pack.pack->avatar_url.empty()) |
| 228 | return QString::fromStdString(pack.pack->avatar_url); |
| 229 | else if (!pack.images.empty()) |
| 230 | return QString::fromStdString(pack.images.begin()->second.url); |
| 231 | else |
| 232 | return QString(); |
| 233 | } |
| 234 | |
| 235 | void |
| 236 | SingleImagePackModel::setStatekey(QString val) |
| 237 | { |
| 238 | auto val_ = val.toStdString(); |
| 239 | if (val_ != statekey_) { |
| 240 | statekey_ = val_; |
| 241 | |
| 242 | // prevent deleting current pack |
| 243 | if (!roomid_.empty() && statekey_ != old_statekey_) { |
| 244 | statekey_ = unconflictingStatekey(roomid_, statekey_); |
| 245 | } |
| 246 | |
| 247 | emit statekeyChanged(); |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | void |
| 252 | SingleImagePackModel::setIsStickerPack(bool val) |
| 253 | { |
| 254 | using mtx::events::msc2545::PackUsage; |
| 255 | if (val != pack.pack->is_sticker()) { |
| 256 | pack.pack->usage.set(PackUsage::Sticker, val); |
| 257 | if (!val) |
| 258 | pack.pack->usage.set(PackUsage::Emoji, true); |
| 259 | emit isEmotePackChanged(); |
| 260 | emit isStickerPackChanged(); |
| 261 | } |
| 262 | } |
| 263 | |
| 264 | void |
| 265 | SingleImagePackModel::setIsEmotePack(bool val) |
| 266 | { |
| 267 | using mtx::events::msc2545::PackUsage; |
| 268 | if (val != pack.pack->is_emoji()) { |
| 269 | pack.pack->usage.set(PackUsage::Emoji, val); |
| 270 | if (!val) |
| 271 | pack.pack->usage.set(PackUsage::Sticker, true); |
| 272 | emit isEmotePackChanged(); |
| 273 | emit isStickerPackChanged(); |
| 274 | } |
| 275 | } |
| 276 | |
| 277 | void |
| 278 | SingleImagePackModel::save() |
| 279 | { |
| 280 | if (roomid_.empty()) { |
| 281 | http::client()->put_account_data(pack, [](mtx::http::RequestErr e) { |
| 282 | if (e) |
| 283 | ChatPage::instance()->showNotification( |
| 284 | tr("Failed to update image pack: %1") |
| 285 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 286 | }); |
| 287 | } else { |
| 288 | if (old_statekey_ != statekey_) { |
| 289 | this->remove(); |
| 290 | } |
| 291 | |
| 292 | http::client()->send_state_event( |
| 293 | roomid_, |
| 294 | statekey_, |
| 295 | pack, |
| 296 | [this](const mtx::responses::EventId &, mtx::http::RequestErr e) { |
| 297 | if (e) |
| 298 | ChatPage::instance()->showNotification( |
| 299 | tr("Failed to update image pack: %1") |
| 300 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 301 | |
| 302 | nhlog::net()->info("Uploaded image pack: %1", statekey_); |
| 303 | }); |
| 304 | } |
| 305 | } |
| 306 | |
| 307 | void |
| 308 | SingleImagePackModel::remove() |
| 309 | { |
| 310 | // handle account pack deletion. |
| 311 | // Sadly we cannot actually delete the pack, |
| 312 | // so we just send an empty pack to clear out its information. |
| 313 | if (roomid_.empty()) { |
| 314 | http::client()->put_account_data( |
| 315 | mtx::events::msc2545::ImagePack(), [](mtx::http::RequestErr e) { |
| 316 | if (e) |
| 317 | ChatPage::instance()->showNotification( |
| 318 | tr("Failed to update image pack: %1") |
| 319 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 320 | }); |
| 321 | return; |
| 322 | } |
| 323 | |
| 324 | http::client()->send_state_event( |
| 325 | roomid_, |
| 326 | to_string(mtx::events::EventType::ImagePackInRoom), |
| 327 | old_statekey_, |
| 328 | nlohmann::json::object(), |
| 329 | [](const mtx::responses::EventId &, mtx::http::RequestErr e) { |
| 330 | if (e) |
| 331 | ChatPage::instance()->showNotification( |
| 332 | tr("Failed to delete old image pack: %1") |
| 333 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 334 | }); |
| 335 | old_statekey_ = statekey_; |
| 336 | } |
| 337 | |
| 338 | void |
| 339 | SingleImagePackModel::addStickers(QList<QUrl> files) |
| 340 | { |
| 341 | for (const auto &f : files) { |
| 342 | auto file = QFile(f.toLocalFile()); |
| 343 | if (!file.open(QFile::ReadOnly)) { |
| 344 | ChatPage::instance()->showNotification( |
| 345 | tr("Failed to open image: %1").arg(f.toLocalFile())); |
| 346 | return; |
| 347 | } |
| 348 | |
| 349 | auto bytes = file.readAll(); |
| 350 | auto img = utils::readImage(bytes); |
| 351 | |
| 352 | mtx::common::ImageInfo info{}; |
| 353 | |
| 354 | auto sz = img.size() / 2; |
| 355 | if (sz.width() > 512 || sz.height() > 512) { |
| 356 | sz.scale(512, 512, Qt::AspectRatioMode::KeepAspectRatio); |
| 357 | } else if (img.height() < 128 && img.width() < 128) { |
| 358 | sz = img.size(); |
| 359 | } |
| 360 | |
| 361 | info.h = sz.height(); |
| 362 | info.w = sz.width(); |
| 363 | info.size = bytes.size(); |
| 364 | info.mimetype = QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString(); |
| 365 | |
| 366 | auto filename = f.fileName().toStdString(); |
| 367 | auto basename = QFileInfo(file).baseName().toStdString(); |
| 368 | http::client()->upload( |
| 369 | bytes.toStdString(), |
| 370 | QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString(), |
| 371 | filename, |
| 372 | [this, basename, info](const mtx::responses::ContentURI &uri, mtx::http::RequestErr e) { |
| 373 | if (e) { |
| 374 | ChatPage::instance()->showNotification( |
| 375 | tr("Failed to upload image: %1") |
| 376 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 377 | return; |
| 378 | } |
| 379 | |
| 380 | emit addImage(uri.content_uri, basename, info); |
| 381 | }); |
| 382 | } |
| 383 | } |
| 384 | |
| 385 | void |
| 386 | SingleImagePackModel::setAvatar(QUrl f) |
| 387 | { |
| 388 | auto file = QFile(f.toLocalFile()); |
| 389 | if (!file.open(QFile::ReadOnly)) { |
| 390 | ChatPage::instance()->showNotification(tr("Failed to open image: %1").arg(f.toLocalFile())); |
| 391 | return; |
| 392 | } |
| 393 | |
| 394 | auto bytes = file.readAll(); |
| 395 | |
| 396 | auto filename = f.fileName().toStdString(); |
| 397 | http::client()->upload( |
| 398 | bytes.toStdString(), |
| 399 | QMimeDatabase().mimeTypeForFile(f.toLocalFile()).name().toStdString(), |
| 400 | filename, |
| 401 | [this, filename](const mtx::responses::ContentURI &uri, mtx::http::RequestErr e) { |
| 402 | if (e) { |
| 403 | ChatPage::instance()->showNotification( |
| 404 | tr("Failed to upload image: %1") |
| 405 | .arg(QString::fromStdString(e->matrix_error.error))); |
| 406 | return; |
| 407 | } |
| 408 | |
| 409 | emit avatarUploaded(QString::fromStdString(uri.content_uri)); |
| 410 | }); |
| 411 | } |
| 412 | |
| 413 | void |
| 414 | SingleImagePackModel::remove(int idx) |
| 415 | { |
| 416 | if (idx < (int)shortcodes.size() && idx >= 0) { |
| 417 | beginRemoveRows(QModelIndex(), idx, idx); |
| 418 | auto s = shortcodes.at(idx); |
| 419 | shortcodes.erase(shortcodes.begin() + idx); |
| 420 | pack.images.erase(s); |
| 421 | endRemoveRows(); |
| 422 | } |
| 423 | } |
| 424 | |
| 425 | std::string |
| 426 | SingleImagePackModel::unconflictingShortcode(const std::string &shortcode) |
| 427 | { |
| 428 | if (pack.images.count(shortcode)) { |
| 429 | // more images won't fit in an event anyway |
| 430 | for (int i = 0; i < 64'000; i++) { |
| 431 | auto tempCode = shortcode + std::to_string(i); |
| 432 | if (!pack.images.count(tempCode)) { |
| 433 | return tempCode; |
| 434 | } |
| 435 | } |
| 436 | } |
| 437 | return shortcode; |
| 438 | } |
| 439 | |
| 440 | std::string |
| 441 | SingleImagePackModel::unconflictingStatekey(const std::string &roomid, const std::string &key) |
| 442 | { |
| 443 | if (roomid.empty()) |
| 444 | return key; |
| 445 | |
| 446 | std::unordered_set<std::string> statekeys; |
| 447 | auto currentPacks = |
| 448 | cache::client()->getStateEventsWithType<mtx::events::msc2545::ImagePack>(roomid); |
| 449 | for (const auto &pack : currentPacks) { |
| 450 | if (!pack.content.images.empty()) |
| 451 | statekeys.insert(pack.state_key); |
| 452 | } |
| 453 | auto defaultPack = cache::client()->getStateEvent<mtx::events::msc2545::ImagePack>(roomid); |
| 454 | if (defaultPack && defaultPack->content.images.size()) { |
| 455 | statekeys.insert(defaultPack->state_key); |
| 456 | } |
| 457 | |
| 458 | if (statekeys.count(key)) { |
| 459 | // arbitrary count. More than 64k image packs in a room are unlikely and if you have that, |
| 460 | // you probably know what you are doing :) |
| 461 | for (int i = 0; i < 64'000; i++) { |
| 462 | auto tempCode = key + std::to_string(i); |
| 463 | if (!statekeys.count(tempCode)) { |
| 464 | return tempCode; |
| 465 | } |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return key; |
| 470 | } |
| 471 | |
| 472 | void |
| 473 | SingleImagePackModel::addImageCb(std::string uri, std::string filename, mtx::common::ImageInfo info) |
| 474 | { |
| 475 | mtx::events::msc2545::PackImage img{}; |
| 476 | img.url = uri; |
| 477 | img.info = info; |
| 478 | beginInsertRows( |
| 479 | QModelIndex(), static_cast<int>(shortcodes.size()), static_cast<int>(shortcodes.size())); |
| 480 | |
| 481 | auto shortcode = unconflictingShortcode(filename); |
| 482 | |
| 483 | pack.images[shortcode] = img; |
| 484 | shortcodes.push_back(shortcode); |
| 485 | |
| 486 | endInsertRows(); |
| 487 | |
| 488 | if (this->pack.pack->avatar_url.empty()) |
| 489 | this->setAvatarUrl(QString::fromStdString(uri)); |
| 490 | } |
| 491 | |
| 492 | #include "moc_SingleImagePackModel.cpp" |