2019-05-03 06:17:27 +08:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
# The most basic attributes of a topic that we need to create a link for it.
|
|
|
|
class BasicPostSerializer < ApplicationSerializer
|
|
|
|
attributes :id,
|
|
|
|
:name,
|
|
|
|
:username,
|
|
|
|
:avatar_template,
|
|
|
|
:created_at,
|
2014-06-21 05:06:44 +08:00
|
|
|
:cooked,
|
2019-04-05 22:13:36 +08:00
|
|
|
:cooked_hidden
|
2013-06-29 01:55:34 +08:00
|
|
|
|
2019-08-19 15:20:56 +08:00
|
|
|
attr_accessor :topic_view
|
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
def name
|
2015-10-02 13:00:00 +08:00
|
|
|
object.user && object.user.name
|
2013-06-29 01:55:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def username
|
2015-10-02 13:00:00 +08:00
|
|
|
object.user && object.user.username
|
2013-06-29 01:55:34 +08:00
|
|
|
end
|
|
|
|
|
|
|
|
def avatar_template
|
2015-10-02 13:00:00 +08:00
|
|
|
object.user && object.user.avatar_template
|
2013-06-29 01:55:34 +08:00
|
|
|
end
|
|
|
|
|
2014-06-21 05:06:44 +08:00
|
|
|
def cooked_hidden
|
|
|
|
object.hidden && !scope.is_staff?
|
|
|
|
end
|
2015-09-11 08:12:40 +08:00
|
|
|
|
2014-06-21 05:06:44 +08:00
|
|
|
def include_cooked_hidden?
|
|
|
|
cooked_hidden
|
|
|
|
end
|
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
def cooked
|
2014-06-21 05:06:44 +08:00
|
|
|
if cooked_hidden
|
2013-06-29 01:55:34 +08:00
|
|
|
if scope.current_user && object.user_id == scope.current_user.id
|
2017-10-30 12:47:34 +08:00
|
|
|
I18n.t('flagging.you_must_edit', path: "/my/messages")
|
2013-06-29 01:55:34 +08:00
|
|
|
else
|
|
|
|
I18n.t('flagging.user_must_edit')
|
|
|
|
end
|
|
|
|
else
|
2019-08-06 18:28:22 +08:00
|
|
|
cooked = object.filter_quotes(@parent_post)
|
|
|
|
|
|
|
|
if scope&.user
|
2019-08-19 15:20:56 +08:00
|
|
|
|
|
|
|
# PERF: this should not run on every post, only specific ones
|
|
|
|
# also, why is this in basic post serializer?
|
|
|
|
requested_group_id = post_custom_fields['requested_group_id'].to_i
|
|
|
|
|
|
|
|
if requested_group_id > 0
|
|
|
|
group = Group
|
|
|
|
.joins('JOIN group_users ON groups.id = group_users.group_id')
|
|
|
|
.find_by(
|
|
|
|
id: object.custom_fields['requested_group_id'].to_i,
|
|
|
|
group_users: { user_id: scope.user.id, owner: true }
|
|
|
|
)
|
|
|
|
|
|
|
|
if group
|
|
|
|
cooked << <<~EOF
|
|
|
|
<hr />
|
|
|
|
<a href="#{Discourse.base_uri}/g/#{group.name}/requests">
|
|
|
|
#{I18n.t('groups.request_membership_pm.handle')}
|
|
|
|
</a>
|
|
|
|
EOF
|
|
|
|
end
|
2019-08-06 18:28:22 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
cooked
|
2013-06-29 01:55:34 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-10-31 03:45:13 +08:00
|
|
|
def include_name?
|
|
|
|
SiteSetting.enable_names?
|
|
|
|
end
|
|
|
|
|
2019-08-19 15:20:56 +08:00
|
|
|
def post_custom_fields
|
|
|
|
@post_custom_fields ||= if @topic_view
|
|
|
|
(@topic_view.post_custom_fields || {})[object.id] || {}
|
|
|
|
else
|
|
|
|
object.custom_fields
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2013-06-29 01:55:34 +08:00
|
|
|
end
|