forked from Gitlink/forgeplus
add: notice system api
This commit is contained in:
parent
877a20350d
commit
465a3a67a5
|
@ -1,110 +1,80 @@
|
|||
class Users::MessagesController < Users::BaseController
|
||||
before_action :private_user_resources!
|
||||
before_action :find_receivers, only: [:create]
|
||||
|
||||
def index
|
||||
data = {
|
||||
"receiver": 2,
|
||||
"type": @message_type,
|
||||
"unread_total": 5,
|
||||
"unread_notification": 3,
|
||||
"unread_atme": 2,
|
||||
"records": [
|
||||
{
|
||||
"id": 1,
|
||||
"sender": 5,
|
||||
"receiver": 2,
|
||||
"content": "Atme Message Content 1",
|
||||
"status": 1,
|
||||
"type": 2,
|
||||
"source": "PullRequestAtme",
|
||||
"notification_url": "http://www.baidu.com",
|
||||
"created_at": "2021-09-09 14:34:40"
|
||||
},
|
||||
{
|
||||
"id": 2,
|
||||
"sender": 4,
|
||||
"receiver": 2,
|
||||
"content": "Atme Message Content 2",
|
||||
"status": 2,
|
||||
"type": 2,
|
||||
"source": "IssueAtme",
|
||||
"notification_url": "http://www.baidu.com",
|
||||
"created_at": "2021-09-09 14:34:40"
|
||||
},
|
||||
{
|
||||
"id": 3,
|
||||
"sender": -1,
|
||||
"receiver": 2,
|
||||
"content": "Notification Message Content 1",
|
||||
"status": 1,
|
||||
"type": 1,
|
||||
"source": "IssueDelete",
|
||||
"notification_url": "http://www.baidu.com",
|
||||
"created_at": "2021-09-09 14:34:40"
|
||||
},
|
||||
{
|
||||
"id": 4,
|
||||
"sender": -1,
|
||||
"receiver": 2,
|
||||
"content": "Notification Message Content 2",
|
||||
"status": 2,
|
||||
"type": 1,
|
||||
"source": "IssueChanged",
|
||||
"notification_url": "http://www.baidu.com",
|
||||
"created_at": "2021-09-09 14:34:40"
|
||||
},
|
||||
{
|
||||
"id": 5,
|
||||
"sender": -1,
|
||||
"receiver": 2,
|
||||
"content": "Notification Message Content 3",
|
||||
"status": 2,
|
||||
"type": 1,
|
||||
"source": "ProjectJoined",
|
||||
"notification_url": "http://www.baidu.com",
|
||||
"created_at": "2021-09-09 14:34:40"
|
||||
}
|
||||
],
|
||||
"records_count": 5,
|
||||
"page_num": 1,
|
||||
"total_page_size": 1,
|
||||
"page_size": 10
|
||||
}
|
||||
result = [1, "请求成功", data]
|
||||
limit = params[:limit] || params[:per_page]
|
||||
limit = (limit.to_i.zero? || limit.to_i > 15) ? 15 : limit.to_i
|
||||
page = params[:page].to_i.zero? ? 1 : params[:page].to_i
|
||||
result = Notice::Read::ListService.call(observed_user.id, message_type, message_status, page, limit)
|
||||
return render_error if result.nil?
|
||||
puts result
|
||||
@data = result[2].stringify_keys
|
||||
|
||||
@data = result[2]
|
||||
end
|
||||
|
||||
def create
|
||||
return render_forbidden unless %w(3).include(@message_type)
|
||||
return render_forbidden unless %w(atme).include?(params[:type])
|
||||
case params[:type]
|
||||
when 'atme'
|
||||
Notice::Write::CreateAtmeForm.new(atme_params).validate!
|
||||
result = Notice::Write::CreateService.call(@receivers.pluck(:id), '发送了一个@我消息', base_url, "IssueAtme", 2)
|
||||
return render_error if result.nil?
|
||||
end
|
||||
render_ok
|
||||
rescue Exception => e
|
||||
uid_logger_error(e.message)
|
||||
tip_exception(e.message)
|
||||
end
|
||||
|
||||
def delete
|
||||
return render_forbidden unless %w(2).include(@message_type)
|
||||
return render_forbidden unless %w(atme).include?(params[:type])
|
||||
result = Notice::Write::DeleteService.call(params[:ids], observed_user.id)
|
||||
return render_error if result.nil?
|
||||
|
||||
render_ok
|
||||
rescue Exception => e
|
||||
uid_logger_error(e.message)
|
||||
tip_exception(e.message)
|
||||
end
|
||||
|
||||
def read
|
||||
render_ok
|
||||
return render_forbidden unless %w(notification atme).include?(params[:type])
|
||||
result = Notice::Write::ChangeStatusService.call(params[:ids], observed_user.id)
|
||||
if result.nil?
|
||||
render_error
|
||||
else
|
||||
render_ok
|
||||
end
|
||||
rescue Exception => e
|
||||
uid_logger_error(e.message)
|
||||
tip_exception(e.message)
|
||||
end
|
||||
|
||||
private
|
||||
def message_type
|
||||
@message_type = begin
|
||||
case params[:type]
|
||||
when "notification"
|
||||
1
|
||||
when "atme"
|
||||
2
|
||||
when "notification" then 1
|
||||
when "atme" then 2
|
||||
else
|
||||
-1
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def message_status
|
||||
@message_status = begin
|
||||
case params[:status]
|
||||
when "1" then 1
|
||||
else
|
||||
2
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def atme_params
|
||||
params.permit(:atmeable_type, :atmeable_id, receivers_login: [])
|
||||
end
|
||||
|
||||
def message_params
|
||||
{
|
||||
sender: current_user.id,
|
||||
|
@ -114,8 +84,8 @@ class Users::MessagesController < Users::BaseController
|
|||
}
|
||||
end
|
||||
|
||||
|
||||
def find_receiver
|
||||
@receiver = User.find_by(login: params[:receiver_login])
|
||||
def find_receivers
|
||||
@receivers = User.where(login: params[:receivers_login])
|
||||
return render_not_found if @receivers.size == 0
|
||||
end
|
||||
end
|
|
@ -0,0 +1,23 @@
|
|||
class Notice::Write::CreateAtmeForm
|
||||
include ActiveModel::Model
|
||||
|
||||
attr_accessor :receivers_login, :atmeable_type, :atmeable_id
|
||||
|
||||
validate :check_receivers
|
||||
|
||||
def check_receivers
|
||||
receivers_login.each do |login|
|
||||
receiver = User.find_by(login: login) || User.find_by_id(login)
|
||||
raise 'receivers_login值无效.' unless receiver.present?
|
||||
end
|
||||
end
|
||||
|
||||
def check_atmeable
|
||||
begin
|
||||
raise 'atmeable对象无效.' unless atmeable_type.constantize.find_by_id(atmeable_id).present?
|
||||
rescue => exception
|
||||
raise 'atmeable对象无效.'
|
||||
end
|
||||
end
|
||||
|
||||
end
|
|
@ -1,4 +1,4 @@
|
|||
class Notice::ClientService < ApplicationService
|
||||
class Notice::Read::ClientService < ApplicationService
|
||||
attr_reader :url, :params
|
||||
|
||||
def initialize(options={})
|
||||
|
@ -7,7 +7,7 @@ class Notice::ClientService < ApplicationService
|
|||
end
|
||||
|
||||
def post(url, params={})
|
||||
puts "[notice][POST] request params: #{params}"
|
||||
puts "[notice][read][POST] request params: #{params}"
|
||||
conn.post do |req|
|
||||
req.url = full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
|
@ -15,7 +15,7 @@ class Notice::ClientService < ApplicationService
|
|||
end
|
||||
|
||||
def get(url, params={})
|
||||
puts "[notice][GET] request params: #{params}"
|
||||
puts "[notice][read][GET] request params: #{params}"
|
||||
conn.get do |req|
|
||||
req.url full_url(url, 'get')
|
||||
params.each_pair do |key, value|
|
||||
|
@ -25,31 +25,31 @@ class Notice::ClientService < ApplicationService
|
|||
end
|
||||
|
||||
def delete(url, params={})
|
||||
puts "[notice][DELETE] request params: #{params}"
|
||||
puts "[notice][read][DELETE] request params: #{params}"
|
||||
conn.delete do |req|
|
||||
req.url full_url(url)
|
||||
reb.body = params[:data].to_json
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def patch(url, params={})
|
||||
puts "[notice][PATCH] request params: #{params}"
|
||||
puts "[notice][read][PATCH] request params: #{params}"
|
||||
conn.patch do |req|
|
||||
req.url full_url(url)
|
||||
reb.body = params[:data].to_json
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def put(url, params={})
|
||||
puts "[notice][PUT] request params: #{params}"
|
||||
puts "[notice][read][PUT] request params: #{params}"
|
||||
conn.put do |req|
|
||||
req.url full_url(url)
|
||||
reb.body = params[:data].to_json
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
#private
|
||||
def conn
|
||||
def conn
|
||||
@client ||= begin
|
||||
Faraday.new(url: domain) do |req|
|
||||
req.request :url_encoded
|
||||
|
@ -66,7 +66,7 @@ class Notice::ClientService < ApplicationService
|
|||
end
|
||||
|
||||
def domain
|
||||
Notice.notice_config[:domain]
|
||||
Notice.notice_config[:read_domain]
|
||||
end
|
||||
|
||||
def platform
|
||||
|
@ -74,25 +74,25 @@ class Notice::ClientService < ApplicationService
|
|||
end
|
||||
|
||||
def api_url
|
||||
[domain, base_url].join('')
|
||||
[domain, base_url, "/#{platform}"].join('')
|
||||
end
|
||||
|
||||
def full_url(api_rest, action='post')
|
||||
url = [api_url, api_rest].join('').freeze
|
||||
url = action === 'get' ? url : URI.escape(url)
|
||||
url = URI.escape(url) unless url.ascii_only?
|
||||
puts "[notice] request url: #{url}"
|
||||
puts "[notice][read] request url: #{url}"
|
||||
return url
|
||||
end
|
||||
|
||||
def log_error(status, body)
|
||||
puts "[notice] status: #{status}"
|
||||
puts "[notice] body: #{body&.force_encoding('UTF-8')}"
|
||||
puts "[notice][read] status: #{status}"
|
||||
puts "[notice][read] body: #{body}"
|
||||
end
|
||||
|
||||
def render_response(response)
|
||||
status = response.status
|
||||
body = response&.body
|
||||
body = JSON.parse(response&.body)
|
||||
|
||||
log_error(status, body)
|
||||
|
||||
|
@ -100,8 +100,8 @@ class Notice::ClientService < ApplicationService
|
|||
if body["code"] == 1
|
||||
return [body["code"], body["message"], body["data"]]
|
||||
else
|
||||
puts "[notice][ERROR] code: #{body["code"]}"
|
||||
puts "[notice][ERROR] message: #{body["message"]}"
|
||||
puts "[notice][read][ERROR] code: #{body["code"]}"
|
||||
puts "[notice][read][ERROR] message: #{body["message"]}"
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,25 @@
|
|||
class Notice::Read::CountService < Notice::Read::ClientService
|
||||
attr_accessor :receiver, :type
|
||||
|
||||
def initialize(receiver, type=-1)
|
||||
@receiver = receiver
|
||||
@type = type
|
||||
end
|
||||
|
||||
def call
|
||||
result = get(url, request_params)
|
||||
response = render_response(result)
|
||||
end
|
||||
|
||||
private
|
||||
def request_params
|
||||
{
|
||||
receiver: receiver,
|
||||
type: type
|
||||
}.stringify_keys
|
||||
end
|
||||
|
||||
def url
|
||||
"/count".freeze
|
||||
end
|
||||
end
|
|
@ -0,0 +1,40 @@
|
|||
class Notice::Read::ListService < Notice::Read::ClientService
|
||||
attr_accessor :receiver, :type, :status, :page, :size
|
||||
|
||||
def initialize(receiver, type=-1, status=2, page=1, size=15)
|
||||
@receiver = receiver
|
||||
@type = type
|
||||
@status = status
|
||||
@page = page
|
||||
@size = size
|
||||
end
|
||||
|
||||
def call
|
||||
result = get(url, request_params)
|
||||
response = render_response(result)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_status
|
||||
case status
|
||||
when 1 then 1
|
||||
else
|
||||
2
|
||||
end
|
||||
end
|
||||
|
||||
def request_params
|
||||
{
|
||||
receiver: receiver,
|
||||
page: page,
|
||||
show: request_status,
|
||||
size: size,
|
||||
type: type
|
||||
}.stringify_keys
|
||||
end
|
||||
|
||||
def url
|
||||
"/list".freeze
|
||||
end
|
||||
end
|
|
@ -0,0 +1,29 @@
|
|||
class Notice::Write::ChangeStatusService < Notice::Write::ClientService
|
||||
attr_accessor :notification_ids, :receiver, :status
|
||||
|
||||
def initialize(notification_ids, receiver, status=2)
|
||||
@notification_ids = notification_ids
|
||||
@receiver = receiver
|
||||
@status = status
|
||||
end
|
||||
|
||||
def call
|
||||
result = put("", request_params)
|
||||
response = render_response(result)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_notification_ids
|
||||
notification_ids.join(",")
|
||||
end
|
||||
|
||||
def request_params
|
||||
Hash.new.merge(data: {
|
||||
notificationIds: request_notification_ids,
|
||||
receiver: receiver,
|
||||
status: status
|
||||
}.stringify_keys)
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,108 @@
|
|||
class Notice::Write::ClientService < ApplicationService
|
||||
attr_reader :url, :params
|
||||
|
||||
def initialize(options={})
|
||||
@url = options[:url]
|
||||
@params = options[:params]
|
||||
end
|
||||
|
||||
def post(url, params={})
|
||||
puts "[notice][write][POST] request params: #{params}"
|
||||
conn.post do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def get(url, params={})
|
||||
puts "[notice][write][GET] request params: #{params}"
|
||||
conn.get do |req|
|
||||
req.url full_url(url, 'get')
|
||||
params.each_pair do |key, value|
|
||||
req.params["#{key}"] = value
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
def delete(url, params={})
|
||||
puts "[notice][write][DELETE] request params: #{params}"
|
||||
conn.delete do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def patch(url, params={})
|
||||
puts "[notice][write][PATCH] request params: #{params}"
|
||||
conn.patch do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
def put(url, params={})
|
||||
puts "[notice][write][PUT] request params: #{params}"
|
||||
conn.put do |req|
|
||||
req.url full_url(url)
|
||||
req.body = params[:data].to_json
|
||||
end
|
||||
end
|
||||
|
||||
#private
|
||||
def conn
|
||||
@client ||= begin
|
||||
Faraday.new(url: domain) do |req|
|
||||
req.request :url_encoded
|
||||
req.headers['Content-Type'] = 'application/json'
|
||||
req.adapter Faraday.default_adapter
|
||||
end
|
||||
end
|
||||
|
||||
@client
|
||||
end
|
||||
|
||||
def base_url
|
||||
Notice.notice_config[:base_url]
|
||||
end
|
||||
|
||||
def domain
|
||||
Notice.notice_config[:write_domain]
|
||||
end
|
||||
|
||||
def platform
|
||||
Notice.notice_config[:platform]
|
||||
end
|
||||
|
||||
def api_url
|
||||
[domain, base_url, "/#{platform}"].join('')
|
||||
end
|
||||
|
||||
def full_url(api_rest, action='post')
|
||||
url = [api_url, api_rest].join('').freeze
|
||||
url = action === 'get' ? url : URI.escape(url)
|
||||
url = URI.escape(url) unless url.ascii_only?
|
||||
puts "[notice][write] request url: #{url}"
|
||||
return url
|
||||
end
|
||||
|
||||
def log_error(status, body)
|
||||
puts "[notice][write] status: #{status}"
|
||||
puts "[notice][write] body: #{body}"
|
||||
end
|
||||
|
||||
def render_response(response)
|
||||
status = response.status
|
||||
body = JSON.parse(response&.body)
|
||||
|
||||
log_error(status, body)
|
||||
|
||||
if status == 200
|
||||
if body["code"] == 1
|
||||
return [body["code"], body["message"], body["data"]]
|
||||
else
|
||||
puts "[notice][write][ERROR] code: #{body["code"]}"
|
||||
puts "[notice][write][ERROR] message: #{body["message"]}"
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
|
@ -0,0 +1,37 @@
|
|||
class Notice::Write::CreateService < Notice::Write::ClientService
|
||||
attr_accessor :receivers, :sender, :content, :notification_url, :source, :extra, :type
|
||||
|
||||
def initialize(receivers, content, notification_url, source, type=1, extra={},sender=-1)
|
||||
@receivers = receivers
|
||||
@sender = sender
|
||||
@content = content
|
||||
@notification_url = notification_url
|
||||
@source = source
|
||||
@extra = extra
|
||||
@type = type
|
||||
end
|
||||
|
||||
def call
|
||||
result = post("", request_params)
|
||||
response = render_response(result)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_receivers
|
||||
receivers.join(",")
|
||||
end
|
||||
|
||||
def request_params
|
||||
Hash.new.merge(data: {
|
||||
receivers: request_receivers,
|
||||
sender: sender,
|
||||
content: content,
|
||||
notification_url: notification_url,
|
||||
source: source,
|
||||
extra: extra.to_json.to_s,
|
||||
type: type
|
||||
}.stringify_keys)
|
||||
end
|
||||
|
||||
end
|
|
@ -0,0 +1,27 @@
|
|||
class Notice::Write::DeleteService < Notice::Write::ClientService
|
||||
attr_accessor :notification_ids, :receiver
|
||||
|
||||
def initialize(notification_ids, receiver)
|
||||
@notification_ids = notification_ids
|
||||
@receiver = receiver
|
||||
end
|
||||
|
||||
def call
|
||||
result = delete("", request_params)
|
||||
response = render_response(result)
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def request_notification_ids
|
||||
notification_ids.join(",")
|
||||
end
|
||||
|
||||
def request_params
|
||||
Hash.new.merge(data: {
|
||||
notificationIds: request_notification_ids,
|
||||
receiver: receiver
|
||||
}.stringify_keys)
|
||||
end
|
||||
|
||||
end
|
|
@ -58,7 +58,9 @@ default: &default
|
|||
base_url: '/api/v1'
|
||||
|
||||
notice:
|
||||
domain: ''
|
||||
platform: ''
|
||||
write_domain: ''
|
||||
read_domain: ''
|
||||
base_url: ''
|
||||
|
||||
production:
|
||||
|
|
|
@ -302,11 +302,13 @@ Rails.application.routes.draw do
|
|||
# resource :unread_message_info, only: [:show]
|
||||
|
||||
# 通知中心
|
||||
resources :messages, only: [:index, :create, :delete] do
|
||||
resources :messages, only: [:index, :create] do
|
||||
collection do
|
||||
post :read
|
||||
|
||||
end
|
||||
end
|
||||
delete 'messages', to: 'messages#delete'
|
||||
end
|
||||
|
||||
resources :tidings, only: [:index]
|
||||
|
|
Loading…
Reference in New Issue