tdesktop/Telegram/SourceFiles/data/data_chat.h

223 lines
5.8 KiB
C
Raw Normal View History

/*
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
*/
#pragma once
#include "data/data_peer.h"
2021-07-08 12:11:46 +00:00
enum class ChatDataFlag {
Left = (1 << 0),
Kicked = (1 << 1),
Creator = (1 << 2),
Deactivated = (1 << 3),
Forbidden = (1 << 4),
CallActive = (1 << 5),
CallNotEmpty = (1 << 6),
CanSetUsername = (1 << 7),
};
inline constexpr bool is_flag_type(ChatDataFlag) { return true; };
using ChatDataFlags = base::flags<ChatDataFlag>;
class ChatData : public PeerData {
public:
2021-07-08 12:11:46 +00:00
using Flag = ChatDataFlag;
using Flags = Data::Flags<ChatDataFlags>;
using AdminRight = ChatAdminRight;
using Restriction = ChatRestriction;
using AdminRights = ChatAdminRights;
using Restrictions = ChatRestrictions;
using AdminRightFlags = Data::Flags<AdminRights>;
using RestrictionFlags = Data::Flags<Restrictions>;
ChatData(not_null<Data::Session*> owner, PeerId id);
void setName(const QString &newName);
2021-03-30 08:16:05 +00:00
void setPhoto(const MTPChatPhoto &photo);
void invalidateParticipants();
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool noParticipantInfo() const {
return (count > 0 || amIn()) && participants.empty();
}
2021-07-08 12:11:46 +00:00
void setFlags(ChatDataFlags which) {
_flags.set(which);
}
2021-07-08 12:11:46 +00:00
void addFlags(ChatDataFlags which) {
_flags.add(which);
}
2021-07-08 12:11:46 +00:00
void removeFlags(ChatDataFlags which) {
_flags.remove(which);
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] auto flags() const {
return _flags.current();
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] auto flagsValue() const {
return _flags.value();
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] auto adminRights() const {
return _adminRights.current();
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] auto adminRightsValue() const {
return _adminRights.value();
}
void setAdminRights(ChatAdminRights rights);
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool hasAdminRights() const {
return (adminRights() != 0);
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] auto defaultRestrictions() const {
return _defaultRestrictions.current();
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] auto defaultRestrictionsValue() const {
return _defaultRestrictions.value();
}
void setDefaultRestrictions(ChatRestrictions rights);
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool isForbidden() const {
2021-07-08 12:11:46 +00:00
return flags() & Flag::Forbidden;
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool amIn() const {
2019-01-22 13:05:06 +00:00
return !isForbidden()
&& !isDeactivated()
&& !haveLeft()
&& !wasKicked();
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool haveLeft() const {
2021-07-08 12:11:46 +00:00
return flags() & ChatDataFlag::Left;
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool wasKicked() const {
2021-07-08 12:11:46 +00:00
return flags() & ChatDataFlag::Kicked;
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool amCreator() const {
2021-07-08 12:11:46 +00:00
return flags() & ChatDataFlag::Creator;
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool isDeactivated() const {
2021-07-08 12:11:46 +00:00
return flags() & ChatDataFlag::Deactivated;
}
2021-02-12 10:40:55 +00:00
[[nodiscard]] bool isMigrated() const {
2021-07-08 12:11:46 +00:00
return (_migratedTo != nullptr);
}
[[nodiscard]] ChatAdminRightsInfo defaultAdminRights(
not_null<UserData*> user);
// Like in ChannelData.
2021-02-02 16:38:30 +00:00
[[nodiscard]] bool canWrite() const;
[[nodiscard]] bool canEditInformation() const;
[[nodiscard]] bool canEditPermissions() const;
[[nodiscard]] bool canEditUsername() const;
[[nodiscard]] bool canEditPreHistoryHidden() const;
[[nodiscard]] bool canDeleteMessages() const;
[[nodiscard]] bool canAddMembers() const;
[[nodiscard]] bool canAddAdmins() const;
[[nodiscard]] bool canBanMembers() const;
[[nodiscard]] bool canSendPolls() const;
[[nodiscard]] bool anyoneCanAddMembers() const;
void applyEditAdmin(not_null<UserData*> user, bool isAdmin);
void setInviteLink(const QString &newInviteLink);
[[nodiscard]] QString inviteLink() const {
return _inviteLink;
}
[[nodiscard]] bool canHaveInviteLink() const;
void refreshBotStatus();
enum class UpdateStatus {
Good,
TooOld,
Skipped,
};
int version() const {
return _version;
}
void setVersion(int version) {
_version = version;
}
UpdateStatus applyUpdateVersion(int version);
ChannelData *getMigrateToChannel() const;
void setMigrateToChannel(ChannelData *channel);
[[nodiscard]] Data::GroupCall *groupCall() const {
return _call.get();
}
2021-04-12 11:36:19 +00:00
void setGroupCall(
const MTPInputGroupCall &call,
TimeId scheduleDate = 0);
void clearGroupCall();
void setGroupCallDefaultJoinAs(PeerId peerId);
[[nodiscard]] PeerId groupCallDefaultJoinAs() const;
void setBotCommands(const MTPVector<MTPBotInfo> &data);
2021-07-05 17:59:06 +00:00
void setBotCommands(
UserId botId,
const MTPVector<MTPBotCommand> &data);
[[nodiscard]] auto botCommands() const
-> const base::flat_map<UserId, std::vector<BotCommand>> & {
return _botCommands;
}
// Still public data members.
const MTPint inputChat;
int count = 0;
TimeId date = 0;
UserId creator = 0;
base::flat_set<not_null<UserData*>> participants;
base::flat_set<not_null<UserData*>> invitedByMe;
base::flat_set<not_null<UserData*>> admins;
std::deque<not_null<UserData*>> lastAuthors;
base::flat_set<not_null<PeerData*>> markupSenders;
int botStatus = 0; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other
private:
Flags _flags;
QString _inviteLink;
RestrictionFlags _defaultRestrictions;
AdminRightFlags _adminRights;
int _version = 0;
std::unique_ptr<Data::GroupCall> _call;
PeerId _callDefaultJoinAs = 0;
base::flat_map<UserId, std::vector<BotCommand>> _botCommands;
ChannelData *_migratedTo = nullptr;
rpl::lifetime _lifetime;
};
namespace Data {
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPDupdateChatParticipants &update);
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPDupdateChatParticipantAdd &update);
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPDupdateChatParticipantDelete &update);
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPDupdateChatParticipantAdmin &update);
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPDupdateChatDefaultBannedRights &update);
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPDchatFull &update);
void ApplyChatUpdate(
not_null<ChatData*> chat,
const MTPChatParticipants &update);
} // namespace Data