Class: Libertree::Model::Comment

Inherits:
Object
  • Object
show all
Extended by:
HasSearchableText
Includes:
HasDisplayText, IsRemoteOrLocal
Defined in:
lib/libertree/model/comment.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods included from HasSearchableText

search

Methods included from HasDisplayText

#glimpse, #text_as_html, #text_to_nodeset

Methods included from IsRemoteOrLocal

#local?, #public_id, #remote?, #server

Class Method Details

.comments_since_id(comment_id) ⇒ Object

TODO: When more visibilities come, restrict this result set by visibility



132
133
134
# File 'lib/libertree/model/comment.rb', line 132

def self.comments_since_id(comment_id)
  self.where{ id > comment_id }.order(:id)
end

.create(*args) ⇒ Object



65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/libertree/model/comment.rb', line 65

def self.create(*args)
  comment = super
   = comment.member.
  post = comment.post

  post.time_commented = comment.time_created
  post.mark_as_unread_by_all  except: []
  if 
    post.mark_as_read_by 
    .subscribe_to comment.post
  end
  post.notify_about_comment comment
  post.save

  comment
end

.on_post(post, opt = {}) ⇒ Object

Parameters:

  • opt (Hash) (defaults to: {})

    options for restricting the comment set returned

  • opts (Hash)

    a customizable set of options



140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
# File 'lib/libertree/model/comment.rb', line 140

def self.on_post(post, opt = {})
  res = Comment.where(post_id: post.id)
  if opt[:viewing_account]
    # Array() because sometimes opt[:viewing_account] is a strange nil-like object for some reason.
    # Ramaze weirdness?
    res = res.exclude(member_id: Array(opt[:viewing_account].ignored_members).map(&:id))
  end

  # reverse ordering is required in order to get the *last* n
  # comments, rather than the first few when using :limit
  res = res.reverse_order(:id)
  res = res.where{ id >= opt[:from_id].to_i }  if opt[:from_id]
  res = res.where{ id < opt[:to_id].to_i }     if opt[:to_id]
  res = res.limit(opt[:limit].to_i)            if opt[:limit]
  res.all.reverse
end

Instance Method Details

#after_createObject



10
11
12
13
14
15
16
17
18
19
20
21
22
23
# File 'lib/libertree/model/comment.rb', line 10

def after_create
  super

  if self.local? && self.post.distribute?
    Libertree::Model::Job.create_for_forests(
      {
        task: 'request:COMMENT',
        params: { 'comment_id' => self.id, }
      },
      *self.forests
    )
  end
  Libertree::Embedder.autoembed(self.text)
end

#before_destroyObject



35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/libertree/model/comment.rb', line 35

def before_destroy
  if self.post
    remaining_comments = self.post.comments - [self]
    self.post.time_commented = remaining_comments.map(&:time_created).max
  end

  if self.local? && self.post.distribute?
    Libertree::Model::Job.create_for_forests(
      {
        task: 'request:COMMENT-DELETE',
        params: { 'comment_id' => self.id, }
      },
      *self.forests
    )
  end
  super
end

#deleteObject

TODO: the correct method to call is “destroy”



54
55
56
57
# File 'lib/libertree/model/comment.rb', line 54

def delete
  self.before_destroy
  super
end

#delete_cascade(force = false) ⇒ Object

NOTE: deletion is NOT distributed when force=true



60
61
62
63
# File 'lib/libertree/model/comment.rb', line 60

def delete_cascade(force=false)
  self.before_destroy  unless force
  DB.dbh[ "SELECT delete_cascade_comment(?)", self.id ].get
end

#forestsObject

overriding method from IsRemoteOrLocal



113
114
115
116
117
118
119
# File 'lib/libertree/model/comment.rb', line 113

def forests
  if self.post.remote?
    self.post.server.forests
  else
    Libertree::Model::Forest.all_local_is_member
  end
end

#guidObject



157
158
159
160
161
# File 'lib/libertree/model/comment.rb', line 157

def guid
  server = self.member.server
  origin = server ? server.domain : Server.own_domain
  "xmpp:#{origin}?;node=/comments;item=#{self.public_id}"
end

#like_by(member) ⇒ Object



103
104
105
106
107
108
109
110
# File 'lib/libertree/model/comment.rb', line 103

def like_by(member)
  # take advantage of cached self.likes
  if self.likes.is_a? Array
    self.likes.find {|like| like.member.id == member.id}
  else
    CommentLike[ member_id: member.id, comment_id: self.id ]
  end
end

#likesObject



82
83
84
# File 'lib/libertree/model/comment.rb', line 82

def likes
  @likes ||= CommentLike.where(comment_id: self.id).reverse_order(:id)
end

#memberObject

TODO: DB: association



26
27
28
# File 'lib/libertree/model/comment.rb', line 26

def member
  @member = Member[self.member_id]
end

#notify_about_like(like) ⇒ Object



86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
# File 'lib/libertree/model/comment.rb', line 86

def notify_about_like(like)
  notification_attributes = {
    'type'         => 'comment-like',
    'comment_like_id' => like.id,
  }
  local_comment_author = like.comment.member.
  like_author = like.member.

  if(
    local_comment_author &&
    (!like_author || local_comment_author.id != like_author.id) &&
    ! local_comment_author.ignoring?(like.member)
  )
    local_comment_author.notify_about notification_attributes
  end
end

#postObject

TODO: DB: association



31
32
33
# File 'lib/libertree/model/comment.rb', line 31

def post
  @post = Post[self.post_id]
end

#to_hashObject



121
122
123
124
125
126
127
128
129
# File 'lib/libertree/model/comment.rb', line 121

def to_hash
  {
    'id'           => self.id,
    'time_created' => self.time_created,
    'time_updated' => self.time_updated,
    'text'         => self.text,
    'post_id'      => self.post_id,
  }
end