forked from ifmeorg/ifme
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcomment_viewers_service.rb
89 lines (69 loc) · 2.05 KB
/
comment_viewers_service.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
# frozen_string_literal: true
class CommentViewersService
attr_reader :comment, :owner, :current_user, :commentable_viewers
def self.viewers(comment, current_user)
new(comment, current_user).viewers
end
def self.viewable?(comment, current_user)
new(comment, current_user).viewable?
end
def self.deletable?(comment, current_user)
new(comment, current_user).deletable?
end
def initialize(comment, current_user)
commentable = get_commentable(comment)
@comment = comment
@owner = commentable[:user_id] && User.find(commentable[:user_id])
@commentable_viewers =
unless commentable.nil?
commentable[:viewers] || commentable.members&.pluck(:id)
end
@current_user = current_user
end
def viewers
return unless @comment.visibility == 'private' && viewable?
I18n.t('shared.comments.visible_only_between_you_and',
name: other_person.name)
end
def viewable?
!user_banned? &&
(current_user_comment? || commentable_owner? ||
viewer?)
end
def deletable?
current_user_comment? || commentable_owner?
end
private
def user_banned?
User.find_by(id: @comment.comment_by).banned
end
def other_person
return @owner unless commentable_owner?
User.find_by(id: @comment.viewers.first) ||
User.find_by(id: @comment.comment_by)
end
def commentable_owner?
if @comment.commentable_type == 'meeting'
meeting = Meeting.find_by(id: @comment.commentable_id)
return meeting&.led_by?(current_user)
end
@owner.id == @current_user.id
end
def current_user_comment?
@comment.comment_by == @current_user.id
end
def comment_viewer?
@comment.viewers.present? && @comment.viewers.include?(@current_user.id)
end
def commentable_viewer?
@comment.visibility == 'all' &&
@commentable_viewers.include?(@current_user.id)
end
def viewer?
comment_viewer? || commentable_viewer?
end
def get_commentable(comment)
model = comment.commentable_type.classify.constantize
model.find(comment.commentable_id)
end
end