Skip to content

Commit

Permalink
Merge pull request #132 from southbridgeio/develop
Browse files Browse the repository at this point in the history
Develop
  • Loading branch information
vladislav-yashin committed Jul 2, 2021
2 parents c5d955c + cc76bc6 commit ceb99e1
Show file tree
Hide file tree
Showing 29 changed files with 351 additions and 97 deletions.
7 changes: 7 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,10 @@
# 1.6.0

* Send live notifications for closed issues
* Add lazy loading for telegram chats
* Refactor commands
* Add individual chat subscriptions for issues

# 1.5.1

* Remove sidekiq-rate-limiter from gemfile
Expand Down
36 changes: 35 additions & 1 deletion app/controllers/telegram_groups_controller.rb
Original file line number Diff line number Diff line change
@@ -1,10 +1,44 @@
class TelegramGroupsController < ApplicationController
unloadable
ALLOWED_SOURCE_TYPES = %w[Project SettingsTemplate].freeze

layout 'admin'

before_action :require_admin

def index
@telegram_group_chats =
if params[:q].present?
TelegramGroupChat.where('LOWER(title) LIKE ?', "%#{params[:q].downcase}%")
else
TelegramGroupChat.none
end

results = { results: @telegram_group_chats.map { |chat| { id: chat.id, text: chat.title } } }

respond_to do |format|
format.json { render json: results }
end
end

def show
@telegram_group = TelegramGroupChat.find(params[:id])
@priorities = IssuePriority.order(:position)
@statuses = IssueStatus.order(:position)

unless params[:source_type].in?(ALLOWED_SOURCE_TYPES)
render_404
return
end

source_class = params[:source_type].safe_constantize

@settings_source = source_class.find_by(id: params[:source_id]) || source_class.new

respond_to do |format|
format.js
end
end

def destroy
TelegramGroupChat.find(params[:id]).destroy
redirect_to action: 'plugin', id: 'redmine_intouch', controller: 'settings', tab: 'telegram'
Expand Down
14 changes: 14 additions & 0 deletions app/models/intouch/telegram_chat_subscription.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
class Intouch::TelegramChatSubscription < ActiveRecord::Base
belongs_to :issue

validates_presence_of :issue_id, :chat_id

def self.table_name
'intouch_telegram_chat_subscriptions'
end

def self.find_by_chat_id(chat_id)
# Telegram group id is always negative
where("ABS(#{table_name}.chat_id) = ?", chat_id.abs).take
end
end
6 changes: 5 additions & 1 deletion app/models/telegram_group_chat.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
class TelegramGroupChat < ActiveRecord::Base
unloadable
def self.find_by_tid(chat_id)
return nil if chat_id.nil?
# Telegram group id is always negative
where("ABS(#{table_name}.tid) = ?", chat_id.abs).take
end
end
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
<% kind = dom_id(telegram_group) %>
<h4><%= telegram_group.title %></h4>
<%= check_box_tag "intouch_settings[telegram_settings][groups][#{telegram_group.id}][only_unassigned]", '1',
settings_source.telegram_settings.try(:[], 'groups').try(:[], telegram_group.id.to_s).
try(:[], 'only_unassigned'),
id: "telegram_groups_#{telegram_group.id}_only_unassigned"
%>
<%= label_tag "telegram_groups_#{telegram_group.id}_only_unassigned",
t('intouch.project.settings.issue_update.telegram_groups.only_unassigned') %>
<%= check_box_tag "intouch_settings[telegram_settings][groups][#{telegram_group.id}][anytime]", '1',
settings_source.telegram_settings.try(:[], 'groups').try(:[], telegram_group.id.to_s).
try(:[], 'anytime'),
id: "telegram_groups_#{telegram_group.id}_anytime"
%>
<%= label_tag "telegram_groups_#{telegram_group.id}_anytime",
t('intouch.project.settings.issue_update.telegram_groups.anytime') %>
<table>
<thead>
<tr>
<th><%= t('field_status') %> \ <%= t('field_priority') %></th>
<% priorities.each do |priority| %>
<th>
<%= check_box_tag "#{kind}_priority_#{priority.id}", '1', false, class: 'prioritySelectAll',
data: { kind: kind, priority_id: priority.id }
%>
<%= label_tag "#{kind}_priority_#{priority.id}", priority.name %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% statuses.each do |status| %>
<tr>
<th>
<%= check_box_tag "#{kind}_status_#{status.id}", '1', false, class: 'statusSelectAll',
data: {kind: kind, status_id: status.id}
%>
<%= label_tag "#{kind}_status_#{status.id}", status.name %>
</th>
<% priorities.each do |priority| %>

<td>
<%= check_box_tag "intouch_settings[telegram_settings][groups][#{telegram_group.id}][#{status.id}][]", priority.id,
settings_source.telegram_settings.try(:[], 'groups').try(:[], telegram_group.id.to_s).
try(:[], status.id.to_s).try(:include?, priority.id.to_s),
class: 'intouchPriorityStatusCheckbox',
data: {kind: kind, priority_id: priority.id, status_id: status.id}
%>
</td>

<% end %>
</tr>


<% end %>
</tbody>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -11,63 +11,35 @@
<% priorities = IssuePriority.order(:position) %>
<% statuses = IssueStatus.order(:position) %>

<% TelegramGroupChat.find_each do |telegram_group| %>
<% kind = dom_id(telegram_group) %>
<h4><%= telegram_group.title %></h4>
<%= check_box_tag "intouch_settings[telegram_settings][groups][#{telegram_group.id}][only_unassigned]", '1',
settings_source.telegram_settings.try(:[], 'groups').try(:[], telegram_group.id.to_s).
try(:[], 'only_unassigned'),
id: "telegram_groups_#{telegram_group.id}_only_unassigned"
%>
<%= label_tag "telegram_groups_#{telegram_group.id}_only_unassigned",
t('intouch.project.settings.issue_update.telegram_groups.only_unassigned') %>
<%= check_box_tag "intouch_settings[telegram_settings][groups][#{telegram_group.id}][anytime]", '1',
settings_source.telegram_settings.try(:[], 'groups').try(:[], telegram_group.id.to_s).
try(:[], 'anytime'),
id: "telegram_groups_#{telegram_group.id}_anytime"
%>
<%= label_tag "telegram_groups_#{telegram_group.id}_anytime",
t('intouch.project.settings.issue_update.telegram_groups.anytime') %>
<table>
<thead>
<tr>
<th><%= t('field_status') %> \ <%= t('field_priority') %></th>
<% priorities.each do |priority| %>
<th>
<%= check_box_tag "#{kind}_priority_#{priority.id}", '1', false, class: 'prioritySelectAll',
data: { kind: kind, priority_id: priority.id }
%>
<%= label_tag "#{kind}_priority_#{priority.id}", priority.name %>
</th>
<% end %>
</tr>
</thead>
<tbody>
<% statuses.each do |status| %>
<tr>
<th>
<%= check_box_tag "#{kind}_status_#{status.id}", '1', false, class: 'statusSelectAll',
data: {kind: kind, status_id: status.id}
%>
<%= label_tag "#{kind}_status_#{status.id}", status.name %>
</th>
<% priorities.each do |priority| %>

<td>
<%= check_box_tag "intouch_settings[telegram_settings][groups][#{telegram_group.id}][#{status.id}][]", priority.id,
settings_source.telegram_settings.try(:[], 'groups').try(:[], telegram_group.id.to_s).
try(:[], status.id.to_s).try(:include?, priority.id.to_s),
class: 'intouchPriorityStatusCheckbox',
data: {kind: kind, priority_id: priority.id, status_id: status.id}
%>
</td>
<div style="padding-top: 10px">
Добавить чат:
<select style="width: 25%" class="telegram-group-select"></select>
</div>

<% end %>
</tr>
<script>
$('.telegram-group-select').select2({
ajax: {
url: '/telegram_groups',
dataType: 'json',
delay: 500
}
});

$('.telegram-group-select').on('select2:select', function (e) {
var sourceId = '<%= settings_source.id %>'
var sourceType = '<%= settings_source.class %>'

var data = e.params.data

$.get('/telegram_groups/' + data.id + '?source_id=' + sourceId + '&source_type=' + sourceType)
});
</script>

<div id="telegram-groups-list">
<% TelegramGroupChat.where(id: settings_source.telegram_settings&.[]('groups')&.keys.to_a).find_each do |telegram_group| %>
<%= render partial: 'projects/settings/intouch/issue_update/telegram_group',
locals: { telegram_group: telegram_group, priorities: priorities, statuses: statuses, settings_source: settings_source } %>
<% end %>
</div>


<% end %>
</tbody>
</table>
<% end %>
3 changes: 3 additions & 0 deletions app/views/settings_templates/edit.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<% content_for :header_tags do %>
<%= stylesheet_link_tag 'intouch.css', plugin: 'redmine_intouch' %>
<%= javascript_include_tag 'intouch.js', plugin: 'redmine_intouch' %>
<%= stylesheet_link_tag 'select2.css', plugin: 'redmine_intouch' %>
<%= javascript_include_tag 'select2.js', plugin: 'redmine_intouch' %>
<% end %>
3 changes: 3 additions & 0 deletions app/views/settings_templates/new.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,7 @@
<% content_for :header_tags do %>
<%= stylesheet_link_tag 'intouch.css', plugin: 'redmine_intouch' %>
<%= javascript_include_tag 'intouch.js', plugin: 'redmine_intouch' %>
<%= stylesheet_link_tag 'select2.css', plugin: 'redmine_intouch' %>
<%= javascript_include_tag 'select2.js', plugin: 'redmine_intouch' %>
<% end %>
2 changes: 2 additions & 0 deletions app/views/telegram_groups/show.js.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
document.getElementById('telegram-groups-list').insertAdjacentHTML('beforeend', "<%= escape_javascript(render partial: 'projects/settings/intouch/issue_update/telegram_group',
locals: { telegram_group: @telegram_group, priorities: @priorities, statuses: @statuses, settings_source: @settings_source }) %>")
5 changes: 4 additions & 1 deletion app/workers/telegram_group_live_sender_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def perform(issue_id, journal_id)

group_ids = telegram_groups_settings.select do |_k, v|
v.try(:[], issue.status_id.to_s).try(:include?, issue.priority_id.to_s)
end.keys
end.keys.to_set

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

Expand All @@ -42,6 +42,9 @@ def perform(issue_id, journal_id)

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

subscription_group_id = TelegramGroupChat.find_by_tid(Intouch::TelegramChatSubscription.find_by(issue_id: issue.id)&.chat_id)&.id
group_for_send_ids << subscription_group_id if subscription_group_id

return unless group_for_send_ids.present?

message = issue.as_html
Expand Down
14 changes: 10 additions & 4 deletions app/workers/telegram_group_message_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,16 @@ def perform(chat_id, message, issue_id, journal_id)
Intouch.handle_group_upgrade(chat) do |group|
next unless group.tid.present?

RedmineBots::Telegram::Bot::MessageSender.call(message: message,
chat_id: -group.tid,
parse_mode: 'HTML',
**Intouch::Preview::KeyboardMarkup.build_hash(issue_id, journal_id))
bot.send_message(text: message,
chat_id: -group.tid.abs,
parse_mode: 'HTML',
**Intouch::Preview::KeyboardMarkup.build_hash(issue_id, journal_id))
end
end

private

def bot
RedmineBots::Telegram.bot
end
end
5 changes: 2 additions & 3 deletions app/workers/telegram_group_sender_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,8 @@ def group

def send_message(group)
return unless group.tid.present?
RedmineBots::Telegram::Bot::MessageSender.call(message: message,
chat_id: -group.tid,
parse_mode: 'HTML')

RedmineBots::Telegram.bot.async.send_message(chat_id: -group.tid, text: message, parse_mode: 'HTML')
end

def message
Expand Down
12 changes: 8 additions & 4 deletions app/workers/telegram_live_sender_worker.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,10 @@ def perform(issue_id, journal_id, user_id)

logger.debug message

RedmineBots::Telegram::Bot::MessageSender.call(message: message,
chat_id: telegram_account.telegram_id,
parse_mode: 'HTML',
**Intouch::Preview::KeyboardMarkup.build_hash(issue_id, journal_id))
bot.send_message(text: message,
chat_id: telegram_account.telegram_id,
parse_mode: 'HTML',
**Intouch::Preview::KeyboardMarkup.build_hash(issue_id, journal_id))

logger.debug "FINISH for issue_id #{issue_id}"
rescue ActiveRecord::RecordNotFound
Expand All @@ -41,4 +41,8 @@ def perform(issue_id, journal_id, user_id)
def logger
@logger ||= Logger.new(Rails.root.join('log/intouch', 'telegram-live-sender.log'))
end

def bot
RedmineBots::Telegram.bot
end
end
2 changes: 1 addition & 1 deletion app/workers/telegram_message_sender.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,6 @@ class TelegramMessageSender
}

def perform(telegram_account_id, message, params = {})
RedmineBots::Telegram::Bot::MessageSender.call(chat_id: telegram_account_id, message: message, **params.transform_keys(&:to_sym))
RedmineBots::Telegram.bot.send_message(chat_id: telegram_account_id, text: message, **params.transform_keys(&:to_sym))
end
end
6 changes: 3 additions & 3 deletions assets/javascripts/intouch.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
$(function () {
$('.prioritySelectAll').change(function () {
$(document).on('change', '.prioritySelectAll', function () {
var priorityId = $(this).data('priorityId');
var kind = $(this).data('kind');

Expand All @@ -10,8 +10,8 @@ $(function () {
checkboxes.prop('checked', false);
}
});
$('.statusSelectAll').change(function () {

$(document).on('change', '.statusSelectAll', function () {
var statusId = $(this).data('statusId');
var kind = $(this).data('kind');

Expand Down
2 changes: 2 additions & 0 deletions assets/javascripts/select2.min.js

Large diffs are not rendered by default.

Loading

0 comments on commit ceb99e1

Please sign in to comment.