From 87793eac8d0611d8933559cbaa306cae35116823 Mon Sep 17 00:00:00 2001 From: Renato Atilio Date: Tue, 28 Nov 2023 10:00:27 -0300 Subject: [PATCH] PERF: summary serializer for events list (#484) * PERF: summary serializer for events list --- .../discourse_post_event/events_controller.rb | 2 +- .../event_summary_serializer.rb | 31 ++++++++++ .../event_summary_serializer_spec.rb | 26 ++++++++ .../category-events-calendar-outlet-test.js | 30 --------- .../category-events-calendar-test.js | 31 ---------- .../upcoming-events-calendar-test.js | 62 ------------------- .../components/upcoming-events-list-test.gjs | 60 ------------------ 7 files changed, 58 insertions(+), 184 deletions(-) create mode 100644 app/serializers/discourse_post_event/event_summary_serializer.rb create mode 100644 spec/serializers/discourse_post_event/event_summary_serializer_spec.rb diff --git a/app/controllers/discourse_post_event/events_controller.rb b/app/controllers/discourse_post_event/events_controller.rb index b97589e3..8ae2b4d6 100644 --- a/app/controllers/discourse_post_event/events_controller.rb +++ b/app/controllers/discourse_post_event/events_controller.rb @@ -8,7 +8,7 @@ module DiscoursePostEvent render json: ActiveModel::ArraySerializer.new( @events, - each_serializer: EventSerializer, + each_serializer: EventSummarySerializer, scope: guardian, ).as_json end diff --git a/app/serializers/discourse_post_event/event_summary_serializer.rb b/app/serializers/discourse_post_event/event_summary_serializer.rb new file mode 100644 index 00000000..cc19196d --- /dev/null +++ b/app/serializers/discourse_post_event/event_summary_serializer.rb @@ -0,0 +1,31 @@ +# frozen_string_literal: true + +module DiscoursePostEvent + class EventSummarySerializer < ApplicationSerializer + attributes :id + attributes :starts_at + attributes :ends_at + attributes :timezone + attributes :post + attributes :name + attributes :category_id + + # lightweight post object containing + # only needed info for client + def post + { + id: object.post.id, + post_number: object.post.post_number, + url: object.post.url, + topic: { + id: object.post.topic.id, + title: object.post.topic.title, + }, + } + end + + def category_id + object.post.topic.category_id + end + end +end diff --git a/spec/serializers/discourse_post_event/event_summary_serializer_spec.rb b/spec/serializers/discourse_post_event/event_summary_serializer_spec.rb new file mode 100644 index 00000000..cd62a7f9 --- /dev/null +++ b/spec/serializers/discourse_post_event/event_summary_serializer_spec.rb @@ -0,0 +1,26 @@ +# frozen_string_literal: true +require "rails_helper" + +describe DiscoursePostEvent::EventSummarySerializer do + before do + SiteSetting.calendar_enabled = true + SiteSetting.discourse_post_event_enabled = true + end + + fab!(:category) + fab!(:topic) { Fabricate(:topic, category: category) } + fab!(:post) { Fabricate(:post, topic: topic) } + fab!(:event) { Fabricate(:event, post: post) } + + it "returns the event summary" do + json = DiscoursePostEvent::EventSummarySerializer.new(event, scope: Guardian.new).as_json + summary = json[:event_summary] + expect(summary[:starts_at]).to eq(event.starts_at) + expect(summary[:ends_at]).to eq(event.ends_at) + expect(summary[:timezone]).to eq(event.timezone) + expect(summary[:name]).to eq(event.name) + expect(summary[:post][:url]).to eq(post.url) + expect(summary[:post][:topic][:title]).to eq(topic.title) + expect(summary[:category_id]).to eq(category.id) + end +end diff --git a/test/javascripts/acceptance/category-events-calendar-outlet-test.js b/test/javascripts/acceptance/category-events-calendar-outlet-test.js index 3ab52c1d..d6d9e89e 100644 --- a/test/javascripts/acceptance/category-events-calendar-outlet-test.js +++ b/test/javascripts/acceptance/category-events-calendar-outlet-test.js @@ -8,27 +8,9 @@ const eventsPretender = (server, helper) => { events: [ { id: 67501, - creator: { - id: 1500588, - username: "foobar", - name: null, - avatar_template: "/user_avatar/localhost/foobar/{size}/1913_2.png", - assign_icon: "user-plus", - assign_path: "/u/foobar/activity/assigned", - }, - sample_invitees: [], - watching_invitee: null, starts_at: "2022-04-25T15:14:00.000Z", ends_at: "2022-04-30T16:14:00.000Z", timezone: "Asia/Calcutta", - stats: { - going: 0, - interested: 0, - not_going: 0, - invited: 0, - }, - status: "public", - raw_invitees: ["trust_level_0"], post: { id: 67501, post_number: 1, @@ -39,18 +21,6 @@ const eventsPretender = (server, helper) => { }, }, name: "Awesome Event", - can_act_on_discourse_post_event: true, - can_update_attendance: true, - is_expired: false, - is_ongoing: false, - should_display_invitees: false, - url: null, - custom_fields: {}, - is_public: true, - is_private: false, - is_standalone: false, - reminders: [], - recurrence: null, }, ], }); diff --git a/test/javascripts/acceptance/category-events-calendar-test.js b/test/javascripts/acceptance/category-events-calendar-test.js index afeefb64..0d8d591f 100644 --- a/test/javascripts/acceptance/category-events-calendar-test.js +++ b/test/javascripts/acceptance/category-events-calendar-test.js @@ -18,28 +18,9 @@ acceptance("Discourse Calendar - Category Events Calendar", function (needs) { events: [ { id: 67501, - creator: { - id: 1500588, - username: "foobar", - name: null, - avatar_template: - "/user_avatar/localhost/foobar/{size}/1913_2.png", - assign_icon: "user-plus", - assign_path: "/u/foobar/activity/assigned", - }, - sample_invitees: [], - watching_invitee: null, starts_at: "2022-04-25T15:14:00.000Z", ends_at: "2022-04-30T16:14:00.000Z", timezone: "Asia/Calcutta", - stats: { - going: 0, - interested: 0, - not_going: 0, - invited: 0, - }, - status: "public", - raw_invitees: ["trust_level_0"], post: { id: 67501, post_number: 1, @@ -50,18 +31,6 @@ acceptance("Discourse Calendar - Category Events Calendar", function (needs) { }, }, name: "Awesome Event", - can_act_on_discourse_post_event: true, - can_update_attendance: true, - is_expired: false, - is_ongoing: false, - should_display_invitees: false, - url: null, - custom_fields: {}, - is_public: true, - is_private: false, - is_standalone: false, - reminders: [], - recurrence: null, }, ], }); diff --git a/test/javascripts/acceptance/upcoming-events-calendar-test.js b/test/javascripts/acceptance/upcoming-events-calendar-test.js index 8fc92d0e..87f6d84d 100644 --- a/test/javascripts/acceptance/upcoming-events-calendar-test.js +++ b/test/javascripts/acceptance/upcoming-events-calendar-test.js @@ -38,28 +38,9 @@ acceptance("Discourse Calendar - Upcoming Events Calendar", function (needs) { events: [ { id: 67501, - creator: { - id: 1500588, - username: "foobar", - name: null, - avatar_template: - "/user_avatar/localhost/foobar/{size}/1913_2.png", - assign_icon: "user-plus", - assign_path: "/u/foobar/activity/assigned", - }, - sample_invitees: [], - watching_invitee: null, starts_at: tomorrow(), ends_at: null, timezone: "Asia/Calcutta", - stats: { - going: 0, - interested: 0, - not_going: 0, - invited: 0, - }, - status: "public", - raw_invitees: ["trust_level_0"], post: { id: 67501, post_number: 1, @@ -70,44 +51,13 @@ acceptance("Discourse Calendar - Upcoming Events Calendar", function (needs) { }, }, name: "Awesome Event", - can_act_on_discourse_post_event: true, - can_update_attendance: true, - is_expired: false, - is_ongoing: true, - should_display_invitees: false, - url: null, - custom_fields: {}, - is_public: true, - is_private: false, - is_standalone: false, - reminders: [], - recurrence: null, category_id: 1, }, { id: 67502, - creator: { - id: 1500588, - username: "foobar", - name: null, - avatar_template: - "/user_avatar/localhost/foobar/{size}/1913_2.png", - assign_icon: "user-plus", - assign_path: "/u/foobar/activity/assigned", - }, - sample_invitees: [], - watching_invitee: null, starts_at: tomorrow(), ends_at: null, timezone: "Asia/Calcutta", - stats: { - going: 0, - interested: 0, - not_going: 0, - invited: 0, - }, - status: "public", - raw_invitees: ["trust_level_0"], post: { id: 67501, post_number: 1, @@ -118,18 +68,6 @@ acceptance("Discourse Calendar - Upcoming Events Calendar", function (needs) { }, }, name: "Another Awesome Event", - can_act_on_discourse_post_event: true, - can_update_attendance: true, - is_expired: false, - is_ongoing: true, - should_display_invitees: false, - url: null, - custom_fields: {}, - is_public: true, - is_private: false, - is_standalone: false, - reminders: [], - recurrence: null, category_id: 2, }, ], diff --git a/test/javascripts/integration/components/upcoming-events-list-test.gjs b/test/javascripts/integration/components/upcoming-events-list-test.gjs index 40c21a5b..f54fdf1c 100644 --- a/test/javascripts/integration/components/upcoming-events-list-test.gjs +++ b/test/javascripts/integration/components/upcoming-events-list-test.gjs @@ -214,27 +214,9 @@ function twoEventsResponseHandler() { events: [ { id: 67501, - creator: { - id: 1500588, - username: "foobar", - name: null, - avatar_template: "/user_avatar/localhost/foobar/{size}/1913_2.png", - assign_icon: "user-plus", - assign_path: "/u/foobar/activity/assigned", - }, - sample_invitees: [], - watching_invitee: null, starts_at: tomorrowAllDay, ends_at: null, timezone: "Asia/Calcutta", - stats: { - going: 0, - interested: 0, - not_going: 0, - invited: 0, - }, - status: "public", - raw_invitees: ["trust_level_0"], post: { id: 67501, post_number: 1, @@ -245,43 +227,13 @@ function twoEventsResponseHandler() { }, }, name: "Awesome Event", - can_act_on_discourse_post_event: true, - can_update_attendance: true, - is_expired: false, - is_ongoing: true, - should_display_invitees: false, - url: null, - custom_fields: {}, - is_public: true, - is_private: false, - is_standalone: false, - reminders: [], - recurrence: null, category_id: 1, }, { id: 67502, - creator: { - id: 1500588, - username: "foobar", - name: null, - avatar_template: "/user_avatar/localhost/foobar/{size}/1913_2.png", - assign_icon: "user-plus", - assign_path: "/u/foobar/activity/assigned", - }, - sample_invitees: [], - watching_invitee: null, starts_at: nextMonth, ends_at: null, timezone: "Asia/Calcutta", - stats: { - going: 0, - interested: 0, - not_going: 0, - invited: 0, - }, - status: "public", - raw_invitees: ["trust_level_0"], post: { id: 67501, post_number: 1, @@ -292,18 +244,6 @@ function twoEventsResponseHandler() { }, }, name: "Another Awesome Event", - can_act_on_discourse_post_event: true, - can_update_attendance: true, - is_expired: false, - is_ongoing: true, - should_display_invitees: false, - url: null, - custom_fields: {}, - is_public: true, - is_private: false, - is_standalone: false, - reminders: [], - recurrence: null, category_id: 2, }, ],