-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathplugin.rb
80 lines (63 loc) · 2.18 KB
/
plugin.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# frozen_string_literal: true
# name: discourse-perspective-api
# about: Mark uncivil posts using Google's Perspective API
# meta_topic_id: 98733
# version: 1.2
# authors: Erick Guan
# url: https://github.com/discourse/discourse-perspective-api
enabled_site_setting :perspective_enabled
require "excon"
require_relative "lib/discourse_perspective"
after_initialize do
module ::DiscoursePerspectiveApi
PLUGIN_NAME = "discourse-perspective-api"
end
require_relative "jobs/flag_toxic_post"
require_relative "jobs/inspect_toxic_post"
on(:post_created) do |post, params|
if SiteSetting.perspective_flag_post_min_toxicity_enable? &&
DiscoursePerspective.should_check_post?(post)
Jobs.enqueue(:flag_toxic_post, post_id: post.id)
end
end
on(:post_edited) do |post|
if SiteSetting.perspective_flag_post_min_toxicity_enable? &&
DiscoursePerspective.should_check_post?(post)
Jobs.enqueue(:flag_toxic_post, post_id: post.id)
end
end
require_dependency "application_controller"
module ::Perspective
class PostToxicityController < ::ApplicationController
requires_plugin DiscoursePerspectiveApi::PLUGIN_NAME
def post_toxicity
if current_user
RateLimiter.new(current_user, "post-toxicity", 8, 1.minute).performed!
else
RateLimiter.new(nil, "post-toxicity-#{request.remote_ip}", 6, 1.minute).performed!
end
hijack do
begin
if scores = check_content(params[:concat])
render json: scores
else
render json: {}
end
rescue => e
render json: { errors: [e.message] }, status: 403
end
end
end
private
def check_content(content)
content.present? && DiscoursePerspective.check_content_toxicity(content, current_user)
end
end
class Engine < ::Rails::Engine
engine_name DiscoursePerspectiveApi::PLUGIN_NAME
isolate_namespace Perspective
end
end
Perspective::Engine.routes.draw { post "post_toxicity" => "post_toxicity#post_toxicity" }
Discourse::Application.routes.append { mount ::Perspective::Engine, at: "/perspective" }
end