Class: Libertree::Model::Notification

Inherits:
Object
  • Object
show all
Defined in:
lib/libertree/model/notification.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.mark_seen_for_account(account, notification_ids) ⇒ Object



60
61
62
63
64
65
66
67
68
# File 'lib/libertree/model/notification.rb', line 60

def self.(, notification_ids)
  if notification_ids[0] == 'all'
    self.where(account_id: .id).update(seen: true)
  else
    self.where(account_id: .id, id: notification_ids).
      update(seen: true)
  end
  .dirty
end

.mark_seen_for_account_and_comment_id(account, comment_ids) ⇒ Object



33
34
35
36
37
38
39
40
# File 'lib/libertree/model/notification.rb', line 33

def self.(, comment_ids)
  data = comment_ids.map {|id| %|{"type":"comment","comment_id":#{id.to_i}}| }
  data += CommentLike.where(comment_id: comment_ids).
    map {|like| %|{"type":"comment-like","comment_like_id":#{like.id}}| }

  self.where(account_id: .id, data: data).update(seen: true)
  .dirty
end

.mark_seen_for_account_and_message(account, message) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
# File 'lib/libertree/model/notification.rb', line 48

def self.(, message)
  self.where(
    Sequel.lit(
      "account_id = ? AND data = ?",
      .id,
      %|{"type":"message","message_id":#{message.id}}|
    )
  ).update(seen: true)

  .dirty
end

.mark_seen_for_account_and_post(account, post) ⇒ Object



42
43
44
45
46
# File 'lib/libertree/model/notification.rb', line 42

def self.(, post)
  data = post.likes.map {|like| %|{"type":"post-like","post_like_id":#{like.id.to_i}}| }
  self.where(account_id: .id, data: data).update(seen: true)
  .dirty
end

.mark_unseen_for_account(account, notification_ids) ⇒ Object



70
71
72
73
74
# File 'lib/libertree/model/notification.rb', line 70

def self.(, notification_ids)
  self.where(account_id: .id, id: notification_ids).
    update(seen: false)
  .dirty
end

Instance Method Details

#accountObject



6
7
8
# File 'lib/libertree/model/notification.rb', line 6

def 
  @account ||= [self.]
end

#dataObject



10
11
12
13
14
# File 'lib/libertree/model/notification.rb', line 10

def data
  if val = super
    JSON.parse val
  end
end

#subjectObject



16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/libertree/model/notification.rb', line 16

def subject
  @subject ||= case self.data['type']
  when 'comment'
    Libertree::Model::Comment[ self.data['comment_id'] ]
  when 'comment-like'
    Libertree::Model::CommentLike[ self.data['comment_like_id'] ]
  when 'message'
    Libertree::Model::Message[ self.data['message_id'] ]
  when 'post-like'
    Libertree::Model::PostLike[ self.data['post_like_id'] ]
  when 'springing'
    Libertree::Model::PoolPost[ self.data['pool_post_id'] ]
  when 'mention', 'group-post'
    Libertree::Model::Post[ self.data['post_id'] ]
  end
end