add: atme service

This commit is contained in:
yystopf 2021-10-20 09:39:21 +08:00
parent 2273b4817e
commit 1a449d1dc6
5 changed files with 82 additions and 25 deletions

View File

@ -855,4 +855,8 @@ class ApplicationController < ActionController::Base
HotSearchKeyword.add(keyword) HotSearchKeyword.add(keyword)
end end
def find_atme_receivers
@atme_receivers = User.where(login: params[:receivers_login])
end
end end

View File

@ -9,7 +9,7 @@ class IssuesController < ApplicationController
before_action :check_project_public, only: [:index ,:show, :copy, :index_chosen, :close_issue] before_action :check_project_public, only: [:index ,:show, :copy, :index_chosen, :close_issue]
before_action :set_issue, only: [:edit, :update, :destroy, :show, :copy, :close_issue, :lock_issue] before_action :set_issue, only: [:edit, :update, :destroy, :show, :copy, :close_issue, :lock_issue]
before_action :check_token_enough, only: [:create, :update] before_action :check_token_enough, :find_atme_receivers, only: [:create, :update]
include ApplicationHelper include ApplicationHelper
include TagChosenHelper include TagChosenHelper
@ -142,6 +142,10 @@ class IssuesController < ApplicationController
end end
@issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create") @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "create")
Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}"
AtmeService.call(current_user, @atme_receivers, @issue) if @atme_receivers.size > 0
render json: {status: 0, message: "创建成", id: @issue.id} render json: {status: 0, message: "创建成", id: @issue.id}
else else
normal_status(-1, "创建失败") normal_status(-1, "创建失败")
@ -244,6 +248,10 @@ class IssuesController < ApplicationController
post_to_chain(change_type, change_token.abs, current_user.try(:login)) post_to_chain(change_type, change_token.abs, current_user.try(:login))
end end
@issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id) if @issue.previous_changes.present? @issue.create_journal_detail(change_files, issue_files, issue_file_ids, current_user&.id) if @issue.previous_changes.present?
Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}"
AtmeService.call(current_user, @atme_receivers, @issue) if @atme_receivers.size > 0
normal_status(0, "更新成功") normal_status(0, "更新成功")
else else
normal_status(-1, "更新失败") normal_status(-1, "更新失败")

View File

@ -1,6 +1,6 @@
class JournalsController < ApplicationController class JournalsController < ApplicationController
before_action :require_login, except: [:index, :get_children_journals] before_action :require_login, except: [:index, :get_children_journals]
before_action :require_profile_completed, only: [:create] before_action :require_profile_completed, :find_atme_receivers, only: [:create]
before_action :set_issue before_action :set_issue
before_action :check_issue_permission before_action :check_issue_permission
before_action :set_journal, only: [:destroy, :edit, :update] before_action :set_journal, only: [:destroy, :edit, :update]
@ -22,32 +22,35 @@ class JournalsController < ApplicationController
if notes.blank? if notes.blank?
normal_status(-1, "评论内容不能为空") normal_status(-1, "评论内容不能为空")
else else
journal_params = { ActiveRecord::Base.transaction do
journalized_id: @issue.id , journal_params = {
journalized_type: "Issue", journalized_id: @issue.id ,
user_id: current_user.id , journalized_type: "Issue",
notes: notes.to_s.strip, user_id: current_user.id ,
parent_id: params[:parent_id] notes: notes.to_s.strip,
} parent_id: params[:parent_id]
journal = Journal.new journal_params }
if journal.save journal = Journal.new journal_params
if params[:attachment_ids].present? if journal.save
params[:attachment_ids].each do |id| if params[:attachment_ids].present?
attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id) params[:attachment_ids].each do |id|
unless attachment.blank? attachment = Attachment.select(:id, :container_id, :container_type)&.find_by_id(id)
attachment.container = journal unless attachment.blank?
attachment.author_id = current_user.id attachment.container = journal
attachment.description = "" attachment.author_id = current_user.id
attachment.save attachment.description = ""
attachment.save
end
end end
end end
Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}"
AtmeService.call(current_user, @atme_receivers, journal) if @atme_receivers.size > 0
# @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "journal")
render :json => { status: 0, message: "评论成功", id: journal.id}
# normal_status(0, "评论成功")
else
normal_status(-1, "评论失败")
end end
# @issue.project_trends.create(user_id: current_user.id, project_id: @project.id, action_type: "journal")
render :json => { status: 0, message: "评论成功", id: journal.id}
# normal_status(0, "评论成功")
else
normal_status(-1, "评论失败")
end end
end end
end end

View File

@ -5,6 +5,7 @@ class PullRequestsController < ApplicationController
before_action :check_menu_authorize before_action :check_menu_authorize
before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos, :files, :commits] before_action :find_pull_request, except: [:index, :new, :create, :check_can_merge,:get_branches,:create_merge_infos, :files, :commits]
before_action :load_pull_request, only: [:files, :commits] before_action :load_pull_request, only: [:files, :commits]
before_action :find_atme_receivers, only: [:create, :update]
include TagChosenHelper include TagChosenHelper
include ApplicationHelper include ApplicationHelper
@ -61,6 +62,8 @@ class PullRequestsController < ApplicationController
@pull_request.bind_gitea_pull_request!(@gitea_pull_request[:body]["number"], @gitea_pull_request[:body]["id"]) @pull_request.bind_gitea_pull_request!(@gitea_pull_request[:body]["number"], @gitea_pull_request[:body]["id"])
SendTemplateMessageJob.perform_later('PullRequestAssigned', current_user.id, @pull_request&.id) if Site.has_notice_menu? SendTemplateMessageJob.perform_later('PullRequestAssigned', current_user.id, @pull_request&.id) if Site.has_notice_menu?
SendTemplateMessageJob.perform_later('ProjectPullRequest', current_user.id, @pull_request&.id) if Site.has_notice_menu? SendTemplateMessageJob.perform_later('ProjectPullRequest', current_user.id, @pull_request&.id) if Site.has_notice_menu?
Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}"
AtmeService.call(current_user, @atme_receivers, @pull_request) if @atme_receivers.size > 0
else else
render_error("create pull request error: #{@gitea_pull_request[:status]}") render_error("create pull request error: #{@gitea_pull_request[:status]}")
raise ActiveRecord::Rollback raise ActiveRecord::Rollback
@ -106,6 +109,8 @@ class PullRequestsController < ApplicationController
if params[:status_id].to_i == 5 if params[:status_id].to_i == 5
@issue.issue_times.update_all(end_time: Time.now) @issue.issue_times.update_all(end_time: Time.now)
end end
Rails.logger.info "[ATME] maybe to at such users: #{@atme_receivers.pluck(:login)}"
AtmeService.call(current_user, @atme_receivers, @pull_request) if @atme_receivers.size > 0
normal_status(0, "PullRequest更新成功") normal_status(0, "PullRequest更新成功")
else else
normal_status(-1, "PullRequest更新失败") normal_status(-1, "PullRequest更新失败")

View File

@ -0,0 +1,37 @@
class AtmeService < ApplicationService
Error = Class.new(StandardError)
attr_reader :user, :receivers, :atmeable
def initialize(user, receivers, atmeable)
@user = user
@receivers = receivers
@atmeable = atmeable
end
def call
Rails.logger.info "[ATME] service args: [user]=>#{user}, [receivers]=>#{receivers}, [atmeable]=>#{atmeable}"
return if atmeable.nil?
Rails.logger.info "[ATME] atmeable class name is: #{ atmeable.class.name}"
case atmeable.class.name
when 'Issue'
message_source = 'IssueAtme'
when 'PullRequest'
message_source = 'PullRequestAtme'
when 'Journal'
journal = Journal.find_by_id(atmeable.id)
if journal.present?
if journal&.issue&.pull_request.present?
@atmeable = journal&.issue&.pull_request
message_source = 'PullRequestAtme'
else
@atmeable = journal&.issue
message_source = 'IssueAtme'
end
end
else
return
end
SendTemplateMessageJob.perform_now(message_source, receivers, user.id, @atmeable.id) if Site.has_notice_menu?
end
end