data_message_reaction_id.cpp 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. /*
  2. This file is part of Telegram Desktop,
  3. the official desktop application for the Telegram messaging service.
  4. For license and copyright information please follow this link:
  5. https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
  6. */
  7. #include "data/data_message_reaction_id.h"
  8. #include "data/stickers/data_custom_emoji.h"
  9. namespace Data {
  10. QString SearchTagToQuery(const ReactionId &tagId) {
  11. if (const auto customId = tagId.custom()) {
  12. return u"#tag-custom:%1"_q.arg(customId);
  13. } else if (!tagId) {
  14. return QString();
  15. }
  16. return u"#tag-emoji:"_q + tagId.emoji();
  17. }
  18. ReactionId SearchTagFromQuery(const QString &query) {
  19. const auto list = query.split(QChar(' '));
  20. const auto tag = list.isEmpty() ? QString() : list[0];
  21. if (tag.startsWith(u"#tag-custom:"_q)) {
  22. return ReactionId{ DocumentId(tag.mid(12).toULongLong()) };
  23. } else if (tag.startsWith(u"#tag-emoji:"_q)) {
  24. return ReactionId{ tag.mid(11) };
  25. }
  26. return {};
  27. }
  28. std::vector<ReactionId> SearchTagsFromQuery(
  29. const QString &query) {
  30. auto result = std::vector<ReactionId>();
  31. if (const auto tag = SearchTagFromQuery(query)) {
  32. result.push_back(tag);
  33. }
  34. return result;
  35. }
  36. HashtagWithUsername HashtagWithUsernameFromQuery(QStringView query) {
  37. const auto match = TextUtilities::RegExpHashtag(true).match(query);
  38. if (match.hasMatch()) {
  39. const auto username = match.capturedView(2).mid(1).toString();
  40. const auto offset = int(match.capturedLength(1));
  41. const auto full = int(query.size());
  42. const auto length = full
  43. - int(username.size())
  44. - 1
  45. - offset
  46. - int(match.capturedLength(3));
  47. if (!username.isEmpty() && length > 0 && offset + length <= full) {
  48. const auto hashtag = query.mid(offset, length).toString();
  49. return { hashtag, username };
  50. }
  51. }
  52. return {};
  53. }
  54. QString ReactionEntityData(const ReactionId &id) {
  55. if (id.empty()) {
  56. return {};
  57. } else if (const auto custom = id.custom()) {
  58. return SerializeCustomEmojiId(custom);
  59. }
  60. return u"default:"_q + id.emoji();
  61. }
  62. ReactionId ReactionFromMTP(const MTPReaction &reaction) {
  63. return reaction.match([](MTPDreactionEmpty) {
  64. return ReactionId{ QString() };
  65. }, [](const MTPDreactionEmoji &data) {
  66. return ReactionId{ qs(data.vemoticon()) };
  67. }, [](const MTPDreactionCustomEmoji &data) {
  68. return ReactionId{ DocumentId(data.vdocument_id().v) };
  69. }, [](const MTPDreactionPaid &) {
  70. return ReactionId::Paid();
  71. });
  72. }
  73. MTPReaction ReactionToMTP(ReactionId id) {
  74. if (!id) {
  75. return MTP_reactionEmpty();
  76. } else if (id.paid()) {
  77. return MTP_reactionPaid();
  78. } else if (const auto custom = id.custom()) {
  79. return MTP_reactionCustomEmoji(MTP_long(custom));
  80. } else {
  81. return MTP_reactionEmoji(MTP_string(id.emoji()));
  82. }
  83. }
  84. } // namespace Data