feat: history ttl

closes MTQ-86
This commit is contained in:
alina 🌸 2023-10-03 04:05:24 +03:00
parent 6f7cda5544
commit 1686c3f183
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
9 changed files with 135 additions and 23 deletions

View file

@ -73,6 +73,7 @@ import { setChatDefaultPermissions } from './methods/chats/set-chat-default-perm
import { setChatDescription } from './methods/chats/set-chat-description' import { setChatDescription } from './methods/chats/set-chat-description'
import { setChatPhoto } from './methods/chats/set-chat-photo' import { setChatPhoto } from './methods/chats/set-chat-photo'
import { setChatTitle } from './methods/chats/set-chat-title' 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 { setChatUsername } from './methods/chats/set-chat-username'
import { setSlowMode } from './methods/chats/set-slow-mode' import { setSlowMode } from './methods/chats/set-slow-mode'
import { toggleContentProtection } from './methods/chats/toggle-content-protection' import { toggleContentProtection } from './methods/chats/toggle-content-protection'
@ -201,6 +202,7 @@ import {
import { blockUser } from './methods/users/block-user' import { blockUser } from './methods/users/block-user'
import { deleteProfilePhotos } from './methods/users/delete-profile-photos' import { deleteProfilePhotos } from './methods/users/delete-profile-photos'
import { getCommonChats } from './methods/users/get-common-chats' import { getCommonChats } from './methods/users/get-common-chats'
import { getGlobalTtl } from './methods/users/get-global-ttl'
import { getMe } from './methods/users/get-me' import { getMe } from './methods/users/get-me'
import { getMyUsername } from './methods/users/get-my-username' import { getMyUsername } from './methods/users/get-my-username'
import { getProfilePhoto } from './methods/users/get-profile-photo' 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 { iterProfilePhotos } from './methods/users/iter-profile-photos'
import { resolvePeer } from './methods/users/resolve-peer' import { resolvePeer } from './methods/users/resolve-peer'
import { resolvePeerMany } from './methods/users/resolve-peer-many' import { resolvePeerMany } from './methods/users/resolve-peer-many'
import { setGlobalTtl } from './methods/users/set-global-ttl'
import { setOffline } from './methods/users/set-offline' import { setOffline } from './methods/users/set-offline'
import { setProfilePhoto } from './methods/users/set-profile-photo' import { setProfilePhoto } from './methods/users/set-profile-photo'
import { setUsername } from './methods/users/set-username' 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} * If you want to create a supergroup, use {@link createSupergroup}
* instead. * 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<InputPeerLike>): Promise<Chat> 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<InputPeerLike>
/**
* TTL period (in seconds) for the newly created chat
*
* @default 0 (i.e. messages don't expire)
*/
ttlPeriod?: number
}): Promise<Chat>
/** /**
* Create a new supergroup * Create a new supergroup
* *
@ -1107,7 +1124,7 @@ export interface TelegramClient extends BaseTelegramClient {
forum?: boolean 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) * @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 * @param title New chat title, 1-255 characters
*/ */
setChatTitle(chatId: InputPeerLike, title: string): Promise<void> setChatTitle(chatId: InputPeerLike, title: string): Promise<void>
/**
* 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<void>
/** /**
* Change supergroup/channel username * Change supergroup/channel username
* *
@ -4143,6 +4167,11 @@ export interface TelegramClient extends BaseTelegramClient {
* @throws MtInvalidPeerTypeError * @throws MtInvalidPeerTypeError
*/ */
getCommonChats(userId: InputPeerLike): Promise<Chat[]> getCommonChats(userId: InputPeerLike): Promise<Chat[]>
/**
* Gets the current default value of the Time-To-Live setting, applied to all new chats.
*
*/
getGlobalTtl(): Promise<number>
/** /**
* Get currently authorized user's full information * 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 * @param force (default: `false`) Whether to force re-fetch the peer from the server
*/ */
resolvePeer(peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputPeer> resolvePeer(peerId: InputPeerLike, force?: boolean): Promise<tl.TypeInputPeer>
/**
* 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<void>
/** /**
* Change user status to offline or online * Change user status to offline or online
* *
@ -4448,6 +4484,7 @@ export class TelegramClient extends BaseTelegramClient {
setChatDescription = setChatDescription setChatDescription = setChatDescription
setChatPhoto = setChatPhoto setChatPhoto = setChatPhoto
setChatTitle = setChatTitle setChatTitle = setChatTitle
setChatTtl = setChatTtl
setChatUsername = setChatUsername setChatUsername = setChatUsername
setSlowMode = setSlowMode setSlowMode = setSlowMode
toggleContentProtection = toggleContentProtection toggleContentProtection = toggleContentProtection
@ -4577,6 +4614,7 @@ export class TelegramClient extends BaseTelegramClient {
blockUser = blockUser blockUser = blockUser
deleteProfilePhotos = deleteProfilePhotos deleteProfilePhotos = deleteProfilePhotos
getCommonChats = getCommonChats getCommonChats = getCommonChats
getGlobalTtl = getGlobalTtl
getMe = getMe getMe = getMe
getMyUsername = getMyUsername getMyUsername = getMyUsername
getProfilePhoto = getProfilePhoto getProfilePhoto = getProfilePhoto
@ -4585,6 +4623,7 @@ export class TelegramClient extends BaseTelegramClient {
iterProfilePhotos = iterProfilePhotos iterProfilePhotos = iterProfilePhotos
resolvePeerMany = resolvePeerMany resolvePeerMany = resolvePeerMany
resolvePeer = resolvePeer resolvePeer = resolvePeer
setGlobalTtl = setGlobalTtl
setOffline = setOffline setOffline = setOffline
setProfilePhoto = setProfilePhoto setProfilePhoto = setProfilePhoto
setUsername = setUsername setUsername = setUsername

View file

@ -11,17 +11,33 @@ import { assertIsUpdatesGroup } from '../../utils/updates-utils'
* If you want to create a supergroup, use {@link createSupergroup} * If you want to create a supergroup, use {@link createSupergroup}
* instead. * 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 * @internal
*/ */
export async function createGroup( export async function createGroup(
this: TelegramClient, this: TelegramClient,
title: string, params: {
users: MaybeArray<InputPeerLike>, /**
* 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<InputPeerLike>
/**
* TTL period (in seconds) for the newly created chat
*
* @default 0 (i.e. messages don't expire)
*/
ttlPeriod?: number
},
): Promise<Chat> { ): Promise<Chat> {
const { title } = params
let { users } = params
if (!Array.isArray(users)) users = [users] if (!Array.isArray(users)) users = [users]
const peers = await this.resolvePeerMany(users, normalizeToInputUser) const peers = await this.resolvePeerMany(users, normalizeToInputUser)

View file

@ -27,7 +27,7 @@ export async function createSupergroup(
forum?: boolean 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) * @default 0 (i.e. messages don't expire)
*/ */

View file

@ -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<void> {
await this.call({
_: 'messages.setHistoryTTL',
peer: await this.resolvePeer(chatId),
period,
})
}

View file

@ -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<number> {
return this.call({
_: 'messages.getDefaultHistoryTTL',
}).then((r) => r.period)
}

View file

@ -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<void> {
await this.call({
_: 'messages.setDefaultHistoryTTL',
period,
})
}

View file

@ -295,6 +295,13 @@ export class Dialog {
return this._draftMessage return this._draftMessage
} }
/**
* TTL period of all messages in this dialog
*/
get ttlPeriod(): number | null {
return this.raw.ttlPeriod ?? null
}
} }
makeInspectable(Dialog) makeInspectable(Dialog)

View file

@ -418,6 +418,13 @@ export class Message {
return this.raw._ === 'message' && this.raw.media?._ === 'messageMediaDocument' && this.raw.media.nopremium! 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 private _markup?: ReplyMarkup | null
/** /**
* Reply markup provided with this message, if any. * Reply markup provided with this message, if any.

View file

@ -361,16 +361,6 @@ export class Chat {
* Returned only in {@link TelegramClient.getFullChat} * Returned only in {@link TelegramClient.getFullChat}
*/ */
get membersCount(): number | null { 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 && this.fullPeer._ !== 'userFull') {
if (this.fullPeer._ === 'chatFull' && this.fullPeer.participants._ === 'chatParticipants') { if (this.fullPeer._ === 'chatFull' && this.fullPeer.participants._ === 'chatParticipants') {
return this.fullPeer.participants.participants.length return this.fullPeer.participants.participants.length
@ -452,6 +442,15 @@ export class Chat {
return this._linkedChat ?? null 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 private _user?: User
/** /**
* Get a {@link User} from this chat. * Get a {@link User} from this chat.