Class: Libertree::Model::Comment
Class Method Summary
collapse
Instance Method Summary
collapse
search
#glimpse, #text_as_html, #text_to_nodeset
#local?, #public_id, #remote?, #server
Class Method Details
TODO: When more visibilities come, restrict this result set by visibility
132
133
134
|
# File 'lib/libertree/model/comment.rb', line 132
def self.()
self.where{ 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)
= super
account = .member.account
post = .post
post. = .time_created
post.mark_as_unread_by_all except: [account]
if account
post.mark_as_read_by account
account.subscribe_to .post
end
post.
post.save
end
|
.on_post(post, opt = {}) ⇒ Object
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]
res = res.exclude(member_id: Array(opt[:viewing_account].ignored_members).map(&:id))
end
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_create ⇒ Object
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_destroy ⇒ Object
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. - [self]
self.post. = 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
|
#delete ⇒ Object
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
|
#forests ⇒ Object
overriding method from IsRemoteOrLocal
#guid ⇒ Object
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)
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
|
#likes ⇒ Object
82
83
84
|
# File 'lib/libertree/model/comment.rb', line 82
def likes
@likes ||= CommentLike.where(comment_id: self.id).reverse_order(:id)
end
|
#member ⇒ Object
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,
}
= like..member.account
like_author = like.member.account
if(
&&
(!like_author || .id != like_author.id) &&
! .ignoring?(like.member)
)
.notify_about notification_attributes
end
end
|
#post ⇒ Object
31
32
33
|
# File 'lib/libertree/model/comment.rb', line 31
def post
@post = Post[self.post_id]
end
|
#to_hash ⇒ Object
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
|