forked from morganstar/discourse-ethereum
-
Notifications
You must be signed in to change notification settings - Fork 2
/
plugin.rb
75 lines (54 loc) · 1.81 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
# name: discourse-aragon
# version: 0.1.4
# author: ProCourse Team
# url: https://github.com/ResearchCollective/discourse-aragon
enabled_site_setting :discourse_ethereum_enabled
register_asset "stylesheets/common.scss"
register_asset "stylesheets/mobile.scss", :mobile
require_relative "lib/ethereum"
after_initialize {
register_editable_user_custom_field("ethereum_address")
load File.expand_path("../jobs/send_tx_details.rb", __FILE__)
require_dependency "user"
User.class_eval {
def eth_enabled?
!suspended? &&
SiteSetting.discourse_ethereum_enabled &&
(SiteSetting.discourse_ethereum_all_user ||
self.groups.where(name: SiteSetting.discourse_ethereum_groups.split("|")).exists?)
end
}
require_dependency "guardian"
Guardian.class_eval {
def can_do_eth_transaction?(target_user)
return false unless authenticated?
current_user&.eth_enabled? &&
target_user&.eth_enabled? &&
target_user.custom_fields["ethereum_address"].present?
end
}
add_to_serializer(:user, :can_do_eth_transaction) {
scope.can_do_eth_transaction?(object)
}
add_to_serializer(:user, :ethereum_address) {
if scope.user&.eth_enabled? && eth_enabled_for_user
object.custom_fields["ethereum_address"].to_s.downcase
end
}
add_to_serializer(:user, :eth_enabled_for_user) {
@eth_enabled_for_user ||= object&.eth_enabled?
}
require_dependency "application_controller"
class ::EthereumController < ::ApplicationController
requires_plugin("discourse-aragon")
before_action :ensure_logged_in
def send_tx_details
tx = params.require(:tx)
Jobs.enqueue(:send_tx_details, tx.to_unsafe_hash)
render json: success_json
end
end
Discourse::Application.routes.append {
post "ethereum" => "ethereum#send_tx_details"
}
}