| 1 | // SPDX-FileCopyrightText: Nheko Contributors |
| 2 | // |
| 3 | // SPDX-License-Identifier: GPL-3.0-or-later |
| 4 | |
| 5 | #include "EventAccessors.h" |
| 6 | |
| 7 | #include <nlohmann/json.hpp> |
| 8 | |
| 9 | #include <algorithm> |
| 10 | #include <cctype> |
| 11 | #include <type_traits> |
| 12 | |
| 13 | #include <mtx/events/collections.hpp> |
| 14 | |
| 15 | namespace { |
| 16 | |
| 17 | struct IsStateEvent |
| 18 | { |
| 19 | template<class T> |
| 20 | constexpr bool operator()(const mtx::events::StateEvent<T> &) |
| 21 | { |
| 22 | return true; |
| 23 | } |
| 24 | template<class T> |
| 25 | constexpr bool operator()(const mtx::events::Event<T> &) |
| 26 | { |
| 27 | return false; |
| 28 | } |
| 29 | }; |
| 30 | |
| 31 | struct EventMsgType |
| 32 | { |
| 33 | template<class T> |
| 34 | mtx::events::MessageType operator()(const mtx::events::Event<T> &e) |
| 35 | { |
| 36 | if constexpr (requires(decltype(e) t) { t.content.msgtype.value(); }) |
| 37 | return mtx::events::getMessageType(e.content.msgtype.value()); |
| 38 | else if constexpr (requires(decltype(e) t) { std::string{t.content.msgtype}; }) |
| 39 | return mtx::events::getMessageType(e.content.msgtype); |
| 40 | return mtx::events::MessageType::Unknown; |
| 41 | } |
| 42 | }; |
| 43 | |
| 44 | struct EventType |
| 45 | { |
| 46 | template<class T> |
| 47 | mtx::events::EventType operator()(const mtx::events::Event<T> &e) |
| 48 | { |
| 49 | return e.type; |
| 50 | } |
| 51 | }; |
| 52 | |
| 53 | struct CallType |
| 54 | { |
| 55 | template<class T> |
| 56 | std::string operator()(const T &e) |
| 57 | { |
| 58 | if constexpr (std::is_same_v<mtx::events::RoomEvent<mtx::events::voip::CallInvite>, T>) { |
| 59 | const char video[] = "m=video"; |
| 60 | const std::string &sdp = e.content.offer.sdp; |
| 61 | return std::search(sdp.cbegin(), |
| 62 | sdp.cend(), |
| 63 | std::cbegin(video), |
| 64 | std::cend(video) - 1, |
| 65 | [](unsigned char c1, unsigned char c2) { |
| 66 | return std::tolower(c1) == std::tolower(c2); |
| 67 | }) != sdp.cend() |
| 68 | ? "video" |
| 69 | : "voice"; |
| 70 | } |
| 71 | return ""; |
| 72 | } |
| 73 | }; |
| 74 | |
| 75 | struct EventBody |
| 76 | { |
| 77 | template<class T> |
| 78 | const std::string *operator()(const mtx::events::Event<T> &e) |
| 79 | { |
| 80 | if constexpr (requires(decltype(e) t) { t.content.body.value(); }) |
| 81 | return e.content.body ? &e.content.body.value() : nullptr; |
| 82 | else if constexpr (requires(decltype(e) t) { std::string{t.content.body}; }) |
| 83 | return &e.content.body; |
| 84 | return nullptr; |
| 85 | } |
| 86 | }; |
| 87 | |
| 88 | struct EventFormattedBody |
| 89 | { |
| 90 | template<class T> |
| 91 | const std::string *operator()(const mtx::events::RoomEvent<T> &e) |
| 92 | { |
| 93 | if constexpr (requires { T::formatted_body; }) { |
| 94 | if (e.content.format == "org.matrix.custom.html") |
| 95 | return &e.content.formatted_body; |
| 96 | } |
| 97 | return nullptr; |
| 98 | } |
| 99 | }; |
| 100 | |
| 101 | struct EventFile |
| 102 | { |
| 103 | template<class T> |
| 104 | const std::optional<mtx::crypto::EncryptedFile> *operator()(const mtx::events::Event<T> &e) |
| 105 | { |
| 106 | if constexpr (requires { T::file; }) |
| 107 | return &e.content.file; |
| 108 | return nullptr; |
| 109 | } |
| 110 | }; |
| 111 | |
| 112 | struct EventThumbnailFile |
| 113 | { |
| 114 | template<class T> |
| 115 | std::optional<mtx::crypto::EncryptedFile> operator()(const mtx::events::Event<T> &e) |
| 116 | { |
| 117 | if constexpr (requires { e.content.info.thumbnail_file; }) |
| 118 | return e.content.info.thumbnail_file; |
| 119 | return std::nullopt; |
| 120 | } |
| 121 | }; |
| 122 | |
| 123 | struct EventUrl |
| 124 | { |
| 125 | template<class T> |
| 126 | std::string operator()(const mtx::events::Event<T> &e) |
| 127 | { |
| 128 | if constexpr (requires { T::url; }) { |
| 129 | if (auto file = EventFile{}(e); file && *file) |
| 130 | return (*file)->url; |
| 131 | return e.content.url; |
| 132 | } |
| 133 | return ""; |
| 134 | } |
| 135 | }; |
| 136 | |
| 137 | struct EventThumbnailUrl |
| 138 | { |
| 139 | template<class T> |
| 140 | std::string operator()(const mtx::events::Event<T> &e) |
| 141 | { |
| 142 | if constexpr (requires { e.content.info.thumbnail_url; }) { |
| 143 | if (auto file = EventThumbnailFile{}(e)) |
| 144 | return file->url; |
| 145 | return e.content.info.thumbnail_url; |
| 146 | } |
| 147 | return ""; |
| 148 | } |
| 149 | }; |
| 150 | |
| 151 | struct EventDuration |
| 152 | { |
| 153 | template<class T> |
| 154 | uint64_t operator()(const mtx::events::Event<T> &e) |
| 155 | { |
| 156 | if constexpr (requires { e.content.info.duration; }) { |
| 157 | return e.content.info.duration; |
| 158 | } |
| 159 | return 0; |
| 160 | } |
| 161 | }; |
| 162 | |
| 163 | struct EventBlurhash |
| 164 | { |
| 165 | template<class T> |
| 166 | std::string operator()(const mtx::events::Event<T> &e) |
| 167 | { |
| 168 | if constexpr (requires { e.content.info.blurhash; }) { |
| 169 | return e.content.info.blurhash; |
| 170 | } |
| 171 | return ""; |
| 172 | } |
| 173 | }; |
| 174 | |
| 175 | struct EventFilename |
| 176 | { |
| 177 | template<class T> |
| 178 | std::string operator()(const mtx::events::Event<T> &) |
| 179 | { |
| 180 | return ""; |
| 181 | } |
| 182 | std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::Audio> &e) |
| 183 | { |
| 184 | // body may be the original filename |
| 185 | return e.content.body; |
| 186 | } |
| 187 | std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::Video> &e) |
| 188 | { |
| 189 | // body may be the original filename |
| 190 | return e.content.body; |
| 191 | } |
| 192 | std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::Image> &e) |
| 193 | { |
| 194 | // body may be the original filename |
| 195 | return e.content.body; |
| 196 | } |
| 197 | std::string operator()(const mtx::events::RoomEvent<mtx::events::msg::File> &e) |
| 198 | { |
| 199 | // body may be the original filename |
| 200 | if (!e.content.filename.empty()) |
| 201 | return e.content.filename; |
| 202 | return e.content.body; |
| 203 | } |
| 204 | }; |
| 205 | |
| 206 | struct EventMimeType |
| 207 | { |
| 208 | template<class T> |
| 209 | std::string operator()(const mtx::events::Event<T> &e) |
| 210 | { |
| 211 | if constexpr (requires { e.content.info.mimetype; }) { |
| 212 | return e.content.info.mimetype; |
| 213 | } |
| 214 | return ""; |
| 215 | } |
| 216 | }; |
| 217 | |
| 218 | struct EventFilesize |
| 219 | { |
| 220 | template<class T> |
| 221 | int64_t operator()(const mtx::events::RoomEvent<T> &e) |
| 222 | { |
| 223 | if constexpr (requires { e.content.info.size; }) { |
| 224 | return e.content.info.size; |
| 225 | } |
| 226 | return 0; |
| 227 | } |
| 228 | }; |
| 229 | |
| 230 | struct EventRelations |
| 231 | { |
| 232 | inline const static mtx::common::Relations empty; |
| 233 | |
| 234 | template<class T> |
| 235 | const mtx::common::Relations &operator()(const mtx::events::Event<T> &e) |
| 236 | { |
| 237 | if constexpr (requires { T::relations; }) { |
| 238 | return e.content.relations; |
| 239 | } |
| 240 | return empty; |
| 241 | } |
| 242 | }; |
| 243 | |
| 244 | struct EventMentions |
| 245 | { |
| 246 | template<class T> |
| 247 | std::optional<mtx::common::Mentions> operator()(const mtx::events::Event<T> &e) |
| 248 | { |
| 249 | if constexpr (requires { T::mentions; }) { |
| 250 | return e.content.mentions; |
| 251 | } |
| 252 | return std::nullopt; |
| 253 | } |
| 254 | }; |
| 255 | |
| 256 | struct SetEventRelations |
| 257 | { |
| 258 | mtx::common::Relations new_relations; |
| 259 | template<class T> |
| 260 | void operator()(mtx::events::Event<T> &e) |
| 261 | { |
| 262 | if constexpr (requires { T::relations; }) { |
| 263 | e.content.relations = std::move(new_relations); |
| 264 | } |
| 265 | } |
| 266 | }; |
| 267 | |
| 268 | struct EventTransactionId |
| 269 | { |
| 270 | template<class T> |
| 271 | std::string operator()(const mtx::events::RoomEvent<T> &e) |
| 272 | { |
| 273 | return e.unsigned_data.transaction_id; |
| 274 | } |
| 275 | template<class T> |
| 276 | std::string operator()(const mtx::events::Event<T> &e) |
| 277 | { |
| 278 | return e.unsigned_data.transaction_id; |
| 279 | } |
| 280 | }; |
| 281 | |
| 282 | struct EventMediaHeight |
| 283 | { |
| 284 | template<class T> |
| 285 | uint64_t operator()(const mtx::events::Event<T> &e) |
| 286 | { |
| 287 | if constexpr (requires { e.content.info.h; }) { |
| 288 | return e.content.info.h; |
| 289 | } |
| 290 | return -1; |
| 291 | } |
| 292 | }; |
| 293 | |
| 294 | struct EventMediaWidth |
| 295 | { |
| 296 | template<class T> |
| 297 | uint64_t operator()(const mtx::events::Event<T> &e) |
| 298 | { |
| 299 | if constexpr (requires { e.content.info.h; }) { |
| 300 | return e.content.info.w; |
| 301 | } |
| 302 | return -1; |
| 303 | } |
| 304 | }; |
| 305 | |
| 306 | template<class T> |
| 307 | double |
| 308 | eventPropHeight(const mtx::events::RoomEvent<T> &e) |
| 309 | { |
| 310 | auto w = eventWidth(e); |
| 311 | if (w == 0) |
| 312 | w = 1; |
| 313 | |
| 314 | double prop = eventHeight(e) / (double)w; |
| 315 | |
| 316 | return prop > 0 ? prop : 1.; |
| 317 | } |
| 318 | } |
| 319 | |
| 320 | const std::string & |
| 321 | mtx::accessors::event_id(const mtx::events::collections::TimelineEvents &event) |
| 322 | { |
| 323 | return std::visit([](const auto &e) -> const std::string & { return e.event_id; }, event); |
| 324 | } |
| 325 | const std::string & |
| 326 | mtx::accessors::room_id(const mtx::events::collections::TimelineEvents &event) |
| 327 | { |
| 328 | return std::visit([](const auto &e) -> const std::string & { return e.room_id; }, event); |
| 329 | } |
| 330 | |
| 331 | const std::string & |
| 332 | mtx::accessors::sender(const mtx::events::collections::TimelineEvents &event) |
| 333 | { |
| 334 | return std::visit([](const auto &e) -> const std::string & { return e.sender; }, event); |
| 335 | } |
| 336 | |
| 337 | QDateTime |
| 338 | mtx::accessors::origin_server_ts(const mtx::events::collections::TimelineEvents &event) |
| 339 | { |
| 340 | return QDateTime::fromMSecsSinceEpoch(origin_server_ts_ms(event)); |
| 341 | } |
| 342 | |
| 343 | std::uint64_t |
| 344 | mtx::accessors::origin_server_ts_ms(const mtx::events::collections::TimelineEvents &event) |
| 345 | { |
| 346 | return std::visit([](const auto &e) { return e.origin_server_ts; }, event); |
| 347 | } |
| 348 | |
| 349 | std::string |
| 350 | mtx::accessors::filename(const mtx::events::collections::TimelineEvents &event) |
| 351 | { |
| 352 | return std::visit(EventFilename{}, event); |
| 353 | } |
| 354 | |
| 355 | mtx::events::EventType |
| 356 | mtx::accessors::event_type(const mtx::events::collections::TimelineEvents &event) |
| 357 | { |
| 358 | return std::visit(EventType{}, event); |
| 359 | } |
| 360 | mtx::events::MessageType |
| 361 | mtx::accessors::msg_type(const mtx::events::collections::TimelineEvents &event) |
| 362 | { |
| 363 | return std::visit(EventMsgType{}, event); |
| 364 | } |
| 365 | |
| 366 | std::string |
| 367 | mtx::accessors::room_name(const mtx::events::collections::TimelineEvents &event) |
| 368 | { |
| 369 | if (auto c = std::get_if<mtx::events::StateEvent<mtx::events::state::Name>>(&event)) |
| 370 | return c->content.name; |
| 371 | else |
| 372 | return ""; |
| 373 | } |
| 374 | |
| 375 | std::string |
| 376 | mtx::accessors::room_topic(const mtx::events::collections::TimelineEvents &event) |
| 377 | { |
| 378 | if (auto c = std::get_if<mtx::events::StateEvent<mtx::events::state::Topic>>(&event)) |
| 379 | return c->content.topic; |
| 380 | else |
| 381 | return ""; |
| 382 | } |
| 383 | |
| 384 | std::string |
| 385 | mtx::accessors::call_type(const mtx::events::collections::TimelineEvents &event) |
| 386 | { |
| 387 | return std::visit(CallType{}, event); |
| 388 | } |
| 389 | |
| 390 | std::string |
| 391 | mtx::accessors::body(const mtx::events::collections::TimelineEvents &event) |
| 392 | { |
| 393 | auto body = std::visit(EventBody{}, event); |
| 394 | return body ? *body : std::string{}; |
| 395 | } |
| 396 | |
| 397 | std::string |
| 398 | mtx::accessors::formatted_body(const mtx::events::collections::TimelineEvents &event) |
| 399 | { |
| 400 | auto body = std::visit(EventFormattedBody{}, event); |
| 401 | return body ? *body : std::string{}; |
| 402 | } |
| 403 | |
| 404 | QString |
| 405 | mtx::accessors::formattedBodyWithFallback(const mtx::events::collections::TimelineEvents &event) |
| 406 | { |
| 407 | auto formatted = formatted_body(event); |
| 408 | if (!formatted.empty()) |
| 409 | return QString::fromStdString(formatted); |
| 410 | else |
| 411 | return QString::fromStdString(body(event)) |
| 412 | .toHtmlEscaped() |
| 413 | .replace(QLatin1String("\n"), QLatin1String("<br>")); |
| 414 | } |
| 415 | |
| 416 | std::optional<mtx::crypto::EncryptedFile> |
| 417 | mtx::accessors::file(const mtx::events::collections::TimelineEvents &event) |
| 418 | { |
| 419 | auto temp = std::visit(EventFile{}, event); |
| 420 | if (temp) |
| 421 | return *temp; |
| 422 | else |
| 423 | return {}; |
| 424 | } |
| 425 | |
| 426 | std::optional<mtx::crypto::EncryptedFile> |
| 427 | mtx::accessors::thumbnail_file(const mtx::events::collections::TimelineEvents &event) |
| 428 | { |
| 429 | return std::visit(EventThumbnailFile{}, event); |
| 430 | } |
| 431 | |
| 432 | std::string |
| 433 | mtx::accessors::url(const mtx::events::collections::TimelineEvents &event) |
| 434 | { |
| 435 | return std::visit(EventUrl{}, event); |
| 436 | } |
| 437 | std::string |
| 438 | mtx::accessors::thumbnail_url(const mtx::events::collections::TimelineEvents &event) |
| 439 | { |
| 440 | return std::visit(EventThumbnailUrl{}, event); |
| 441 | } |
| 442 | uint64_t |
| 443 | mtx::accessors::duration(const mtx::events::collections::TimelineEvents &event) |
| 444 | { |
| 445 | return std::visit(EventDuration{}, event); |
| 446 | } |
| 447 | std::string |
| 448 | mtx::accessors::blurhash(const mtx::events::collections::TimelineEvents &event) |
| 449 | { |
| 450 | return std::visit(EventBlurhash{}, event); |
| 451 | } |
| 452 | std::string |
| 453 | mtx::accessors::mimetype(const mtx::events::collections::TimelineEvents &event) |
| 454 | { |
| 455 | return std::visit(EventMimeType{}, event); |
| 456 | } |
| 457 | const mtx::common::Relations & |
| 458 | mtx::accessors::relations(const mtx::events::collections::TimelineEvents &event) |
| 459 | { |
| 460 | return std::visit(EventRelations{}, event); |
| 461 | } |
| 462 | std::optional<mtx::common::Mentions> |
| 463 | mtx::accessors::mentions(const mtx::events::collections::TimelineEvents &event) |
| 464 | { |
| 465 | return std::visit(EventMentions{}, event); |
| 466 | } |
| 467 | |
| 468 | void |
| 469 | mtx::accessors::set_relations(mtx::events::collections::TimelineEvents &event, |
| 470 | mtx::common::Relations relations) |
| 471 | { |
| 472 | std::visit(SetEventRelations{std::move(relations)}, event); |
| 473 | } |
| 474 | |
| 475 | std::string |
| 476 | mtx::accessors::transaction_id(const mtx::events::collections::TimelineEvents &event) |
| 477 | { |
| 478 | return std::visit(EventTransactionId{}, event); |
| 479 | } |
| 480 | |
| 481 | int64_t |
| 482 | mtx::accessors::filesize(const mtx::events::collections::TimelineEvents &event) |
| 483 | { |
| 484 | return std::visit(EventFilesize{}, event); |
| 485 | } |
| 486 | |
| 487 | uint64_t |
| 488 | mtx::accessors::media_height(const mtx::events::collections::TimelineEvents &event) |
| 489 | { |
| 490 | return std::visit(EventMediaHeight{}, event); |
| 491 | } |
| 492 | |
| 493 | uint64_t |
| 494 | mtx::accessors::media_width(const mtx::events::collections::TimelineEvents &event) |
| 495 | { |
| 496 | return std::visit(EventMediaWidth{}, event); |
| 497 | } |
| 498 | |
| 499 | nlohmann::json |
| 500 | mtx::accessors::serialize_event(const mtx::events::collections::TimelineEvents &event) |
| 501 | { |
| 502 | return nlohmann::json(event); |
| 503 | } |
| 504 | |
| 505 | bool |
| 506 | mtx::accessors::is_state_event(const mtx::events::collections::StateEvents &event) |
| 507 | { |
| 508 | return std::visit(IsStateEvent{}, event); |
| 509 | } |
| 510 | |
| 511 | bool |
| 512 | mtx::accessors::is_state_event(const mtx::events::collections::TimelineEvents &event) |
| 513 | { |
| 514 | return std::visit(IsStateEvent{}, event); |
| 515 | } |
| 516 | |
| 517 | template<typename T> |
| 518 | static constexpr auto |
| 519 | isMessage(const mtx::events::RoomEvent<T> &e) |
| 520 | -> std::enable_if_t<std::is_same<decltype(e.content.msgtype), std::string>::value, bool> |
| 521 | { |
| 522 | return true; |
| 523 | } |
| 524 | |
| 525 | template<typename T> |
| 526 | static constexpr auto |
| 527 | isMessage(const mtx::events::Event<T> &) |
| 528 | { |
| 529 | return false; |
| 530 | } |
| 531 | |
| 532 | template<typename T> |
| 533 | static constexpr auto |
| 534 | isMessage(const mtx::events::EncryptedEvent<T> &) |
| 535 | { |
| 536 | return true; |
| 537 | } |
| 538 | |
| 539 | static constexpr auto |
| 540 | isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallInvite> &) |
| 541 | { |
| 542 | return true; |
| 543 | } |
| 544 | |
| 545 | static constexpr auto |
| 546 | isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallAnswer> &) |
| 547 | { |
| 548 | return true; |
| 549 | } |
| 550 | static constexpr auto |
| 551 | isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallHangUp> &) |
| 552 | { |
| 553 | return true; |
| 554 | } |
| 555 | |
| 556 | static constexpr auto |
| 557 | isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallReject> &) |
| 558 | { |
| 559 | return true; |
| 560 | } |
| 561 | static constexpr auto |
| 562 | isMessage(const mtx::events::RoomEvent<mtx::events::voip::CallSelectAnswer> &) |
| 563 | { |
| 564 | return true; |
| 565 | } |
| 566 | |
| 567 | bool |
| 568 | mtx::accessors::is_message(const mtx::events::collections::TimelineEvents &event) |
| 569 | { |
| 570 | return std::visit([](const auto &e) { return isMessage(e); }, event); |
| 571 | } |