mirror of
https://gh.wpcy.net/https://github.com/discourse/discourse.git
synced 2026-05-26 09:28:35 +08:00
Users can hide their public profile and presence information by checking
“Hide my public profile and presence features” on the
`u/{username}/preferences/interface` page. In that case, we also don't
want to return user status from the server.
This work has been started in https://github.com/discourse/discourse/pull/23946.
The current PR fixes all the remaining places in Core.
Note that the actual fix is quite simple – https://github.com/discourse/discourse/pull/24300/commits/a5802f484db92ff3f7e535ed25e1591d1f19b03b.
But we had a fair amount of duplication in the code responsible for
the user status serialization, so I had to dry that up first. The refactoring
as well as adding some additional tests is the main part of this PR.
82 lines
1.9 KiB
Ruby
Vendored
82 lines
1.9 KiB
Ruby
Vendored
# frozen_string_literal: true
|
|
|
|
module Chat
|
|
class ChatablesSerializer < ::ApplicationSerializer
|
|
attributes :users
|
|
attributes :groups
|
|
attributes :direct_message_channels
|
|
attributes :category_channels
|
|
|
|
def users
|
|
(object.users || [])
|
|
.map do |user|
|
|
{
|
|
identifier: "u-#{user.id}",
|
|
model:
|
|
::Chat::ChatableUserSerializer.new(
|
|
user,
|
|
scope: scope,
|
|
root: false,
|
|
include_status: true,
|
|
),
|
|
type: "user",
|
|
}
|
|
end
|
|
.as_json
|
|
end
|
|
|
|
def groups
|
|
(object.groups || [])
|
|
.map do |group|
|
|
{
|
|
identifier: "g-#{group.id}",
|
|
model: ::Chat::ChatableGroupSerializer.new(group, scope: scope, root: false),
|
|
type: "group",
|
|
}
|
|
end
|
|
.as_json
|
|
end
|
|
|
|
def direct_message_channels
|
|
(object.direct_message_channels || [])
|
|
.map do |channel|
|
|
{
|
|
identifier: "c-#{channel.id}",
|
|
type: "channel",
|
|
model:
|
|
::Chat::ChannelSerializer.new(
|
|
channel,
|
|
scope: scope,
|
|
root: false,
|
|
membership: channel_membership(channel.id),
|
|
),
|
|
}
|
|
end
|
|
.as_json
|
|
end
|
|
|
|
def category_channels
|
|
(object.category_channels || [])
|
|
.map do |channel|
|
|
{
|
|
identifier: "c-#{channel.id}",
|
|
type: "channel",
|
|
model:
|
|
::Chat::ChannelSerializer.new(
|
|
channel,
|
|
scope: scope,
|
|
root: false,
|
|
membership: channel_membership(channel.id),
|
|
),
|
|
}
|
|
end
|
|
.as_json
|
|
end
|
|
|
|
private
|
|
|
|
def channel_membership(channel_id)
|
|
object.memberships.find { |membership| membership.chat_channel_id == channel_id }
|
|
end
|
|
end
|
|
end
|