2017-06-22 15:11:41 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
2018-01-03 10:23:14 +00:00
|
|
|
the official desktop application for the Telegram messaging service.
|
2017-06-22 15:11:41 +00:00
|
|
|
|
2018-01-03 10:23:14 +00:00
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
2017-06-22 15:11:41 +00:00
|
|
|
*/
|
|
|
|
#include "history/history_service.h"
|
|
|
|
|
|
|
|
#include "lang/lang_keys.h"
|
|
|
|
#include "mainwidget.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "auth_session.h"
|
2017-06-22 15:11:41 +00:00
|
|
|
#include "apiwrap.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "layout.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "history/history.h"
|
2017-06-22 15:11:41 +00:00
|
|
|
#include "history/history_media_types.h"
|
|
|
|
#include "history/history_message.h"
|
2017-12-18 15:44:50 +00:00
|
|
|
#include "history/history_item_components.h"
|
2018-01-09 17:08:31 +00:00
|
|
|
#include "history/view/history_view_service_message.h"
|
2018-01-05 15:57:18 +00:00
|
|
|
#include "data/data_feed.h"
|
2018-01-13 12:45:11 +00:00
|
|
|
#include "data/data_session.h"
|
2018-01-14 16:02:25 +00:00
|
|
|
#include "data/data_media_types.h"
|
2017-06-22 15:11:41 +00:00
|
|
|
#include "window/notifications_manager.h"
|
2018-01-11 19:33:26 +00:00
|
|
|
#include "window/window_controller.h"
|
2017-09-04 11:40:02 +00:00
|
|
|
#include "storage/storage_shared_media.h"
|
2017-12-28 13:06:06 +00:00
|
|
|
#include "ui/text_options.h"
|
2017-06-22 15:11:41 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
|
|
|
constexpr auto kPinnedMessageTextLimit = 16;
|
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
void HistoryService::setMessageByAction(const MTPmessageAction &action) {
|
|
|
|
auto prepareChatAddUserText = [this](const MTPDmessageActionChatAddUser &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
auto &users = action.vusers.v;
|
|
|
|
if (users.size() == 1) {
|
|
|
|
auto u = App::user(peerFromUser(users[0]));
|
|
|
|
if (u == _from) {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_user_joined(lt_from, fromLinkText());
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
2017-07-05 18:11:31 +00:00
|
|
|
result.links.push_back(u->createOpenLink());
|
2017-06-22 15:11:41 +00:00
|
|
|
result.text = lng_action_add_user(lt_from, fromLinkText(), lt_user, textcmdLink(2, u->name));
|
|
|
|
}
|
|
|
|
} else if (users.isEmpty()) {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_add_user(lt_from, fromLinkText(), lt_user, "somebody");
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
for (auto i = 0, l = users.size(); i != l; ++i) {
|
|
|
|
auto user = App::user(peerFromUser(users[i]));
|
2017-07-05 18:11:31 +00:00
|
|
|
result.links.push_back(user->createOpenLink());
|
2017-06-22 15:11:41 +00:00
|
|
|
|
|
|
|
auto linkText = textcmdLink(i + 2, user->name);
|
|
|
|
if (i == 0) {
|
|
|
|
result.text = linkText;
|
|
|
|
} else if (i + 1 == l) {
|
|
|
|
result.text = lng_action_add_users_and_last(lt_accumulated, result.text, lt_user, linkText);
|
|
|
|
} else {
|
|
|
|
result.text = lng_action_add_users_and_one(lt_accumulated, result.text, lt_user, linkText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
result.text = lng_action_add_users_many(lt_from, fromLinkText(), lt_users, result.text);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChatJoinedByLink = [this](const MTPDmessageActionChatJoinedByLink &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_user_joined_by_link(lt_from, fromLinkText());
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChatCreate = [this](const MTPDmessageActionChatCreate &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
result.links.push_back(fromLink());
|
2017-07-06 11:37:42 +00:00
|
|
|
result.text = lng_action_created_chat(lt_from, fromLinkText(), lt_title, TextUtilities::Clean(qs(action.vtitle)));
|
2017-06-22 15:11:41 +00:00
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChannelCreate = [this](const MTPDmessageActionChannelCreate &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
if (isPost()) {
|
|
|
|
result.text = lang(lng_action_created_channel);
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
2017-07-06 11:37:42 +00:00
|
|
|
result.text = lng_action_created_chat(lt_from, fromLinkText(), lt_title, TextUtilities::Clean(qs(action.vtitle)));
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChatDeletePhoto = [this] {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
if (isPost()) {
|
|
|
|
result.text = lang(lng_action_removed_photo_channel);
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_removed_photo(lt_from, fromLinkText());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChatDeleteUser = [this](const MTPDmessageActionChatDeleteUser &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
if (peerFromUser(action.vuser_id) == _from->id) {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_user_left(lt_from, fromLinkText());
|
|
|
|
} else {
|
|
|
|
auto user = App::user(peerFromUser(action.vuser_id));
|
|
|
|
result.links.push_back(fromLink());
|
2017-07-05 18:11:31 +00:00
|
|
|
result.links.push_back(user->createOpenLink());
|
2017-06-22 15:11:41 +00:00
|
|
|
result.text = lng_action_kick_user(lt_from, fromLinkText(), lt_user, textcmdLink(2, user->name));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChatEditPhoto = [this](const MTPDmessageActionChatEditPhoto &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
if (isPost()) {
|
|
|
|
result.text = lang(lng_action_changed_photo_channel);
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_changed_photo(lt_from, fromLinkText());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
|
|
|
auto prepareChatEditTitle = [this](const MTPDmessageActionChatEditTitle &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
if (isPost()) {
|
2017-07-06 11:37:42 +00:00
|
|
|
result.text = lng_action_changed_title_channel(lt_title, TextUtilities::Clean(qs(action.vtitle)));
|
2017-06-22 15:11:41 +00:00
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
2017-07-06 11:37:42 +00:00
|
|
|
result.text = lng_action_changed_title(lt_from, fromLinkText(), lt_title, TextUtilities::Clean(qs(action.vtitle)));
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-07-12 19:14:20 +00:00
|
|
|
auto prepareScreenshotTaken = [this] {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
if (out()) {
|
|
|
|
result.text = lang(lng_action_you_took_screenshot);
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_took_screenshot(lt_from, fromLinkText());
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-11-20 19:54:05 +00:00
|
|
|
auto prepareCustomAction = [&](const MTPDmessageActionCustomAction &action) {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
result.text = qs(action.vmessage);
|
|
|
|
return result;
|
|
|
|
};
|
|
|
|
|
2017-06-22 15:11:41 +00:00
|
|
|
auto messageText = PreparedText {};
|
|
|
|
|
|
|
|
switch (action.type()) {
|
|
|
|
case mtpc_messageActionChatAddUser: messageText = prepareChatAddUserText(action.c_messageActionChatAddUser()); break;
|
|
|
|
case mtpc_messageActionChatJoinedByLink: messageText = prepareChatJoinedByLink(action.c_messageActionChatJoinedByLink()); break;
|
|
|
|
case mtpc_messageActionChatCreate: messageText = prepareChatCreate(action.c_messageActionChatCreate()); break;
|
|
|
|
case mtpc_messageActionChannelCreate: messageText = prepareChannelCreate(action.c_messageActionChannelCreate()); break;
|
|
|
|
case mtpc_messageActionHistoryClear: break; // Leave empty text.
|
|
|
|
case mtpc_messageActionChatDeletePhoto: messageText = prepareChatDeletePhoto(); break;
|
|
|
|
case mtpc_messageActionChatDeleteUser: messageText = prepareChatDeleteUser(action.c_messageActionChatDeleteUser()); break;
|
|
|
|
case mtpc_messageActionChatEditPhoto: messageText = prepareChatEditPhoto(action.c_messageActionChatEditPhoto()); break;
|
|
|
|
case mtpc_messageActionChatEditTitle: messageText = prepareChatEditTitle(action.c_messageActionChatEditTitle()); break;
|
|
|
|
case mtpc_messageActionChatMigrateTo: messageText.text = lang(lng_action_group_migrate); break;
|
|
|
|
case mtpc_messageActionChannelMigrateFrom: messageText.text = lang(lng_action_group_migrate); break;
|
|
|
|
case mtpc_messageActionPinMessage: messageText = preparePinnedText(); break;
|
|
|
|
case mtpc_messageActionGameScore: messageText = prepareGameScoreText(); break;
|
|
|
|
case mtpc_messageActionPhoneCall: Unexpected("PhoneCall type in HistoryService.");
|
|
|
|
case mtpc_messageActionPaymentSent: messageText = preparePaymentSentText(); break;
|
2017-07-12 19:14:20 +00:00
|
|
|
case mtpc_messageActionScreenshotTaken: messageText = prepareScreenshotTaken(); break;
|
2017-11-20 19:54:05 +00:00
|
|
|
case mtpc_messageActionCustomAction: messageText = prepareCustomAction(action.c_messageActionCustomAction()); break;
|
2017-06-22 15:11:41 +00:00
|
|
|
default: messageText.text = lang(lng_message_empty); break;
|
|
|
|
}
|
|
|
|
|
|
|
|
setServiceText(messageText);
|
|
|
|
|
|
|
|
// Additional information.
|
|
|
|
switch (action.type()) {
|
|
|
|
case mtpc_messageActionChatAddUser: {
|
|
|
|
if (auto channel = history()->peer->asMegagroup()) {
|
|
|
|
auto &users = action.c_messageActionChatAddUser().vusers;
|
|
|
|
for_const (auto &item, users.v) {
|
2017-08-04 14:54:32 +00:00
|
|
|
if (item.v == Auth().userId()) {
|
2017-06-22 15:11:41 +00:00
|
|
|
channel->mgInfo->joinedMessageFound = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatJoinedByLink: {
|
|
|
|
if (_from->isSelf() && history()->peer->isMegagroup()) {
|
|
|
|
history()->peer->asChannel()->mgInfo->joinedMessageFound = true;
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatEditPhoto: {
|
|
|
|
auto &photo = action.c_messageActionChatEditPhoto().vphoto;
|
|
|
|
if (photo.type() == mtpc_photo) {
|
2018-01-14 16:02:25 +00:00
|
|
|
_media = std::make_unique<Data::MediaPhoto>(
|
|
|
|
this,
|
|
|
|
history()->peer,
|
2018-01-17 16:21:01 +00:00
|
|
|
Auth().data().photo(photo.c_photo()));
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
case mtpc_messageActionChatMigrateTo:
|
|
|
|
case mtpc_messageActionChannelMigrateFrom: {
|
|
|
|
_flags |= MTPDmessage_ClientFlag::f_is_group_migrate;
|
|
|
|
} break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:09:55 +00:00
|
|
|
void HistoryService::setSelfDestruct(HistoryServiceSelfDestruct::Type type, int ttlSeconds) {
|
|
|
|
UpdateComponents(HistoryServiceSelfDestruct::Bit());
|
|
|
|
auto selfdestruct = Get<HistoryServiceSelfDestruct>();
|
|
|
|
selfdestruct->timeToLive = ttlSeconds * 1000LL;
|
|
|
|
selfdestruct->type = type;
|
|
|
|
}
|
|
|
|
|
2017-06-22 15:11:41 +00:00
|
|
|
bool HistoryService::updateDependent(bool force) {
|
|
|
|
auto dependent = GetDependentData();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(dependent != nullptr);
|
2017-06-22 15:11:41 +00:00
|
|
|
|
|
|
|
if (!force) {
|
|
|
|
if (!dependent->msgId || dependent->msg) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!dependent->lnk) {
|
|
|
|
dependent->lnk = goToMessageClickHandler(history()->peer, dependent->msgId);
|
|
|
|
}
|
2017-11-23 15:41:13 +00:00
|
|
|
auto gotDependencyItem = false;
|
2017-06-22 15:11:41 +00:00
|
|
|
if (!dependent->msg) {
|
|
|
|
dependent->msg = App::histItemById(channelId(), dependent->msgId);
|
|
|
|
if (dependent->msg) {
|
2017-11-23 15:41:13 +00:00
|
|
|
if (dependent->msg->isEmpty()) {
|
|
|
|
// Really it is deleted.
|
|
|
|
dependent->msg = nullptr;
|
|
|
|
force = true;
|
|
|
|
} else {
|
|
|
|
App::historyRegDependency(this, dependent->msg);
|
|
|
|
gotDependencyItem = true;
|
|
|
|
}
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if (dependent->msg) {
|
|
|
|
updateDependentText();
|
|
|
|
} else if (force) {
|
|
|
|
if (dependent->msgId > 0) {
|
|
|
|
dependent->msgId = 0;
|
|
|
|
gotDependencyItem = true;
|
|
|
|
}
|
|
|
|
updateDependentText();
|
|
|
|
}
|
|
|
|
if (force && gotDependencyItem) {
|
2017-08-04 14:54:32 +00:00
|
|
|
Auth().notifications().checkDelayed();
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
return (dependent->msg || !dependent->msgId);
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryService::PreparedText HistoryService::preparePinnedText() {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
auto pinned = Get<HistoryServicePinned>();
|
|
|
|
if (pinned && pinned->msg) {
|
2018-01-14 16:02:25 +00:00
|
|
|
const auto mediaText = [&] {
|
|
|
|
if (const auto media = pinned->msg->media()) {
|
|
|
|
return media->pinnedTextSubstring();
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
return QString();
|
2018-01-14 16:02:25 +00:00
|
|
|
}();
|
2017-06-22 15:11:41 +00:00
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.links.push_back(pinned->lnk);
|
|
|
|
if (mediaText.isEmpty()) {
|
|
|
|
auto original = pinned->msg->originalText().text;
|
|
|
|
auto cutAt = 0;
|
|
|
|
auto limit = kPinnedMessageTextLimit;
|
|
|
|
auto size = original.size();
|
|
|
|
for (; limit != 0;) {
|
|
|
|
--limit;
|
|
|
|
if (cutAt >= size) break;
|
|
|
|
if (original.at(cutAt).isLowSurrogate() && cutAt + 1 < size && original.at(cutAt + 1).isHighSurrogate()) {
|
|
|
|
cutAt += 2;
|
|
|
|
} else {
|
|
|
|
++cutAt;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (!limit && cutAt + 5 < size) {
|
|
|
|
original = original.mid(0, cutAt) + qstr("...");
|
|
|
|
}
|
|
|
|
result.text = lng_action_pinned_message(lt_from, fromLinkText(), lt_text, textcmdLink(2, original));
|
|
|
|
} else {
|
|
|
|
result.text = lng_action_pinned_media(lt_from, fromLinkText(), lt_media, textcmdLink(2, mediaText));
|
|
|
|
}
|
|
|
|
} else if (pinned && pinned->msgId) {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.links.push_back(pinned->lnk);
|
|
|
|
result.text = lng_action_pinned_media(lt_from, fromLinkText(), lt_media, textcmdLink(2, lang(lng_contacts_loading)));
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_action_pinned_media(lt_from, fromLinkText(), lt_media, lang(lng_deleted_message));
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryService::PreparedText HistoryService::prepareGameScoreText() {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
auto gamescore = Get<HistoryServiceGameScore>();
|
|
|
|
|
|
|
|
auto computeGameTitle = [gamescore, &result]() -> QString {
|
|
|
|
if (gamescore && gamescore->msg) {
|
2018-01-14 16:02:25 +00:00
|
|
|
if (const auto media = gamescore->msg->media()) {
|
|
|
|
if (const auto game = media->game()) {
|
2017-12-18 13:13:41 +00:00
|
|
|
const auto row = 0;
|
|
|
|
const auto column = 0;
|
|
|
|
result.links.push_back(
|
|
|
|
std::make_shared<ReplyMarkupClickHandler>(
|
|
|
|
row,
|
|
|
|
column,
|
|
|
|
gamescore->msg->fullId()));
|
2018-01-14 16:02:25 +00:00
|
|
|
auto titleText = game->title;
|
2017-06-22 15:11:41 +00:00
|
|
|
return textcmdLink(result.links.size(), titleText);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return lang(lng_deleted_message);
|
|
|
|
} else if (gamescore && gamescore->msgId) {
|
|
|
|
return lang(lng_contacts_loading);
|
|
|
|
}
|
|
|
|
return QString();
|
|
|
|
};
|
|
|
|
|
2017-12-18 13:13:41 +00:00
|
|
|
const auto scoreNumber = gamescore ? gamescore->score : 0;
|
2017-06-22 15:11:41 +00:00
|
|
|
if (_from->isSelf()) {
|
|
|
|
auto gameTitle = computeGameTitle();
|
|
|
|
if (gameTitle.isEmpty()) {
|
2017-12-18 13:13:41 +00:00
|
|
|
result.text = lng_action_game_you_scored_no_game(
|
|
|
|
lt_count,
|
|
|
|
scoreNumber);
|
2017-06-22 15:11:41 +00:00
|
|
|
} else {
|
2017-12-18 13:13:41 +00:00
|
|
|
result.text = lng_action_game_you_scored(
|
|
|
|
lt_count,
|
|
|
|
scoreNumber,
|
|
|
|
lt_game,
|
|
|
|
gameTitle);
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
auto gameTitle = computeGameTitle();
|
|
|
|
if (gameTitle.isEmpty()) {
|
2017-12-18 13:13:41 +00:00
|
|
|
result.text = lng_action_game_score_no_game(
|
|
|
|
lt_count,
|
|
|
|
scoreNumber,
|
|
|
|
lt_from,
|
|
|
|
fromLinkText());
|
2017-06-22 15:11:41 +00:00
|
|
|
} else {
|
2017-12-18 13:13:41 +00:00
|
|
|
result.text = lng_action_game_score(
|
|
|
|
lt_count,
|
|
|
|
scoreNumber,
|
|
|
|
lt_from,
|
|
|
|
fromLinkText(),
|
|
|
|
lt_game,
|
|
|
|
gameTitle);
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryService::PreparedText HistoryService::preparePaymentSentText() {
|
|
|
|
auto result = PreparedText {};
|
|
|
|
auto payment = Get<HistoryServicePayment>();
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
auto invoiceTitle = [&] {
|
2017-06-22 15:11:41 +00:00
|
|
|
if (payment && payment->msg) {
|
2018-01-14 16:02:25 +00:00
|
|
|
if (const auto media = payment->msg->media()) {
|
|
|
|
if (const auto invoice = media->invoice()) {
|
|
|
|
return invoice->title;
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return lang(lng_deleted_message);
|
|
|
|
} else if (payment && payment->msgId) {
|
|
|
|
return lang(lng_contacts_loading);
|
|
|
|
}
|
|
|
|
return QString();
|
2018-01-14 16:02:25 +00:00
|
|
|
}();
|
2017-06-22 15:11:41 +00:00
|
|
|
|
|
|
|
if (invoiceTitle.isEmpty()) {
|
|
|
|
result.text = lng_action_payment_done(lt_amount, payment->amount, lt_user, history()->peer->name);
|
|
|
|
} else {
|
|
|
|
result.text = lng_action_payment_done_for(lt_amount, payment->amount, lt_user, history()->peer->name, lt_invoice, invoiceTitle);
|
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
HistoryService::HistoryService(not_null<History*> history, const MTPDmessage &message) :
|
2017-07-17 20:09:55 +00:00
|
|
|
HistoryItem(history, message.vid.v, message.vflags.v, ::date(message.vdate), message.has_from_id() ? message.vfrom_id.v : 0) {
|
|
|
|
createFromMtp(message);
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
HistoryService::HistoryService(
|
|
|
|
not_null<History*> history,
|
|
|
|
const MTPDmessageService &message)
|
|
|
|
: HistoryItem(
|
|
|
|
history,
|
|
|
|
message.vid.v,
|
|
|
|
mtpCastFlags(message.vflags.v),
|
|
|
|
::date(message.vdate),
|
|
|
|
message.has_from_id() ? message.vfrom_id.v : 0) {
|
2017-06-22 15:11:41 +00:00
|
|
|
createFromMtp(message);
|
|
|
|
}
|
|
|
|
|
2018-01-14 16:02:25 +00:00
|
|
|
HistoryService::HistoryService(
|
|
|
|
not_null<History*> history,
|
|
|
|
MsgId msgId,
|
|
|
|
QDateTime date,
|
|
|
|
const PreparedText &message,
|
|
|
|
MTPDmessage::Flags flags,
|
|
|
|
UserId from,
|
|
|
|
PhotoData *photo)
|
|
|
|
: HistoryItem(history, msgId, flags, date, from) {
|
2017-06-22 15:11:41 +00:00
|
|
|
setServiceText(message);
|
|
|
|
if (photo) {
|
2018-01-14 16:02:25 +00:00
|
|
|
_media = std::make_unique<Data::MediaPhoto>(
|
|
|
|
this,
|
|
|
|
history->peer,
|
|
|
|
photo);
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bool HistoryService::updateDependencyItem() {
|
|
|
|
if (GetDependentData()) {
|
|
|
|
return updateDependent(true);
|
|
|
|
}
|
|
|
|
return HistoryItem::updateDependencyItem();
|
|
|
|
}
|
|
|
|
|
2017-09-05 17:21:56 +00:00
|
|
|
QString HistoryService::inDialogsText(DrawInDialog way) const {
|
2017-07-06 11:37:42 +00:00
|
|
|
return textcmdLink(1, TextUtilities::Clean(notificationText()));
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
QString HistoryService::inReplyText() const {
|
|
|
|
QString result = HistoryService::notificationText();
|
|
|
|
return result.trimmed().startsWith(author()->name) ? result.trimmed().mid(author()->name.size()).trimmed() : result;
|
|
|
|
}
|
|
|
|
|
2018-01-11 19:33:26 +00:00
|
|
|
std::unique_ptr<HistoryView::Element> HistoryService::createView(
|
2018-01-17 16:21:01 +00:00
|
|
|
not_null<HistoryView::ElementDelegate*> delegate) {
|
|
|
|
return delegate->elementCreate(this);
|
2018-01-11 19:33:26 +00:00
|
|
|
}
|
|
|
|
|
2017-06-22 15:11:41 +00:00
|
|
|
void HistoryService::setServiceText(const PreparedText &prepared) {
|
2017-12-28 13:06:06 +00:00
|
|
|
_text.setText(
|
|
|
|
st::serviceTextStyle,
|
|
|
|
prepared.text,
|
|
|
|
Ui::ItemTextServiceOptions());
|
2017-06-22 15:11:41 +00:00
|
|
|
auto linkIndex = 0;
|
|
|
|
for_const (auto &link, prepared.links) {
|
|
|
|
// Link indices start with 1.
|
|
|
|
_text.setLink(++linkIndex, link);
|
|
|
|
}
|
|
|
|
_textWidth = -1;
|
|
|
|
_textHeight = 0;
|
|
|
|
}
|
|
|
|
|
2017-07-17 20:09:55 +00:00
|
|
|
void HistoryService::markMediaAsReadHook() {
|
|
|
|
if (auto selfdestruct = Get<HistoryServiceSelfDestruct>()) {
|
|
|
|
if (!selfdestruct->destructAt) {
|
|
|
|
selfdestruct->destructAt = getms(true) + selfdestruct->timeToLive;
|
|
|
|
App::histories().selfDestructIn(this, selfdestruct->timeToLive);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
TimeMs HistoryService::getSelfDestructIn(TimeMs now) {
|
|
|
|
if (auto selfdestruct = Get<HistoryServiceSelfDestruct>()) {
|
|
|
|
if (selfdestruct->destructAt > 0) {
|
|
|
|
if (selfdestruct->destructAt <= now) {
|
|
|
|
auto text = [selfdestruct] {
|
|
|
|
switch (selfdestruct->type) {
|
|
|
|
case HistoryServiceSelfDestruct::Type::Photo: return lang(lng_ttl_photo_expired);
|
|
|
|
case HistoryServiceSelfDestruct::Type::Video: return lang(lng_ttl_video_expired);
|
|
|
|
}
|
|
|
|
Unexpected("Type in HistoryServiceSelfDestruct::Type");
|
|
|
|
};
|
|
|
|
setServiceText({ text() });
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return selfdestruct->destructAt - now;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryService::createFromMtp(const MTPDmessage &message) {
|
|
|
|
auto mediaType = message.vmedia.type();
|
|
|
|
switch (mediaType) {
|
|
|
|
case mtpc_messageMediaPhoto: {
|
|
|
|
if (message.is_media_unread()) {
|
|
|
|
auto &photo = message.vmedia.c_messageMediaPhoto();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(photo.has_ttl_seconds());
|
2017-07-17 20:09:55 +00:00
|
|
|
setSelfDestruct(HistoryServiceSelfDestruct::Type::Photo, photo.vttl_seconds.v);
|
|
|
|
if (out()) {
|
|
|
|
setServiceText({ lang(lng_ttl_photo_sent) });
|
|
|
|
} else {
|
|
|
|
auto result = PreparedText();
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_ttl_photo_received(lt_from, fromLinkText());
|
|
|
|
setServiceText(std::move(result));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setServiceText({ lang(lng_ttl_photo_expired) });
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
case mtpc_messageMediaDocument: {
|
|
|
|
if (message.is_media_unread()) {
|
|
|
|
auto &document = message.vmedia.c_messageMediaDocument();
|
2017-08-17 09:06:26 +00:00
|
|
|
Assert(document.has_ttl_seconds());
|
2017-07-17 20:09:55 +00:00
|
|
|
setSelfDestruct(HistoryServiceSelfDestruct::Type::Video, document.vttl_seconds.v);
|
|
|
|
if (out()) {
|
|
|
|
setServiceText({ lang(lng_ttl_video_sent) });
|
|
|
|
} else {
|
|
|
|
auto result = PreparedText();
|
|
|
|
result.links.push_back(fromLink());
|
|
|
|
result.text = lng_ttl_video_received(lt_from, fromLinkText());
|
|
|
|
setServiceText(std::move(result));
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
setServiceText({ lang(lng_ttl_video_expired) });
|
|
|
|
}
|
|
|
|
} break;
|
|
|
|
|
|
|
|
default: Unexpected("Media type in HistoryService::createFromMtp()");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-06-22 15:11:41 +00:00
|
|
|
void HistoryService::createFromMtp(const MTPDmessageService &message) {
|
|
|
|
if (message.vaction.type() == mtpc_messageActionGameScore) {
|
|
|
|
UpdateComponents(HistoryServiceGameScore::Bit());
|
|
|
|
Get<HistoryServiceGameScore>()->score = message.vaction.c_messageActionGameScore().vscore.v;
|
|
|
|
} else if (message.vaction.type() == mtpc_messageActionPaymentSent) {
|
|
|
|
UpdateComponents(HistoryServicePayment::Bit());
|
|
|
|
auto amount = message.vaction.c_messageActionPaymentSent().vtotal_amount.v;
|
|
|
|
auto currency = qs(message.vaction.c_messageActionPaymentSent().vcurrency);
|
|
|
|
Get<HistoryServicePayment>()->amount = HistoryInvoice::fillAmountAndCurrency(amount, currency);
|
|
|
|
}
|
|
|
|
if (message.has_reply_to_msg_id()) {
|
|
|
|
if (message.vaction.type() == mtpc_messageActionPinMessage) {
|
|
|
|
UpdateComponents(HistoryServicePinned::Bit());
|
|
|
|
}
|
|
|
|
if (auto dependent = GetDependentData()) {
|
|
|
|
dependent->msgId = message.vreply_to_msg_id.v;
|
2017-08-04 14:54:32 +00:00
|
|
|
if (!updateDependent()) {
|
2017-11-23 15:41:13 +00:00
|
|
|
Auth().api().requestMessageData(
|
|
|
|
history()->peer->asChannel(),
|
|
|
|
dependent->msgId,
|
|
|
|
HistoryDependentItemCallback(fullId()));
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
setMessageByAction(message.vaction);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryService::applyEdition(const MTPDmessageService &message) {
|
|
|
|
clearDependency();
|
|
|
|
UpdateComponents(0);
|
|
|
|
|
|
|
|
createFromMtp(message);
|
|
|
|
|
|
|
|
if (message.vaction.type() == mtpc_messageActionHistoryClear) {
|
|
|
|
removeMedia();
|
|
|
|
finishEditionToEmpty();
|
|
|
|
} else {
|
|
|
|
finishEdition(-1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryService::removeMedia() {
|
|
|
|
if (!_media) return;
|
|
|
|
|
|
|
|
_media.reset();
|
2018-01-14 16:02:25 +00:00
|
|
|
_textWidth = -1;
|
|
|
|
_textHeight = 0;
|
2018-01-18 11:46:45 +00:00
|
|
|
Auth().data().requestItemResize(this);
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
|
2017-09-04 11:40:02 +00:00
|
|
|
Storage::SharedMediaTypesMask HistoryService::sharedMediaTypes() const {
|
2018-01-14 16:02:25 +00:00
|
|
|
if (auto media = this->media()) {
|
2017-09-04 11:40:02 +00:00
|
|
|
return media->sharedMediaTypes();
|
|
|
|
}
|
|
|
|
return {};
|
|
|
|
}
|
|
|
|
|
2017-06-22 15:11:41 +00:00
|
|
|
void HistoryService::updateDependentText() {
|
|
|
|
auto text = PreparedText {};
|
|
|
|
if (Has<HistoryServicePinned>()) {
|
|
|
|
text = preparePinnedText();
|
|
|
|
} else if (Has<HistoryServiceGameScore>()) {
|
|
|
|
text = prepareGameScoreText();
|
|
|
|
} else if (Has<HistoryServicePayment>()) {
|
|
|
|
text = preparePaymentSentText();
|
|
|
|
} else {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
setServiceText(text);
|
2018-01-18 11:46:45 +00:00
|
|
|
Auth().data().requestItemResize(this);
|
2017-06-22 15:11:41 +00:00
|
|
|
if (history()->textCachedFor == this) {
|
|
|
|
history()->textCachedFor = nullptr;
|
|
|
|
}
|
2018-01-05 15:57:18 +00:00
|
|
|
if (const auto feed = history()->peer->feed()) {
|
|
|
|
if (feed->textCachedFor == this) {
|
|
|
|
feed->textCachedFor = nullptr;
|
|
|
|
feed->updateChatListEntry();
|
|
|
|
}
|
|
|
|
}
|
2017-06-22 15:11:41 +00:00
|
|
|
if (App::main()) {
|
2018-01-05 15:57:18 +00:00
|
|
|
// #TODO feeds search results
|
2018-01-04 17:15:04 +00:00
|
|
|
App::main()->dlgUpdated(history(), id);
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
App::historyUpdateDependent(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
void HistoryService::clearDependency() {
|
|
|
|
if (auto dependent = GetDependentData()) {
|
|
|
|
if (dependent->msg) {
|
|
|
|
App::historyUnregDependency(this, dependent->msg);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HistoryService::~HistoryService() {
|
|
|
|
clearDependency();
|
|
|
|
_media.reset();
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
HistoryJoined::HistoryJoined(not_null<History*> history, const QDateTime &inviteDate, not_null<UserData*> inviter, MTPDmessage::Flags flags)
|
2017-06-22 15:11:41 +00:00
|
|
|
: HistoryService(history, clientMsgId(), inviteDate, GenerateText(history, inviter), flags) {
|
|
|
|
}
|
|
|
|
|
2017-08-17 08:31:24 +00:00
|
|
|
HistoryJoined::PreparedText HistoryJoined::GenerateText(not_null<History*> history, not_null<UserData*> inviter) {
|
2017-08-04 14:54:32 +00:00
|
|
|
if (inviter->id == Auth().userPeerId()) {
|
2017-09-28 20:11:10 +00:00
|
|
|
if (history->isMegagroup()) {
|
|
|
|
auto self = App::user(Auth().userPeerId());
|
|
|
|
auto result = PreparedText {};
|
|
|
|
result.links.push_back(self->createOpenLink());
|
|
|
|
result.text = lng_action_user_joined(lt_from, textcmdLink(1, self->name));
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
return { lang(lng_action_you_joined) };
|
2017-06-22 15:11:41 +00:00
|
|
|
}
|
|
|
|
auto result = PreparedText {};
|
2017-07-05 18:11:31 +00:00
|
|
|
result.links.push_back(inviter->createOpenLink());
|
2017-07-05 13:23:52 +00:00
|
|
|
result.text = (history->isMegagroup() ? lng_action_add_you_group : lng_action_add_you)(lt_from, textcmdLink(1, inviter->name));
|
2017-06-22 15:11:41 +00:00
|
|
|
return result;
|
|
|
|
}
|