mirror of
https://github.com/discourse/discourse.git
synced 2024-11-23 03:54:59 +08:00
REFACTOR: Use UserAuthTokenLogSerializer
instead of conditionals.
This commit is contained in:
parent
45f092a49d
commit
7256b3bf68
71
app/serializers/concerns/user_auth_tokens_mixin.rb
Normal file
71
app/serializers/concerns/user_auth_tokens_mixin.rb
Normal file
|
@ -0,0 +1,71 @@
|
|||
module UserAuthTokensMixin
|
||||
extend ActiveSupport::Concern
|
||||
|
||||
included do
|
||||
attributes :id,
|
||||
:client_ip,
|
||||
:os,
|
||||
:device_name,
|
||||
:icon,
|
||||
:created_at
|
||||
end
|
||||
|
||||
def client_ip
|
||||
object.client_ip.to_s
|
||||
end
|
||||
|
||||
def os
|
||||
case object.user_agent
|
||||
when /Android/i
|
||||
'Android'
|
||||
when /iPhone|iPad|iPod/i
|
||||
'iOS'
|
||||
when /Macintosh/i
|
||||
'macOS'
|
||||
when /Linux/i
|
||||
'Linux'
|
||||
when /Windows/i
|
||||
'Windows'
|
||||
else
|
||||
I18n.t('staff_action_logs.unknown')
|
||||
end
|
||||
end
|
||||
|
||||
def device_name
|
||||
case object.user_agent
|
||||
when /Android/i
|
||||
I18n.t('user_auth_tokens.devices.android')
|
||||
when /iPad/i
|
||||
I18n.t('user_auth_tokens.devices.ipad')
|
||||
when /iPhone/i
|
||||
I18n.t('user_auth_tokens.devices.iphone')
|
||||
when /iPod/i
|
||||
I18n.t('user_auth_tokens.devices.ipod')
|
||||
when /Mobile/i
|
||||
I18n.t('user_auth_tokens.devices.mobile')
|
||||
when /Macintosh/i
|
||||
I18n.t('user_auth_tokens.devices.mac')
|
||||
when /Linux/i
|
||||
I18n.t('user_auth_tokens.devices.linux')
|
||||
when /Windows/i
|
||||
I18n.t('user_auth_tokens.devices.windows')
|
||||
else
|
||||
I18n.t('user_auth_tokens.devices.unknown')
|
||||
end
|
||||
end
|
||||
|
||||
def icon
|
||||
case os
|
||||
when 'Android'
|
||||
'android'
|
||||
when 'macOS', 'iOS'
|
||||
'apple'
|
||||
when 'Linux'
|
||||
'linux'
|
||||
when 'Windows'
|
||||
'windows'
|
||||
else
|
||||
'question'
|
||||
end
|
||||
end
|
||||
end
|
16
app/serializers/user_auth_token_log_serializer.rb
Normal file
16
app/serializers/user_auth_token_log_serializer.rb
Normal file
|
@ -0,0 +1,16 @@
|
|||
class UserAuthTokenLogSerializer < ApplicationSerializer
|
||||
include UserAuthTokensMixin
|
||||
|
||||
attributes :action
|
||||
|
||||
def action
|
||||
case object.action
|
||||
when 'generate'
|
||||
I18n.t('log_in')
|
||||
when 'destroy'
|
||||
I18n.t('unsubscribe.log_out')
|
||||
else
|
||||
I18n.t('staff_action_logs.unknown')
|
||||
end
|
||||
end
|
||||
end
|
|
@ -1,88 +1,5 @@
|
|||
class UserAuthTokenSerializer < ApplicationSerializer
|
||||
attributes :id,
|
||||
:action,
|
||||
:client_ip,
|
||||
:created_at,
|
||||
:seen_at,
|
||||
:os,
|
||||
:device_name,
|
||||
:icon
|
||||
include UserAuthTokensMixin
|
||||
|
||||
def action
|
||||
case object.action
|
||||
when 'generate'
|
||||
I18n.t('log_in')
|
||||
when 'destroy'
|
||||
I18n.t('unsubscribe.log_out')
|
||||
else
|
||||
I18n.t('staff_action_logs.unknown')
|
||||
end
|
||||
end
|
||||
|
||||
def include_action?
|
||||
object.has_attribute?(:action)
|
||||
end
|
||||
|
||||
def client_ip
|
||||
object.client_ip.to_s
|
||||
end
|
||||
|
||||
def include_seen_at?
|
||||
object.has_attribute?(:seen_at)
|
||||
end
|
||||
|
||||
def os
|
||||
case object.user_agent
|
||||
when /Android/i
|
||||
'Android'
|
||||
when /iPhone|iPad|iPod/i
|
||||
'iOS'
|
||||
when /Macintosh/i
|
||||
'macOS'
|
||||
when /Linux/i
|
||||
'Linux'
|
||||
when /Windows/i
|
||||
'Windows'
|
||||
else
|
||||
I18n.t('staff_action_logs.unknown')
|
||||
end
|
||||
end
|
||||
|
||||
def device_name
|
||||
case object.user_agent
|
||||
when /Android/i
|
||||
I18n.t('user_auth_tokens.devices.android')
|
||||
when /iPad/i
|
||||
I18n.t('user_auth_tokens.devices.ipad')
|
||||
when /iPhone/i
|
||||
I18n.t('user_auth_tokens.devices.iphone')
|
||||
when /iPod/i
|
||||
I18n.t('user_auth_tokens.devices.ipod')
|
||||
when /Mobile/i
|
||||
I18n.t('user_auth_tokens.devices.mobile')
|
||||
when /Macintosh/i
|
||||
I18n.t('user_auth_tokens.devices.mac')
|
||||
when /Linux/i
|
||||
I18n.t('user_auth_tokens.devices.linux')
|
||||
when /Windows/i
|
||||
I18n.t('user_auth_tokens.devices.windows')
|
||||
else
|
||||
I18n.t('user_auth_tokens.devices.unknown')
|
||||
end
|
||||
end
|
||||
|
||||
def icon
|
||||
case os
|
||||
when 'Android'
|
||||
'android'
|
||||
when 'macOS', 'iOS'
|
||||
'apple'
|
||||
when 'Linux'
|
||||
'linux'
|
||||
when 'Windows'
|
||||
'windows'
|
||||
else
|
||||
'question'
|
||||
end
|
||||
end
|
||||
attributes :seen_at
|
||||
end
|
||||
|
|
|
@ -196,11 +196,19 @@ class UserSerializer < BasicUserSerializer
|
|||
end
|
||||
|
||||
def user_auth_tokens
|
||||
ActiveModel::ArraySerializer.new(object.user_auth_tokens.order(:seen_at).reverse_order, each_serializer: UserAuthTokenSerializer)
|
||||
ActiveModel::ArraySerializer.new(
|
||||
object.user_auth_tokens.order(:seen_at).reverse_order,
|
||||
each_serializer: UserAuthTokenSerializer
|
||||
)
|
||||
end
|
||||
|
||||
def user_auth_token_logs
|
||||
ActiveModel::ArraySerializer.new(object.user_auth_token_logs.where(action: UserAuthToken::USER_ACTIONS).order(:created_at).reverse_order, each_serializer: UserAuthTokenSerializer)
|
||||
ActiveModel::ArraySerializer.new(
|
||||
object.user_auth_token_logs.where(
|
||||
action: UserAuthToken::USER_ACTIONS
|
||||
).order(:created_at).reverse_order,
|
||||
each_serializer: UserAuthTokenLogSerializer
|
||||
)
|
||||
end
|
||||
|
||||
def bio_raw
|
||||
|
|
Loading…
Reference in New Issue
Block a user