From 1686c3f1836eb1a15f9d3d01bfc963e80664a3af Mon Sep 17 00:00:00 2001 From: Alina Sireneva Date: Tue, 3 Oct 2023 04:05:24 +0300 Subject: [PATCH] feat: history ttl closes MTQ-86 --- packages/client/src/client.ts | 51 ++++++++++++++++--- .../client/src/methods/chats/create-group.ts | 28 +++++++--- .../src/methods/chats/create-supergroup.ts | 2 +- .../client/src/methods/chats/set-chat-ttl.ts | 17 +++++++ .../src/methods/users/get-global-ttl.ts | 12 +++++ .../src/methods/users/set-global-ttl.ts | 15 ++++++ packages/client/src/types/messages/dialog.ts | 7 +++ packages/client/src/types/messages/message.ts | 7 +++ packages/client/src/types/peers/chat.ts | 19 ++++--- 9 files changed, 135 insertions(+), 23 deletions(-) create mode 100644 packages/client/src/methods/chats/set-chat-ttl.ts create mode 100644 packages/client/src/methods/users/get-global-ttl.ts create mode 100644 packages/client/src/methods/users/set-global-ttl.ts diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index 199e7b73..468cdd54 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -73,6 +73,7 @@ import { setChatDefaultPermissions } from './methods/chats/set-chat-default-perm import { setChatDescription } from './methods/chats/set-chat-description' import { setChatPhoto } from './methods/chats/set-chat-photo' import { setChatTitle } from './methods/chats/set-chat-title' +import { setChatTtl } from './methods/chats/set-chat-ttl' import { setChatUsername } from './methods/chats/set-chat-username' import { setSlowMode } from './methods/chats/set-slow-mode' import { toggleContentProtection } from './methods/chats/toggle-content-protection' @@ -201,6 +202,7 @@ import { import { blockUser } from './methods/users/block-user' import { deleteProfilePhotos } from './methods/users/delete-profile-photos' import { getCommonChats } from './methods/users/get-common-chats' +import { getGlobalTtl } from './methods/users/get-global-ttl' import { getMe } from './methods/users/get-me' import { getMyUsername } from './methods/users/get-my-username' import { getProfilePhoto } from './methods/users/get-profile-photo' @@ -209,6 +211,7 @@ import { getUsers } from './methods/users/get-users' import { iterProfilePhotos } from './methods/users/iter-profile-photos' import { resolvePeer } from './methods/users/resolve-peer' import { resolvePeerMany } from './methods/users/resolve-peer-many' +import { setGlobalTtl } from './methods/users/set-global-ttl' import { setOffline } from './methods/users/set-offline' import { setProfilePhoto } from './methods/users/set-profile-photo' import { setUsername } from './methods/users/set-username' @@ -1079,12 +1082,26 @@ export interface TelegramClient extends BaseTelegramClient { * If you want to create a supergroup, use {@link createSupergroup} * instead. * - * @param title Group title - * @param users - * User(s) to be invited in the group (ID(s), username(s) or phone number(s)). - * Due to Telegram limitations, you can't create a legacy group with yourself. */ - createGroup(title: string, users: MaybeArray): Promise + createGroup(params: { + /** + * Group title + */ + title: string + + /** + * User(s) to be invited in the group (ID(s), username(s) or phone number(s)). + * Due to Telegram limitations, you can't create a legacy group with just yourself. + */ + users: MaybeArray + + /** + * TTL period (in seconds) for the newly created chat + * + * @default 0 (i.e. messages don't expire) + */ + ttlPeriod?: number + }): Promise /** * Create a new supergroup * @@ -1107,7 +1124,7 @@ export interface TelegramClient extends BaseTelegramClient { forum?: boolean /** - * TTL period (in seconds) for the newly created channel + * TTL period (in seconds) for the newly created supergroup * * @default 0 (i.e. messages don't expire) */ @@ -1509,6 +1526,13 @@ export interface TelegramClient extends BaseTelegramClient { * @param title New chat title, 1-255 characters */ setChatTitle(chatId: InputPeerLike, title: string): Promise + /** + * Set maximum Time-To-Live of all newly sent messages in the specified chat + * + * @param chatId Chat ID + * @param period New TTL period, in seconds (or 0 to disable) + */ + setChatTtl(chatId: InputPeerLike, period: number): Promise /** * Change supergroup/channel username * @@ -4143,6 +4167,11 @@ export interface TelegramClient extends BaseTelegramClient { * @throws MtInvalidPeerTypeError */ getCommonChats(userId: InputPeerLike): Promise + /** + * Gets the current default value of the Time-To-Live setting, applied to all new chats. + * + */ + getGlobalTtl(): Promise /** * Get currently authorized user's full information * @@ -4257,6 +4286,13 @@ export interface TelegramClient extends BaseTelegramClient { * @param force (default: `false`) Whether to force re-fetch the peer from the server */ resolvePeer(peerId: InputPeerLike, force?: boolean): Promise + /** + * Changes the current default value of the Time-To-Live setting, + * applied to all new chats. + * + * @param period New TTL period, in seconds (or 0 to disable) + */ + setGlobalTtl(period: number): Promise /** * Change user status to offline or online * @@ -4448,6 +4484,7 @@ export class TelegramClient extends BaseTelegramClient { setChatDescription = setChatDescription setChatPhoto = setChatPhoto setChatTitle = setChatTitle + setChatTtl = setChatTtl setChatUsername = setChatUsername setSlowMode = setSlowMode toggleContentProtection = toggleContentProtection @@ -4577,6 +4614,7 @@ export class TelegramClient extends BaseTelegramClient { blockUser = blockUser deleteProfilePhotos = deleteProfilePhotos getCommonChats = getCommonChats + getGlobalTtl = getGlobalTtl getMe = getMe getMyUsername = getMyUsername getProfilePhoto = getProfilePhoto @@ -4585,6 +4623,7 @@ export class TelegramClient extends BaseTelegramClient { iterProfilePhotos = iterProfilePhotos resolvePeerMany = resolvePeerMany resolvePeer = resolvePeer + setGlobalTtl = setGlobalTtl setOffline = setOffline setProfilePhoto = setProfilePhoto setUsername = setUsername diff --git a/packages/client/src/methods/chats/create-group.ts b/packages/client/src/methods/chats/create-group.ts index 4b8e4bff..63bcd9c0 100644 --- a/packages/client/src/methods/chats/create-group.ts +++ b/packages/client/src/methods/chats/create-group.ts @@ -11,17 +11,33 @@ import { assertIsUpdatesGroup } from '../../utils/updates-utils' * If you want to create a supergroup, use {@link createSupergroup} * instead. * - * @param title Group title - * @param users - * User(s) to be invited in the group (ID(s), username(s) or phone number(s)). - * Due to Telegram limitations, you can't create a legacy group with yourself. * @internal */ export async function createGroup( this: TelegramClient, - title: string, - users: MaybeArray, + params: { + /** + * Group title + */ + title: string + + /** + * User(s) to be invited in the group (ID(s), username(s) or phone number(s)). + * Due to Telegram limitations, you can't create a legacy group with just yourself. + */ + users: MaybeArray + + /** + * TTL period (in seconds) for the newly created chat + * + * @default 0 (i.e. messages don't expire) + */ + ttlPeriod?: number + }, ): Promise { + const { title } = params + let { users } = params + if (!Array.isArray(users)) users = [users] const peers = await this.resolvePeerMany(users, normalizeToInputUser) diff --git a/packages/client/src/methods/chats/create-supergroup.ts b/packages/client/src/methods/chats/create-supergroup.ts index 4a1cb054..3274845e 100644 --- a/packages/client/src/methods/chats/create-supergroup.ts +++ b/packages/client/src/methods/chats/create-supergroup.ts @@ -27,7 +27,7 @@ export async function createSupergroup( forum?: boolean /** - * TTL period (in seconds) for the newly created channel + * TTL period (in seconds) for the newly created supergroup * * @default 0 (i.e. messages don't expire) */ diff --git a/packages/client/src/methods/chats/set-chat-ttl.ts b/packages/client/src/methods/chats/set-chat-ttl.ts new file mode 100644 index 00000000..9cdc536e --- /dev/null +++ b/packages/client/src/methods/chats/set-chat-ttl.ts @@ -0,0 +1,17 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' + +/** + * Set maximum Time-To-Live of all newly sent messages in the specified chat + * + * @param chatId Chat ID + * @param period New TTL period, in seconds (or 0 to disable) + * @internal + */ +export async function setChatTtl(this: TelegramClient, chatId: InputPeerLike, period: number): Promise { + await this.call({ + _: 'messages.setHistoryTTL', + peer: await this.resolvePeer(chatId), + period, + }) +} diff --git a/packages/client/src/methods/users/get-global-ttl.ts b/packages/client/src/methods/users/get-global-ttl.ts new file mode 100644 index 00000000..b64ce05f --- /dev/null +++ b/packages/client/src/methods/users/get-global-ttl.ts @@ -0,0 +1,12 @@ +import { TelegramClient } from '../../client' + +/** + * Gets the current default value of the Time-To-Live setting, applied to all new chats. + * + * @internal + */ +export async function getGlobalTtl(this: TelegramClient): Promise { + return this.call({ + _: 'messages.getDefaultHistoryTTL', + }).then((r) => r.period) +} diff --git a/packages/client/src/methods/users/set-global-ttl.ts b/packages/client/src/methods/users/set-global-ttl.ts new file mode 100644 index 00000000..2e51fc3c --- /dev/null +++ b/packages/client/src/methods/users/set-global-ttl.ts @@ -0,0 +1,15 @@ +import { TelegramClient } from '../../client' + +/** + * Changes the current default value of the Time-To-Live setting, + * applied to all new chats. + * + * @param period New TTL period, in seconds (or 0 to disable) + * @internal + */ +export async function setGlobalTtl(this: TelegramClient, period: number): Promise { + await this.call({ + _: 'messages.setDefaultHistoryTTL', + period, + }) +} diff --git a/packages/client/src/types/messages/dialog.ts b/packages/client/src/types/messages/dialog.ts index ec13f29d..9d2201ce 100644 --- a/packages/client/src/types/messages/dialog.ts +++ b/packages/client/src/types/messages/dialog.ts @@ -295,6 +295,13 @@ export class Dialog { return this._draftMessage } + + /** + * TTL period of all messages in this dialog + */ + get ttlPeriod(): number | null { + return this.raw.ttlPeriod ?? null + } } makeInspectable(Dialog) diff --git a/packages/client/src/types/messages/message.ts b/packages/client/src/types/messages/message.ts index 8ad03de5..162c2864 100644 --- a/packages/client/src/types/messages/message.ts +++ b/packages/client/src/types/messages/message.ts @@ -418,6 +418,13 @@ export class Message { return this.raw._ === 'message' && this.raw.media?._ === 'messageMediaDocument' && this.raw.media.nopremium! } + /** + * TTL period of the message, in seconds. + */ + get ttlPeriod(): number | null { + return this.raw.ttlPeriod ?? null + } + private _markup?: ReplyMarkup | null /** * Reply markup provided with this message, if any. diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index 60bdf2e5..696044ed 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -361,16 +361,6 @@ export class Chat { * Returned only in {@link TelegramClient.getFullChat} */ get membersCount(): number | null { - // return this.fullPeer && this.fullPeer._ !== 'userFull' ? - // this.fullPeer._ === 'chatFull' ? - // this.fullPeer.participants._ === 'chatParticipants' ? - // this.fullPeer.participants.participants.length : - // null : - // this.fullPeer._ === 'channelFull' ? - // this.fullPeer.participantsCount ?? null : - // null : - // null - if (this.fullPeer && this.fullPeer._ !== 'userFull') { if (this.fullPeer._ === 'chatFull' && this.fullPeer.participants._ === 'chatParticipants') { return this.fullPeer.participants.participants.length @@ -452,6 +442,15 @@ export class Chat { return this._linkedChat ?? null } + /** + * TTL of all messages in this chat, in seconds + * + * Returned only in {@link TelegramClient.getFullChat} + */ + get ttlPeriod(): number | null { + return this.fullPeer?.ttlPeriod ?? null + } + private _user?: User /** * Get a {@link User} from this chat.