diff --git a/packages/client/src/client.ts b/packages/client/src/client.ts index c66152bc..06495571 100644 --- a/packages/client/src/client.ts +++ b/packages/client/src/client.ts @@ -74,6 +74,8 @@ 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 { toggleJoinRequests } from './methods/chats/toggle-join-requests' +import { toggleJoinToSend } from './methods/chats/toggle-join-to-send' import { unarchiveChats } from './methods/chats/unarchive-chats' import { unbanChatMember } from './methods/chats/unban-chat-member' import { addContact } from './methods/contacts/add-contact' @@ -1477,6 +1479,26 @@ 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 + /** + * Set whether a channel/supergroup has join requests enabled. + * + * > **Note**: this method only affects primary invite links. + * > Additional invite links may exist with the opposite setting. + * + * @param chatId Chat ID or username + * @param enabled (default: `false`) Whether join requests should be enabled + */ + toggleJoinRequests(chatId: InputPeerLike, enabled?: boolean): Promise + /** + * Set whether a channel/supergroup has join-to-send setting enabled. + * + * This only affects discussion groups where users can send messages + * without joining the group. + * + * @param chatId Chat ID or username + * @param enabled (default: `false`) Whether join-to-send setting should be enabled + */ + toggleJoinToSend(chatId: InputPeerLike, enabled?: boolean): Promise /** * Unarchive one or more chats * @@ -4145,6 +4167,8 @@ export class TelegramClient extends BaseTelegramClient { setChatTitle = setChatTitle setChatUsername = setChatUsername setSlowMode = setSlowMode + toggleJoinRequests = toggleJoinRequests + toggleJoinToSend = toggleJoinToSend unarchiveChats = unarchiveChats unbanChatMember = unbanChatMember unrestrictChatMember = unbanChatMember diff --git a/packages/client/src/methods/chats/toggle-join-requests.ts b/packages/client/src/methods/chats/toggle-join-requests.ts new file mode 100644 index 00000000..bf8bfdd9 --- /dev/null +++ b/packages/client/src/methods/chats/toggle-join-requests.ts @@ -0,0 +1,22 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' +import { normalizeToInputChannel } from '../../utils/peer-utils' + +/** + * Set whether a channel/supergroup has join requests enabled. + * + * > **Note**: this method only affects primary invite links. + * > Additional invite links may exist with the opposite setting. + * + * @param chatId Chat ID or username + * @param enabled Whether join requests should be enabled + * @internal + */ +export async function toggleJoinRequests(this: TelegramClient, chatId: InputPeerLike, enabled = false): Promise { + const res = await this.call({ + _: 'channels.toggleJoinRequest', + channel: normalizeToInputChannel(await this.resolvePeer(chatId), chatId), + enabled, + }) + this._handleUpdate(res) +} diff --git a/packages/client/src/methods/chats/toggle-join-to-send.ts b/packages/client/src/methods/chats/toggle-join-to-send.ts new file mode 100644 index 00000000..f5f32dd4 --- /dev/null +++ b/packages/client/src/methods/chats/toggle-join-to-send.ts @@ -0,0 +1,22 @@ +import { TelegramClient } from '../../client' +import { InputPeerLike } from '../../types' +import { normalizeToInputChannel } from '../../utils/peer-utils' + +/** + * Set whether a channel/supergroup has join-to-send setting enabled. + * + * This only affects discussion groups where users can send messages + * without joining the group. + * + * @param chatId Chat ID or username + * @param enabled Whether join-to-send setting should be enabled + * @internal + */ +export async function toggleJoinToSend(this: TelegramClient, chatId: InputPeerLike, enabled = false): Promise { + const res = await this.call({ + _: 'channels.toggleJoinToSend', + channel: normalizeToInputChannel(await this.resolvePeer(chatId), chatId), + enabled, + }) + this._handleUpdate(res) +} diff --git a/packages/client/src/types/peers/chat-event/actions.ts b/packages/client/src/types/peers/chat-event/actions.ts index b2fa9006..2944121c 100644 --- a/packages/client/src/types/peers/chat-event/actions.ts +++ b/packages/client/src/types/peers/chat-event/actions.ts @@ -7,6 +7,7 @@ import { ChatInviteLink } from '../chat-invite-link' import { ChatLocation } from '../chat-location' import { ChatMember } from '../chat-member' import { ChatPermissions } from '../chat-permissions' +import { User } from '../user' /** A user has joined the group (in the case of big groups, info of the user that has joined isn't shown) */ export interface ChatActionUserJoined { @@ -21,6 +22,17 @@ export interface ChatActionUserJoinedInvite { link: ChatInviteLink } +/** A user has joined the group using an invite link and was approved by an admin */ +export interface ChatActionUserJoinedApproved { + type: 'user_joined_approved' + + /** Invite link user to join */ + link: ChatInviteLink + + /** User who approved the join */ + approvedBy: User +} + /** A user has left the group (in the case of big groups, info of the user that has joined isn't shown) */ export interface ChatActionUserLeft { type: 'user_left' @@ -315,6 +327,7 @@ export type ChatAction = | ChatActionInviteLinkDeleted | ChatActionInviteLinkEdited | ChatActionInviteLinkRevoked + | ChatActionUserJoinedApproved | ChatActionTtlChanged | null @@ -324,8 +337,7 @@ export function _actionFromTl( client: TelegramClient, peers: PeersIndex, ): ChatAction { - // todo - MTQ-78 - // channelAdminLogEventActionParticipantJoinByRequest#afb6144a invite:ExportedChatInvite approved_by:long + // todo - MTQ-84 // channelAdminLogEventActionToggleNoForwards#cb2ac766 new_value:Bool = ChannelAdminLogEventAction; // todo - MTQ-57 // channelAdminLogEventActionChangeUsernames#f04fb3a9 prev_value:Vector new_value:Vector @@ -502,6 +514,12 @@ export function _actionFromTl( old: e.prevValue, new: e.newValue, } + case 'channelAdminLogEventActionParticipantJoinByRequest': + return { + type: 'user_joined_approved', + link: new ChatInviteLink(client, e.invite, peers), + approvedBy: new User(client, peers.user(e.approvedBy)), + } default: return null } diff --git a/packages/client/src/types/peers/chat-event/filters.ts b/packages/client/src/types/peers/chat-event/filters.ts index 936194b3..842ad2a2 100644 --- a/packages/client/src/types/peers/chat-event/filters.ts +++ b/packages/client/src/types/peers/chat-event/filters.ts @@ -32,6 +32,8 @@ export function normalizeChatEventFilters(input: InputChatEventFilters): ChatEve switch (type) { case 'user_joined': + case 'user_joined_invite': + case 'user_joined_approved': serverFilter.join = true break case 'user_left': @@ -47,6 +49,8 @@ export function normalizeChatEventFilters(input: InputChatEventFilters): ChatEve case 'photo_changed': case 'username_changed': case 'stickerset_changed': + case 'slow_mode_changed': + case 'ttl_changed': serverFilter.info = true break case 'invites_toggled': @@ -75,12 +79,6 @@ export function normalizeChatEventFilters(input: InputChatEventFilters): ChatEve serverFilter.promote = true serverFilter.demote = true break - case 'slow_mode_changed': - case 'ttl_changed': - // not documented so idk, enable both - serverFilter.settings = true - serverFilter.info = true - break case 'call_started': case 'call_ended': serverFilter.groupCall = true @@ -91,12 +89,6 @@ export function normalizeChatEventFilters(input: InputChatEventFilters): ChatEve serverFilter.settings = true serverFilter.info = true break - case 'user_joined_invite': - // not documented so idk, enable all - serverFilter.join = true - serverFilter.invite = true - serverFilter.invites = true - break case 'invite_deleted': case 'invite_edited': case 'invite_revoked': diff --git a/packages/client/src/types/peers/chat.ts b/packages/client/src/types/peers/chat.ts index c8afbca7..3cefd20e 100644 --- a/packages/client/src/types/peers/chat.ts +++ b/packages/client/src/types/peers/chat.ts @@ -202,6 +202,16 @@ export class Chat { return this.peer._ === 'user' && this.peer.contact! } + /** Whether this group is a channel/supergroup with join requests enabled */ + get hasJoinRequests(): boolean { + return this.peer._ === 'channel' && this.peer.joinRequest! + } + + /** Whether this group is a supergroup with join-to-send rule enabled */ + get hasJoinToSend(): boolean { + return this.peer._ === 'channel' && this.peer.joinToSend! + } + /** * Title, for supergroups, channels and groups */