feat: multiple usernames

closes MTQ-57
This commit is contained in:
alina 🌸 2023-10-03 03:23:23 +03:00
parent 544e5a68dc
commit efaba06102
Signed by: teidesu
SSH key fingerprint: SHA256:uNeCpw6aTSU4aIObXLvHfLkDa82HWH9EiOj9AXOIRpI
6 changed files with 170 additions and 14 deletions

View file

@ -66,6 +66,7 @@ import { joinChat } from './methods/chats/join-chat'
import { kickChatMember } from './methods/chats/kick-chat-member'
import { leaveChat } from './methods/chats/leave-chat'
import { markChatUnread } from './methods/chats/mark-chat-unread'
import { reorderUsernames } from './methods/chats/reorder-usernames'
import { restrictChatMember } from './methods/chats/restrict-chat-member'
import { saveDraft } from './methods/chats/save-draft'
import { setChatDefaultPermissions } from './methods/chats/set-chat-default-permissions'
@ -74,6 +75,7 @@ import { setChatPhoto } from './methods/chats/set-chat-photo'
import { setChatTitle } from './methods/chats/set-chat-title'
import { setChatUsername } from './methods/chats/set-chat-username'
import { setSlowMode } from './methods/chats/set-slow-mode'
import { toggleFragmentUsername } from './methods/chats/toggle-fragment-username'
import { toggleJoinRequests } from './methods/chats/toggle-join-requests'
import { toggleJoinToSend } from './methods/chats/toggle-join-to-send'
import { unarchiveChats } from './methods/chats/unarchive-chats'
@ -208,9 +210,9 @@ import { resolvePeer } from './methods/users/resolve-peer'
import { resolvePeerMany } from './methods/users/resolve-peer-many'
import { setOffline } from './methods/users/set-offline'
import { setProfilePhoto } from './methods/users/set-profile-photo'
import { setUsername } from './methods/users/set-username'
import { unblockUser } from './methods/users/unblock-user'
import { updateProfile } from './methods/users/update-profile'
import { updateUsername } from './methods/users/update-username'
import {
ArrayPaginated,
ArrayWithTotal,
@ -1419,6 +1421,12 @@ export interface TelegramClient extends BaseTelegramClient {
* @param chatId Chat ID
*/
markChatUnread(chatId: InputPeerLike): Promise<void>
/**
* Reorder usernames
*
* @param peerId Bot, channel or "me"/"self"
*/
reorderUsernames(peerId: InputPeerLike, order: string[]): Promise<void>
/**
* Restrict a user in a supergroup.
*
@ -1520,6 +1528,28 @@ export interface TelegramClient extends BaseTelegramClient {
* Valid values are: `0 (off), 10, 30, 60 (1m), 300 (5m), 900 (15m) or 3600 (1h)`
*/
setSlowMode(chatId: InputPeerLike, seconds?: number): Promise<void>
/**
* Toggle a collectible (Fragment) username
*
* > **Note**: non-collectible usernames must still be changed
* > using {@link setUsername}/{@link setChatUsername}
*
* @param peerId Bot, channel or "me"/"self"
*/
toggleFragmentUsername(
peerId: InputPeerLike,
params: {
/**
* Username to toggle
*/
username: string
/**
* Whether to enable or disable the username
*/
active: boolean
},
): Promise<void>
/**
* Set whether a channel/supergroup has join requests enabled.
*
@ -4241,6 +4271,15 @@ export interface TelegramClient extends BaseTelegramClient {
media: InputFileLike | tl.TypeInputPhoto,
previewSec?: number,
): Promise<Photo>
/**
* Change username of the current user.
*
* Note that bots usernames must be changed through
* bot support or re-created from scratch.
*
* @param username New username (5-32 chars, allowed chars: `a-zA-Z0-9_`), or `null` to remove
*/
setUsername(username: string | null): Promise<User>
/**
* Unblock a user
*
@ -4270,15 +4309,6 @@ export interface TelegramClient extends BaseTelegramClient {
*/
bio?: string
}): Promise<User>
/**
* Change username of the current user.
*
* Note that bots usernames must be changed through
* bot support or re-created from scratch.
*
* @param username New username (5-32 chars, allowed chars: `a-zA-Z0-9_`), or `null` to remove
*/
updateUsername(username: string | null): Promise<User>
}
export class TelegramClient extends BaseTelegramClient {
@ -4403,6 +4433,7 @@ export class TelegramClient extends BaseTelegramClient {
kickChatMember = kickChatMember
leaveChat = leaveChat
markChatUnread = markChatUnread
reorderUsernames = reorderUsernames
restrictChatMember = restrictChatMember
saveDraft = saveDraft
setChatDefaultPermissions = setChatDefaultPermissions
@ -4411,6 +4442,7 @@ export class TelegramClient extends BaseTelegramClient {
setChatTitle = setChatTitle
setChatUsername = setChatUsername
setSlowMode = setSlowMode
toggleFragmentUsername = toggleFragmentUsername
toggleJoinRequests = toggleJoinRequests
toggleJoinToSend = toggleJoinToSend
unarchiveChats = unarchiveChats
@ -4546,7 +4578,7 @@ export class TelegramClient extends BaseTelegramClient {
resolvePeer = resolvePeer
setOffline = setOffline
setProfilePhoto = setProfilePhoto
setUsername = setUsername
unblockUser = unblockUser
updateProfile = updateProfile
updateUsername = updateUsername
}

View file

@ -0,0 +1,40 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { isInputPeerChannel, isInputPeerUser, normalizeToInputChannel, normalizeToInputUser } from '../../utils'
/**
* Reorder usernames
*
* @param peerId Bot, channel or "me"/"self"
* @internal
*/
export async function reorderUsernames(this: TelegramClient, peerId: InputPeerLike, order: string[]): Promise<void> {
const peer = await this.resolvePeer(peerId)
if (isInputPeerUser(peer)) {
// either a bot or self
if (peer._ === 'inputPeerSelf' || peer.userId === this._userId) {
// self
await this.call({
_: 'account.reorderUsernames',
order,
})
return
}
// bot
await this.call({
_: 'bots.reorderUsernames',
bot: normalizeToInputUser(peer, peerId),
order,
})
} else if (isInputPeerChannel(peer)) {
await this.call({
_: 'channels.reorderUsernames',
channel: normalizeToInputChannel(peer, peerId),
order,
})
}
}

View file

@ -0,0 +1,62 @@
import { TelegramClient } from '../../client'
import { InputPeerLike } from '../../types'
import { isInputPeerChannel, isInputPeerUser, normalizeToInputChannel, normalizeToInputUser } from '../../utils'
/**
* Toggle a collectible (Fragment) username
*
* > **Note**: non-collectible usernames must still be changed
* > using {@link setUsername}/{@link setChatUsername}
*
* @param peerId Bot, channel or "me"/"self"
* @internal
*/
export async function toggleFragmentUsername(
this: TelegramClient,
peerId: InputPeerLike,
params: {
/**
* Username to toggle
*/
username: string
/**
* Whether to enable or disable the username
*/
active: boolean
},
): Promise<void> {
const { username, active } = params
const peer = await this.resolvePeer(peerId)
if (isInputPeerUser(peer)) {
// either a bot or self
if (peer._ === 'inputPeerSelf' || peer.userId === this._userId) {
// self
await this.call({
_: 'account.toggleUsername',
username,
active,
})
return
}
// bot
await this.call({
_: 'bots.toggleUsername',
bot: normalizeToInputUser(peer, peerId),
username,
active,
})
} else if (isInputPeerChannel(peer)) {
await this.call({
_: 'channels.toggleUsername',
channel: normalizeToInputChannel(peer, peerId),
username,
active,
})
}
}

View file

@ -10,7 +10,7 @@ import { User } from '../../types'
* @param username New username (5-32 chars, allowed chars: `a-zA-Z0-9_`), or `null` to remove
* @internal
*/
export async function updateUsername(this: TelegramClient, username: string | null): Promise<User> {
export async function setUsername(this: TelegramClient, username: string | null): Promise<User> {
if (username === null) username = ''
const res = await this.call({

View file

@ -223,7 +223,21 @@ export class Chat {
* Username, for private chats, bots, supergroups and channels if available
*/
get username(): string | null {
return 'username' in this.peer ? this.peer.username ?? null : null
if (!('username' in this.peer)) return null
return this.peer.username ?? this.peer.usernames?.[0].username ?? null
}
/**
* Usernames (inclufing collectibles), for private chats, bots, supergroups and channels if available
*/
get usernames(): ReadonlyArray<tl.RawUsername> | null {
if (!('usernames' in this.peer)) return null
return (
this.peer.usernames ??
(this.peer.username ? [{ _: 'username', username: this.peer.username, active: true }] : null)
)
}
/**

View file

@ -197,7 +197,15 @@ export class User {
/** User's or bot's username */
get username(): string | null {
return this.raw.username ?? null
return this.raw.username ?? this.raw.usernames?.[0].username ?? null
}
/** User's or bot's usernames (including collectibles) */
get usernames(): ReadonlyArray<tl.RawUsername> | null {
return (
this.raw.usernames ??
(this.raw.username ? [{ _: 'username', username: this.raw.username, active: true }] : null)
)
}
/** IETF language tag of the user's language */