From 20efd494ef7a3c5c71f022df86d4b7ee3ed4907f Mon Sep 17 00:00:00 2001 From: Andrei Prigorshnev Date: Wed, 26 Oct 2022 16:41:31 +0400 Subject: [PATCH] DEV: move BasicUserWithStatusSerializer from Discourse Chat (#18745) --- .../basic_user_with_status_serializer.rb | 13 ++++++ .../basic_user_with_status_serializer_spec.rb | 44 +++++++++++++++++++ 2 files changed, 57 insertions(+) create mode 100644 app/serializers/basic_user_with_status_serializer.rb create mode 100644 spec/serializers/basic_user_with_status_serializer_spec.rb diff --git a/app/serializers/basic_user_with_status_serializer.rb b/app/serializers/basic_user_with_status_serializer.rb new file mode 100644 index 00000000000..8eb1a7ce2d6 --- /dev/null +++ b/app/serializers/basic_user_with_status_serializer.rb @@ -0,0 +1,13 @@ +# frozen_string_literal: true + +class BasicUserWithStatusSerializer < BasicUserSerializer + attributes :status + + def include_status? + SiteSetting.enable_user_status && user.has_status? + end + + def status + UserStatusSerializer.new(user.user_status, root: false) + end +end diff --git a/spec/serializers/basic_user_with_status_serializer_spec.rb b/spec/serializers/basic_user_with_status_serializer_spec.rb new file mode 100644 index 00000000000..7f5076db127 --- /dev/null +++ b/spec/serializers/basic_user_with_status_serializer_spec.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +RSpec.describe BasicUserWithStatusSerializer do + fab!(:user_status) { Fabricate(:user_status) } + fab!(:user) { Fabricate(:user, user_status: user_status) } + let(:serializer) { described_class.new(user, scope: Guardian.new(user), root: false) } + + it "adds user status when enabled" do + SiteSetting.enable_user_status = true + + json = serializer.as_json + + expect(json[:status]).to_not be_nil do |status| + expect(status.description).to eq(user_status.description) + expect(status.emoji).to eq(user_status.emoji) + end + end + + it "doesn't add user status when status is disabled in site settings" do + SiteSetting.enable_user_status = false + json = serializer.as_json + expect(json.keys).not_to include :status + end + + it "doesn't add expired user status" do + SiteSetting.enable_user_status = true + + user.user_status.ends_at = 1.minutes.ago + serializer = described_class.new(user, scope: Guardian.new(user), root: false) + json = serializer.as_json + + expect(json.keys).not_to include :status + end + + it "doesn't return status if user doesn't have it set" do + SiteSetting.enable_user_status = true + + user.clear_status! + user.reload + json = serializer.as_json + + expect(json.keys).not_to include :status + end +end