Skip to content

Commit

Permalink
Merge pull request #41 from centosadmin/develop
Browse files Browse the repository at this point in the history
Regular and live message refactoring and tuning
  • Loading branch information
arturtr authored Mar 30, 2017
2 parents 3939298 + d8d9be4 commit b9aa187
Show file tree
Hide file tree
Showing 36 changed files with 789 additions and 422 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
# 0.6.0

* Regular and live message refactoring and tuning
* Fix issues [#33](https://github.com/centosadmin/redmine_intouch/issues/33) and [#43](https://github.com/centosadmin/redmine_intouch/issues/43)

# 0.5.3

* Fix: Always send live message for required recipients for settings template
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<div class="box">
<p class="description">
<%= raw t('intouch.project.settings.assigner_groups.description') %>
<%= t('intouch.project.settings.assigner_groups.description_html') %>
</p>

<% Group.find_each do |group| %>
Expand Down
8 changes: 8 additions & 0 deletions app/workers/live_handler_worker.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
class LiveHandlerWorker
include Sidekiq::Worker

def perform(journal_id)
journal = Journal.find(journal_id)
Intouch::Live::Handler::UpdatedIssue.new(journal).call
end
end
50 changes: 39 additions & 11 deletions app/workers/telegram_group_sender_worker.rb
Original file line number Diff line number Diff line change
@@ -1,22 +1,50 @@
class TelegramGroupSenderWorker
include Sidekiq::Worker
TELEGRAM_GROUP_SENDER_LOG = Logger.new(Rails.root.join('log/intouch', 'telegram-group-sender.log'))

def perform(issue_id, group_ids, state)
return unless group_ids.present?

Intouch.set_locale
issue = Issue.find issue_id
@issue = Issue.find_by(id: issue_id)
@group_ids = group_ids
@state = state

return unless issue.notificable_for_state? state
return unless @issue.present?
return unless notificable?
return unless groups.present?

message = issue.telegram_message
groups.each { |group| send_message(group) }
end

private

attr_reader :issue, :state, :group_ids

def notificable?
Intouch::Regular::Checker::Base.new(
issue: issue,
state: state,
project: project
).required?
end

def groups
@groups ||= TelegramGroupChat.where(id: group_ids).uniq
end

def send_message(group)
return unless group.tid.present?
TelegramMessageSender.perform_async(-group.tid, message)
end

def message
@message ||= issue.telegram_message
end

def project
@project ||= issue.project
end

TelegramGroupChat.where(id: group_ids).uniq.each do |group|
next unless group.tid.present?
TelegramMessageSender.perform_async(-group.tid, message)
end
rescue ActiveRecord::RecordNotFound => e
# ignore
def logger
@logger ||= Logger.new(Rails.root.join('log/intouch', 'telegram-group-sender.log'))
end
end
70 changes: 40 additions & 30 deletions app/workers/telegram_sender_worker.rb
Original file line number Diff line number Diff line change
@@ -1,45 +1,55 @@
require_relative '../../lib/intouch/regular/checker/base'
require_relative '../../lib/intouch/regular/recipients_list'
require_relative '../../lib/intouch/regular/message/private'

class TelegramSenderWorker
include Sidekiq::Worker
TELEGRAM_SENDER_LOG = Logger.new(Rails.root.join('log/intouch', 'telegram-sender.log'))

def perform(issue_id, state)
Intouch.set_locale
issue = Issue.find issue_id

return unless issue.notificable_for_state? state
@issue = Issue.find_by(id: issue_id)
@state = state

base_message = issue.telegram_message
return unless @issue.present?
return unless notificable?
return unless users.present?

issue.intouch_recipients('telegram', state).each do |user|
telegram_account = user.telegram_account
next unless telegram_account.present? && telegram_account.active?
users.each { |user| send_message(user) }
end

roles_in_issue = []
private

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
roles_in_issue << 'author' if issue.author_id == user.id
attr_reader :issue, :state

project = issue.project
settings = project.active_telegram_settings.try(:[], state)
def notificable?
Intouch::Regular::Checker::Base.new(
issue: issue,
state: state,
project: project
).required?
end

if settings.present?
recipients = settings.select do |key, _value|
%w(author assigned_to watchers).include?(key)
end.keys
def users
@users ||= Intouch::Regular::RecipientsList.new(
issue: issue,
state: state,
protocol: 'telegram'
).call
end

prefix = (roles_in_issue & recipients).map do |role|
I18n.t("intouch.telegram_message.recipient.#{role}")
end.join(', ')
else
prefix = nil
end
def send_message(user)
Intouch::Regular::Message::Private.new(
issue: issue,
user: user,
state: state,
project: project
).send
end

message = prefix.present? ? "#{prefix}\n#{base_message}" : base_message
def project
@project ||= issue.project
end

TelegramMessageSender.perform_async(telegram_account.telegram_id, message)
end
rescue ActiveRecord::RecordNotFound => e
# ignore
def logger
@logger ||= Logger.new(Rails.root.join('log/intouch', 'telegram-sender.log'))
end
end
4 changes: 3 additions & 1 deletion config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ en:
project:
settings:
assigner_groups:
description: Notifications addressed to <strong>Assigner</strong>, will be sent only if the <strong>Assigner</strong> is a part of one of the groups mentioned below.
description_html: |
Notifications addressed to <strong>Assigner</strong>, will be sent only if the <strong>Assigner</strong> is a part of one of the groups mentioned below.<br>
Notifications about inactivity in issue will be sent only if the issue is <strong>assigned to the Assigner</strong>.
common:
copy: Copy
copy_settings_from_tab: Copy the values from tab
Expand Down
4 changes: 3 additions & 1 deletion config/locales/ru.yml
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,9 @@ ru:
project:
settings:
assigner_groups:
description: "Уведомления, адресованные <strong>Исполнителю</strong>, будут отправлены только, если <strong>Исполнитель</strong> входит в одну из отмеченных ниже групп."
description_html: |
Уведомления, адресованные <strong>Исполнителю</strong>, будут отправлены только, если <strong>Исполнитель</strong> входит в одну из отмеченных ниже групп.<br>
Уведомления о бездействии в задачах будет отправлено только в случае, если задача <strong>назначена на Исполнителя</strong>.
common:
copy: "Скопировать"
copy_settings_from_tab: "Скопировать значения со вкладки"
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/010_change_tid_limit_in_telegram_group_chats.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ChangeTidLimitInTelegramGroupChats < ActiveRecord::Migration
def up
change_column :telegram_group_chats, :tid, :integer, limit: 8
end

def down
change_column :telegram_group_chats, :tid, :integer, limit: 4
end
end
16 changes: 11 additions & 5 deletions init.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,23 @@
name 'Redmine Intouch plugin'
url 'https://github.com/centosadmin/redmine_intouch'
description 'This is a plugin for Redmine which sends a reminder email and Telegram messages to the assignee workign on a task, whose status is not updated with-in allowed duration'
version '0.5.3'
version '0.6.0'
author 'Centos-admin.ru'
author_url 'https://centos-admin.ru'

requires_redmine version_or_higher: '3.0'

settings(default: { 'active_protocols' => %w(email), 'work_day_from' => '10:00', 'work_day_to' => '18:00' },
partial: 'settings/intouch')
settings(
default: {
'active_protocols' => %w(email),
'work_day_from' => '10:00',
'work_day_to' => '18:00'
},
partial: 'settings/intouch')

project_module :intouch do
permission :manage_intouch_settings, projects: :settings,
intouch_settings: :save
permission :manage_intouch_settings,
projects: :settings,
intouch_settings: :save
end
end
2 changes: 1 addition & 1 deletion lib/intouch.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def self.send_notifications(issues, state)

if active &&
interval.present? &&
Intouch::Message::Regular.new(issue).latest_action_on < interval.to_i.hours.ago &&
Intouch::Regular::Message::Base.new(issue).latest_action_on < interval.to_i.hours.ago &&
(last_notification.nil? || last_notification < interval.to_i.hours.ago)

if active_protocols.include? 'email'
Expand Down
18 changes: 0 additions & 18 deletions lib/intouch/checker/notification_required.rb

This file was deleted.

30 changes: 0 additions & 30 deletions lib/intouch/checker/private_message_required.rb

This file was deleted.

51 changes: 51 additions & 0 deletions lib/intouch/live/checker/base.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
module Intouch::Live::Checker
class Base
attr_reader :project, :issue, :journal

def initialize(issue:, project:, journal: nil)
@issue = issue
@project = project
@journal = journal

logger.info 'Initialized with:'
logger.debug "Issue: #{issue.inspect}"
logger.debug "Project: #{project.inspect}"
logger.debug "Journal: #{journal.inspect}"
end

def required?
project_enabled? && issue_open?
end

def project_enabled?
logger.info 'Project enabled?'

logger.debug "project.module_enabled?(:intouch) => #{project.module_enabled?(:intouch).inspect}"
logger.debug "project.active? => #{project.active?.inspect}"

project.module_enabled?(:intouch) && project.active?
end

def issue_open?
logger.debug "!issue.closed? => #{!issue.closed?.inspect}"

!issue.closed? || journal_issue_state_open?
end

def journal_issue_state_open?
logger.debug "journal.present? => #{journal.present?.inspect}"
return false unless journal.present?

logger.debug "journal.new_status => #{journal.new_status.inspect}"
logger.debug "!journal.new_status&.is_closed? => #{!journal.new_status&.is_closed?.inspect}"

journal.new_status && !journal.new_status&.is_closed?
end

private

def logger
@logger ||= Logger.new(Rails.root.join('log/intouch', 'live-checker-base.log'))
end
end
end
28 changes: 28 additions & 0 deletions lib/intouch/live/checker/private.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
module Intouch::Live::Checker
class Private
attr_reader :project, :issue

def initialize(issue, project)
@issue = issue
@project = project
end

def required?
issue.alarm? || Intouch.work_time? || notify_always?
end

def required_recipients
@required_recipients ||= always_notify_settings.keys
end

private

def notify_always?
required_recipients.present?
end

def always_notify_settings
project.active_intouch_settings['always_notify'] || {}
end
end
end
Loading

0 comments on commit b9aa187

Please sign in to comment.