-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
drupal_user.rb
210 lines (174 loc) · 4.47 KB
/
drupal_user.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
class DrupalUser < ActiveRecord::Base
attr_accessible :title, :body, :name, :pass, :mail, :mode, :sort, :threshold, :theme, :signature, :signature_format, :created, :access, :login, :status, :timezone, :language, :picture, :init, :data, :timezone_id, :timezone_name
## User status can be:
# 0: banned
# 1: normal
# 5: moderated
self.table_name = 'users'
self.primary_key = 'uid'
has_many :node, foreign_key: 'uid'
has_many :drupal_profile_values, foreign_key: 'uid'
has_many :node_selections, foreign_key: :user_id
has_many :answers, foreign_key: :uid
has_many :answer_selections, foreign_key: :user_id
has_many :comments, foreign_key: 'uid'
def user
User.where(id: self.id).first
end
def bio
user.bio
end
def username
name
end
def using_new_site?
!User.find_by(username: name).nil?
end
# Rails-style adaptors:
def created_at
Time.at(created)
end
# End rails-style adaptors
def role
user&.role
end
def moderate
self.status = 5
save
update_user_status(5)
# user is logged out next time they access current_user in a controller; see application controller
self
end
def unmoderate
self.status = 1
save
update_user_status(1)
self
end
def ban
self.status = 0
decrease_likes_banned
save
update_user_status(0)
# user is logged out next time they access current_user in a controller; see application controller
self
end
def unban
self.status = 1
increase_likes_unbanned
save
update_user_status(1)
self
end
def email
mail
end
def update_user_status(status)
u = self.user
u.status = status
u.save!
end
def first_time_poster
user.first_time_poster
end
def likes
NodeSelection.where(user_id: uid, liking: true)
end
def like_count
NodeSelection.where(user_id: uid, liking: true).count
end
def liked_notes
Node.includes(:node_selections)
.references(:node_selections)
.where("type = 'note' AND node_selections.liking = ? AND node_selections.user_id = ? AND node.status = 1", true, uid)
.order('node_selections.nid DESC')
end
def liked_pages
NodeSelection.where("status = 1 AND user_id = ? AND liking = ? AND (node.type = 'page' OR node.type = 'tool' OR node.type = 'place')", uid, true)
.includes(:node)
.references(:node)
.collect(&:node)
.reverse
end
# last node
def last
Node.limit(1)
.where(uid: uid)
.order('changed DESC')
.first
end
def profile_values
drupal_profile_values
end
def notes
user.notes
end
def note_count
Node.where(status: 1, uid: uid, type: 'note').count
end
def node_count
Node.where(status: 1, uid: uid).count + Revision.where(uid: uid).count
end
# accepts array of tag names (strings)
def notes_for_tags(tagnames)
all_nodes = Node.order('nid DESC').where(type: 'note', status: 1, uid: uid)
node_ids = []
all_nodes.each do |node|
node.tags.each do |tag|
tagnames.each do |tagname|
node_ids << node.nid if tag.name == tagname
end
end
end
Node.find(node_ids.uniq, order: 'nid DESC')
end
def user_tags
self.user.user_tags
end
def tags(limit = 10)
self.user.tags(limit)
end
def tagnames(limit = 20, defaults = true)
self.user.tagnames(limit, defaults)
end
def tag_counts
tags = {}
Node.order('nid DESC').where(type: 'note', status: 1, uid: uid).limit(20).each do |node|
node.tags.each do |tag|
if tags[tag.name]
tags[tag.name] += 1
else
tags[tag.name] = 1
end
end
end
tags
end
def migrate
u = User.new(username: name,
id: uid,
email: mail,
openid_identifier: '//old.publiclab.org/user/' + uid.to_s + '/identity')
u.persistence_token = rand(100_000_000)
if u.save(validate: false) # <= because validations checks for conflict with existing drupal_user.name
key = u.generate_reset_key
PasswordResetMailer.reset_notify(u, key)
return true
else
return false
end
end
private
def decrease_likes_banned
node_selections.each do |selection|
selection.node.cached_likes = selection.node.cached_likes - 1
selection.node.save!
end
end
def increase_likes_unbanned
node_selections.each do |selection|
selection.node.cached_likes = selection.node.cached_likes + 1
selection.node.save!
end
end
end