forked from poshboytl/peatio
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmember.rb
135 lines (106 loc) · 2.82 KB
/
member.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
class Member < ActiveRecord::Base
acts_as_taggable
has_many :orders
has_many :accounts
has_many :withdraws
has_many :fund_sources
has_many :deposits
has_many :api_tokens
has_many :two_factors
has_one :id_document
has_one :sms_token
delegate :activated?, to: :two_factors, prefix: true, allow_nil: true
delegate :verified?, to: :id_document, prefix: true, allow_nil: true
delegate :verified?, to: :sms_token, prefix: true
has_many :authentications, dependent: :destroy
validates :sn, presence: true
before_validation :generate_sn
alias_attribute :full_name, :name
after_create :touch_accounts
class << self
def from_auth(auth_hash)
member = locate_auth(auth_hash) || locate_email(auth_hash) || create_from_auth(auth_hash)
member
end
private
def locate_auth(auth_hash)
Authentication.locate(auth_hash).try(:member)
end
def locate_email(auth_hash)
member = find_by_email(auth_hash['info']['email'])
return nil unless member
member.add_auth(auth_hash)
member
end
def create_from_auth(auth_hash)
member = create(email: auth_hash['info']['email'], activated: false)
member.add_auth(auth_hash)
member.send_activation
member
end
end
def self.admins
Figaro.env.admin.split(',')
end
def active
self.update_column(:activated, true)
end
def admin?
@is_admin ||= self.class.admins.include?(self.email)
end
def add_auth(auth_hash)
authentications.build_auth(auth_hash).save
end
def trigger(event, data)
AMQPQueue.enqueue(:pusher_member, {member_id: id, event: event, data: data})
end
def notify(event, data)
::Pusher["private-#{sn}"].trigger_async event, data
end
def to_s
"#{name || email} - #{sn}"
end
def to_muut
{
id: sn,
displayname: name,
email: email,
avatar: gravatar,
is_admin: admin?
}
end
def gravatar
"//gravatar.com/avatar/" + Digest::MD5.hexdigest(email.strip.downcase) + "?d=retro"
end
def initial?
name? and !name.empty?
end
def get_account(currency)
account = accounts.with_currency(currency.to_sym).first
if account.nil?
touch_accounts
account = accounts.with_currency(currency.to_sym).first
end
account
end
alias :ac :get_account
def touch_accounts
less = Currency.codes - self.accounts.map(&:currency).map(&:to_sym)
less.each do |code|
self.accounts.create(currency: code, balance: 0, locked: 0)
end
end
def identity
Identity.find(authentications.find_by_provider('identity').uid)
end
def send_activation
Activation.create(member: self)
end
private
def generate_sn
self.sn and return
begin
self.sn = "PEA#{ROTP::Base32.random_base32(8).upcase}TIO"
end while Member.where(:sn => self.sn).any?
end
end