tdesktop/Telegram/SourceFiles/data/data_channel.h

505 lines
14 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"
#include "data/data_pts_waiter.h"
2019-06-21 12:27:46 +00:00
#include "data/data_location.h"
#include "data/data_chat_participant_status.h"
#include "data/data_peer_bot_commands.h"
2019-06-21 12:27:46 +00:00
struct ChannelLocation {
QString address;
Data::LocationPoint point;
friend inline bool operator==(
const ChannelLocation &a,
const ChannelLocation &b) {
return a.address.isEmpty()
? b.address.isEmpty()
: (a.address == b.address && a.point == b.point);
}
friend inline bool operator!=(
const ChannelLocation &a,
const ChannelLocation &b) {
return !(a == b);
}
};
2021-07-08 13:11:09 +00:00
enum class ChannelDataFlag {
Left = (1 << 0),
Creator = (1 << 1),
Forbidden = (1 << 2),
CallActive = (1 << 3),
CallNotEmpty = (1 << 4),
Signatures = (1 << 5),
Verified = (1 << 6),
Scam = (1 << 7),
Fake = (1 << 8),
Megagroup = (1 << 9),
Broadcast = (1 << 10),
Gigagroup = (1 << 11),
Username = (1 << 12),
Location = (1 << 13),
CanSetUsername = (1 << 14),
CanSetStickers = (1 << 15),
PreHistoryHidden = (1 << 16),
CanViewParticipants = (1 << 17),
HasLink = (1 << 18),
SlowmodeEnabled = (1 << 19),
NoForwards = (1 << 20),
JoinToWrite = (1 << 21),
RequestToJoin = (1 << 22),
Forum = (1 << 23),
2021-07-08 13:11:09 +00:00
};
inline constexpr bool is_flag_type(ChannelDataFlag) { return true; };
using ChannelDataFlags = base::flags<ChannelDataFlag>;
class MegagroupInfo {
public:
MegagroupInfo();
~MegagroupInfo();
struct Admin {
explicit Admin(ChatAdminRightsInfo rights)
: rights(rights) {
}
Admin(ChatAdminRightsInfo rights, bool canEdit)
: rights(rights)
, canEdit(canEdit) {
}
ChatAdminRightsInfo rights;
bool canEdit = false;
};
struct Restricted {
explicit Restricted(ChatRestrictionsInfo rights)
: rights(rights) {
}
ChatRestrictionsInfo rights;
};
ChatData *getMigrateFromChat() const;
void setMigrateFromChat(ChatData *chat);
2019-06-21 12:27:46 +00:00
const ChannelLocation *getLocation() const;
void setLocation(const ChannelLocation &location);
Data::ChatBotCommands::Changed setBotCommands(
const std::vector<Data::BotCommands> &commands);
[[nodiscard]] const Data::ChatBotCommands &botCommands() const {
return _botCommands;
}
void ensureForum(not_null<ChannelData*> that);
[[nodiscard]] Data::Forum *forum() const;
[[nodiscard]] std::unique_ptr<Data::Forum> takeForumData();
std::deque<not_null<UserData*>> lastParticipants;
base::flat_map<not_null<UserData*>, Admin> lastAdmins;
base::flat_map<not_null<UserData*>, Restricted> lastRestricted;
base::flat_set<not_null<PeerData*>> markupSenders;
base::flat_set<not_null<UserData*>> bots;
// For admin badges, full admins list with ranks.
base::flat_map<UserId, QString> admins;
UserData *creator = nullptr; // nullptr means unknown
QString creatorRank;
int botStatus = 0; // -1 - no bots, 0 - unknown, 1 - one bot, that sees all history, 2 - other
bool joinedMessageFound = false;
StickerSetIdentifier stickerSet;
enum LastParticipantsStatus {
LastParticipantsUpToDate = 0x00,
LastParticipantsOnceReceived = 0x01,
LastParticipantsCountOutdated = 0x02,
};
mutable int lastParticipantsStatus = LastParticipantsUpToDate;
int lastParticipantsCount = 0;
private:
ChatData *_migratedFrom = nullptr;
2019-06-21 12:27:46 +00:00
ChannelLocation _location;
Data::ChatBotCommands _botCommands;
std::unique_ptr<Data::Forum> _forum;
};
class ChannelData final : public PeerData {
public:
2021-07-08 13:11:09 +00:00
using Flag = ChannelDataFlag;
using Flags = Data::Flags<ChannelDataFlags>;
using AdminRight = ChatAdminRight;
using Restriction = ChatRestriction;
using AdminRights = ChatAdminRights;
using Restrictions = ChatRestrictions;
using AdminRightFlags = Data::Flags<AdminRights>;
using RestrictionFlags = Data::Flags<Restrictions>;
ChannelData(not_null<Data::Session*> owner, PeerId id);
void setName(const QString &name, const QString &username);
2021-03-30 08:16:05 +00:00
void setPhoto(const MTPChatPhoto &photo);
2020-02-07 16:07:21 +00:00
void setAccessHash(uint64 accessHash);
void setFlags(ChannelDataFlags which);
void addFlags(ChannelDataFlags which);
void removeFlags(ChannelDataFlags which);
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto flags() const {
return _flags.current();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto flagsValue() const {
return _flags.value();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] int membersCount() const {
2019-05-20 18:40:53 +00:00
return std::max(_membersCount, 1);
}
void setMembersCount(int newMembersCount);
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool membersCountKnown() const {
2019-05-20 18:40:53 +00:00
return (_membersCount >= 0);
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] int adminsCount() const {
return _adminsCount;
}
void setAdminsCount(int newAdminsCount);
2019-07-16 11:46:50 +00:00
[[nodiscard]] int restrictedCount() const {
return _restrictedCount;
}
void setRestrictedCount(int newRestrictedCount);
2019-07-16 11:46:50 +00:00
[[nodiscard]] int kickedCount() const {
return _kickedCount;
}
void setKickedCount(int newKickedCount);
[[nodiscard]] int pendingRequestsCount() const {
return _pendingRequestsCount;
}
[[nodiscard]] const std::vector<UserId> &recentRequesters() const {
return _recentRequesters;
}
void setPendingRequestsCount(
int count,
const QVector<MTPlong> &recentRequesters);
void setPendingRequestsCount(
int count,
std::vector<UserId> recentRequesters);
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool haveLeft() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Left;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool amIn() const {
return !isForbidden() && !haveLeft();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool addsSignature() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Signatures;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isForbidden() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Forbidden;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isVerified() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Verified;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isScam() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Scam;
2019-06-23 12:18:33 +00:00
}
2021-01-21 12:39:40 +00:00
[[nodiscard]] bool isFake() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Fake;
2021-01-21 12:39:40 +00:00
}
[[nodiscard]] static ChatRestrictionsInfo KickedRestrictedRights(
not_null<PeerData*> participant);
static constexpr auto kRestrictUntilForever = TimeId(INT_MAX);
2019-07-16 11:46:50 +00:00
[[nodiscard]] static bool IsRestrictedForever(TimeId until) {
return !until || (until == kRestrictUntilForever);
}
void applyEditAdmin(
not_null<UserData*> user,
ChatAdminRightsInfo oldRights,
ChatAdminRightsInfo newRights,
const QString &rank);
void applyEditBanned(
2021-03-19 14:01:21 +00:00
not_null<PeerData*> participant,
ChatRestrictionsInfo oldRights,
ChatRestrictionsInfo newRights);
void markForbidden();
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isGroupAdmin(not_null<UserData*> user) const;
[[nodiscard]] bool lastParticipantsRequestNeeded() const;
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isMegagroup() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Megagroup;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isBroadcast() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Broadcast;
}
2021-02-12 15:07:41 +00:00
[[nodiscard]] bool isGigagroup() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Gigagroup;
2021-02-12 15:07:41 +00:00
}
[[nodiscard]] bool isForum() const {
return flags() & Flag::Forum;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool hasUsername() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Username;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool hasLocation() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Location;
2019-06-21 12:27:46 +00:00
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool isPublic() const {
2019-06-21 12:27:46 +00:00
return hasUsername() || hasLocation();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool amCreator() const {
2021-07-08 13:11:09 +00:00
return flags() & Flag::Creator;
}
[[nodiscard]] bool joinToWrite() const {
return flags() & Flag::JoinToWrite;
}
[[nodiscard]] bool requestToJoin() const {
return flags() & Flag::RequestToJoin;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto adminRights() const {
return _adminRights.current();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto adminRightsValue() const {
return _adminRights.value();
}
void setAdminRights(ChatAdminRights rights);
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool hasAdminRights() const {
return (adminRights() != 0);
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto restrictions() const {
return _restrictions.current();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto restrictionsValue() const {
return _restrictions.value();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] TimeId restrictedUntil() const {
return _restrictedUntil;
}
void setRestrictions(ChatRestrictionsInfo rights);
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool hasRestrictions() const {
return (restrictions() != 0);
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool hasRestrictions(TimeId now) const {
return hasRestrictions()
&& (restrictedUntil() > now);
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto defaultRestrictions() const {
return _defaultRestrictions.current();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] auto defaultRestrictionsValue() const {
return _defaultRestrictions.value();
}
void setDefaultRestrictions(ChatRestrictions rights);
// Like in ChatData.
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool canWrite() const;
[[nodiscard]] bool allowsForwarding() const;
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool canEditInformation() const;
[[nodiscard]] bool canEditPermissions() const;
[[nodiscard]] bool canEditUsername() const;
[[nodiscard]] bool canEditPreHistoryHidden() const;
[[nodiscard]] bool canAddMembers() const;
[[nodiscard]] bool canAddAdmins() const;
[[nodiscard]] bool canBanMembers() const;
[[nodiscard]] bool canSendPolls() const;
[[nodiscard]] bool anyoneCanAddMembers() const;
[[nodiscard]] bool canEditMessages() const;
[[nodiscard]] bool canDeleteMessages() const;
[[nodiscard]] bool hiddenPreHistory() const;
[[nodiscard]] bool canPublish() const;
[[nodiscard]] bool canViewMembers() const;
[[nodiscard]] bool canViewAdmins() const;
[[nodiscard]] bool canViewBanned() const;
[[nodiscard]] bool canEditSignatures() const;
[[nodiscard]] bool canEditStickers() const;
[[nodiscard]] bool canDelete() const;
[[nodiscard]] bool canEditAdmin(not_null<UserData*> user) const;
[[nodiscard]] bool canRestrictParticipant(
not_null<PeerData*> participant) const;
void setInviteLink(const QString &newInviteLink);
[[nodiscard]] QString inviteLink() const {
return _inviteLink;
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool canHaveInviteLink() const;
2019-06-21 12:27:46 +00:00
void setLocation(const MTPChannelLocation &data);
2019-07-16 11:46:50 +00:00
[[nodiscard]] const ChannelLocation *getLocation() const;
2019-06-21 12:27:46 +00:00
void setLinkedChat(ChannelData *linked);
2019-07-16 11:46:50 +00:00
[[nodiscard]] ChannelData *linkedChat() const;
[[nodiscard]] bool linkedChatKnown() const;
void ptsInit(int32 pts) {
_ptsWaiter.init(pts);
}
void ptsReceived(int32 pts) {
_ptsWaiter.updateAndApply(this, pts, 0);
}
bool ptsUpdateAndApply(int32 pts, int32 count) {
return _ptsWaiter.updateAndApply(this, pts, count);
}
bool ptsUpdateAndApply(
int32 pts,
int32 count,
const MTPUpdate &update) {
return _ptsWaiter.updateAndApply(this, pts, count, update);
}
bool ptsUpdateAndApply(
2019-07-16 11:46:50 +00:00
int32 pts,
int32 count,
const MTPUpdates &updates) {
return _ptsWaiter.updateAndApply(this, pts, count, updates);
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] int32 pts() const {
return _ptsWaiter.current();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool ptsInited() const {
return _ptsWaiter.inited();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool ptsRequesting() const {
return _ptsWaiter.requesting();
}
void ptsSetRequesting(bool isRequesting) {
return _ptsWaiter.setRequesting(isRequesting);
}
2019-07-16 11:46:50 +00:00
// < 0 - not waiting
void ptsWaitingForShortPoll(int32 ms) {
return _ptsWaiter.setWaitingForShortPoll(this, ms);
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool ptsWaitingForSkipped() const {
return _ptsWaiter.waitingForSkipped();
}
2019-07-16 11:46:50 +00:00
[[nodiscard]] bool ptsWaitingForShortPoll() const {
return _ptsWaiter.waitingForShortPoll();
}
void setUnavailableReasons(
std::vector<Data::UnavailableReason> &&reason);
2019-07-16 11:46:50 +00:00
[[nodiscard]] MsgId availableMinId() const {
return _availableMinId;
}
void setAvailableMinId(MsgId availableMinId);
2019-07-16 11:46:50 +00:00
[[nodiscard]] ChatData *getMigrateFromChat() const;
void setMigrateFromChat(ChatData *chat);
2019-07-16 11:46:50 +00:00
[[nodiscard]] int slowmodeSeconds() const;
void setSlowmodeSeconds(int seconds);
[[nodiscard]] TimeId slowmodeLastMessage() const;
2019-07-16 13:59:50 +00:00
void growSlowmodeLastMessage(TimeId when);
2019-07-16 11:46:50 +00:00
2020-07-01 14:19:25 +00:00
void setInvitePeek(const QString &hash, TimeId expires);
void clearInvitePeek();
[[nodiscard]] TimeId invitePeekExpires() const;
[[nodiscard]] QString invitePeekHash() const;
void privateErrorReceived();
[[nodiscard]] Data::GroupCall *groupCall() const {
2020-11-20 19:25:35 +00:00
return _call.get();
}
void migrateCall(std::unique_ptr<Data::GroupCall> call);
2021-04-12 11:36:19 +00:00
void setGroupCall(
const MTPInputGroupCall &call,
TimeId scheduleDate = 0,
bool rtmp = false);
void clearGroupCall();
void setGroupCallDefaultJoinAs(PeerId peerId);
[[nodiscard]] PeerId groupCallDefaultJoinAs() const;
2020-11-20 19:25:35 +00:00
2022-08-22 09:15:34 +00:00
void setAllowedReactions(Data::AllowedReactions value);
[[nodiscard]] const Data::AllowedReactions &allowedReactions() const;
[[nodiscard]] Data::Forum *forum() const {
return mgInfo ? mgInfo->forum() : nullptr;
}
// Still public data members.
uint64 access = 0;
MTPinputChannel inputChannel = MTP_inputChannelEmpty();
QString username;
int32 date = 0;
std::unique_ptr<MegagroupInfo> mgInfo;
2019-07-16 11:46:50 +00:00
// > 0 - user who invited me to channel, < 0 - not in channel.
UserId inviter = 0;
TimeId inviteDate = 0;
bool inviteViaRequest = false;
private:
2020-07-01 14:19:25 +00:00
struct InvitePeek {
QString hash;
TimeId expires = 0;
};
auto unavailableReasons() const
-> const std::vector<Data::UnavailableReason> & override;
bool canEditLastAdmin(not_null<UserData*> user) const;
2021-07-08 13:11:09 +00:00
Flags _flags = ChannelDataFlags(Flag::Forbidden);
PtsWaiter _ptsWaiter;
2019-05-20 18:40:53 +00:00
int _membersCount = -1;
int _adminsCount = 1;
int _restrictedCount = 0;
int _kickedCount = 0;
int _pendingRequestsCount = 0;
std::vector<UserId> _recentRequesters;
MsgId _availableMinId = 0;
RestrictionFlags _defaultRestrictions;
AdminRightFlags _adminRights;
RestrictionFlags _restrictions;
TimeId _restrictedUntil;
std::vector<Data::UnavailableReason> _unavailableReasons;
2020-07-01 14:19:25 +00:00
std::unique_ptr<InvitePeek> _invitePeek;
QString _inviteLink;
std::optional<ChannelData*> _linkedChat;
2022-08-22 09:15:34 +00:00
Data::AllowedReactions _allowedReactions;
2020-11-20 19:25:35 +00:00
std::unique_ptr<Data::GroupCall> _call;
PeerId _callDefaultJoinAs = 0;
2020-11-20 19:25:35 +00:00
2019-07-16 11:46:50 +00:00
int _slowmodeSeconds = 0;
TimeId _slowmodeLastMessage = 0;
};
namespace Data {
void ApplyMigration(
not_null<ChatData*> chat,
not_null<ChannelData*> channel);
void ApplyChannelUpdate(
not_null<ChannelData*> channel,
const MTPDupdateChatDefaultBannedRights &update);
void ApplyChannelUpdate(
not_null<ChannelData*> channel,
const MTPDchannelFull &update);
} // namespace Data