tdesktop/Telegram/SourceFiles/api/api_invite_links.h

208 lines
5.2 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
class ApiWrap;
namespace Api {
struct InviteLink {
QString link;
not_null<UserData*> admin;
TimeId date = 0;
TimeId startDate = 0;
TimeId expireDate = 0;
int usageLimit = 0;
int usage = 0;
bool permanent = false;
bool revoked = false;
};
struct PeerInviteLinks {
std::vector<InviteLink> links;
int count = 0;
};
struct JoinedByLinkUser {
not_null<UserData*> user;
TimeId date = 0;
};
struct JoinedByLinkSlice {
std::vector<JoinedByLinkUser> users;
int count = 0;
};
struct InviteLinkUpdate {
not_null<PeerData*> peer;
not_null<UserData*> admin;
QString was;
std::optional<InviteLink> now;
};
2021-02-02 10:43:38 +00:00
[[nodiscard]] JoinedByLinkSlice ParseJoinedByLinkSlice(
not_null<PeerData*> peer,
const MTPmessages_ChatInviteImporters &slice);
2021-01-19 10:56:01 +00:00
class InviteLinks final {
public:
explicit InviteLinks(not_null<ApiWrap*> api);
using Link = InviteLink;
using Links = PeerInviteLinks;
using Update = InviteLinkUpdate;
void create(
not_null<PeerData*> peer,
Fn<void(Link)> done = nullptr,
TimeId expireDate = 0,
int usageLimit = 0);
void edit(
not_null<PeerData*> peer,
not_null<UserData*> admin,
const QString &link,
TimeId expireDate,
int usageLimit,
Fn<void(Link)> done = nullptr);
void revoke(
not_null<PeerData*> peer,
not_null<UserData*> admin,
const QString &link,
Fn<void(Link)> done = nullptr);
void revokePermanent(
not_null<PeerData*> peer,
not_null<UserData*> admin,
const QString &link,
2021-02-02 10:43:38 +00:00
Fn<void()> done = nullptr);
2021-01-19 10:26:00 +00:00
void destroy(
not_null<PeerData*> peer,
not_null<UserData*> admin,
2021-01-19 10:26:00 +00:00
const QString &link,
Fn<void()> done = nullptr);
void destroyAllRevoked(
not_null<PeerData*> peer,
not_null<UserData*> admin,
2021-01-19 10:26:00 +00:00
Fn<void()> done = nullptr);
void setMyPermanent(
not_null<PeerData*> peer,
const MTPExportedChatInvite &invite);
void clearMyPermanent(not_null<PeerData*> peer);
void requestMyLinks(not_null<PeerData*> peer);
[[nodiscard]] const Links &myLinks(not_null<PeerData*> peer) const;
[[nodiscard]] rpl::producer<JoinedByLinkSlice> joinedFirstSliceValue(
not_null<PeerData*> peer,
const QString &link,
int fullCount);
2021-01-19 10:56:01 +00:00
[[nodiscard]] std::optional<JoinedByLinkSlice> joinedFirstSliceLoaded(
not_null<PeerData*> peer,
const QString &link) const;
[[nodiscard]] rpl::producer<Update> updates(
2021-02-05 14:36:37 +00:00
not_null<PeerData*> peer,
not_null<UserData*> admin) const;
2021-01-19 10:26:00 +00:00
[[nodiscard]] rpl::producer<> allRevokedDestroyed(
2021-02-05 14:36:37 +00:00
not_null<PeerData*> peer,
not_null<UserData*> admin) const;
void requestMoreLinks(
not_null<PeerData*> peer,
not_null<UserData*> admin,
2021-01-19 10:56:01 +00:00
TimeId lastDate,
const QString &lastLink,
bool revoked,
Fn<void(Links)> done);
private:
struct LinkKey {
not_null<PeerData*> peer;
QString link;
friend inline bool operator<(const LinkKey &a, const LinkKey &b) {
return (a.peer == b.peer)
? (a.link < b.link)
: (a.peer < b.peer);
}
friend inline bool operator==(const LinkKey &a, const LinkKey &b) {
return (a.peer == b.peer) && (a.link == b.link);
}
};
2021-02-02 10:43:38 +00:00
[[nodiscard]] Links parseSlice(
not_null<PeerData*> peer,
const MTPmessages_ExportedChatInvites &slice) const;
[[nodiscard]] Link parse(
not_null<PeerData*> peer,
const MTPExportedChatInvite &invite) const;
2021-02-05 17:35:29 +00:00
[[nodiscard]] Link *lookupMyPermanent(not_null<PeerData*> peer);
[[nodiscard]] Link *lookupMyPermanent(Links &links);
[[nodiscard]] const Link *lookupMyPermanent(const Links &links) const;
Link prepend(
not_null<PeerData*> peer,
not_null<UserData*> admin,
const MTPExportedChatInvite &invite);
2021-02-05 17:35:29 +00:00
void prependMyToFirstSlice(
not_null<PeerData*> peer,
not_null<UserData*> admin,
const Link &link);
void notify(not_null<PeerData*> peer);
void editPermanentLink(
not_null<PeerData*> peer,
const QString &link);
void performEdit(
not_null<PeerData*> peer,
not_null<UserData*> admin,
const QString &link,
Fn<void(Link)> done,
bool revoke,
TimeId expireDate = 0,
int usageLimit = 0);
void performCreate(
not_null<PeerData*> peer,
Fn<void(Link)> done,
bool revokeLegacyPermanent,
TimeId expireDate = 0,
int usageLimit = 0);
void requestJoinedFirstSlice(LinkKey key);
2021-01-19 10:56:01 +00:00
[[nodiscard]] std::optional<JoinedByLinkSlice> lookupJoinedFirstSlice(
LinkKey key) const;
const not_null<ApiWrap*> _api;
base::flat_map<not_null<PeerData*>, Links> _firstSlices;
base::flat_map<not_null<PeerData*>, mtpRequestId> _firstSliceRequests;
base::flat_map<LinkKey, JoinedByLinkSlice> _firstJoined;
base::flat_map<LinkKey, mtpRequestId> _firstJoinedRequests;
rpl::event_stream<LinkKey> _joinedFirstSliceLoaded;
base::flat_map<
not_null<PeerData*>,
std::vector<Fn<void(Link)>>> _createCallbacks;
base::flat_map<LinkKey, std::vector<Fn<void(Link)>>> _editCallbacks;
2021-01-19 10:26:00 +00:00
base::flat_map<LinkKey, std::vector<Fn<void()>>> _deleteCallbacks;
base::flat_map<
not_null<PeerData*>,
std::vector<Fn<void()>>> _deleteRevokedCallbacks;
rpl::event_stream<Update> _updates;
2021-02-05 14:36:37 +00:00
struct AllRevokedDestroyed {
not_null<PeerData*> peer;
not_null<UserData*> admin;
};
rpl::event_stream<AllRevokedDestroyed> _allRevokedDestroyed;
};
} // namespace Api