forked from discourse/discourse
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathpost_readers_controller.rb
More file actions
56 lines (47 loc) · 1.32 KB
/
Copy pathpost_readers_controller.rb
File metadata and controls
56 lines (47 loc) · 1.32 KB
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
# frozen_string_literal: true
class PostReadersController < ApplicationController
requires_login
def index
post = Post.includes(topic: %i[topic_allowed_groups topic_allowed_users]).find(params[:id])
guardian.ensure_can_see!(post)
ensure_can_see_readers!(post)
readers =
User
.real
.where(staged: false)
.where.not(id: post.user_id)
.joins(:topic_users)
.where.not(topic_users: { last_read_post_number: nil })
.where(
"topic_users.topic_id = ? AND topic_users.last_read_post_number >= ?",
post.topic_id,
post.post_number,
)
readers = readers.where("admin OR moderator") if post.whisper?
readers =
readers.map do |r|
{
id: r.id,
avatar_template: r.avatar_template,
username: r.username,
username_lower: r.username_lower,
}
end
render_json_dump(post_readers: readers)
end
private
def ensure_can_see_readers!(post)
show_readers =
GroupUser
.where(user: current_user)
.joins(:group)
.where(
groups: {
id: post.topic.topic_allowed_groups.map(&:group_id),
publish_read_state: true,
},
)
.exists?
raise Discourse::InvalidAccess unless show_readers
end
end