From eea1570ff1bb7e24d62da121305f984079a8ea7c Mon Sep 17 00:00:00 2001 From: nevrfl Date: Mon, 2 Sep 2024 14:02:23 +0300 Subject: [PATCH] update tdlib methods --- Gemfile | 4 +- lib/redmine_bots/telegram/tdlib/add_bot.rb | 4 +- lib/redmine_bots/telegram/tdlib/close_chat.rb | 8 +-- .../telegram/tdlib/create_chat.rb | 41 +++++++++++---- .../telegram/tdlib/fetch_all_chats.rb | 23 +++++---- .../telegram/tdlib/get_chat_link.rb | 2 +- .../telegram/tdlib/toggle_chat_admin.rb | 50 ++++++++++++------- 7 files changed, 83 insertions(+), 49 deletions(-) diff --git a/Gemfile b/Gemfile index 956e97c..d3c228f 100644 --- a/Gemfile +++ b/Gemfile @@ -5,8 +5,8 @@ gem 'sidekiq-rate-limiter', '0.1.3', require: 'sidekiq-rate-limiter/server' gem 'telegram-bot-ruby', '>= 0.11', '< 1.0' gem 'slack-ruby-bot' gem 'celluloid-io' -gem 'tdlib-ruby', '3.0.2' -gem 'tdlib-schema', git: 'https://github.com/southbridgeio/tdlib-schema', branch: 'update' +gem 'tdlib-ruby', git: 'https://github.com/southbridgeio/tdlib-ruby', tag: "v3.0.3" +gem 'tdlib-schema', git: 'https://github.com/southbridgeio/tdlib-schema' gem 'jwt' gem 'filelock' gem 'patron' diff --git a/lib/redmine_bots/telegram/tdlib/add_bot.rb b/lib/redmine_bots/telegram/tdlib/add_bot.rb index 93db870..14d139c 100644 --- a/lib/redmine_bots/telegram/tdlib/add_bot.rb +++ b/lib/redmine_bots/telegram/tdlib/add_bot.rb @@ -3,11 +3,11 @@ class AddBot < Command def call(bot_name) client.search_public_chat(username: bot_name).then do |chat| message = TD::Types::InputMessageContent::Text.new(text: TD::Types::FormattedText.new(text: '/start', entities: []), - disable_web_page_preview: true, + link_preview_options: nil, clear_draft: false) client.send_message(chat_id: chat.id, message_thread_id: nil, - reply_to_message_id: nil, + reply_to: nil, options: nil, reply_markup: nil, input_message_content: message) diff --git a/lib/redmine_bots/telegram/tdlib/close_chat.rb b/lib/redmine_bots/telegram/tdlib/close_chat.rb index 76f657e..3315bde 100644 --- a/lib/redmine_bots/telegram/tdlib/close_chat.rb +++ b/lib/redmine_bots/telegram/tdlib/close_chat.rb @@ -6,7 +6,7 @@ def call(chat_id) when TD::Types::ChatType::BasicGroup fetch_robot_ids.then { |*robot_ids| close_basic_group(chat, robot_ids) }.flat when TD::Types::ChatType::Supergroup - close_super_group(chat.type) + close_super_group(chat.id) else raise 'Unsupported chat type' end @@ -39,12 +39,12 @@ def close_basic_group(chat, robot_ids) end.flat end - def close_super_group(chat_type) - client.delete_supergroup(supergroup_id: chat_type.supergroup_id) + def close_super_group(chat_id) + client.delete_chat(chat_id: chat_id) end def delete_member(chat_id, user_id) - client.set_chat_member_status(chat_id: chat_id, user_id: user_id, status: ChatMemberStatus::Left.new) + client.set_chat_member_status(chat_id: chat_id, member_id: user_id, status: ChatMemberStatus::Left.new) end end end diff --git a/lib/redmine_bots/telegram/tdlib/create_chat.rb b/lib/redmine_bots/telegram/tdlib/create_chat.rb index 41791d1..18e2517 100644 --- a/lib/redmine_bots/telegram/tdlib/create_chat.rb +++ b/lib/redmine_bots/telegram/tdlib/create_chat.rb @@ -1,10 +1,8 @@ module RedmineBots::Telegram::Tdlib class CreateChat < Command def call(title, user_ids) - puts title - puts user_ids Promises.zip(*user_ids.map { |id| client.get_user(user_id: id) }).then do - client.create_new_supergroup_chat(title: title, is_channel: false, description: '', location: nil).then do |chat| + client.create_new_supergroup_chat(**supergroup_chat_params(title)).then do |chat| client.add_chat_members(chat_id: chat.id, user_ids: user_ids).then do client.set_chat_permissions(chat_id: chat.id, permissions: permissions).then { chat } end.flat @@ -15,14 +13,35 @@ def call(title, user_ids) private def permissions - ChatPermissions.new(can_send_messages: true, - can_send_media_messages: true, - can_send_polls: true, - can_send_other_messages: true, - can_add_web_page_previews: true, - can_change_info: false, - can_invite_users: false, - can_pin_messages: false) + ChatPermissions.new( + can_send_basic_messages: true, + can_send_audios: true, + can_send_documents: true, + can_send_photos: true, + can_send_videos: true, + can_send_video_notes: true, + can_send_voice_notes: true, + can_send_polls: true, + can_send_other_messages: true, + can_add_link_previews: true, + can_send_media_messages: true, + can_change_info: false, + can_invite_users: false, + can_pin_messages: false, + can_create_topics: false + ) + end + + def supergroup_chat_params(title) + { + title: title, + is_channel: false, + description: '', + location: nil, + is_forum: false, + message_auto_delete_time: 0, + for_import: true + } end end end diff --git a/lib/redmine_bots/telegram/tdlib/fetch_all_chats.rb b/lib/redmine_bots/telegram/tdlib/fetch_all_chats.rb index d4abc6f..8942a68 100644 --- a/lib/redmine_bots/telegram/tdlib/fetch_all_chats.rb +++ b/lib/redmine_bots/telegram/tdlib/fetch_all_chats.rb @@ -4,8 +4,6 @@ def initialize(*) super @chat_list = ChatList::Main.new - @offset_order = 2**63 - 1 - @offset_chat_id = 0 @limit = 100 @chat_futures = [] end @@ -17,18 +15,21 @@ def call private attr_reader :limit - attr_accessor :chat_list, :offset_order, :offset_chat_id + attr_accessor :chat_list def fetch - client.get_chats(chat_list: chat_list, offset_order: offset_order, offset_chat_id: offset_chat_id, limit: limit).then do |update| - chat_ids = update.chat_ids - next Concurrent::Promises.fulfilled_future(nil) if chat_ids.empty? - - client.get_chat(chat_id: chat_ids.last).then do |chat| - self.offset_chat_id, self.offset_order = chat.id, chat.positions.find { |p| p.list.is_a?(ChatList::Main) }.order + client.load_chats(chat_list: chat_list, limit: limit).then do |update| + case update + when TD::Types::Ok fetch.wait! - end.flat - end.flat + else + next Concurrent::Promises.fulfilled_future(nil) + end + end.flat.rescue do |error| + if error.code != 404 + raise error + end + end end end end diff --git a/lib/redmine_bots/telegram/tdlib/get_chat_link.rb b/lib/redmine_bots/telegram/tdlib/get_chat_link.rb index 4dc4685..931224d 100644 --- a/lib/redmine_bots/telegram/tdlib/get_chat_link.rb +++ b/lib/redmine_bots/telegram/tdlib/get_chat_link.rb @@ -1,7 +1,7 @@ module RedmineBots::Telegram::Tdlib class GetChatLink < Command def call(chat_id) - client.generate_chat_invite_link(chat_id: chat_id) + client.create_chat_invite_link(chat_id: chat_id, expiration_date: 0, member_limit: 0, creates_join_request: false, name: "issue") end end end diff --git a/lib/redmine_bots/telegram/tdlib/toggle_chat_admin.rb b/lib/redmine_bots/telegram/tdlib/toggle_chat_admin.rb index e77bf6c..d1f2826 100644 --- a/lib/redmine_bots/telegram/tdlib/toggle_chat_admin.rb +++ b/lib/redmine_bots/telegram/tdlib/toggle_chat_admin.rb @@ -2,24 +2,38 @@ module RedmineBots::Telegram::Tdlib class ToggleChatAdmin < Command def call(chat_id, user_id, admin = true) status = - if admin - TD::Types::ChatMemberStatus::Administrator.new( - is_anonymous: false, - can_post_messages: true, - can_be_edited: true, - can_change_info: true, - can_edit_messages: true, - can_delete_messages: true, - can_invite_users: true, - can_restrict_members: true, - can_pin_messages: true, - can_promote_members: true, - custom_title: 'Redmine admin' - ) - else - TD::Types::ChatMemberStatus::Member.new - end - client.get_user(user_id: user_id).then { client.set_chat_member_status(chat_id: chat_id, user_id: user_id, status: status) }.flat + if admin + TD::Types::ChatMemberStatus::Administrator.new( + rights: rights, + can_be_edited: true, + custom_title: 'Redmine admin' + ) + else + TD::Types::ChatMemberStatus::Member.new + end + client.get_user(user_id: user_id).then { client.set_chat_member_status(chat_id: chat_id, member_id: user_id, status: status) }.flat + end + + private + + def rights + TD::Types::ChatAdministratorRights.new( + can_manage_topics: true, + can_manage_chat: true, + can_change_info: true, + can_post_messages: true, + can_edit_messages: true, + can_delete_messages: true, + can_invite_users: true, + can_restrict_members: true, + can_pin_messages: true, + can_promote_members: true, + can_manage_video_chats: true, + can_post_stories: false, + can_edit_stories: false, + can_delete_stories: false, + is_anonymous: false + ) end end end