Add Sync Forum

This commit is contained in:
sylor_huang@126.com 2020-10-15 18:45:50 +08:00
parent e404843eb7
commit 458e7147c5
3 changed files with 118 additions and 0 deletions

View File

@ -0,0 +1,27 @@
class SyncCreateMemoJob < ApplicationJob
queue_as :default
def perform(memo_params)
all_memos = Memo.all
memo_params.each do |m|
SyncLog.sync_log("==========mmeo_params============id:#{m}")
begin
if all_memos.exists?(m[:id])
memo = all_memos.where(id: m[:id]).first
memo.update_attributes!(m.merge(hidden: false))
else
memo = Memo.new(m.merge({forum_section_id: m[:forum_id], hidden: false}))
if memo.save!
SyncLog.sync_log("==========create_memo_success============id:#{memo.id}")
else
SyncLog.sync_log("==========create_memo_failed============id:#{m[:id]}")
end
end
rescue => e
SyncLog.sync_log("==========sync_memos_1111111_to_forge_failed============errors:#{e}")
end
end
end
end

View File

@ -0,0 +1,34 @@
require 'uri'
require 'net/http'
class SyncMemosJob < ApplicationJob
queue_as :default
def perform(forum_id, main_url)
SyncLog.sync_log("==========sync_memos_to_forge===forum_id==#{forum_id}=========")
begin
url = "#{main_url}/sync_forges/get_memos"
uri = URI.parse(url)
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = main_url.include?("https")
# headers = { "Content-Type" => "application/json" }
sync_params = {
forum_id: forum_id
}
response = http.send_request('GET', uri.path, sync_params.to_json, {'Content-Type' => 'application/json'})
# response = http.get(uri.path,{forum_id: forum_id}.to_json, headers)
if response.code == '200'
target_jsons = eval(response.body)
SyncLog.sync_log("==========sync_memos_jobs_target========#{target_jsons[:all_memos]}====")
SyncCreateMemoJob.perform_later(target_jsons[:all_memos])
# create_target(target_jsons[:all_memos])
else
SyncLog.sync_log("==========sync_memos_to_forge_failed============")
end
rescue => e
SyncLog.sync_log("==========sync_memos_to_forge_failed=____2222===========errors:#{e}")
end
end
end

View File

@ -0,0 +1,57 @@
require 'uri'
require 'net/http'
class SyncForumsRake
# 运行示例: bundle exec rails runner "SyncForumsRake.new.call()"
def call
SyncLog.sync_log("==========begin_to_sync_forums=============")
begin
# url = "https://www.trustie.net/sync_forges/get_forums" #trustie上的相关路由
main_url = "https://www.trustie.net"
url = "#{main_url}/sync_forges/get_forums"
uri = URI.parse(url)
http = Net::HTTP.new(uri.hostname, uri.port)
http.use_ssl = main_url.include?("https")
headers = { "Content-Type" => "application/json" }
response = http.get(uri.path, headers)
# response = http.send_request('GET', uri.path,{'Content-Type' => 'application/json'})
if response.code == '200'
target_jsons = eval(response.body)
create_target(target_jsons[:all_forums], main_url)
else
SyncLog.sync_log("==========sync_forums_to_forge_failed============")
end
rescue => e
SyncLog.sync_log("==========sync_forums_to_forge_failed============errors:#{e}")
end
end
def create_target(targets, main_url)
curreunt_sections = ForumSection.all
positions = curreunt_sections.pluck(:position).select { |a| a.is_a? Integer }
positions = positions.max.to_i
targets.each_with_index do |t, index|
section_params = {
id: t[:id],
title: t[:name],
position: positions + index + 1,
description: t[:description],
user_id: t[:creator_id]
}
if curreunt_sections.exists?(t[:id])
current_section = curreunt_sections.where(id: t[:id]).first
if current_section.update_attributes(section_params)
SyncMemosJob.perform_later(t[:id], main_url)
end
else
section = ForumSection.new(section_params)
if section.save!
SyncMemosJob.perform_later(section.id, main_url)
end
end
end
end
end