Skip to content

Commit

Permalink
Add TelegramGroup model, update old logic for use it.
Browse files Browse the repository at this point in the history
  • Loading branch information
arturtr committed Apr 5, 2016
1 parent 48c4df7 commit ffba883
Show file tree
Hide file tree
Showing 8 changed files with 74 additions and 28 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,4 +53,4 @@ bundle exec rake chat_telegram:bot PID_DIR='/pid/dir'

### Usage

Open the ticket. You'll see the new link `Create Telegram chat` on the right side of the ticket. Click it and the Telegram group chat associated with this ticket will be created. The link will change to `Enter Telegram chat`. Click on in to join the chat in your Telegram client. You'll be able to copy and pass the link to anyone you want to invite them to the Group Chat.
Open the ticket. You'll see the new link `Create Telegram chat` on the right side of the ticket. Click it and the Telegram group chat associated with this ticket will be created. The link will change to `Enter Telegram chat`. Click on in to join the chat in your Telegram client. You'll be able to copy and pass the link to anyone you want to invite them to the Group Chat.
19 changes: 10 additions & 9 deletions app/controllers/telegram_group_chats_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,27 @@ def create

cli_path = REDMINE_CHAT_TELEGRAM_CONFIG['telegram_cli_path']
public_key_path = REDMINE_CHAT_TELEGRAM_CONFIG['telegram_cli_public_key_path']
cli_base = "#{cli_path} -W -k #{public_key_path} -e "
cli_base = "#{cli_path} -WCI -k #{public_key_path} -e "

@issue = Issue.visible.find(params[:issue_id])

subject = "#{@issue.project.name} ##{@issue.id}"
bot_name = Setting.plugin_redmine_chat_telegram['bot_name']

%x(#{cli_base} "create_group_chat \\"#{subject}\\" #{bot_name}" )
result = %x(#{cli_base} "create_group_chat \\"#{subject}\\" #{bot_name}" )
telegram_id = result.match(/chat#(\d+)/)[1].to_i
chat_id = result.match(/chat#(\d+)/).to_s

cmd = "#{cli_base} \"export_chat_link #{subject.gsub(' ', '_').gsub('#', '@')}\""
cmd = "#{cli_base} \"export_chat_link #{chat_id}\""
result = %x( #{cmd} )

telegram_chat_url = result.match(/https:\/\/telegram.me\/joinchat\/[\w-]+/).to_s

begin
@issue.telegram_chat_url = telegram_chat_url
@issue.save
rescue ActiveRecord::StaleObjectError
@issue.reload
retry
if @issue.telegram_group.present?
@issue.telegram_group.update shared_url: telegram_chat_url
else
@issue.create_telegram_group telegram_id: telegram_id,
shared_url: telegram_chat_url
end

@issue.init_journal(current_user, "По ссылке #{telegram_chat_url} создан чат.")
Expand Down
5 changes: 5 additions & 0 deletions app/models/redmine_chat_telegram/telegram_group.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class RedmineChatTelegram::TelegramGroup < ActiveRecord::Base
unloadable

belongs_to :issue
end
21 changes: 21 additions & 0 deletions db/migrate/007_create_redmine_chat_telegram_telegram_groups.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
class CreateRedmineChatTelegramTelegramGroups < ActiveRecord::Migration
def up
create_table :redmine_chat_telegram_telegram_groups do |t|
t.belongs_to :issue, index: true, foreign_key: true
t.integer :telegram_id
t.string :shared_url
t.datetime :need_to_close_at
t.datetime :last_notification_at
end
add_index :redmine_chat_telegram_telegram_groups, :telegram_id

Issue.where.not(telegram_id: nil).find_each do |issue|
issue.create_telegram_group telegram_id: issue.telegram_id.abs,
shared_url: issue.telegram_chat_url
end
end

def down
drop_table :redmine_chat_telegram_telegram_groups
end
end
4 changes: 4 additions & 0 deletions lib/redmine_chat_telegram.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
module RedmineChatTelegram
def self.table_name_prefix
'redmine_chat_telegram_'
end

def self.issue_url(issue_id)
if Setting['protocol'] == 'https'
URI::HTTPS.build({ host: Setting['host_name'], path: "/issues/#{issue_id}" }).to_s
Expand Down
16 changes: 16 additions & 0 deletions lib/redmine_chat_telegram/patches/issue_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,22 @@ def self.included(base) # :nodoc:
unloadable

has_many :telegram_messages
has_one :telegram_group, class_name: 'RedmineChatTelegram::TelegramGroup'

def set_need_to_close
if telegram_group.present?
telegram_group.update need_to_close_at: 2.weeks.from_now,
last_notification_at: (1.week.from_now - 12.hours)

end
end

def reset_need_to_close
if telegram_group.present?
telegram_group.update need_to_close_at: nil,
last_notification_at: nil
end
end
end
end

Expand Down
26 changes: 8 additions & 18 deletions lib/tasks/chat_telegram.rake
Original file line number Diff line number Diff line change
@@ -1,18 +1,3 @@
def set_telegram_id(message, telegram_chat_id)
chat_title = message.chat.title
issue_id = chat_title.match(/#(\d+)/)[1]
issue = Issue.find(issue_id)

begin
issue.telegram_id = telegram_chat_id
issue.save
rescue ActiveRecord::StaleObjectError
issue.reload
retry
end
issue
end

def chat_user_full_name(telegram_user)
[telegram_user.first_name, telegram_user.last_name].compact.join
end
Expand Down Expand Up @@ -85,8 +70,15 @@ namespace :chat_telegram do
from_last_name = message.from.last_name
from_username = message.from.username

begin
issue = Issue.find_by!(telegram_id: telegram_chat_id)
rescue Exception => e
LOG.error "#{e.class}: #{e.message}\n#{e.backtrace.join("\n")}"
next
end


if message.group_chat_created
issue = set_telegram_id(message, telegram_chat_id)

issue_url = RedmineChatTelegram.issue_url(issue.id)
bot.send_message(chat_id: telegram_chat_id,
Expand All @@ -101,8 +93,6 @@ namespace :chat_telegram do
from_last_name: from_last_name, from_username: from_username,
is_system: true
else
issue = Issue.find_by(telegram_id: telegram_chat_id)
issue = set_telegram_id(message, telegram_chat_id) unless issue.present?

if message.new_chat_participant.present?
new_chat_participant = message.new_chat_participant
Expand Down
9 changes: 9 additions & 0 deletions test/unit/redmine_chat_telegram/telegram_group_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
require File.expand_path('../../test_helper', __FILE__)

class RedmineChatTelegram::TelegramGroupTest < ActiveSupport::TestCase

# Replace this with your real tests.
def test_truth
assert true
end
end

0 comments on commit ffba883

Please sign in to comment.