feat(client): deleteUserHistory method, also properly handle messages.affectedHistory
i suppose? this is an incredibly bad hack but i guess it works so who cares?
This commit is contained in:
parent
c678a0ef6a
commit
f3e4a34eab
7 changed files with 125 additions and 17 deletions
|
@ -26,6 +26,7 @@ import { deleteChannel } from './methods/chats/delete-channel'
|
||||||
import { deleteChatPhoto } from './methods/chats/delete-chat-photo'
|
import { deleteChatPhoto } from './methods/chats/delete-chat-photo'
|
||||||
import { deleteGroup } from './methods/chats/delete-group'
|
import { deleteGroup } from './methods/chats/delete-group'
|
||||||
import { deleteHistory } from './methods/chats/delete-history'
|
import { deleteHistory } from './methods/chats/delete-history'
|
||||||
|
import { deleteUserHistory } from './methods/chats/delete-user-history'
|
||||||
import { getChatMember } from './methods/chats/get-chat-member'
|
import { getChatMember } from './methods/chats/get-chat-member'
|
||||||
import { getChatMembers } from './methods/chats/get-chat-members'
|
import { getChatMembers } from './methods/chats/get-chat-members'
|
||||||
import { getChatPreview } from './methods/chats/get-chat-preview'
|
import { getChatPreview } from './methods/chats/get-chat-preview'
|
||||||
|
@ -662,6 +663,16 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
mode?: 'delete' | 'clear' | 'revoke',
|
mode?: 'delete' | 'clear' | 'revoke',
|
||||||
maxId?: number
|
maxId?: number
|
||||||
): Promise<void>
|
): Promise<void>
|
||||||
|
/**
|
||||||
|
* Delete all messages of a user in a supergroup
|
||||||
|
*
|
||||||
|
* @param chatId Chat ID
|
||||||
|
* @param userId User ID
|
||||||
|
*/
|
||||||
|
deleteUserHistory(
|
||||||
|
chatId: InputPeerLike,
|
||||||
|
userId: InputPeerLike
|
||||||
|
): Promise<void>
|
||||||
/**
|
/**
|
||||||
* Get information about a single chat member
|
* Get information about a single chat member
|
||||||
*
|
*
|
||||||
|
@ -1271,7 +1282,7 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
* > **Note**: each administrator has their own primary invite link,
|
* > **Note**: each administrator has their own primary invite link,
|
||||||
* > and bots by default don't have one.
|
* > and bots by default don't have one.
|
||||||
*
|
*
|
||||||
* @param chatId Chat ID
|
* @param chatId Chat IDs
|
||||||
*/
|
*/
|
||||||
exportInviteLink(chatId: InputPeerLike): Promise<ChatInviteLink>
|
exportInviteLink(chatId: InputPeerLike): Promise<ChatInviteLink>
|
||||||
/**
|
/**
|
||||||
|
@ -1369,7 +1380,7 @@ export interface TelegramClient extends BaseTelegramClient {
|
||||||
chatId: InputPeerLike,
|
chatId: InputPeerLike,
|
||||||
ids: MaybeArray<number>,
|
ids: MaybeArray<number>,
|
||||||
revoke?: boolean
|
revoke?: boolean
|
||||||
): Promise<boolean>
|
): Promise<void>
|
||||||
/**
|
/**
|
||||||
* Edit sent inline message text, media and reply markup.
|
* Edit sent inline message text, media and reply markup.
|
||||||
*
|
*
|
||||||
|
@ -2670,6 +2681,7 @@ export class TelegramClient extends BaseTelegramClient {
|
||||||
deleteChatPhoto = deleteChatPhoto
|
deleteChatPhoto = deleteChatPhoto
|
||||||
deleteGroup = deleteGroup
|
deleteGroup = deleteGroup
|
||||||
deleteHistory = deleteHistory
|
deleteHistory = deleteHistory
|
||||||
|
deleteUserHistory = deleteUserHistory
|
||||||
getChatMember = getChatMember
|
getChatMember = getChatMember
|
||||||
getChatMembers = getChatMembers
|
getChatMembers = getChatMembers
|
||||||
getChatPreview = getChatPreview
|
getChatPreview = getChatPreview
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
import { TelegramClient } from '../../client'
|
import { TelegramClient } from '../../client'
|
||||||
import { InputPeerLike } from '../../types'
|
import { InputPeerLike } from '../../types'
|
||||||
import { normalizeToInputPeer } from '../../utils/peer-utils'
|
import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils'
|
||||||
|
import { createDummyUpdate } from '../../utils/updates-utils'
|
||||||
|
import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete communication history (for private chats
|
* Delete communication history (for private chats
|
||||||
|
@ -23,11 +25,20 @@ export async function deleteHistory(
|
||||||
mode: 'delete' | 'clear' | 'revoke' = 'delete',
|
mode: 'delete' | 'clear' | 'revoke' = 'delete',
|
||||||
maxId = 0
|
maxId = 0
|
||||||
): Promise<void> {
|
): Promise<void> {
|
||||||
await this.call({
|
const peer = normalizeToInputPeer(await this.resolvePeer(chat))
|
||||||
|
|
||||||
|
const res = await this.call({
|
||||||
_: 'messages.deleteHistory',
|
_: 'messages.deleteHistory',
|
||||||
justClear: mode === 'clear',
|
justClear: mode === 'clear',
|
||||||
revoke: mode === 'revoke',
|
revoke: mode === 'revoke',
|
||||||
peer: normalizeToInputPeer(await this.resolvePeer(chat)),
|
peer,
|
||||||
maxId
|
maxId
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const channel = normalizeToInputChannel(peer)
|
||||||
|
if (channel) {
|
||||||
|
this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount, (channel as tl.RawInputChannel).channelId))
|
||||||
|
} else {
|
||||||
|
this._handleUpdate(createDummyUpdate(res.pts, res.ptsCount))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
41
packages/client/src/methods/chats/delete-user-history.ts
Normal file
41
packages/client/src/methods/chats/delete-user-history.ts
Normal file
|
@ -0,0 +1,41 @@
|
||||||
|
import { TelegramClient } from '../../client'
|
||||||
|
import { InputPeerLike, MtCuteInvalidPeerTypeError } from '../../types'
|
||||||
|
import {
|
||||||
|
normalizeToInputChannel,
|
||||||
|
normalizeToInputUser,
|
||||||
|
} from '../../utils/peer-utils'
|
||||||
|
import { tl } from '@mtcute/tl'
|
||||||
|
import { createDummyUpdate } from '../../utils/updates-utils'
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Delete all messages of a user in a supergroup
|
||||||
|
*
|
||||||
|
* @param chatId Chat ID
|
||||||
|
* @param userId User ID
|
||||||
|
* @internal
|
||||||
|
*/
|
||||||
|
export async function deleteUserHistory(
|
||||||
|
this: TelegramClient,
|
||||||
|
chatId: InputPeerLike,
|
||||||
|
userId: InputPeerLike
|
||||||
|
): Promise<void> {
|
||||||
|
const channel = normalizeToInputChannel(await this.resolvePeer(chatId))
|
||||||
|
if (!channel) throw new MtCuteInvalidPeerTypeError(chatId, 'channel')
|
||||||
|
|
||||||
|
const user = normalizeToInputUser(await this.resolvePeer(userId))
|
||||||
|
if (!user) throw new MtCuteInvalidPeerTypeError(userId, 'user')
|
||||||
|
|
||||||
|
const res = await this.call({
|
||||||
|
_: 'channels.deleteUserHistory',
|
||||||
|
channel,
|
||||||
|
userId: user,
|
||||||
|
})
|
||||||
|
|
||||||
|
this._handleUpdate(
|
||||||
|
createDummyUpdate(
|
||||||
|
res.pts,
|
||||||
|
res.ptsCount,
|
||||||
|
(channel as tl.RawInputChannel).channelId
|
||||||
|
)
|
||||||
|
)
|
||||||
|
}
|
|
@ -2,6 +2,8 @@ import { TelegramClient } from '../../client'
|
||||||
import { InputPeerLike } from '../../types'
|
import { InputPeerLike } from '../../types'
|
||||||
import { MaybeArray } from '@mtcute/core'
|
import { MaybeArray } from '@mtcute/core'
|
||||||
import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils'
|
import { normalizeToInputChannel, normalizeToInputPeer } from '../../utils/peer-utils'
|
||||||
|
import { createDummyUpdate } from '../../utils/updates-utils'
|
||||||
|
import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Delete messages, including service messages.
|
* Delete messages, including service messages.
|
||||||
|
@ -16,28 +18,29 @@ export async function deleteMessages(
|
||||||
chatId: InputPeerLike,
|
chatId: InputPeerLike,
|
||||||
ids: MaybeArray<number>,
|
ids: MaybeArray<number>,
|
||||||
revoke = true
|
revoke = true
|
||||||
): Promise<boolean> {
|
): Promise<void> {
|
||||||
if (!Array.isArray(ids)) ids = [ids]
|
if (!Array.isArray(ids)) ids = [ids]
|
||||||
|
|
||||||
const peer = await this.resolvePeer(chatId)
|
const peer = await this.resolvePeer(chatId)
|
||||||
const inputPeer = normalizeToInputPeer(peer)
|
const inputPeer = normalizeToInputPeer(peer)
|
||||||
|
|
||||||
let res
|
let upd
|
||||||
if (inputPeer._ === 'inputPeerChannel') {
|
if (inputPeer._ === 'inputPeerChannel') {
|
||||||
res = await this.call({
|
const channel = normalizeToInputChannel(peer)!
|
||||||
|
const res = await this.call({
|
||||||
_: 'channels.deleteMessages',
|
_: 'channels.deleteMessages',
|
||||||
channel: normalizeToInputChannel(peer)!,
|
channel,
|
||||||
id: ids
|
id: ids
|
||||||
})
|
})
|
||||||
|
upd = createDummyUpdate(res.pts, res.ptsCount, (channel as tl.RawInputChannel).channelId)
|
||||||
} else {
|
} else {
|
||||||
res = await this.call({
|
const res = await this.call({
|
||||||
_: 'messages.deleteMessages',
|
_: 'messages.deleteMessages',
|
||||||
id: ids,
|
id: ids,
|
||||||
revoke
|
revoke
|
||||||
})
|
})
|
||||||
|
upd = createDummyUpdate(res.pts, res.ptsCount)
|
||||||
}
|
}
|
||||||
|
|
||||||
this._pts = res.pts
|
this._handleUpdate(upd)
|
||||||
|
|
||||||
return !!res.ptsCount
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -10,6 +10,7 @@ import { extractChannelIdFromUpdate } from '../utils/misc-utils'
|
||||||
import { Lock } from '../utils/lock'
|
import { Lock } from '../utils/lock'
|
||||||
import bigInt from 'big-integer'
|
import bigInt from 'big-integer'
|
||||||
import { MAX_CHANNEL_ID } from '@mtcute/core'
|
import { MAX_CHANNEL_ID } from '@mtcute/core'
|
||||||
|
import { isDummyUpdate, isDummyUpdates } from '../utils/updates-utils'
|
||||||
|
|
||||||
const debug = require('debug')('mtcute:upds')
|
const debug = require('debug')('mtcute:upds')
|
||||||
|
|
||||||
|
@ -488,7 +489,7 @@ export function _handleUpdate(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (!noDispatch) {
|
if (!isDummyUpdate(upd) && !noDispatch) {
|
||||||
this.dispatchUpdate(upd, users, chats)
|
this.dispatchUpdate(upd, users, chats)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -502,8 +503,10 @@ export function _handleUpdate(
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// this._seq = update.seq
|
if (!isDummyUpdates(update)) {
|
||||||
this._date = update.date
|
// this._seq = update.seq
|
||||||
|
this._date = update.date
|
||||||
|
}
|
||||||
} else if (update._ === 'updateShort') {
|
} else if (update._ === 'updateShort') {
|
||||||
const upd = update.update
|
const upd = update.update
|
||||||
if (upd._ === 'updateDcOptions' && this._config) {
|
if (upd._ === 'updateDcOptions' && this._config) {
|
||||||
|
|
|
@ -25,7 +25,7 @@ export function extractChannelIdFromUpdate(
|
||||||
upd: tl.TypeUpdate
|
upd: tl.TypeUpdate
|
||||||
): number | undefined {
|
): number | undefined {
|
||||||
// holy shit
|
// holy shit
|
||||||
return 'channelId' in upd
|
const res = 'channelId' in upd
|
||||||
? upd.channelId
|
? upd.channelId
|
||||||
: 'message' in upd &&
|
: 'message' in upd &&
|
||||||
typeof upd.message !== 'string' &&
|
typeof upd.message !== 'string' &&
|
||||||
|
@ -34,6 +34,8 @@ export function extractChannelIdFromUpdate(
|
||||||
'channelId' in upd.message.peerId
|
'channelId' in upd.message.peerId
|
||||||
? upd.message.peerId.channelId
|
? upd.message.peerId.channelId
|
||||||
: undefined
|
: undefined
|
||||||
|
if (res === 0) return undefined
|
||||||
|
return res
|
||||||
}
|
}
|
||||||
|
|
||||||
export function normalizeDate(
|
export function normalizeDate(
|
||||||
|
|
36
packages/client/src/utils/updates-utils.ts
Normal file
36
packages/client/src/utils/updates-utils.ts
Normal file
|
@ -0,0 +1,36 @@
|
||||||
|
import { tl } from '@mtcute/tl'
|
||||||
|
|
||||||
|
// dummy updates which are used for methods that return messages.affectedHistory.
|
||||||
|
// that is not an update, but it carries info about pts, and we need to handle it
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
export function createDummyUpdate(pts: number, ptsCount: number, channelId = 0): tl.TypeUpdates {
|
||||||
|
return {
|
||||||
|
_: 'updates',
|
||||||
|
seq: 0,
|
||||||
|
date: 0,
|
||||||
|
chats: [],
|
||||||
|
users: [],
|
||||||
|
updates: [
|
||||||
|
{
|
||||||
|
_: 'updatePinnedChannelMessages',
|
||||||
|
channelId,
|
||||||
|
pts,
|
||||||
|
ptsCount,
|
||||||
|
// since message id cant be negative, using negative 42
|
||||||
|
// here makes it distinctive from real updates
|
||||||
|
messages: [-42]
|
||||||
|
}
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
export function isDummyUpdate(upd: tl.TypeUpdate): boolean {
|
||||||
|
return upd._ === 'updatePinnedChannelMessages' && upd.messages.length === 1 && upd.messages[0] === -42
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @internal */
|
||||||
|
export function isDummyUpdates(upd: tl.TypeUpdates): boolean {
|
||||||
|
return upd._ === 'updates' && upd.updates.length === 1 && isDummyUpdate(upd.updates[0])
|
||||||
|
}
|
Loading…
Reference in a new issue