Skip to content

Commit

Permalink
Merge pull request #99 from centosadmin/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vladislav-yashin authored Aug 8, 2018
2 parents 2cc786e + 3320237 commit 5d16606
Show file tree
Hide file tree
Showing 40 changed files with 325 additions and 290 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
# 1.3.0

* Use redmine_bots instead of redmine_telegram_common
* Add Slack support (and transparent support for custom protocols)
* Fix redmine_helpdesk integration

# 1.2.1

* Fix helpdesk support
Expand Down
10 changes: 7 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,7 @@ Please help us make this plugin better telling us of any [issues](https://github

* **Ruby 2.3+**
* **Redmine 3.1+**
* Configured [redmine_telegram_common](https://github.com/centosadmin/redmine_telegram_common)
* You should have Telegram bot account
* Configured [redmine_bots](https://github.com/centosadmin/redmine_bots)
* Install [Redis](https://redis.io) 2.8 or higher. Run Redis and add it to autorun.
* Install the [redmine_sidekiq](https://github.com/ogom/redmine_sidekiq) plugin. [Redis](https://redis.io) 2.8 or greater is required.
* You need to configure Sidekiq queues `default` and `telegram`. [Config example](https://github.com/centosadmin/redmine_intouch/blob/master/extras/sidekiq.yml) - place it to `redmine/config` directory (Or copy from plugins/redmine_intouch/extras/sidekiq.yml to config/sidekiq.yml).
Expand All @@ -33,6 +32,11 @@ bundle exec rake redmine:plugins:migrate RAILS_ENV=production

The `extras` folder has the examples of the plugin config files and the `init.d` startup script

### Upgrade from 1.2 to 1.3+

From 1.3.0 redmine_intouch depends on [redmine_bots](https://github.com/centosadmin/redmine_bots) instead of redmine_telegram_common.
Make sure to remove redmine_telegram_common from plugins and follow installation instruction for [redmine_bots](https://github.com/centosadmin/redmine_bots).

### Upgrade from 1.0.2 to 1.1.0+

From 1.1.0 redmine_intouch (as well as other Southbridge telegram plugins) is using bot from redmine_telegram_common.
Expand Down Expand Up @@ -66,7 +70,7 @@ Since 0.4 version, model `TelegramUser` will be removed, also table `telegram_us

## General settings

You should specify all the necessary notification protocols in the "Protocols" section. These are 'telegram' and 'email' available at the moment.
You should specify all the necessary notification protocols in the "Protocols" section. These are 'telegram', 'slack' and 'email' available at the moment.

The section "Working days" should contain:

Expand Down
27 changes: 0 additions & 27 deletions app/models/intouch_sender.rb

This file was deleted.

2 changes: 1 addition & 1 deletion app/models/settings_template.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ class SettingsTemplate < ActiveRecord::Base

attr_accessible :name, :intouch_settings

store :intouch_settings, accessors: %w[assigner_groups assigner_roles reminder_settings telegram_settings email_settings]
store :intouch_settings, accessors: %w[assigner_groups assigner_roles reminder_settings] | Intouch.protocols.keys.map { |p| "#{p}_settings" }

def copy_from(settings_template)
self.attributes = settings_template.attributes.dup.except('id', 'created_on', 'updated_on')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
</th>
<% IssuePriority.order(:position).each do |priority| %>
<td>
<% Intouch.active_protocols.each do |protocol| %>
<% Intouch.active_protocols.each do |protocol, _| %>
<%= check_box_tag "intouch_settings[#{protocol}_settings][#{kind}][#{status.id}][]",
priority.id,
settings_source.send("#{protocol}_settings").try(:[], kind).
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
<tr>
<% %w(author assigned_to watchers).each do |receiver| %>
<td>
<% Intouch.active_protocols.each do |protocol| %>
<% Intouch.active_protocols.each do |protocol, _| %>
<%= check_box_tag "intouch_settings[#{protocol}_settings][#{status}][#{receiver}]", '1',
settings_source.send("#{protocol}_settings").try(:[], status).try(:[], receiver),
class: "#{protocol}_#{receiver}",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
<tr>
<% %w(author assigned_to watchers).each do |receiver| %>
<td>
<% Intouch.active_protocols.each do |protocol| %>
<% Intouch.active_protocols.each do |protocol, _| %>
<%= check_box_tag "intouch_settings[#{protocol}_settings][overdue][#{receiver}]",
'1',
settings_source.send("#{protocol}_settings").try(:[], 'overdue').try(:[], receiver),
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
<tr>
<% %w(author watchers).each do |receiver| %>
<td>
<% Intouch.active_protocols.each do |protocol| %>
<% Intouch.active_protocols.each do |protocol, _| %>
<%= check_box_tag "intouch_settings[#{protocol}_settings][unassigned][#{receiver}]", '1',
settings_source.send("#{protocol}_settings").try(:[], 'unassigned').try(:[], receiver),
class: "#{protocol}_#{receiver}",
Expand Down
2 changes: 1 addition & 1 deletion app/views/settings/_common.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<p class="description"><%= t 'intouch.settings.common.protocols.description' %></p>

<ul class="inline-checkboxes">
<% Intouch::AVAILABLE_PROTOCOLS.each do |protocol| %>
<% Intouch.protocols.each do |protocol, _| %>
<li>
<%= check_box_tag "settings[active_protocols][]", protocol,
@settings["active_protocols"].try(:include?, protocol),
Expand Down
7 changes: 0 additions & 7 deletions app/views/settings/_intouch.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,6 @@
{name: 'sidekiq_cron_jobs', partial: 'settings/sidekiq_cron_jobs', label: 'intouch.label.sidekiq_cron_jobs'}
] %>

<% unless Redmine::Plugin.installed?('redmine_telegram_common') %>
<h1 style="text-align: center">
!!! <a href="https://github.com/centosadmin/redmine_telegram_common" target="_blank">redmine_telegram_common</a>
<%= t('intouch.settings.required') %> !!!
</h1>
<% end %>

<% unless Redmine::Plugin.installed?('redmine_sidekiq') %>
<h1 style="text-align: center">
!!! <a href="https://github.com/ogom/redmine_sidekiq" target="_blank">redmine_sidekiq</a>
Expand Down
2 changes: 1 addition & 1 deletion app/workers/cron_feedback_regular_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class CronFeedbackRegularNotification

def perform
# Feedback
Intouch.send_notifications Issue.open.joins(:project).feedbacks, 'feedback'
Intouch.send_notifications Issue.open.joins(:project).feedbacks.distinct, 'feedback'
end
end
12 changes: 4 additions & 8 deletions app/workers/cron_overdue_regular_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class CronOverdueRegularNotification
include Sidekiq::Worker

def perform
if Intouch.active_protocols.include? 'email'
if Intouch.active_protocols.keys.include? 'email'
# Overdue and Without due date (email only)
overdue_issue_ids = Issue.open.joins(:project).where('due_date < ?', Date.today).pluck :id
without_due_date_issue_ids = Issue.open.where(due_date: nil).where('created_on < ?', 1.day.ago).pluck :id
Expand All @@ -15,13 +15,9 @@ def perform
Intouch.send_bulk_email_notifications Issue.open.where(id: issue_ids), 'overdue'
end

if Intouch.active_protocols.include? 'telegram'
# Overdue (telegram only)
Intouch.send_notifications Issue.open.joins(:project).where('due_date < ?', Date.today), 'overdue'
Intouch.send_notifications Issue.open.joins(:project).where('due_date < ?', Date.today).distinct, 'overdue'

# Without due date (telegram only)
Intouch.send_notifications Issue.open.where(due_date: nil)
.where('created_on < ?', 1.day.ago), 'overdue'
end
Intouch.send_notifications Issue.open.where(due_date: nil)
.where('created_on < ?', 1.day.ago).distinct, 'overdue'
end
end
4 changes: 2 additions & 2 deletions app/workers/cron_unassigned_regular_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ class CronUnassignedRegularNotification

def perform
# Unassigned
Intouch.send_notifications Issue.open.joins(:project).where(assigned_to_id: nil), 'unassigned'
Intouch.send_notifications Issue.open.joins(:project).where(assigned_to_id: nil).distinct, 'unassigned'

# Assigned to Group
Intouch.send_notifications Issue.open.joins(:project, :assigned_to).where(users: { type: 'Group' }), 'unassigned'
Intouch.send_notifications Issue.open.joins(:project, :assigned_to).where(users: { type: 'Group' }).distinct, 'unassigned'
end
end
2 changes: 1 addition & 1 deletion app/workers/cron_working_regular_notification.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ class CronWorkingRegularNotification

def perform
# Working
Intouch.send_notifications Issue.open.joins(:project).working, 'working'
Intouch.send_notifications Issue.open.joins(:project).working.distinct, 'working'
end
end
10 changes: 6 additions & 4 deletions app/workers/email_live_sender_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,14 @@ class EmailLiveSenderWorker
include Sidekiq::Worker
EMAIL_LIVE_SENDER_LOG = Logger.new(Rails.root.join('log/intouch', 'email-live-sender.log'))

def perform(issue_id, journal_id, _required_recipients = [])
def perform(issue_id, journal_id, grouped_recipient_ids)
Intouch.set_locale
issue = Intouch::IssueDecorator.new(Issue.find(issue_id), journal_id)
issue = Intouch::IssueDecorator.new(Issue.find(issue_id), journal_id, protocol: 'email')

issue.intouch_live_recipients('email').each do |user|
IntouchMailer.reminder_email(user, issue).deliver if user.present?
grouped_recipient_ids.each do |klass, ids|
klass.constantize.where(id: ids).each do |user|
IntouchMailer.reminder_email(user, issue).deliver
end
end
rescue ActiveRecord::RecordNotFound => e
EMAIL_LIVE_SENDER_LOG.error "#{e.class}: #{e.message}"
Expand Down
24 changes: 24 additions & 0 deletions app/workers/slack_live_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
class SlackLiveWorker
include Sidekiq::Worker

def perform(issue_id, journal_id, recipient_ids)
return if recipient_ids.blank?

issue = Issue.find_by(id: issue_id) || return
journal = Journal.find_by(id: journal_id)

issue = Intouch::IssueDecorator.new(issue, journal, protocol: 'slack')

slack_accounts = SlackAccount.where(user_id: recipient_ids)

client = RedmineBots::Slack.bot_client

channels = client.im_list

return unless channels['ok']

channels.ims.select { |im| im.user.in?(slack_accounts.map(&:slack_id)) }.each do |channel|
client.chat_postMessage(text: issue.as_markdown, channel: channel.id)
end
end
end
38 changes: 38 additions & 0 deletions app/workers/slack_regular_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
class SlackRegularWorker
include Sidekiq::Worker

def perform(issue_id, state)
issue = Issue.find_by(id: issue_id) || return
project = issue.project

return unless Intouch::Regular::Checker::Base.new(
issue: issue,
state: state,
project: project
).required?

slack_accounts = SlackAccount.where(
user_id: Intouch::Regular::RecipientsList.new(
issue: issue,
state: state,
protocol: 'slack'
).call.map(&:id)
).to_a

return if slack_accounts.empty?

client = RedmineBots::Slack.bot_client

channels = client.im_list

return unless channels['ok']

channels = client.im_list

return unless channels['ok']

channels.ims.select { |im| im.user.in?(slack_accounts.map(&:slack_id)) }.each do |channel|
client.chat_postMessage(text: issue.as_markdown, channel: channel.id)
end
end
end
4 changes: 2 additions & 2 deletions app/workers/telegram_group_live_sender_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ def perform(issue_id, journal_id)
logger.debug "START for issue_id #{issue_id}"
Intouch.set_locale

issue = Intouch::IssueDecorator.new(Issue.find(issue_id), journal_id)
issue = Intouch::IssueDecorator.new(Issue.find(issue_id), journal_id, protocol: 'telegram')
logger.debug issue.inspect

telegram_groups_settings = issue.project.active_telegram_settings.try(:[], 'groups')
Expand Down Expand Up @@ -44,7 +44,7 @@ def perform(issue_id, journal_id)

return unless group_for_send_ids.present?

message = issue.telegram_live_message
message = issue.as_markdown

logger.debug "message: #{message}"

Expand Down
59 changes: 7 additions & 52 deletions app/workers/telegram_live_sender_worker.rb
Original file line number Diff line number Diff line change
@@ -1,53 +1,22 @@
class TelegramLiveSenderWorker
include Sidekiq::Worker

def perform(issue_id, journal_id, required_recipients = [])
@required_recipients = required_recipients
def perform(issue_id, journal_id, recipient_ids)
logger.debug "START for issue_id #{issue_id}"

Intouch.set_locale

issue = Intouch::IssueDecorator.new(Issue.find(issue_id), journal_id)
issue = Intouch::IssueDecorator.new(Issue.find(issue_id), journal_id, protocol: 'telegram')
logger.debug issue.inspect

base_message = issue.telegram_live_message

logger.debug "base_message: #{base_message}"

issue.intouch_live_recipients('telegram').each do |user|
User.where(id: recipient_ids).each do |user|
message = issue.as_markdown(user_id: user.id)

logger.debug "user: #{user.inspect}"

telegram_account = user.telegram_account
telegram_account = TelegramAccount.find_by(user_id: user.id)
logger.debug "telegram_account: #{telegram_account.inspect}"
next unless telegram_account.present? && telegram_account.active?

roles_in_issue = []

roles_in_issue << 'assigned_to' if issue.assigned_to_id == user.id
roles_in_issue << 'watchers' if issue.watchers.pluck(:user_id).include?(user.id) || IntouchSubscription.find_by(user_id: user.id, project_id: issue.project_id)&.active?
roles_in_issue << 'author' if issue.author_id == user.id

logger.debug "roles_in_issue: #{roles_in_issue.inspect}"

next unless need_notification?(roles_in_issue)

project = issue.project
settings = project.active_telegram_settings

if settings.present?
recipients = settings.select do |key, value|
%w(author assigned_to watchers).include?(key) &&
value.try(:[], issue.status_id.to_s).try(:include?, issue.priority_id.to_s)
end.keys

prefix = roles_for_prefix(recipients, roles_in_issue).map do |role|
I18n.t("intouch.telegram_message.recipient.#{role}")
end.join(', ')
else
prefix = nil
end

message = prefix.present? ? "#{prefix}\n#{base_message}" : base_message
next unless telegram_account.present?

logger.debug message

Expand All @@ -63,21 +32,7 @@ def perform(issue_id, journal_id, required_recipients = [])

private

attr_reader :required_recipients

def logger
@logger ||= Logger.new(Rails.root.join('log/intouch', 'telegram-live-sender.log'))
end

def need_notification?(roles_in_issue)
return roles_in_issue unless required_recipients.present?

(roles_in_issue & required_recipients).present?
end

def roles_for_prefix(recipients, roles_in_issue)
return roles_in_issue & recipients unless required_recipients.present?

roles_in_issue & recipients & required_recipients
end
end
6 changes: 2 additions & 4 deletions app/workers/telegram_message_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,13 +26,11 @@ def perform(telegram_account_id, message)
TELEGRAM_MESSAGE_SENDER_ERRORS_LOG.info "MESSAGE: #{message}"

telegram_account = (telegram_account_id > 0) ?
TelegramCommon::Account.find_by(telegram_id: telegram_account_id) :
TelegramAccount.find_by(telegram_id: telegram_account_id) :
TelegramGroupChat.find_by(tid: telegram_account_id.abs)

if e.message.include? 'Bot was kicked'

telegram_account.deactivate! if telegram_account.is_a? TelegramCommon::Account
TELEGRAM_MESSAGE_SENDER_ERRORS_LOG.info "Bot was kicked from chat. Deactivate #{telegram_account.inspect}"
TELEGRAM_MESSAGE_SENDER_ERRORS_LOG.info "Bot was kicked from chat."

elsif e.message.include?('429') || e.message.include?('retry later')

Expand Down
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,7 @@ en:
protocols:
email: E-mail
telegram: Telegram
slack: Slack
title: Protocols
recipient:
assigned_to: Assigner
Expand Down
Loading

0 comments on commit 5d16606

Please sign in to comment.