-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathplugin.rb
145 lines (115 loc) · 4.3 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
# name: discourse-topic-trade-buttons
# about: Adds one or all buttons (Sold, Purchased, Exchanged) to designated categories
# meta_topic_id: 71308
# version: 0.0.1
# authors: Janno Liivak
# frozen_string_literal: true
enabled_site_setting :topic_trade_buttons_enabled
PLUGIN_NAME ||= "discourse_topic_trade_buttons".freeze
after_initialize do
add_to_serializer(:topic_view, :category_enable_sold_button, include_condition: -> { object.topic.category }) do
object.topic.category.custom_fields["enable_sold_button"]
end
add_to_serializer(:topic_view, :category_enable_purchased_button, include_condition: -> { object.topic.category }) do
object.topic.category.custom_fields["enable_purchased_button"]
end
add_to_serializer(:topic_view, :category_enable_exchanged_button, include_condition: -> { object.topic.category }) do
object.topic.category.custom_fields["enable_exchanged_button"]
end
add_to_serializer(:topic_view, :category_enable_cancelled_button, include_condition: -> { object.topic.category }) do
object.topic.category.custom_fields["enable_cancelled_button"]
end
add_to_serializer(:topic_view, :custom_fields, include_condition: -> { object.topic.category && scope.user&.admin? }) do
object.topic.custom_fields
end
module ::DiscourseTopicTradeButtons
class Engine < ::Rails::Engine
engine_name PLUGIN_NAME
isolate_namespace DiscourseTopicTradeButtons
end
end
class DiscourseTopicTradeButtons::Trade
class << self
def sold(topic_id, user)
trade("sold", topic_id, user)
end
def purchased(topic_id, user)
trade("purchased", topic_id, user)
end
def exchanged(topic_id, user)
trade("exchanged", topic_id, user)
end
def cancelled(topic_id, user)
trade("cancelled", topic_id, user)
end
def trade(transaction, topic_id, user)
DistributedMutex.synchronize("#{PLUGIN_NAME}-#{topic_id}") do
user_id = user.id
topic = Topic.find_by_id(topic_id)
# topic must not be deleted
raise StandardError.new I18n.t("topic.topic_is_deleted") if topic.nil? || topic.trashed?
# topic must not be archived
raise StandardError.new I18n.t("topic.topic_must_be_open_to_edit") if topic.try(:archived)
topic.archived = true
i18n_transaction =
I18n
.t("topic_trading.#{transaction}", locale: (SiteSetting.default_locale || :en))
.mb_chars
.upcase
topic.title = "[#{i18n_transaction}] #{topic.title}"
topic.custom_fields["#{transaction}_at"] = Time.zone.now.iso8601
topic.save!
return topic
end
end
end
end
require_dependency "application_controller"
class DiscourseTopicTradeButtons::TradeController < ::ApplicationController
requires_plugin PLUGIN_NAME
before_action :ensure_logged_in
def sold
topic_id = params.require(:topic_id)
begin
topic = DiscourseTopicTradeButtons::Trade.sold(topic_id, current_user)
render json: { topic: topic }
rescue StandardError => e
render_json_error e.message
end
end
def purchased
topic_id = params.require(:topic_id)
begin
topic = DiscourseTopicTradeButtons::Trade.purchased(topic_id, current_user)
render json: { topic: topic }
rescue StandardError => e
render_json_error e.message
end
end
def exchanged
topic_id = params.require(:topic_id)
begin
topic = DiscourseTopicTradeButtons::Trade.exchanged(topic_id, current_user)
render json: { topic: topic }
rescue StandardError => e
render_json_error e.message
end
end
def cancelled
topic_id = params.require(:topic_id)
begin
topic = DiscourseTopicTradeButtons::Trade.cancelled(topic_id, current_user)
render json: { topic: topic }
rescue StandardError => e
render_json_error e.message
end
end
end
DiscourseTopicTradeButtons::Engine.routes.draw do
put "/sold" => "trade#sold"
put "/purchased" => "trade#purchased"
put "/exchanged" => "trade#exchanged"
put "/cancelled" => "trade#cancelled"
end
Discourse::Application.routes.append { mount ::DiscourseTopicTradeButtons::Engine, at: "/topic" }
end