Skip to content

Commit

Permalink
Merge pull request #17 from centosadmin/develop
Browse files Browse the repository at this point in the history
Release 0.2.0
  • Loading branch information
vladislav-yashin authored Dec 26, 2018
2 parents 3955da2 + 67929e0 commit 9e0d33e
Show file tree
Hide file tree
Showing 23 changed files with 77 additions and 51 deletions.
5 changes: 3 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
language: ruby
rvm:
- 2.3.6
- 2.5.1
- 2.3.8
- 2.6.0

branches:
only:
Expand All @@ -13,6 +13,7 @@ addons:

env:
- REDMINE_VER=3.4-stable
- REDMINE_VER=4.0-stable

install: "echo skip bundle install"

Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# 0.2.0

* Fix edit group admin command
* Prevent duplicate chats
* Fix IssueChatKickLockedUsersWorker error
* Truncate issue notes in bot messages
* Proxy telegram links through redmine
* Fix /issue command
* Fix /new command
* Adapt for Redmine 4
* Fix settings caching issues in sidekiq

# 0.1.0

* Initial release
2 changes: 1 addition & 1 deletion app/controllers/chat_messages_controller.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class ChatMessagesController < ApplicationController
unloadable


def index
@issue = Issue.visible.find(params[:id])
Expand Down
17 changes: 11 additions & 6 deletions app/controllers/issue_chats_controller.rb
Original file line number Diff line number Diff line change
@@ -1,18 +1,19 @@
class IssueChatsController < ApplicationController
include Redmine2chat::Operations

unloadable
skip_before_action :check_if_login_required, only: :tg_join

def create
@issue = Issue.visible.find(params[:issue_id])
@issue.with_lock do
if @issue.active_chat.present? && @issue.active_chat.shared_url.present?
redirect_to issue_path(@issue)
return
end

if @issue.active_chat.present? and @issue.active_chat.shared_url.present?
redirect_to issue_path(@issue)
return
CreateChat.(@issue)
end

CreateChat.(@issue)

@project = @issue.project

@last_journal = @issue.journals.visible.order('created_on').last
Expand All @@ -29,4 +30,8 @@ def destroy
@last_journal = @issue.journals.visible.order('created_on').last
redirect_to "#{issue_path(@issue)}#change-#{@last_journal.id}"
end

def tg_join
redirect_to "tg://join?invite=#{params[:invite_id]}"
end
end
2 changes: 1 addition & 1 deletion app/models/chat_message.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
class ChatMessage < ActiveRecord::Base
include Redmine::I18n

unloadable


default_scope {joins(issue: :project).order(sent_at: :desc)}
scope :reverse_scope, -> {unscope(:order).order('sent_at ASC')}
Expand Down
4 changes: 2 additions & 2 deletions app/models/telegram_executing_command.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class TelegramExecutingCommand < ActiveRecord::Base
unloadable


serialize :data

Expand All @@ -17,6 +17,6 @@ def cancel(command)
bot_token: RedmineBots::Telegram.bot_token,
chat_id: command.chat.id,
message: I18n.t('redmine_2chat.bot.command_canceled'),
reply_markup: ::Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true))
reply_markup: ::Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true).to_json)
end
end
2 changes: 1 addition & 1 deletion app/workers/issue_chat_daily_report_cron_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def perform
time_to = yesterday.end_of_day

Issue.joins(:chat_messages).where('chat_messages.sent_at >= ? and chat_messages.sent_at <= ?',
time_from, time_to).uniq.find_each do |issue|
time_from, time_to).uniq.each do |issue|
IssueChatDailyReportWorker.perform_async(issue.id, yesterday.to_s)
end
end
Expand Down
6 changes: 4 additions & 2 deletions app/workers/issue_chat_daily_report_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class IssueChatDailyReportWorker
include Redmine::I18n

def perform(issue_id, yesterday_string)
settings = Setting.find_by_name(:plugin_redmine_bots).value

I18n.locale = Setting['default_language']

yesterday = Date.parse yesterday_string
Expand All @@ -13,8 +15,8 @@ def perform(issue_id, yesterday_string)
chat_messages = issue.chat_messages
.where(sent_at: time_from..time_to)
.where(is_system: false)
.where.not(im_id: [Setting.plugin_redmine_bots['telegram_bot_id'],
Setting.plugin_redmine_bots['telegram_robot_id']])
.where.not(im_id: [settings['telegram_bot_id'],
settings['telegram_robot_id']])

if chat_messages.present?
date_string = format_date(yesterday)
Expand Down
4 changes: 3 additions & 1 deletion app/workers/issue_chat_kick_locked_users_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ def perform
private

def kick_locked_users(client)
IssueChat.where(platform_name: 'telegram').all.each do |group|
IssueChat.where(platform_name: 'telegram').where.not(im_id: nil).each do |group|
chat = client.broadcast_and_receive('@type' => 'getChat', 'chat_id' => group.im_id)

group_info = client.broadcast_and_receive('@type' => 'getBasicGroupFullInfo',
'basic_group_id' => chat.dig('type', 'basic_group_id')
)
next unless group_info.present?

(@logger.warn("Error while fetching group ##{group.im_id}: #{group_info.inspect}") && next) if group_info['@type'] == 'error'

telegram_user_ids = group_info['members'].map { |m| m['user_id'] }
Expand Down
4 changes: 3 additions & 1 deletion app/workers/issue_chat_notifications_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ class IssueChatNotificationsWorker
include IssuesHelper
include CustomFieldsHelper

MAX_NOTES_LENGTH = 500

ISSUE_NOTIFICATIONS_LOG = Logger.new(Rails.root.join('log/redmine_2chat', 'telegram-issue-notifications.log'))

def perform(im_id, platform_name, journal_id)
Expand All @@ -17,7 +19,7 @@ def perform(im_id, platform_name, journal_id)

message << "\n#{details_to_strings(journal.visible_details, true).join("\n")}" if journal.details.present?

message << "\n<pre>#{ActionView::Base.full_sanitizer.sanitize(journal.notes)}</pre>" if journal.notes.present?
message << "\n<pre>#{ActionView::Base.full_sanitizer.sanitize(journal.notes).truncate(MAX_NOTES_LENGTH, separator: ' ')}</pre>" if journal.notes.present?

ISSUE_NOTIFICATIONS_LOG.info "MESSAGE: #{message}"

Expand Down
2 changes: 2 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
resources :issue_chats, only: [:create, :destroy]
get 'issues/:id/chat_messages' => 'chat_messages#index', as: 'issue_chat_messages'
post 'issues/:id/chat_messages/publish' => 'chat_messages#publish', as: 'publish_issue_chat_messages'

get 'tg/:invite_id' => 'issue_chats#tg_join'
2 changes: 1 addition & 1 deletion db/migrate/001_create_issue_chats.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateIssueChats < ActiveRecord::Migration
class CreateIssueChats < Rails.version < '5.0' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
def change
create_table :issue_chats do |t|
t.belongs_to :issue, index: true, foreign_key: true
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/002_create_chat_messages.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateChatMessages < ActiveRecord::Migration
class CreateChatMessages < Rails.version < '5.0' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
def change
create_table :chat_messages do |t|
t.string :im_id
Expand Down
2 changes: 1 addition & 1 deletion db/migrate/003_create_executing_commands.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
class CreateExecutingCommands < ActiveRecord::Migration
class CreateExecutingCommands < Rails.version < '5.0' ? ActiveRecord::Migration : ActiveRecord::Migration[4.2]
def change
create_table :telegram_executing_commands do |t|
t.integer :account_id
Expand Down
4 changes: 2 additions & 2 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,11 +48,11 @@
name 'Redmine 2Chat'
url 'https://github.com/centosadmin/redmine_2chat'
description 'This is a plugin for Redmine which adds group chats to Redmine issues on different chat platforms such as Slack and Telegram.'
version '0.1.0'
version '0.2.0'
author 'Southbridge'
author_url 'https://github.com/centosadmin'

requires_redmine_plugin :redmine_bots, '0.1.0'
requires_redmine_plugin :redmine_bots, '0.2.0'

settings(default: {
'daily_report' => '1',
Expand Down
4 changes: 1 addition & 3 deletions lib/redmine2chat/patches/issue_patch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ module Redmine2chat::Patches
module IssuePatch
def self.included(base) # :nodoc:
base.class_eval do
unloadable

has_many :chat_messages, through: :chats, source: :messages
has_many :chats, class_name: 'IssueChat'
has_many :chat_messages, through: :chats, source: :messages
has_one :active_chat, -> { active }, class_name: 'IssueChat'

before_save :set_need_to_close, :reset_need_to_close
Expand Down
11 changes: 9 additions & 2 deletions lib/redmine2chat/platforms/telegram.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,12 @@ def inactive_icon_path
end

def create_chat(title)
bot_id = Setting.plugin_redmine_bots['telegram_bot_id']
bot_id = Setting.find_by_name(:plugin_redmine_bots).value['telegram_bot_id']
result = Utils.create_chat.(title, [bot_id])
chat_id = result['id']
result = Utils.get_chat_link.(chat_id)

{ im_id: chat_id, chat_url: result['invite_link'] }
{ im_id: chat_id, chat_url: convert_link(result['invite_link']) }
end

def close_chat(im_id, message)
Expand All @@ -39,5 +39,12 @@ def send_message(im_id, message, params = {})

RedmineBots::Telegram::Bot::MessageSender.call(message_params)
end

private

def convert_link(link)
invite_id = Addressable::URI.parse(link).request_uri.split('/').last
"#{Setting.protocol}://#{Setting.host_name}/tg/#{invite_id}"
end
end
end
3 changes: 2 additions & 1 deletion lib/redmine2chat/telegram/bot/group_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def group_commands
def handle_group_command
if !group_commands.include?(command_name) && command_name.present?
if private_commands.include?(command_name)
send_message(I18n.t('redmine_bots.bot.group.private_command'))
send_message(I18n.t('redmine_bots.telegram.bot.group.private_command'))
end
else
if group_common_command?
Expand Down Expand Up @@ -136,6 +136,7 @@ def can_manage_chat?(telegram_user)
end

def edit_group_admin(telegram_user, is_admin = true)
return unless issue.active_chat
toggle_chat_admin.(issue.active_chat.im_id, telegram_user.id, is_admin)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/redmine2chat/telegram/commands/edit_issue_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ def finish_with_error
executing_command.destroy
send_message(
locale('incorrect_value'),
reply_markup: Telegram::Bot::Types::ReplyKeyboardRemove.new(hide_keyboard: true))
reply_markup: Telegram::Bot::Types::ReplyKeyboardRemove.new(hide_keyboard: true).to_json)
end

def executing_command
Expand Down
10 changes: 5 additions & 5 deletions lib/redmine2chat/telegram/commands/new_issue_command.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ def execute_step_2
executing_command.destroy
send_message(
I18n.t('redmine_2chat.bot.new_issue.user_not_found'),
reply_markup: Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true))
reply_markup: Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true).to_json)
end
end

Expand Down Expand Up @@ -71,7 +71,7 @@ def execute_step_5
keyboard: [[I18n.t('redmine_2chat.bot.new_issue.yes_answer'),
I18n.t('redmine_2chat.bot.new_issue.no_answer')]],
one_time_keyboard: true,
resize_keyboard: true)
resize_keyboard: true).to_json

send_message(message_text,
reply_markup: keyboard)
Expand All @@ -94,7 +94,7 @@ def create_chat

send_message(
I18n.t('redmine_2chat.bot.creating_chat'),
reply_markup: Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true)
reply_markup: Telegram::Bot::Types::ReplyKeyboardRemove.new(remove_keyboard: true).to_json
)

CreateChat.(issue)
Expand Down Expand Up @@ -158,7 +158,7 @@ def projects_list_markup(all_projects)
Telegram::Bot::Types::ReplyKeyboardMarkup.new(
keyboard: project_names.each_slice(2).to_a,
one_time_keyboard: true,
resize_keyboard: true)
resize_keyboard: true).to_json
end

def assignable_list_markup(assignables)
Expand All @@ -174,7 +174,7 @@ def assignable_list_markup(assignables)
Telegram::Bot::Types::ReplyKeyboardMarkup.new(
keyboard: assignables_names.each_slice(2).to_a,
one_time_keyboard: true,
resize_keyboard: true)
resize_keyboard: true).to_json
end

def executing_command
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,7 @@ class Redmine2chat::Telegram::Commands::EditIssueCommandTest < ActiveSupport::Te
Telegram::Bot::Types::ReplyKeyboardRemove.expects(:new).returns(nil)
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text, reply_markup: nil)
.with(text, reply_markup: 'null')
Redmine2chat::Telegram::Commands::EditIssueCommand.new(command).execute
end
end
Expand Down Expand Up @@ -188,7 +188,7 @@ class Redmine2chat::Telegram::Commands::EditIssueCommandTest < ActiveSupport::Te
Telegram::Bot::Types::ReplyKeyboardRemove.expects(:new).returns(nil)
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text, reply_markup: nil)
.with(text, reply_markup: 'null')
Redmine2chat::Telegram::Commands::EditIssueCommand.new(command).execute
end
end
Expand Down Expand Up @@ -216,7 +216,7 @@ class Redmine2chat::Telegram::Commands::EditIssueCommandTest < ActiveSupport::Te
Telegram::Bot::Types::ReplyKeyboardRemove.expects(:new).returns(nil)
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text, reply_markup: nil)
.with(text, reply_markup: 'null')
Redmine2chat::Telegram::Commands::EditIssueCommand.new(command).execute
end
end
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ class Redmine2chat::Telegram::Commands::NewIssueCommandTest < ActiveSupport::Tes
text = I18n.t('redmine_2chat.bot.new_issue.choice_project_without_page')
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text, reply_markup: nil)
.with(text, reply_markup: 'null')

Redmine2chat::Telegram::Commands::NewIssueCommand.new(command).execute
end
Expand Down Expand Up @@ -75,7 +75,7 @@ class Redmine2chat::Telegram::Commands::NewIssueCommandTest < ActiveSupport::Tes
text = I18n.t('redmine_2chat.bot.new_issue.choice_user')
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text, reply_markup: nil)
.with(text, reply_markup: 'null')

Redmine2chat::Telegram::Commands::NewIssueCommand.new(command).execute
end
Expand All @@ -85,7 +85,7 @@ class Redmine2chat::Telegram::Commands::NewIssueCommandTest < ActiveSupport::Tes
Telegram::Bot::Types::ReplyKeyboardRemove.expects(:new).returns(nil)
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text, reply_markup: nil)
.with(text, reply_markup: 'null')

Redmine2chat::Telegram::Commands::NewIssueCommand.new(command).execute
end
Expand Down Expand Up @@ -159,7 +159,7 @@ class Redmine2chat::Telegram::Commands::NewIssueCommandTest < ActiveSupport::Tes
HTML
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
.with(text.chomp, reply_markup: nil)
.with(text.chomp, reply_markup: 'null')

Redmine2chat::Telegram::Commands::NewIssueCommand.new(command).execute
end
Expand Down Expand Up @@ -188,7 +188,7 @@ class Redmine2chat::Telegram::Commands::NewIssueCommandTest < ActiveSupport::Tes
.expects(:send_message)
.with(
I18n.t('redmine_2chat.bot.creating_chat'),
reply_markup: nil
reply_markup: 'null'
)
Redmine2chat::Telegram::Commands::BaseBotCommand.any_instance
.expects(:send_message)
Expand Down
9 changes: 0 additions & 9 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,6 @@

Minitest::Reporters.use! Minitest::Reporters::SpecReporter.new

DatabaseCleaner.strategy = :transaction
DatabaseCleaner.clean_with(:truncation)

class Minitest::Spec
around do |tests|
DatabaseCleaner.cleaning(&tests)
end
end

class ActiveSupport::TestCase
# Add spec DSL
extend Minitest::Spec::DSL
Expand Down

0 comments on commit 9e0d33e

Please sign in to comment.