-
Notifications
You must be signed in to change notification settings - Fork 1.8k
/
Copy pathuser.rb
348 lines (299 loc) · 10.9 KB
/
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
class UniqueUsernameValidator < ActiveModel::Validator
def validate(record)
if DrupalUser.find_by(name: record.username) && record.openid_identifier.nil?
record.errors[:base] << 'That username is already taken. If this is your username, you can simply log in to this site.'
end
end
end
class User < ActiveRecord::Base
self.table_name = 'rusers'
attr_accessible :username, :email, :password, :password_confirmation, :openid_identifier, :key, :photo, :photo_file_name, :bio
alias_attribute :name, :username
acts_as_authentic do |c|
c.openid_required_fields = %i[nickname email]
c.validates_format_of_email_field_options = { with: /@/ }
c.crypto_provider = Authlogic::CryptoProviders::Sha512
end
has_attached_file :photo, styles: { thumb: '200x200#', medium: '500x500#', large: '800x800#' },
url: '/system/profile/photos/:id/:style/:basename.:extension'
#:path => ":rails_root/public/system/images/photos/:id/:style/:basename.:extension"
do_not_validate_attachment_file_type :photo_file_name
# validates_attachment_content_type :photo_file_name, :content_type => %w(image/jpeg image/jpg image/png)
# this doesn't work... we should have a uid field on User
# has_one :drupal_users, :conditions => proc { ["drupal_users.name = ?", self.username] }
has_many :images, foreign_key: :uid
has_many :node, foreign_key: 'uid'
has_many :user_tags, foreign_key: 'uid', dependent: :destroy
has_many :active_relationships, class_name: 'Relationship', foreign_key: 'follower_id', dependent: :destroy
has_many :passive_relationships, class_name: 'Relationship', foreign_key: 'followed_id', dependent: :destroy
has_many :following_users, through: :active_relationships, source: :followed
has_many :followers, through: :passive_relationships, source: :follower
validates_with UniqueUsernameValidator, on: :create
validates_format_of :username, with: /\A[A-Za-z\d_\-]+\z/
before_create :create_drupal_user
before_save :set_token
after_destroy :destroy_drupal_user
def self.search(query)
User.where('MATCH(username, bio) AGAINST(?)', query)
end
def create_drupal_user
self.bio ||= ''
if drupal_user.nil?
drupal_user = DrupalUser.new(name: username,
pass: rand(100_000_000_000_000_000_000),
mail: email,
mode: 0,
sort: 0,
threshold: 0,
theme: '',
signature: '',
signature_format: 0,
created: DateTime.now.to_i,
access: DateTime.now.to_i,
login: DateTime.now.to_i,
status: 1,
timezone: nil,
language: '',
picture: '',
init: '',
data: nil,
timezone_id: 0,
timezone_name: '')
drupal_user.save!
self.id = drupal_user.uid
else
self.id = DrupalUser.find_by(name: username).uid
end
end
def destroy_drupal_user
drupal_user.destroy
end
def set_token
self.token = SecureRandom.uuid if self.token.nil?
end
# this is ridiculous. We need to store uid in this model.
# ...migration is in progress. start getting rid of these calls...
def drupal_user
DrupalUser.find_by(name: username)
end
def notes
Node.where(uid: uid)
.where(type: 'note')
.order('created DESC')
end
def coauthored_notes
coauthored_tag = "with:" + self.name.downcase
Node.where(status: 1, type: "note")
.includes(:revision, :tag)
.references(:term_data, :node_revisions)
.where('term_data.name = ? OR term_data.parent = ?', coauthored_tag.to_s , coauthored_tag.to_s)
end
def generate_reset_key
# invent a key and save it
key = ''
20.times do
key += [*'a'..'z'].sample
end
self.reset_key = key
key
end
def uid
drupal_user.uid
end
def title
self.username
end
def path
"/profile/#{self.username}"
end
def lat
self.get_value_of_power_tag('lat')
end
def lon
self.get_value_of_power_tag('lon')
end
# we can revise/improve this for m2m later...
def has_role(r)
role == r
end
def tags(limit = 10)
Tag.where('name in (?)', tagnames).limit(limit)
end
def tagnames(limit = 20, defaults = true)
tagnames = []
Node.order('nid DESC').where(type: 'note', status: 1, uid: self.id).limit(limit).each do |node|
tagnames += node.tags.collect(&:name)
end
tagnames += ['balloon-mapping', 'spectrometer', 'near-infrared-camera', 'thermal-photography', 'newsletter'] if tagnames.empty? && defaults
tagnames.uniq
end
def has_tag(tagname)
user_tags.collect(&:value).include?(tagname)
end
# power tags have "key:value" format, and should be searched with a "key:*" wildcard
def has_power_tag(key)
tids = self.user_tags.where('value LIKE ?' , key + ':%').collect(&:id)
!tids.blank?
end
def get_value_of_power_tag(key)
tname = self.user_tags.where('value LIKE ?' , key + ':%')
tvalue = tname.first.name.partition(':').last
tvalue
end
def subscriptions(type = :tag)
if type == :tag
TagSelection.where(user_id: uid,
following: true)
end
end
def following(tagname)
tids = Tag.where(name: tagname).collect(&:tid)
!TagSelection.where(following: true, tid: tids, user_id: uid).empty?
end
def add_to_lists(lists)
lists.each do |list|
WelcomeMailer.add_to_list(self, list)
end
end
def weekly_note_tally(span = 52)
weeks = {}
(0..span).each do |week|
weeks[span - week] = Node.select(:created)
.where( uid: drupal_user.uid,
type: 'note',
status: 1,
created: Time.now.to_i - week.weeks.to_i..Time.now.to_i - (week - 1).weeks.to_i)
.count
end
weeks
end
def weekly_comment_tally(span = 52)
weeks = {}
(0..span).each do |week|
weeks[span - week] = Comment.select(:timestamp)
.where( uid: drupal_user.uid,
status: 1,
timestamp: Time.now.to_i - week.weeks.to_i..Time.now.to_i - (week - 1).weeks.to_i)
.count
end
weeks
end
def note_streak(span = 365)
days = {}
streak = 0
note_count = 0
(0..span).each do |day|
days[day] = Node.select(:created)
.where( uid: drupal_user.uid,
type: 'note',
status: 1,
created: Time.now.midnight.to_i - day.days.to_i..Time.now.midnight.to_i - (day - 1).days.to_i)
.count
break if days[day] == 0
streak += 1
note_count += days[day]
end
[streak, note_count]
end
def wiki_edit_streak(span = 365)
days = {}
streak = 0
wiki_edit_count = 0
(0..span).each do |day|
days[day] = Revision.joins(:node)
.where( uid: drupal_user.uid,
status: 1,
timestamp: Time.now.midnight.to_i - day.days.to_i..Time.now.midnight.to_i - (day - 1).days.to_i)
.where('node.type != ?', 'note')
.count
break if days[day] == 0
streak += 1
wiki_edit_count += days[day]
end
[streak, wiki_edit_count]
end
def comment_streak(span = 365)
days = {}
streak = 0
comment_count = 0
(0..span).each do |day|
days[day] = Comment.select(:timestamp)
.where( uid: drupal_user.uid,
status: 1,
timestamp: Time.now.midnight.to_i - day.days.to_i..Time.now.midnight.to_i - (day - 1).days.to_i)
.count
break if days[day] == 0
streak += 1
comment_count += days[day]
end
[streak, comment_count]
end
def streak(span = 365)
note_streak = self.note_streak(span)
wiki_edit_streak = self.wiki_edit_streak(span)
comment_streak = self.comment_streak(span)
streak_count = [note_streak[1], wiki_edit_streak[1], comment_streak[1]]
streak = [note_streak[0], wiki_edit_streak[0], comment_streak[0]]
[streak.max, streak_count]
end
def barnstars
NodeTag.includes(:node, :tag)
.references(:term_data)
.where('type = ? AND term_data.name LIKE ? AND node.uid = ?', 'note', 'barnstar:%', uid)
end
def photo_path(size = :medium)
photo.url(size)
end
def first_time_poster
notes.where(status: 1).count == 0
end
def follow(other_user)
active_relationships.create(followed_id: other_user.id)
end
def unfollow(other_user)
active_relationships.where(followed_id: other_user.id).first.destroy
end
def following?(other_user)
following_users.include?(other_user)
end
def profile_image
if photo_file_name
puts photo_path(:thumb)
photo_path(:thumb)
else
"https://www.gravatar.com/avatar/#{Digest::MD5.hexdigest(email)}"
end
end
def questions
Node.questions.where(status: 1, uid: id)
end
def content_followed_in_past_period(time_period)
self.node.where("created >= #{time_period.to_i} AND changed >= #{time_period.to_i}")
end
def social_link(site)
if has_power_tag(site)
user_name = get_value_of_power_tag(site)
link = "https://#{site}.com/#{user_name}"
return link
end
nil
end
private
def map_openid_registration(registration)
self.email = registration['email'] if email.blank?
self.username = registration['nickname'] if username.blank?
end
def self.find_by_username_case_insensitive(username)
User.where('lower(username) = ?', username.downcase).first
end
# all uses who've posted a node, comment, or answer in the given period
def self.contributor_count_for(start_time,end_time)
notes = Node.where(type: 'note', status: 1, created: start_time.to_i..end_time.to_i).pluck(:uid)
answers = Answer.where(created_at: start_time..end_time).pluck(:uid)
questions = Node.questions.where(status: 1, created: start_time.to_i..end_time.to_i).pluck(:uid)
comments = Comment.where(timestamp: start_time.to_i..end_time.to_i).pluck(:uid)
revisions = Revision.where(timestamp: start_time.to_i..end_time.to_i).pluck(:uid)
contributors = (notes+answers+questions+comments+revisions).compact.uniq.length
contributors
end
end