Module: Inkwell::ActsAsInkwellUser::InstanceMethods

Includes:
Common, Constants
Defined in:
lib/acts_as_inkwell_user/base.rb

Instance Method Summary collapse

Methods included from Common

#category_class, #check_post, #check_user, #community_class, #community_id_attr, #get_class_for_item_type, #get_item_type, #get_owner_type, #post_class, #user_class, #user_id_attr

Instance Method Details

#approve_invitation_request(options = {}) ⇒ Object



355
356
357
358
359
360
# File 'lib/acts_as_inkwell_user/base.rb', line 355

def approve_invitation_request(options = {})
  options.symbolize_keys!
  community = options[:community]
  user = options[:user]
  community.accept_invitation_request :user => user, :admin => self
end

#ban(options = {}) ⇒ Object



380
381
382
383
384
385
# File 'lib/acts_as_inkwell_user/base.rb', line 380

def ban(options = {})
  options.symbolize_keys!
  in_community = options[:in_community]
  user = options[:user]
  in_community.ban_user :user => user, :admin => self
end

#blogline(options = {}) ⇒ Object



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
# File 'lib/acts_as_inkwell_user/base.rb', line 32

def blogline(options = {})
  options.symbolize_keys!
  last_shown_obj_id = options[:last_shown_obj_id]
  limit = options[:limit] || 10
  for_user = options[:for_user]
  category = options[:category]

  if category
    child_categories = ActiveSupport::JSON.decode category.child_ids
    category_ids = [category.id] + child_categories
    if last_shown_obj_id
      blog_items_categories = ::Inkwell::BlogItemCategory.where(:category_id => category_ids).where("blog_item_created_at < ?", Inkwell::BlogItem.find(last_shown_obj_id).created_at).order("blog_item_created_at DESC").limit(limit)
    else
      blog_items_categories = ::Inkwell::BlogItemCategory.where(:category_id => category_ids).order("blog_item_created_at DESC").limit(limit)
    end

    blog_items_ids = []
    blog_items_categories.each do |record|
      blog_items_ids << record.blog_item_id
    end
    blog_items = ::Inkwell::BlogItem.where(:id => blog_items_ids, :owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC")
  else
    if last_shown_obj_id
      blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where("created_at < ?", Inkwell::BlogItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
    else
      blog_items = ::Inkwell::BlogItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(limit)
    end
  end

  result = []
  blog_items.each do |item|
    if item.item_type == ItemTypes::COMMENT
      blog_obj = ::Inkwell::Comment.find item.item_id
    else
      blog_obj = post_class.find item.item_id
    end

    blog_obj.item_id_in_line = item.id
    blog_obj.is_reblog_in_blogline = item.is_reblog

    if for_user
      blog_obj.is_reblogged = for_user.reblog? blog_obj
      blog_obj.is_favorited = for_user.favorite? blog_obj
    end

    result << blog_obj
  end
  result
end

#can_send_post_to_community?(community) ⇒ Boolean

Returns:

  • (Boolean)


408
409
410
411
412
413
414
# File 'lib/acts_as_inkwell_user/base.rb', line 408

def can_send_post_to_community?(community)
  relation = ::Inkwell::CommunityUser.where(user_id_attr => self.id, community_id_attr => community.id).first
  return false unless relation
  return false if relation.muted
  return false unless relation.user_access == CommunityAccessLevels::WRITE
  true
end

#communities_rowObject



93
94
95
96
97
98
99
100
101
102
103
# File 'lib/acts_as_inkwell_user/base.rb', line 93

def communities_row
  user_id = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
  community_id = "#{::Inkwell::Engine::config.community_table.to_s.singularize}_id"

  relations = ::Inkwell::CommunityUser.where user_id => self.id
  result = []
  relations.each do |relation|
    result << relation.send(community_id)
  end
  result
end

#create_category(options = {}) ⇒ Object

wrappers for category methods



447
448
449
450
451
452
# File 'lib/acts_as_inkwell_user/base.rb', line 447

def create_category(options = {})
  options.symbolize_keys!
  options[:owner_id] = self.id
  options[:owner_type] = OwnerTypes::USER
  category_class.create options
end

#create_comment(options = {}) ⇒ Object



82
83
84
85
86
87
88
89
90
91
# File 'lib/acts_as_inkwell_user/base.rb', line 82

def create_comment(options = {})
  options.symbolize_keys!
  raise "for_object should be passed" unless options[:for_object]
  raise "comment body should be passed" unless options[:body]
  for_object = options[:for_object]
  options[:topmost_obj_id] = for_object.id
  options[:topmost_obj_type] = get_item_type for_object
  options.delete :for_object
  self.comments.create options
end

#destroy_processingObject



459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
# File 'lib/acts_as_inkwell_user/base.rb', line 459

def destroy_processing
  if ::Inkwell::Engine::config.respond_to?('community_table')
    raise "there is community where this user is owner. Change their owner before destroy this user." unless community_class.where(:owner_id => self.id).empty?

    communities_relations = ::Inkwell::CommunityUser.where user_id_attr => self.id
    communities_relations.each do |relation|
      community = community_class.find relation.send(community_id_attr)
      if relation.active
        if relation.user_access == CommunityAccessLevels::WRITE
          community.writer_count -= 1
        end
        community.admin_count -= 1 if relation.is_admin
        community.muted_count -= 1 if relation.muted
        community.user_count -= 1
      else
        community.banned_count -= 1 if relation.banned
        community.invitation_count -= 1 if relation.asked_invitation
      end

      community.save
    end

    ::Inkwell::CommunityUser.delete_all user_id_attr => self.id
  end

  if ::Inkwell::Engine::config.respond_to?('category_table')
    categories = category_class.where :owner_id => self.id, :owner_type => OwnerTypes::USER
    category_ids = []
    categories.each do |category|
      category_ids << category.id
    end
    category_class.delete_all :id => category_ids
    ::Inkwell::BlogItemCategory.delete_all :category_id => category_ids
  end
end

#favorite(obj) ⇒ Object



105
106
107
108
109
110
111
112
113
114
# File 'lib/acts_as_inkwell_user/base.rb', line 105

def favorite(obj)
  return if self.favorite? obj

  FavoriteItem.create :item_id => obj.id, :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_type => get_item_type(obj)

  users_ids_who_favorite_it = ActiveSupport::JSON.decode obj.users_ids_who_favorite_it
  users_ids_who_favorite_it << self.id
  obj.users_ids_who_favorite_it = ActiveSupport::JSON.encode users_ids_who_favorite_it
  obj.save
end

#favorite?(obj) ⇒ Boolean

Returns:

  • (Boolean)


116
117
118
# File 'lib/acts_as_inkwell_user/base.rb', line 116

def favorite?(obj)
  FavoriteItem.where(:item_id => obj.id, :item_type => get_item_type(obj), :owner_id => self.id, :owner_type => OwnerTypes::USER).first ? true : false
end

#favoriteline(options = {}) ⇒ Object



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
# File 'lib/acts_as_inkwell_user/base.rb', line 131

def favoriteline(options = {})
  options.symbolize_keys!
  last_shown_obj_id = options[:last_shown_obj_id]
  limit = options[:limit] || 10
  for_user = options[:for_user]

  if last_shown_obj_id
    favorites = ::Inkwell::FavoriteItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where("created_at < ?", Inkwell::FavoriteItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
  else
    favorites = ::Inkwell::FavoriteItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(limit)
  end

  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
  result = []
  favorites.each do |item|
    if item.item_type == ItemTypes::COMMENT
      favorited_obj = ::Inkwell::Comment.find item.item_id
    else
      favorited_obj = post_class.find item.item_id
    end

    favorited_obj.item_id_in_line = item.id

    if for_user
      favorited_obj.is_reblogged = for_user.reblog? favorited_obj
      favorited_obj.is_favorited = for_user.favorite? favorited_obj
    end

    result << favorited_obj
  end
  result
end

#follow(user) ⇒ Object



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
# File 'lib/acts_as_inkwell_user/base.rb', line 164

def follow(user)
  raise "user tries to follow already followed user" if self.follow? user
  raise "user tries to follow himself." if self == user

  ::Inkwell::Following.create :follower_id => self.id, :followed_id => user.id

  self.following_count += 1
  self.save

  user.follower_count += 1
  user.save

  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
  ::Inkwell::BlogItem.where(:owner_id => user.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(10).each do |blog_item|
    if blog_item.is_reblog
      item_class = blog_item.item_type == ItemTypes::COMMENT ? ::Inkwell::Comment : post_class
      next if item_class.find(blog_item.item_id).send(user_id_attr) == self.id
    end

    item = TimelineItem.where(:item_id => blog_item.item_id, :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_type => blog_item.item_type).first
    if item
      item.has_many_sources = true unless item.has_many_sources
      sources = ActiveSupport::JSON.decode item.from_source
      if blog_item.is_reblog
        sources << Hash['user_id' => user.id, 'type' => 'reblog']
      else
        sources << Hash['user_id' => user.id, 'type' => 'following']
      end
      item.from_source = ActiveSupport::JSON.encode sources
      item.save
    else
      sources = []
      if blog_item.is_reblog
        sources << Hash['user_id' => user.id, 'type' => 'reblog']
      else
        sources << Hash['user_id' => user.id, 'type' => 'following']
      end
      ::Inkwell::TimelineItem.create :item_id => blog_item.item_id, :item_type => blog_item.item_type, :owner_id => self.id, :owner_type => OwnerTypes::USER,
                                     :from_source => ActiveSupport::JSON.encode(sources), :created_at => blog_item.created_at
    end
  end
end

#follow?(user) ⇒ Boolean

Returns:

  • (Boolean)


231
232
233
# File 'lib/acts_as_inkwell_user/base.rb', line 231

def follow?(user)
  ::Inkwell::Following.exists? :follower_id => self.id, :followed_id => user.id
end

#followers_rowObject



235
236
237
238
239
240
241
242
# File 'lib/acts_as_inkwell_user/base.rb', line 235

def followers_row
  records = ::Inkwell::Following.where :followed_id => self.id
  result = []
  records.each do |rec|
    result << rec.follower_id
  end
  result
end

#followings_rowObject



244
245
246
247
248
249
250
251
# File 'lib/acts_as_inkwell_user/base.rb', line 244

def followings_row
  records = ::Inkwell::Following.where :follower_id => self.id
  result = []
  records.each do |rec|
    result << rec.followed_id
  end
  result
end

#get_categoriesObject



454
455
456
# File 'lib/acts_as_inkwell_user/base.rb', line 454

def get_categories
  category_class.get_categories_for :object => self, :type => OwnerTypes::USER
end

#grant_admin_permissions(options = {}) ⇒ Object



431
432
433
434
435
436
# File 'lib/acts_as_inkwell_user/base.rb', line 431

def grant_admin_permissions(options = {})
  options.symbolize_keys!
  to_user = options[:to_user]
  in_community = options[:in_community]
  in_community.add_admin :user => to_user, :admin => self
end

#join(open_community) ⇒ Object

wrappers for community methods



346
347
348
349
# File 'lib/acts_as_inkwell_user/base.rb', line 346

def join(open_community)
  raise "it is impossible to join private community. use invitation request to do it." unless open_community.public
  open_community.add_user :user => self
end

#kick(options = {}) ⇒ Object



373
374
375
376
377
378
# File 'lib/acts_as_inkwell_user/base.rb', line 373

def kick(options = {})
  options.symbolize_keys!
  from_community = options[:from_community]
  user = options[:user]
  from_community.remove_user :user => user, :admin => self
end

#leave(community) ⇒ Object



369
370
371
# File 'lib/acts_as_inkwell_user/base.rb', line 369

def leave(community)
  community.remove_user :user => self
end

#mute(options = {}) ⇒ Object



394
395
396
397
398
399
# File 'lib/acts_as_inkwell_user/base.rb', line 394

def mute(options = {})
  options.symbolize_keys!
  in_community = options[:in_community]
  user = options[:user]
  in_community.mute_user :user => user, :admin => self
end

#reblog(obj) ⇒ Object



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
# File 'lib/acts_as_inkwell_user/base.rb', line 253

def reblog(obj)
  return if self.reblog? obj
  raise "User tries to reblog his post." if self.id == obj.user_id

  item_type = get_item_type(obj)

  user_id_attr = "#{::Inkwell::Engine::config.user_table.to_s.singularize}_id"
  BlogItem.create :item_id => obj.id, :is_reblog => true, :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_type => item_type

  users_ids_who_reblog_it = ActiveSupport::JSON.decode obj.users_ids_who_reblog_it
  users_ids_who_reblog_it << self.id
  obj.users_ids_who_reblog_it = ActiveSupport::JSON.encode users_ids_who_reblog_it
  obj.save

  self.followers_row.each do |user_id|
    next if obj.send(user_id_attr) == user_id
    item = TimelineItem.where(:item_id => obj.id, :owner_id => user_id, :owner_type => OwnerTypes::USER, :item_type => item_type).first
    if item
      item.has_many_sources = true unless item.has_many_sources
      sources = ActiveSupport::JSON.decode item.from_source
      sources << Hash['user_id' => self.id, 'type' => 'reblog']
      item.from_source = ActiveSupport::JSON.encode sources
      item.save
    else
      encode_sources = ActiveSupport::JSON.encode [Hash['user_id' => self.id, 'type' => 'reblog']]
      TimelineItem.create :item_id => obj.id, :created_at => obj.created_at, :owner_id => user_id, :owner_type => OwnerTypes::USER, :from_source => encode_sources, :item_type => item_type
    end
  end
end

#reblog?(obj) ⇒ Boolean

Returns:

  • (Boolean)


283
284
285
# File 'lib/acts_as_inkwell_user/base.rb', line 283

def reblog?(obj)
  BlogItem.exists? :item_id => obj.id, :owner_id => self.id, :owner_type => OwnerTypes::USER, :is_reblog => true, :item_type => get_item_type(obj)
end

#reject_invitation_request(options = {}) ⇒ Object



362
363
364
365
366
367
# File 'lib/acts_as_inkwell_user/base.rb', line 362

def reject_invitation_request(options = {})
  options.symbolize_keys!
  community = options[:community]
  user = options[:user]
  community.reject_invitation_request :user => user, :admin => self
end

#remove_post_from_community(options = {}) ⇒ Object



424
425
426
427
428
429
# File 'lib/acts_as_inkwell_user/base.rb', line 424

def remove_post_from_community(options = {})
  options.symbolize_keys!
  from_community = options[:from_community]
  post = options[:post]
  from_community.remove_post :post => post, :user => self
end

#request_invitation(community) ⇒ Object



351
352
353
# File 'lib/acts_as_inkwell_user/base.rb', line 351

def request_invitation(community)
  community.create_invitation_request(self)
end

#revoke_admin_permissions(options = {}) ⇒ Object



438
439
440
441
442
443
# File 'lib/acts_as_inkwell_user/base.rb', line 438

def revoke_admin_permissions(options = {})
  options.symbolize_keys!
  user = options[:user]
  in_community = options[:in_community]
  in_community.remove_admin :user => user, :admin => self
end

#send_post_to_community(options = {}) ⇒ Object



416
417
418
419
420
421
422
# File 'lib/acts_as_inkwell_user/base.rb', line 416

def send_post_to_community(options = {})
  options.symbolize_keys!
  to_community = options[:to_community]
  post = options[:post]
  raise "this user have no permissions to send post to this community" unless self.can_send_post_to_community? to_community
  to_community.add_post :post => post, :user => self
end

#timeline(options = {}) ⇒ Object



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
# File 'lib/acts_as_inkwell_user/base.rb', line 310

def timeline(options = {})
  options.symbolize_keys!
  last_shown_obj_id = options[:last_shown_obj_id]
  limit = options[:limit] || 10
  for_user = options[:for_user]

  if last_shown_obj_id
    timeline_items = ::Inkwell::TimelineItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where("created_at < ?", Inkwell::TimelineItem.find(last_shown_obj_id).created_at).order("created_at DESC").limit(limit)
  else
    timeline_items = ::Inkwell::TimelineItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).order("created_at DESC").limit(limit)
  end

  post_class = Object.const_get ::Inkwell::Engine::config.post_table.to_s.singularize.capitalize
  result = []
  timeline_items.each do |item|
    if item.item_type == ItemTypes::COMMENT
      timeline_obj = ::Inkwell::Comment.find item.item_id
    else
      timeline_obj = post_class.find item.item_id
    end

    timeline_obj.item_id_in_line = item.id
    timeline_obj.from_sources_in_timeline = item.from_source

    if for_user
      timeline_obj.is_reblogged = for_user.reblog? timeline_obj
      timeline_obj.is_favorited = for_user.favorite? timeline_obj
    end

    result << timeline_obj
  end
  result
end

#unban(options = {}) ⇒ Object



387
388
389
390
391
392
# File 'lib/acts_as_inkwell_user/base.rb', line 387

def unban(options = {})
  options.symbolize_keys!
  in_community = options[:in_community]
  user = options[:user]
  in_community.unban_user :user => user, :admin => self
end

#unfavorite(obj) ⇒ Object



120
121
122
123
124
125
126
127
128
129
# File 'lib/acts_as_inkwell_user/base.rb', line 120

def unfavorite(obj)
  return unless self.favorite? obj

  ::Inkwell::FavoriteItem.where(:item_id => obj.id, :item_type => get_item_type(obj), :owner_id => self.id, :owner_type => OwnerTypes::USER).destroy_all

  users_ids_who_favorite_it = ActiveSupport::JSON.decode obj.users_ids_who_favorite_it
  users_ids_who_favorite_it.delete self.id
  obj.users_ids_who_favorite_it = ActiveSupport::JSON.encode users_ids_who_favorite_it
  obj.save
end

#unfollow(user) ⇒ Object



208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
# File 'lib/acts_as_inkwell_user/base.rb', line 208

def unfollow(user)
  raise "user tries to unfollow not followed user" unless self.follow? user
  raise "user tries to unfollow himself." if self == user

  ::Inkwell::Following.delete_all :follower_id => self.id, :followed_id => user.id

  self.following_count -= 1
  self.save

  user.follower_count -= 1
  user.save

  timeline_items = ::Inkwell::TimelineItem.where(:owner_id => self.id, :owner_type => OwnerTypes::USER).where "from_source like '%{\"user_id\":#{user.id}%'"
  timeline_items.delete_all :has_many_sources => false
  timeline_items.each do |item|
    from_source = ActiveSupport::JSON.decode item.from_source
    from_source.delete_if { |rec| rec['user_id'] == user.id }
    item.from_source = ActiveSupport::JSON.encode from_source
    item.has_many_sources = false if from_source.size < 2
    item.save
  end
end

#unmute(options = {}) ⇒ Object



401
402
403
404
405
406
# File 'lib/acts_as_inkwell_user/base.rb', line 401

def unmute(options = {})
  options.symbolize_keys!
  in_community = options[:in_community]
  user = options[:user]
  in_community.unmute_user :user => user, :admin => self
end

#unreblog(obj) ⇒ Object



287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
# File 'lib/acts_as_inkwell_user/base.rb', line 287

def unreblog(obj)
  return unless self.reblog? obj
  raise "User tries to unreblog his post." if self.id == obj.user_id

  item_type = get_item_type(obj)

  users_ids_who_reblog_it = ActiveSupport::JSON.decode obj.users_ids_who_reblog_it
  users_ids_who_reblog_it.delete self.id
  obj.users_ids_who_reblog_it = ActiveSupport::JSON.encode users_ids_who_reblog_it
  obj.save

  ::Inkwell::BlogItem.delete_all :owner_id => self.id, :owner_type => OwnerTypes::USER, :item_id => obj.id, :is_reblog => true, :item_type => item_type

  TimelineItem.delete_all :owner_id => self.followers_row, :owner_type => OwnerTypes::USER, :has_many_sources => false, :item_id => obj.id, :item_type => item_type
  TimelineItem.where(:owner_id => self.followers_row, :owner_type => OwnerTypes::USER, :item_id => obj.id, :item_type => item_type).each do |item|
      sources = ActiveSupport::JSON.decode item.from_source
      sources.delete Hash['user_id' => self.id, 'type' => 'reblog']
      item.has_many_sources = false if sources.size < 2
      item.from_source = ActiveSupport::JSON.encode sources
      item.save
  end
end