forked from arkhitech/redmine_update_reminder
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #41 from centosadmin/develop
Regular and live message refactoring and tuning
- Loading branch information
Showing
36 changed files
with
789 additions
and
422 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
2 changes: 1 addition & 1 deletion
2
app/views/projects/settings/intouch/common/_assigner_groups.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
Oops, something went wrong.