mirror of
https://github.com/discourse/discourse.git
synced 2025-05-22 21:52:43 +08:00
make code climate a bit happier
This commit is contained in:
@ -106,23 +106,15 @@ class ApplicationController < ActionController::Base
|
|||||||
|
|
||||||
# If we are rendering HTML, preload the session data
|
# If we are rendering HTML, preload the session data
|
||||||
def preload_json
|
def preload_json
|
||||||
|
|
||||||
# We don't preload JSON on xhr or JSON request
|
# We don't preload JSON on xhr or JSON request
|
||||||
return if request.xhr?
|
return if request.xhr?
|
||||||
|
|
||||||
if guardian.current_user
|
preload_anonymous_data
|
||||||
guardian.current_user.sync_notification_channel_position
|
|
||||||
|
if current_user
|
||||||
|
preload_current_user_data
|
||||||
|
current_user.sync_notification_channel_position
|
||||||
end
|
end
|
||||||
|
|
||||||
store_preloaded("site", Site.cached_json(guardian))
|
|
||||||
|
|
||||||
if current_user.present?
|
|
||||||
store_preloaded("currentUser", MultiJson.dump(CurrentUserSerializer.new(current_user, root: false)))
|
|
||||||
|
|
||||||
serializer = ActiveModel::ArraySerializer.new(TopicTrackingState.report([current_user.id]), each_serializer: TopicTrackingStateSerializer)
|
|
||||||
store_preloaded("topicTrackingStates", MultiJson.dump(serializer))
|
|
||||||
end
|
|
||||||
store_preloaded("siteSettings", SiteSetting.client_settings_json)
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
||||||
@ -201,6 +193,17 @@ class ApplicationController < ActionController::Base
|
|||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
|
def preload_anonymous_data
|
||||||
|
store_preloaded("site", Site.cached_json(guardian))
|
||||||
|
store_preloaded("siteSettings", SiteSetting.client_settings_json)
|
||||||
|
end
|
||||||
|
|
||||||
|
def preload_current_user_data
|
||||||
|
store_preloaded("currentUser", MultiJson.dump(CurrentUserSerializer.new(current_user, root: false)))
|
||||||
|
serializer = ActiveModel::ArraySerializer.new(TopicTrackingState.report([current_user.id]), each_serializer: TopicTrackingStateSerializer)
|
||||||
|
store_preloaded("topicTrackingStates", MultiJson.dump(serializer))
|
||||||
|
end
|
||||||
|
|
||||||
def render_json_error(obj)
|
def render_json_error(obj)
|
||||||
if obj.present?
|
if obj.present?
|
||||||
render json: MultiJson.dump(errors: obj.errors.full_messages), status: 422
|
render json: MultiJson.dump(errors: obj.errors.full_messages), status: 422
|
||||||
@ -257,12 +260,10 @@ class ApplicationController < ActionController::Base
|
|||||||
end
|
end
|
||||||
|
|
||||||
def check_xhr
|
def check_xhr
|
||||||
unless (controller_name == 'forums' || controller_name == 'user_open_ids')
|
|
||||||
# bypass xhr check on PUT / POST / DELETE provided api key is there, otherwise calling api is annoying
|
# bypass xhr check on PUT / POST / DELETE provided api key is there, otherwise calling api is annoying
|
||||||
return if !request.get? && api_key_valid?
|
return if !request.get? && api_key_valid?
|
||||||
raise RenderEmpty.new unless ((request.format && request.format.json?) || request.xhr?)
|
raise RenderEmpty.new unless ((request.format && request.format.json?) || request.xhr?)
|
||||||
end
|
end
|
||||||
end
|
|
||||||
|
|
||||||
def ensure_logged_in
|
def ensure_logged_in
|
||||||
raise Discourse::NotLoggedIn.new unless current_user.present?
|
raise Discourse::NotLoggedIn.new unless current_user.present?
|
||||||
|
@ -28,21 +28,21 @@ class UserActionSerializer < ApplicationSerializer
|
|||||||
end
|
end
|
||||||
|
|
||||||
def avatar_template
|
def avatar_template
|
||||||
user = User.new
|
avatar_for(
|
||||||
user[:email] = object.email
|
object.email,
|
||||||
user[:use_uploaded_avatar] = object.use_uploaded_avatar
|
object.use_uploaded_avatar,
|
||||||
user[:uploaded_avatar_template] = object.uploaded_avatar_template
|
object.uploaded_avatar_template,
|
||||||
user[:uploaded_avatar_id] = object.uploaded_avatar_id
|
object.uploaded_avatar_id
|
||||||
user.avatar_template
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def acting_avatar_template
|
def acting_avatar_template
|
||||||
acting_user = User.new
|
avatar_for(
|
||||||
acting_user[:email] = object.acting_email
|
object.acting_email,
|
||||||
acting_user[:use_uploaded_avatar] = object.acting_use_uploaded_avatar
|
object.acting_use_uploaded_avatar,
|
||||||
acting_user[:uploaded_avatar_template] = object.acting_uploaded_avatar_template
|
object.acting_uploaded_avatar_template,
|
||||||
acting_user[:uploaded_avatar_id] = object.acting_uploaded_avatar_id
|
object.acting_uploaded_avatar_id
|
||||||
acting_user.avatar_template
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
def slug
|
def slug
|
||||||
@ -53,4 +53,15 @@ class UserActionSerializer < ApplicationSerializer
|
|||||||
object.post_type == Post.types[:moderator_action]
|
object.post_type == Post.types[:moderator_action]
|
||||||
end
|
end
|
||||||
|
|
||||||
|
private
|
||||||
|
def avatar_for(email, use_uploaded_avatar, uploaded_avatar_template, uploaded_avatar_id)
|
||||||
|
# NOTE: id is required for cases where the template is blank (during initial population)
|
||||||
|
User.new(
|
||||||
|
email: email,
|
||||||
|
use_uploaded_avatar: use_uploaded_avatar,
|
||||||
|
uploaded_avatar_template: uploaded_avatar_template,
|
||||||
|
uploaded_avatar_id: uploaded_avatar_id
|
||||||
|
).avatar_template
|
||||||
|
end
|
||||||
|
|
||||||
end
|
end
|
||||||
|
Reference in New Issue
Block a user