Skip to content

Commit

Permalink
PERF: summary serializer for events list (#484)
Browse files Browse the repository at this point in the history
* PERF: summary serializer for events list
  • Loading branch information
renato authored Nov 28, 2023
1 parent 61ec1f7 commit 87793ea
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 184 deletions.
2 changes: 1 addition & 1 deletion app/controllers/discourse_post_event/events_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def index
render json:
ActiveModel::ArraySerializer.new(
@events,
each_serializer: EventSerializer,
each_serializer: EventSummarySerializer,
scope: guardian,
).as_json
end
Expand Down
31 changes: 31 additions & 0 deletions app/serializers/discourse_post_event/event_summary_serializer.rb
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
],
});
Expand Down
31 changes: 0 additions & 31 deletions test/javascripts/acceptance/category-events-calendar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
},
],
});
Expand Down
62 changes: 0 additions & 62 deletions test/javascripts/acceptance/upcoming-events-calendar-test.js
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
},
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand All @@ -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,
},
],
Expand Down

0 comments on commit 87793ea

Please sign in to comment.