forked from gdpelican/retort
-
Notifications
You must be signed in to change notification settings - Fork 3
/
plugin.rb
106 lines (85 loc) · 3.08 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# frozen_string_literal: true
# name: retort
# about: Reactions plugin for Discourse
# version: 1.3.9
# authors: Jiajun Du, pangbo. original: James Kiesel (gdpelican)
# url: https://github.com/ShuiyuanSJTU/retort
register_asset "stylesheets/common/retort.scss"
register_asset "stylesheets/mobile/retort.scss", :mobile
register_asset "stylesheets/desktop/retort.scss", :desktop
enabled_site_setting :retort_enabled
after_initialize do
module ::DiscourseRetort
PLUGIN_NAME ||= "retort".freeze
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseRetort
end
end
require_relative "app/controllers/retorts_controller.rb"
require_relative "app/models/retort.rb"
DiscoursePluginRegistry.serialized_current_user_fields << "hide_ignored_retorts"
DiscoursePluginRegistry.serialized_current_user_fields << "disable_retorts"
User.register_custom_field_type "hide_ignored_retorts", :boolean
User.register_custom_field_type "disable_retorts", :boolean
register_editable_user_custom_field :hide_ignored_retorts
register_editable_user_custom_field :disable_retorts
require_relative "lib/guardian/retort_guardian.rb"
::Guardian.prepend DiscourseRetort::RetortGuardian
require_relative "lib/override_post_serializer.rb"
::PostSerializer.prepend DiscourseRetort::OverridePostSerializer
register_stat("retort", expose_via_api: true) do
{
:last_day => Retort.where("created_at > ?", 1.days.ago).count,
"7_days" => Retort.where("created_at > ?", 7.days.ago).count,
"30_days" => Retort.where("created_at > ?", 30.days.ago).count,
:previous_30_days =>
Retort.where(
"created_at BETWEEN ? AND ?",
60.days.ago,
30.days.ago
).count,
:count => Retort.count
}
end
DiscourseRetort::Engine.routes.draw do
put "/:post_id" => "retorts#create"
delete "/:post_id" => "retorts#withdraw"
delete "/:post_id/all" => "retorts#remove"
end
Discourse::Application.routes.append do
mount ::DiscourseRetort::Engine, at: "/retorts"
end
module DiscourseRetort::OverrideUser
def self.included(klass)
klass.has_many :retorts, dependent: :destroy
end
end
::User.include(DiscourseRetort::OverrideUser)
module DiscourseRetort::OverridePost
def self.included(klass)
klass.has_many :retorts, dependent: :destroy
end
end
::Post.include(DiscourseRetort::OverridePost)
class ::Chat::ChatController
before_action :check_react, only: [:react]
def check_react
params.require(%i[emoji])
disabled_emojis = SiteSetting.retort_chat_disabled_emojis.split("|")
if disabled_emojis.include?(params[:emoji])
render json: {
error: I18n.t("retort.error.disabled_emojis")
},
status: :unprocessable_entity
end
end
end
module DiscourseRetort::OverrideTopicView
def initialize(topic_or_topic_id, user = nil, options = {})
super
@posts = @posts.includes(:retorts).includes(retorts: :user)
end
end
TopicView.prepend(DiscourseRetort::OverrideTopicView)
end