This repository has been archived by the owner on Mar 6, 2018. It is now read-only.
/
user.rb
277 lines (227 loc) · 7.12 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
# coding: utf-8
class User
include Mongoid::Document
include Mongoid::Timestamps
include Mongoid::Voter
include Mongoid::Sphinx
include BaseModel
devise :invitable, :database_authenticatable, :registerable,
:recoverable, :rememberable, :validatable
field :name
field :slug
field :tagline
field :bio
field :avatar
field :website
# 是否是女人
field :girl, :type => Boolean, :default => false
# 软删除标记,1 表示已经删除
field :deleted, :type => Integer
# 不感兴趣的问题
field :muted_ask_ids, :type => Array, :default => []
# 关注的问题
field :followed_ask_ids, :type => Array, :default => []
# 回答过的问题
field :answered_ask_ids, :type => Array, :default => []
# Email 提醒的状态
field :mail_be_followed, :type => Boolean, :default => true
field :mail_new_answer, :type => Boolean, :default => true
field :mail_invite_to_ask, :type => Boolean, :default => true
field :mail_ask_me, :type => Boolean, :default => true
field :thanked_answer_ids, :type => Array, :default => []
# 邀请字段
field :invitation_token
field :invitation_sent_at, :type => DateTime
field :asks_count, :type => Integer, :default => 0
has_many :asks
field :answers_count, :type => Integer, :default => 0
has_many :answers
has_many :notifications
references_and_referenced_in_many :followed_asks, :stored_as => :array, :inverse_of => :followers, :class_name => "Ask"
references_and_referenced_in_many :followed_topics, :stored_as => :array, :inverse_of => :followers, :class_name => "Topic"
references_and_referenced_in_many :following, :class_name => 'User', :inverse_of => :followers, :index => true, :stored_as => :array
references_and_referenced_in_many :followers, :class_name => 'User', :inverse_of => :following, :index => true, :stored_as => :array
embeds_many :authorizations
has_many :logs, :class_name => "Log", :foreign_key => "target_id"
attr_accessor :password_confirmation
attr_accessible :email, :password,:name, :slug, :tagline, :bio, :avatar, :website, :girl,
:mail_new_answer, :mail_be_followed, :mail_invite_to_ask, :mail_ask_me
validates_presence_of :name, :slug
validates_uniqueness_of :slug
validates_format_of :slug, :with => /[a-z0-9\-\_]+/i
# 以下两个方法是给 redis search index 用
def avatar_small
self.avatar.small.url
end
def avatar_small_changed?
self.avatar_changed?
end
# FullText indexes
search_index(:fields => [:name,:slug],
:attributes => [:name,:slug,:avatar_small,:tagline],
:attribute_types => {:avatar_small => String, :tagline => String},
:options => {} )
redis_search_index(:title_field => :name,
:ext_fields => [:slug,:avatar_small,:tagline])
# 敏感词验证
before_validation :check_spam_words
def check_spam_words
if self.spam?("tagline")
return false
end
if self.spam?("name")
return false
end
if self.spam?("slug")
return false
end
if self.spam?("bio")
return false
end
end
def password_required?
!persisted? || password.present? || password_confirmation.present?
end
mount_uploader :avatar, AvatarUploader
def self.create_from_hash(auth)
user = User.new
user.name = auth["user_info"]["name"]
user.email = auth['user_info']['email']
if user.email.blank?
user.errors.add("Email","三方网站没有提供你的Email信息,无法直接注册。")
return user
end
user.save
user
end
before_validation :auto_slug
# 此方法用于处理开始注册是自动生成 slug, 因为没表单,只能自动
def auto_slug
if self.slug.blank?
if !self.email.blank?
self.slug = self.email.split("@")[0]
self.slug = self.slug.safe_slug
end
# 如果 slug 被 safe_slug 后是空的,就用 id 代替
if self.slug.blank?
self.slug = self.id.to_s
end
else
self.slug = self.slug.safe_slug
end
# 防止重复 slug
old_user = User.find_by_slug(self.slug)
if !old_user.blank? and old_user.id != self.id
self.slug = self.id.to_s
end
end
def auths
self.authorizations.collect { |a| a.provider }
end
def self.find_by_slug(slug)
first(:conditions => {:slug => slug})
end
# 不感兴趣问题
def ask_muted?(ask_id)
self.muted_ask_ids.include?(ask_id)
end
def ask_followed?(ask)
# Rails.logger.info { "user: #{self.inspect}" }
# Rails.logger.info { "asks: #{self.followed_asks.inspect}" }
# Rails.logger.info { "ask: #{ask.inspect}" }
self.followed_asks.include?(ask)
end
def followed?(user)
self.following.include?(user)
end
def topic_followed?(topic)
self.followed_topics.include?(topic)
end
def mute_ask(ask_id)
self.muted_ask_ids ||= []
return if self.muted_ask_ids.index(ask_id)
self.muted_ask_ids << ask_id
self.save
end
def unmute_ask(ask_id)
self.muted_ask_ids.delete(ask_id)
self.save
end
def follow_ask(ask)
ask.followers << self
ask.save
insert_follow_log("FOLLOW_ASK", ask)
end
def unfollow_ask(ask)
self.followed_asks.delete(ask)
self.save
ask.followers.delete(self)
ask.save
insert_follow_log("UNFOLLOW_ASK", ask)
end
def follow_topic(topic)
topic.followers << self
topic.save
insert_follow_log("FOLLOW_TOPIC", topic)
end
def unfollow_topic(topic)
self.followed_topics.delete(topic)
self.save
topic.followers.delete(self)
topic.save
insert_follow_log("UNFOLLOW_TOPIC", topic)
end
def follow(user)
user.followers << self
user.save
# 发送被 Follow 的邮件
UserMailer.be_followed(user.id,self.id).deliver
insert_follow_log("FOLLOW_USER", user)
end
def unfollow(user)
self.following.delete(user)
self.save
user.followers.delete(self)
user.save
insert_follow_log("UNFOLLOW_USER", user)
end
# 感谢回答
def thank_answer(answer)
self.thanked_answer_ids ||= []
return true if self.thanked_answer_ids.index(answer.id)
self.thanked_answer_ids << answer.id
self.save
insert_follow_log("THANK_ANSWER", answer, answer.ask)
end
# 软删除
# 只是把用户信息修改了
def soft_delete
# assuming you have deleted_at column added already
return false if self.deleted == 1
self.deleted = 1
self.name = "#{self.name}[已注销]"
self.email = "#{self.id}@zheye.org"
self.slug = "#{self.id}"
self.save
end
protected
def insert_follow_log(action, item, parent_item = nil)
begin
log = UserLog.new
log.user_id = self.id
log.title = self.name
log.target_id = item.id
log.action = action
if parent_item.blank?
log.target_parent_id = item.id
log.target_parent_title = item.is_a?(Ask) ? item.title : item.name
else
log.target_parent_id = parent_item.id
log.target_parent_title = parent_item.title
end
log.diff = ""
log.save
rescue Exception => e
end
end
end