2019-01-04 11:09:48 +00:00
|
|
|
/*
|
|
|
|
This file is part of Telegram Desktop,
|
|
|
|
the official desktop application for the Telegram messaging service.
|
|
|
|
|
|
|
|
For license and copyright information please follow this link:
|
|
|
|
https://github.com/telegramdesktop/tdesktop/blob/master/LEGAL
|
|
|
|
*/
|
|
|
|
#include "data/data_chat.h"
|
|
|
|
|
2019-01-13 08:03:34 +00:00
|
|
|
#include "data/data_user.h"
|
2019-01-14 06:34:51 +00:00
|
|
|
#include "data/data_channel.h"
|
2019-01-13 08:03:34 +00:00
|
|
|
#include "data/data_session.h"
|
2020-06-12 12:12:34 +00:00
|
|
|
#include "data/data_changes.h"
|
2020-12-14 12:52:18 +00:00
|
|
|
#include "data/data_group_call.h"
|
2021-12-07 11:11:27 +00:00
|
|
|
#include "data/data_message_reactions.h"
|
2019-01-13 08:03:34 +00:00
|
|
|
#include "history/history.h"
|
2019-07-24 11:45:24 +00:00
|
|
|
#include "main/main_session.h"
|
2019-01-13 08:03:34 +00:00
|
|
|
#include "apiwrap.h"
|
2021-01-14 13:13:43 +00:00
|
|
|
#include "api/api_invite_links.h"
|
2019-01-04 11:09:48 +00:00
|
|
|
|
|
|
|
namespace {
|
|
|
|
|
2020-06-12 12:12:34 +00:00
|
|
|
using UpdateFlag = Data::PeerUpdate::Flag;
|
2019-01-04 11:09:48 +00:00
|
|
|
|
|
|
|
} // namespace
|
|
|
|
|
|
|
|
ChatData::ChatData(not_null<Data::Session*> owner, PeerId id)
|
|
|
|
: PeerData(owner, id)
|
2021-08-25 08:15:05 +00:00
|
|
|
, inputChat(MTP_long(peerToChat(id).bare)) {
|
2020-12-14 12:52:18 +00:00
|
|
|
_flags.changes(
|
|
|
|
) | rpl::start_with_next([=](const Flags::Change &change) {
|
2021-11-05 14:03:17 +00:00
|
|
|
if (change.diff & Flag::CallNotEmpty) {
|
2020-12-14 12:52:18 +00:00
|
|
|
if (const auto history = this->owner().historyLoaded(this)) {
|
|
|
|
history->updateChatListEntry();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}, _lifetime);
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::setPhoto(const MTPChatPhoto &photo) {
|
2019-03-22 14:19:43 +00:00
|
|
|
photo.match([&](const MTPDchatPhoto &data) {
|
2021-03-30 08:16:05 +00:00
|
|
|
updateUserpic(data.vphoto_id().v, data.vdc_id().v);
|
2019-03-22 14:19:43 +00:00
|
|
|
}, [&](const MTPDchatPhotoEmpty &) {
|
2019-01-04 11:09:48 +00:00
|
|
|
clearUserpic();
|
2019-03-22 14:19:43 +00:00
|
|
|
});
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:34:06 +00:00
|
|
|
ChatAdminRightsInfo ChatData::defaultAdminRights(not_null<UserData*> user) {
|
2021-04-01 21:04:10 +00:00
|
|
|
const auto isCreator = (creator == peerToUser(user->id))
|
2021-02-12 10:40:55 +00:00
|
|
|
|| (user->isSelf() && amCreator());
|
2021-11-21 19:48:08 +00:00
|
|
|
using Flag = ChatAdminRight;
|
2021-07-08 10:34:06 +00:00
|
|
|
return ChatAdminRightsInfo(Flag::Other
|
|
|
|
| Flag::ChangeInfo
|
|
|
|
| Flag::DeleteMessages
|
|
|
|
| Flag::BanUsers
|
|
|
|
| Flag::InviteUsers
|
|
|
|
| Flag::PinMessages
|
|
|
|
| Flag::ManageCall
|
|
|
|
| (isCreator ? Flag::AddAdmins : Flag(0)));
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 10:50:04 +00:00
|
|
|
bool ChatData::canWrite() const {
|
|
|
|
// Duplicated in Data::CanWriteValue().
|
2021-11-21 19:48:08 +00:00
|
|
|
return amIn() && !amRestricted(ChatRestriction::SendMessages);
|
2019-01-05 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 14:03:17 +00:00
|
|
|
bool ChatData::allowsForwarding() const {
|
|
|
|
return !(flags() & Flag::NoForwards);
|
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
bool ChatData::canEditInformation() const {
|
2021-11-21 19:48:08 +00:00
|
|
|
return amIn() && !amRestricted(ChatRestriction::ChangeInfo);
|
2019-01-05 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
2019-01-07 12:55:49 +00:00
|
|
|
bool ChatData::canEditPermissions() const {
|
2019-01-22 13:05:06 +00:00
|
|
|
return amIn()
|
2021-11-21 19:48:08 +00:00
|
|
|
&& (amCreator() || (adminRights() & ChatAdminRight::BanUsers));
|
2019-01-07 12:55:49 +00:00
|
|
|
}
|
|
|
|
|
2019-01-08 13:57:22 +00:00
|
|
|
bool ChatData::canEditUsername() const {
|
|
|
|
return amCreator()
|
2021-11-05 14:03:17 +00:00
|
|
|
&& (flags() & Flag::CanSetUsername);
|
2019-01-08 13:57:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatData::canEditPreHistoryHidden() const {
|
|
|
|
return amCreator();
|
|
|
|
}
|
|
|
|
|
2021-02-02 16:38:30 +00:00
|
|
|
bool ChatData::canDeleteMessages() const {
|
|
|
|
return amCreator()
|
2021-11-21 19:48:08 +00:00
|
|
|
|| (adminRights() & ChatAdminRight::DeleteMessages);
|
2021-02-02 16:38:30 +00:00
|
|
|
}
|
|
|
|
|
2019-01-05 10:50:04 +00:00
|
|
|
bool ChatData::canAddMembers() const {
|
2021-11-21 19:48:08 +00:00
|
|
|
return amIn() && !amRestricted(ChatRestriction::InviteUsers);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatData::canSendPolls() const {
|
2021-11-21 19:48:08 +00:00
|
|
|
return amIn() && !amRestricted(ChatRestriction::SendPolls);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatData::canAddAdmins() const {
|
2019-01-22 13:05:06 +00:00
|
|
|
return amIn() && amCreator();
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatData::canBanMembers() const {
|
|
|
|
return amCreator()
|
2021-11-21 19:48:08 +00:00
|
|
|
|| (adminRights() & ChatAdminRight::BanUsers);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
bool ChatData::anyoneCanAddMembers() const {
|
2021-11-21 19:48:08 +00:00
|
|
|
return !(defaultRestrictions() & ChatRestriction::InviteUsers);
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::setName(const QString &newName) {
|
|
|
|
updateNameDelayed(newName.isEmpty() ? name : newName, QString(), QString());
|
|
|
|
}
|
|
|
|
|
2019-01-10 06:26:08 +00:00
|
|
|
void ChatData::applyEditAdmin(not_null<UserData*> user, bool isAdmin) {
|
|
|
|
if (isAdmin) {
|
|
|
|
admins.emplace(user);
|
|
|
|
} else {
|
|
|
|
admins.remove(user);
|
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::Admins);
|
2019-01-10 06:26:08 +00:00
|
|
|
}
|
|
|
|
|
2019-01-04 11:09:48 +00:00
|
|
|
void ChatData::invalidateParticipants() {
|
|
|
|
participants.clear();
|
|
|
|
admins.clear();
|
2021-07-08 10:34:06 +00:00
|
|
|
setAdminRights(ChatAdminRights());
|
|
|
|
//setDefaultRestrictions(ChatRestrictions());
|
2019-01-04 11:09:48 +00:00
|
|
|
invitedByMe.clear();
|
|
|
|
botStatus = 0;
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(
|
2019-01-05 10:50:04 +00:00
|
|
|
this,
|
2020-06-12 12:12:34 +00:00
|
|
|
UpdateFlag::Members | UpdateFlag::Admins);
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::setInviteLink(const QString &newInviteLink) {
|
2021-01-14 13:13:43 +00:00
|
|
|
_inviteLink = newInviteLink;
|
2019-01-04 11:09:48 +00:00
|
|
|
}
|
2019-01-05 10:50:04 +00:00
|
|
|
|
2020-12-14 12:52:18 +00:00
|
|
|
bool ChatData::canHaveInviteLink() const {
|
|
|
|
return amCreator()
|
2021-11-21 19:48:08 +00:00
|
|
|
|| (adminRights() & ChatAdminRight::InviteUsers);
|
2020-12-14 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:34:06 +00:00
|
|
|
void ChatData::setAdminRights(ChatAdminRights rights) {
|
|
|
|
if (rights == adminRights()) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-08 10:34:06 +00:00
|
|
|
_adminRights.set(rights);
|
2021-10-12 08:39:07 +00:00
|
|
|
if (!canHaveInviteLink()) {
|
2021-10-27 05:57:37 +00:00
|
|
|
setPendingRequestsCount(0, std::vector<UserId>{});
|
2021-10-12 08:39:07 +00:00
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(
|
2019-01-05 10:50:04 +00:00
|
|
|
this,
|
2020-06-12 12:12:34 +00:00
|
|
|
UpdateFlag::Rights | UpdateFlag::Admins | UpdateFlag::BannedUsers);
|
2019-01-05 10:50:04 +00:00
|
|
|
}
|
|
|
|
|
2021-07-08 10:34:06 +00:00
|
|
|
void ChatData::setDefaultRestrictions(ChatRestrictions rights) {
|
|
|
|
if (rights == defaultRestrictions()) {
|
2019-01-05 10:50:04 +00:00
|
|
|
return;
|
|
|
|
}
|
2021-07-08 10:34:06 +00:00
|
|
|
_defaultRestrictions.set(rights);
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::Rights);
|
2019-01-05 10:50:04 +00:00
|
|
|
}
|
2019-01-13 08:03:34 +00:00
|
|
|
|
|
|
|
void ChatData::refreshBotStatus() {
|
|
|
|
if (participants.empty()) {
|
|
|
|
botStatus = 0;
|
|
|
|
} else {
|
2020-05-18 19:33:14 +00:00
|
|
|
const auto bot = ranges::none_of(participants, &UserData::isBot);
|
|
|
|
botStatus = bot ? -1 : 2;
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
auto ChatData::applyUpdateVersion(int version) -> UpdateStatus {
|
|
|
|
if (_version > version) {
|
|
|
|
return UpdateStatus::TooOld;
|
|
|
|
} else if (_version + 1 < version) {
|
|
|
|
invalidateParticipants();
|
2019-03-15 13:34:47 +00:00
|
|
|
session().api().requestFullPeer(this);
|
2019-01-13 08:03:34 +00:00
|
|
|
return UpdateStatus::Skipped;
|
|
|
|
}
|
|
|
|
setVersion(version);
|
|
|
|
return UpdateStatus::Good;
|
|
|
|
}
|
|
|
|
|
2019-01-14 06:34:51 +00:00
|
|
|
ChannelData *ChatData::getMigrateToChannel() const {
|
|
|
|
return _migratedTo;
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::setMigrateToChannel(ChannelData *channel) {
|
|
|
|
if (_migratedTo != channel) {
|
|
|
|
_migratedTo = channel;
|
|
|
|
if (channel->amIn()) {
|
2020-06-12 12:12:34 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::Migration);
|
2019-01-14 06:34:51 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-04-12 11:36:19 +00:00
|
|
|
void ChatData::setGroupCall(
|
|
|
|
const MTPInputGroupCall &call,
|
|
|
|
TimeId scheduleDate) {
|
2020-12-14 12:52:18 +00:00
|
|
|
if (migrateTo()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
call.match([&](const MTPDinputGroupCall &data) {
|
|
|
|
if (_call && _call->id() == data.vid().v) {
|
|
|
|
return;
|
|
|
|
} else if (!_call && !data.vid().v) {
|
|
|
|
return;
|
|
|
|
} else if (!data.vid().v) {
|
|
|
|
clearGroupCall();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
const auto hasCall = (_call != nullptr);
|
|
|
|
if (hasCall) {
|
|
|
|
owner().unregisterGroupCall(_call.get());
|
|
|
|
}
|
|
|
|
_call = std::make_unique<Data::GroupCall>(
|
|
|
|
this,
|
|
|
|
data.vid().v,
|
2021-04-12 11:36:19 +00:00
|
|
|
data.vaccess_hash().v,
|
|
|
|
scheduleDate);
|
2020-12-14 12:52:18 +00:00
|
|
|
owner().registerGroupCall(_call.get());
|
|
|
|
session().changes().peerUpdated(this, UpdateFlag::GroupCall);
|
2021-11-05 14:03:17 +00:00
|
|
|
addFlags(Flag::CallActive);
|
2020-12-14 12:52:18 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::clearGroupCall() {
|
|
|
|
if (!_call) {
|
|
|
|
return;
|
|
|
|
} else if (const auto group = migrateTo(); group && !group->groupCall()) {
|
|
|
|
group->migrateCall(base::take(_call));
|
|
|
|
} else {
|
|
|
|
owner().unregisterGroupCall(_call.get());
|
|
|
|
_call = nullptr;
|
|
|
|
}
|
|
|
|
session().changes().peerUpdated(this, UpdateFlag::GroupCall);
|
2021-11-05 14:03:17 +00:00
|
|
|
removeFlags(Flag::CallActive | Flag::CallNotEmpty);
|
2020-12-14 12:52:18 +00:00
|
|
|
}
|
|
|
|
|
2021-03-05 08:14:34 +00:00
|
|
|
void ChatData::setGroupCallDefaultJoinAs(PeerId peerId) {
|
|
|
|
_callDefaultJoinAs = peerId;
|
|
|
|
}
|
|
|
|
|
|
|
|
PeerId ChatData::groupCallDefaultJoinAs() const {
|
|
|
|
return _callDefaultJoinAs;
|
|
|
|
}
|
|
|
|
|
2021-07-01 11:05:15 +00:00
|
|
|
void ChatData::setBotCommands(const MTPVector<MTPBotInfo> &data) {
|
|
|
|
if (Data::UpdateBotCommands(_botCommands, data)) {
|
|
|
|
owner().botCommandsChanged(this);
|
2021-07-05 17:59:06 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::setBotCommands(
|
|
|
|
UserId botId,
|
|
|
|
const MTPVector<MTPBotCommand> &data) {
|
|
|
|
if (Data::UpdateBotCommands(_botCommands, botId, data)) {
|
|
|
|
owner().botCommandsChanged(this);
|
2021-07-01 11:05:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-10-27 05:57:37 +00:00
|
|
|
void ChatData::setPendingRequestsCount(
|
|
|
|
int count,
|
|
|
|
const QVector<MTPlong> &recentRequesters) {
|
|
|
|
setPendingRequestsCount(count, ranges::views::all(
|
|
|
|
recentRequesters
|
|
|
|
) | ranges::views::transform([&](const MTPlong &value) {
|
|
|
|
return UserId(value);
|
|
|
|
}) | ranges::to_vector);
|
|
|
|
}
|
|
|
|
|
|
|
|
void ChatData::setPendingRequestsCount(
|
|
|
|
int count,
|
|
|
|
std::vector<UserId> recentRequesters) {
|
|
|
|
if (_pendingRequestsCount != count
|
|
|
|
|| _recentRequesters != recentRequesters) {
|
2021-10-12 08:39:07 +00:00
|
|
|
_pendingRequestsCount = count;
|
2021-10-27 05:57:37 +00:00
|
|
|
_recentRequesters = std::move(recentRequesters);
|
2021-10-12 08:39:07 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::PendingRequests);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-12-07 11:11:27 +00:00
|
|
|
void ChatData::setAllowedReactions(std::vector<QString> list) {
|
2021-12-13 11:26:19 +00:00
|
|
|
if (_allowedReactions != list) {
|
2021-12-28 22:06:01 +00:00
|
|
|
const auto toggled = (_allowedReactions.empty() != list.empty());
|
2021-12-13 11:26:19 +00:00
|
|
|
_allowedReactions = std::move(list);
|
2021-12-28 22:06:01 +00:00
|
|
|
if (toggled) {
|
|
|
|
owner().reactions().updateAllInHistory(
|
|
|
|
this,
|
|
|
|
!_allowedReactions.empty());
|
|
|
|
}
|
2021-12-13 11:26:19 +00:00
|
|
|
session().changes().peerUpdated(this, UpdateFlag::Reactions);
|
|
|
|
}
|
2021-12-07 11:11:27 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const std::vector<QString> &ChatData::allowedReactions() const {
|
|
|
|
return _allowedReactions;
|
|
|
|
}
|
|
|
|
|
2019-01-13 08:03:34 +00:00
|
|
|
namespace Data {
|
|
|
|
|
|
|
|
void ApplyChatUpdate(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
const MTPDupdateChatParticipants &update) {
|
2019-07-05 13:38:38 +00:00
|
|
|
ApplyChatUpdate(chat, update.vparticipants());
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyChatUpdate(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
const MTPDupdateChatParticipantAdd &update) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (chat->applyUpdateVersion(update.vversion().v)
|
2019-01-13 08:03:34 +00:00
|
|
|
!= ChatData::UpdateStatus::Good) {
|
|
|
|
return;
|
|
|
|
} else if (chat->count < 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto user = chat->owner().userLoaded(update.vuser_id().v);
|
2020-06-12 12:12:34 +00:00
|
|
|
const auto session = &chat->session();
|
2019-01-13 08:03:34 +00:00
|
|
|
if (!user
|
|
|
|
|| (!chat->participants.empty()
|
|
|
|
&& chat->participants.contains(user))) {
|
|
|
|
chat->invalidateParticipants();
|
|
|
|
++chat->count;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (chat->participants.empty()) {
|
|
|
|
if (chat->count > 0) { // If the count is known.
|
|
|
|
++chat->count;
|
|
|
|
}
|
|
|
|
chat->botStatus = 0;
|
|
|
|
} else {
|
|
|
|
chat->participants.emplace(user);
|
2021-04-01 21:04:10 +00:00
|
|
|
if (UserId(update.vinviter_id()) == session->userId()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
chat->invitedByMe.insert(user);
|
|
|
|
} else {
|
|
|
|
chat->invitedByMe.remove(user);
|
|
|
|
}
|
|
|
|
++chat->count;
|
|
|
|
if (user->isBot()) {
|
|
|
|
chat->botStatus = 2;
|
|
|
|
if (!user->botInfo->inited) {
|
2020-06-12 12:12:34 +00:00
|
|
|
session->api().requestFullPeer(user);
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
session->changes().peerUpdated(chat, UpdateFlag::Members);
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyChatUpdate(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
const MTPDupdateChatParticipantDelete &update) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (chat->applyUpdateVersion(update.vversion().v)
|
2019-01-13 08:03:34 +00:00
|
|
|
!= ChatData::UpdateStatus::Good) {
|
|
|
|
return;
|
|
|
|
} else if (chat->count <= 0) {
|
|
|
|
return;
|
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto user = chat->owner().userLoaded(update.vuser_id().v);
|
2019-01-13 08:03:34 +00:00
|
|
|
if (!user
|
|
|
|
|| (!chat->participants.empty()
|
|
|
|
&& !chat->participants.contains(user))) {
|
|
|
|
chat->invalidateParticipants();
|
|
|
|
--chat->count;
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (chat->participants.empty()) {
|
|
|
|
if (chat->count > 0) {
|
|
|
|
chat->count--;
|
|
|
|
}
|
|
|
|
chat->botStatus = 0;
|
|
|
|
} else {
|
|
|
|
chat->participants.erase(user);
|
|
|
|
chat->count--;
|
|
|
|
chat->invitedByMe.remove(user);
|
|
|
|
chat->admins.remove(user);
|
|
|
|
if (user->isSelf()) {
|
2021-07-08 10:34:06 +00:00
|
|
|
chat->setAdminRights(ChatAdminRights());
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
if (const auto history = chat->owner().historyLoaded(chat)) {
|
|
|
|
if (history->lastKeyboardFrom == user->id) {
|
|
|
|
history->clearLastKeyboard();
|
|
|
|
}
|
|
|
|
}
|
2019-07-18 08:51:11 +00:00
|
|
|
if (chat->botStatus > 0 && user->isBot()) {
|
2019-01-13 08:03:34 +00:00
|
|
|
chat->refreshBotStatus();
|
|
|
|
}
|
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
chat->session().changes().peerUpdated(chat, UpdateFlag::Members);
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyChatUpdate(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
const MTPDupdateChatParticipantAdmin &update) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (chat->applyUpdateVersion(update.vversion().v)
|
2019-01-13 08:03:34 +00:00
|
|
|
!= ChatData::UpdateStatus::Good) {
|
|
|
|
return;
|
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
const auto session = &chat->session();
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto user = chat->owner().userLoaded(update.vuser_id().v);
|
2019-01-13 08:03:34 +00:00
|
|
|
if (!user) {
|
|
|
|
chat->invalidateParticipants();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (user->isSelf()) {
|
2021-07-08 10:34:06 +00:00
|
|
|
chat->setAdminRights(mtpIsTrue(update.vis_admin())
|
|
|
|
? chat->defaultAdminRights(user).flags
|
|
|
|
: ChatAdminRights());
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
if (mtpIsTrue(update.vis_admin())) {
|
2019-01-13 08:03:34 +00:00
|
|
|
if (chat->noParticipantInfo()) {
|
2020-06-12 12:12:34 +00:00
|
|
|
session->api().requestFullPeer(chat);
|
2019-01-13 08:03:34 +00:00
|
|
|
} else {
|
|
|
|
chat->admins.emplace(user);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
chat->admins.erase(user);
|
|
|
|
}
|
2020-06-12 12:12:34 +00:00
|
|
|
session->changes().peerUpdated(chat, UpdateFlag::Admins);
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyChatUpdate(
|
|
|
|
not_null<ChatData*> chat,
|
|
|
|
const MTPDupdateChatDefaultBannedRights &update) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (chat->applyUpdateVersion(update.vversion().v)
|
2019-01-13 08:03:34 +00:00
|
|
|
!= ChatData::UpdateStatus::Good) {
|
|
|
|
return;
|
|
|
|
}
|
2021-11-21 19:48:08 +00:00
|
|
|
chat->setDefaultRestrictions(ChatRestrictionsInfo(
|
|
|
|
update.vdefault_banned_rights()).flags);
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
|
2019-04-23 09:40:14 +00:00
|
|
|
void ApplyChatUpdate(not_null<ChatData*> chat, const MTPDchatFull &update) {
|
2019-07-05 13:38:38 +00:00
|
|
|
ApplyChatUpdate(chat, update.vparticipants());
|
2019-04-23 09:40:14 +00:00
|
|
|
|
2020-12-14 12:52:18 +00:00
|
|
|
if (const auto call = update.vcall()) {
|
|
|
|
chat->setGroupCall(*call);
|
|
|
|
} else {
|
|
|
|
chat->clearGroupCall();
|
|
|
|
}
|
2021-03-05 08:14:34 +00:00
|
|
|
if (const auto as = update.vgroupcall_default_join_as()) {
|
|
|
|
chat->setGroupCallDefaultJoinAs(peerFromMTP(*as));
|
|
|
|
} else {
|
|
|
|
chat->setGroupCallDefaultJoinAs(0);
|
|
|
|
}
|
2020-12-14 12:52:18 +00:00
|
|
|
|
2021-02-15 10:31:04 +00:00
|
|
|
chat->setMessagesTTL(update.vttl_period().value_or_empty());
|
2019-07-05 13:38:38 +00:00
|
|
|
if (const auto info = update.vbot_info()) {
|
2021-07-01 11:05:15 +00:00
|
|
|
chat->setBotCommands(*info);
|
|
|
|
} else {
|
|
|
|
chat->setBotCommands(MTP_vector<MTPBotInfo>());
|
2019-04-23 09:40:14 +00:00
|
|
|
}
|
2021-07-08 12:11:46 +00:00
|
|
|
using Flag = ChatDataFlag;
|
|
|
|
const auto mask = Flag::CanSetUsername;
|
|
|
|
chat->setFlags((chat->flags() & ~mask)
|
|
|
|
| (update.is_can_set_username() ? Flag::CanSetUsername : Flag()));
|
2019-07-05 13:38:38 +00:00
|
|
|
if (const auto photo = update.vchat_photo()) {
|
|
|
|
chat->setUserpicPhoto(*photo);
|
|
|
|
} else {
|
|
|
|
chat->setUserpicPhoto(MTP_photoEmpty(MTP_long(0)));
|
|
|
|
}
|
2020-12-29 12:39:10 +00:00
|
|
|
if (const auto invite = update.vexported_invite()) {
|
2021-02-05 13:22:02 +00:00
|
|
|
chat->session().api().inviteLinks().setMyPermanent(chat, *invite);
|
2020-12-29 12:39:10 +00:00
|
|
|
} else {
|
2021-02-05 13:22:02 +00:00
|
|
|
chat->session().api().inviteLinks().clearMyPermanent(chat);
|
2020-12-29 12:39:10 +00:00
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
if (const auto pinned = update.vpinned_msg_id()) {
|
2020-10-21 11:07:49 +00:00
|
|
|
SetTopPinnedMessageId(chat, pinned->v);
|
2019-04-23 09:40:14 +00:00
|
|
|
}
|
2019-07-05 13:38:38 +00:00
|
|
|
chat->checkFolder(update.vfolder_id().value_or_empty());
|
2021-08-25 16:16:50 +00:00
|
|
|
chat->setThemeEmoji(qs(update.vtheme_emoticon().value_or_empty()));
|
2021-12-07 11:11:27 +00:00
|
|
|
chat->setAllowedReactions(
|
|
|
|
Data::Reactions::ParseAllowed(update.vavailable_reactions()));
|
2019-04-23 09:40:14 +00:00
|
|
|
chat->fullUpdated();
|
2020-03-24 08:58:39 +00:00
|
|
|
chat->setAbout(qs(update.vabout()));
|
2021-10-12 08:39:07 +00:00
|
|
|
chat->setPendingRequestsCount(
|
2021-10-27 05:57:37 +00:00
|
|
|
update.vrequests_pending().value_or_empty(),
|
|
|
|
update.vrecent_requesters().value_or_empty());
|
2019-04-23 09:40:14 +00:00
|
|
|
|
|
|
|
chat->session().api().applyNotifySettings(
|
|
|
|
MTP_inputNotifyPeer(chat->input),
|
2019-07-05 13:38:38 +00:00
|
|
|
update.vnotify_settings());
|
2019-04-23 09:40:14 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void ApplyChatUpdate(
|
2019-01-13 08:03:34 +00:00
|
|
|
not_null<ChatData*> chat,
|
|
|
|
const MTPChatParticipants &participants) {
|
2020-06-12 12:12:34 +00:00
|
|
|
const auto session = &chat->session();
|
2019-01-13 08:03:34 +00:00
|
|
|
participants.match([&](const MTPDchatParticipantsForbidden &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
if (const auto self = data.vself_participant()) {
|
|
|
|
// self->
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
chat->count = -1;
|
|
|
|
chat->invalidateParticipants();
|
|
|
|
}, [&](const MTPDchatParticipants &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto status = chat->applyUpdateVersion(data.vversion().v);
|
2019-01-13 08:03:34 +00:00
|
|
|
if (status == ChatData::UpdateStatus::TooOld) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// Even if we skipped some updates, we got current participants
|
|
|
|
// and we've requested peer from API to have current rights.
|
2019-07-05 13:38:38 +00:00
|
|
|
chat->setVersion(data.vversion().v);
|
2019-01-13 08:03:34 +00:00
|
|
|
|
2019-07-05 13:38:38 +00:00
|
|
|
const auto &list = data.vparticipants().v;
|
2019-01-13 08:03:34 +00:00
|
|
|
chat->count = list.size();
|
|
|
|
chat->participants.clear();
|
|
|
|
chat->invitedByMe.clear();
|
|
|
|
chat->admins.clear();
|
2021-07-08 10:34:06 +00:00
|
|
|
chat->setAdminRights(ChatAdminRights());
|
2020-06-12 12:12:34 +00:00
|
|
|
const auto selfUserId = session->userId();
|
2019-01-13 08:03:34 +00:00
|
|
|
for (const auto &participant : list) {
|
|
|
|
const auto userId = participant.match([&](const auto &data) {
|
2019-07-05 13:38:38 +00:00
|
|
|
return data.vuser_id().v;
|
2019-01-13 08:03:34 +00:00
|
|
|
});
|
|
|
|
const auto user = chat->owner().userLoaded(userId);
|
|
|
|
if (!user) {
|
|
|
|
chat->invalidateParticipants();
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
chat->participants.emplace(user);
|
|
|
|
|
|
|
|
const auto inviterId = participant.match([&](
|
|
|
|
const MTPDchatParticipantCreator &data) {
|
2021-04-01 21:04:10 +00:00
|
|
|
return UserId(0);
|
2019-01-13 08:03:34 +00:00
|
|
|
}, [&](const auto &data) {
|
2021-04-01 21:04:10 +00:00
|
|
|
return UserId(data.vinviter_id());
|
2019-01-13 08:03:34 +00:00
|
|
|
});
|
|
|
|
if (inviterId == selfUserId) {
|
|
|
|
chat->invitedByMe.insert(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
participant.match([&](const MTPDchatParticipantCreator &data) {
|
|
|
|
chat->creator = userId;
|
|
|
|
}, [&](const MTPDchatParticipantAdmin &data) {
|
|
|
|
chat->admins.emplace(user);
|
|
|
|
if (user->isSelf()) {
|
2021-07-08 10:34:06 +00:00
|
|
|
chat->setAdminRights(
|
|
|
|
chat->defaultAdminRights(user).flags);
|
2019-01-13 08:03:34 +00:00
|
|
|
}
|
|
|
|
}, [](const MTPDchatParticipant &) {
|
|
|
|
});
|
|
|
|
}
|
|
|
|
if (chat->participants.empty()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
if (const auto history = chat->owner().historyLoaded(chat)) {
|
|
|
|
if (history->lastKeyboardFrom) {
|
|
|
|
const auto i = ranges::find(
|
|
|
|
chat->participants,
|
|
|
|
history->lastKeyboardFrom,
|
|
|
|
&UserData::id);
|
|
|
|
if (i == end(chat->participants)) {
|
|
|
|
history->clearLastKeyboard();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
chat->refreshBotStatus();
|
2020-06-12 12:12:34 +00:00
|
|
|
session->changes().peerUpdated(
|
2019-01-13 08:03:34 +00:00
|
|
|
chat,
|
2020-06-12 12:12:34 +00:00
|
|
|
UpdateFlag::Members | UpdateFlag::Admins);
|
2019-01-13 08:03:34 +00:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace Data
|