diff --git a/app/controllers/watchers_controller.rb b/app/controllers/watchers_controller.rb index aa364261..d469e431 100644 --- a/app/controllers/watchers_controller.rb +++ b/app/controllers/watchers_controller.rb @@ -14,6 +14,7 @@ class WatchersController < ApplicationController return normal_status(2, "你还没有关注哦") unless current_user.watched?(@target) current_user.unwatch!(@target) render_ok({watchers_count: @target.watchers_count, watched: false}) + Users::SynchronizationService.call(current_user, @target, false) if target_type == "project" rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) @@ -26,6 +27,7 @@ class WatchersController < ApplicationController return normal_status(2, "你已关注了") if current_user.watched?(@target) current_user.watch!(@target) render_ok({watchers_count: @target.watchers_count, watched: true}) + Users::SynchronizationService.call(current_user, @target, true) if target_type == "project" rescue Exception => e uid_logger_error(e.message) tip_exception(e.message) diff --git a/app/services/users/synchronization_service.rb b/app/services/users/synchronization_service.rb new file mode 100644 index 00000000..032df144 --- /dev/null +++ b/app/services/users/synchronization_service.rb @@ -0,0 +1,40 @@ +class Users::SynchronizationService + def initialize(user, target, watched) + @user = user + @target = target + @watched = watched + @data = nil + end + + def call + if current_user.platform == "smartdoaction" + watcher_data + sync_to_smart_doaction + end + end + + private + + def sync_to_smart_doaction + uri = URI("http://smart.doaction.tech/smartApi/callback/mulan/project/favorite") + http = Net::HTTP.new(uri.host, uri.port) + req = Net::HTTP::Post.new(uri.path, initheader = {'Content-Type' => 'text/plain;charset=utf-8'}) + req.body = @data.to_json + res = http.request(req) + body = res.body + end + + def watcher_data + @data = { + email: @user.mail, + projectId: "#{@target.owner.login}/#{@target.name}", + operateType: @watched ==true ? 1 : 2, + projectUrl:"https://code.mulanos.cn/#{@target.owner.login}/#{@target.name}", + projectTitle: @target.name, + projectDesc: @target.description, + operateTime: Time.now.to_i, + } + end + + end + \ No newline at end of file