11# Сервис для работы с Telegram каналами
22module Telegram
33 class ChannelService
4- def initialize ( bot )
4+ def initialize ( bot = Telegram . bot )
55 @bot = bot
66 end
77
@@ -97,38 +97,28 @@ def get_channel_info(username)
9797 end
9898 end
9999
100- # Добавляет канал в подписки пользователя
101- # @param user [TelegramUser] - пользователь
102- # @param channel_username [String] - username канала
103- # @return [Hash] - результат операции { success: true/false, message: String, channel: Channel }
104- def add_channel_for_user ( user , channel_username )
100+ # Добавляет или обновляет канал в базе данных по username
101+ # @param channel_username [String] - username канала (может быть в разных форматах)
102+ # @return [Hash] - результат операции { success: true/false, channel: Channel, message: String }
103+ def add_channel_to_database ( channel_username )
105104 # Парсим username
106105 username = parse_channel_username ( channel_username )
107106
108107 unless username
109108 return {
110109 success : false ,
110+ channel : nil ,
111111 message : I18n . t ( 'telegram_bot.channels.add.invalid_format' )
112112 }
113113 end
114114
115- # Проверяем лимит каналов для бесплатных пользователей
116- limit_checker = Limits ::LimitChecker . new ( user )
117- unless limit_checker . can_add_channel?
118- return {
119- success : false ,
120- message : I18n . t ( 'telegram_bot.channels.add.limit_reached' ,
121- limit : ApplicationConfig . free_channels_limit ,
122- current : limit_checker . current_channels_count )
123- }
124- end
125-
126115 # Получаем информацию о канале
127116 channel_info = get_channel_info ( username )
128117
129118 unless channel_info
130119 return {
131120 success : false ,
121+ channel : nil ,
132122 message : I18n . t ( 'telegram_bot.channels.add.not_found' , channel : "@#{ username } " )
133123 }
134124 end
@@ -147,6 +137,7 @@ def add_channel_for_user(user, channel_username)
147137 unless channel . save
148138 return {
149139 success : false ,
140+ channel : nil ,
150141 message : I18n . t ( 'telegram_bot.channels.add.error' , error : channel . errors . full_messages . join ( ', ' ) )
151142 }
152143 end
@@ -159,6 +150,70 @@ def add_channel_for_user(user, channel_username)
159150 )
160151 end
161152
153+ {
154+ success : true ,
155+ channel : channel ,
156+ message : nil
157+ }
158+ rescue StandardError => e
159+ Bugsnag . notify ( e ) { |b | b . metadata = { channel_username : channel_username , action : 'add_channel_to_database' } }
160+
161+ ErrorNotificationService . notify_service_error ( e ,
162+ service : self . class ,
163+ method : 'add_channel_to_database' ,
164+ metadata : {
165+ channel_username : channel_username ,
166+ error_class : e . class . name ,
167+ error_message : e . message
168+ }
169+ )
170+ Rails . logger . error "Error adding channel to database: #{ e . message } "
171+
172+ {
173+ success : false ,
174+ channel : nil ,
175+ message : I18n . t ( 'telegram_bot.channels.add.error' , error : e . message )
176+ }
177+ end
178+
179+ # Добавляет канал в подписки пользователя
180+ # @param user [TelegramUser] - пользователь
181+ # @param channel_username [String] - username канала
182+ # @return [Hash] - результат операции { success: true/false, message: String, channel: Channel }
183+ def add_channel_for_user ( user , channel_username )
184+ # Парсим username
185+ username = parse_channel_username ( channel_username )
186+
187+ unless username
188+ return {
189+ success : false ,
190+ message : I18n . t ( 'telegram_bot.channels.add.invalid_format' )
191+ }
192+ end
193+
194+ # Проверяем лимит каналов для бесплатных пользователей
195+ limit_checker = Limits ::LimitChecker . new ( user )
196+ unless limit_checker . can_add_channel?
197+ return {
198+ success : false ,
199+ message : I18n . t ( 'telegram_bot.channels.add.limit_reached' ,
200+ limit : ApplicationConfig . free_channels_limit ,
201+ current : limit_checker . current_channels_count )
202+ }
203+ end
204+
205+ # Добавляем или обновляем канал в базе данных
206+ channel_result = add_channel_to_database ( channel_username )
207+
208+ unless channel_result [ :success ]
209+ return {
210+ success : false ,
211+ message : channel_result [ :message ]
212+ }
213+ end
214+
215+ channel = channel_result [ :channel ]
216+
162217 # Проверяем, не подписан ли уже пользователь
163218 subscription = user . subscriptions . find_by ( channel : channel )
164219
0 commit comments