Skip to content

Commit

Permalink
FIX: prevents pushing null watching invitee (#629)
Browse files Browse the repository at this point in the history
This commit has 3 parts:

- Ensuring than when attempting to join a event we return an invitee even if the user was already part of the event, before we would return null which would have required a special handling. In this case we were just adding a null invitee.

- Ensures invitees/suggestedUsers/sampleInvitees use tracked properties correctly. And do not require to manually create a TrackedArray, the model should always handle this implementation detail.

- When joining an event we were pushing the watchingInvitee to the sampleInvitees even when the watchingInvitee was null, which would create an error.
  • Loading branch information
jjaffeux authored Oct 24, 2024
1 parent 223697e commit 52ebb59
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 32 deletions.
1 change: 1 addition & 0 deletions app/models/discourse_post_event/invitee.rb
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ def self.create_attendance!(user_id, post_id, status)
invitee
rescue ActiveRecord::RecordNotUnique
# do nothing in case multiple new attendances would be created very fast
Invitee.find_by(post_id: post_id, user_id: user_id)
end

def update_attendance!(status)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ export default class DiscoursePostEventEvent {
this.startsAt = args.starts_at;
this.endsAt = args.ends_at;
this.rawInvitees = args.raw_invitees;
this.sampleInvitees = args.sample_invitees;
this.sampleInvitees = args.sample_invitees || [];
this.url = args.url;
this.timezone = args.timezone;
this.status = args.status;
Expand Down Expand Up @@ -87,9 +87,9 @@ export default class DiscoursePostEventEvent {
return this._sampleInvitees;
}

set sampleInvitees(invitees) {
set sampleInvitees(invitees = []) {
this._sampleInvitees = new TrackedArray(
(invitees || []).map((u) => DiscoursePostEventInvitee.create(u))
invitees.map((i) => DiscoursePostEventInvitee.create(i))
);
}

Expand Down Expand Up @@ -143,7 +143,7 @@ export default class DiscoursePostEventEvent {
this.canActOnDiscoursePostEvent = event.canActOnDiscoursePostEvent;
this.shouldDisplayInvitees = event.shouldDisplayInvitees;
this.stats = event.stats;
this.sampleInvitees = event.sampleInvitees;
this.sampleInvitees = event.sampleInvitees || [];
this.reminders = event.reminders;
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { tracked } from "@glimmer/tracking";
import { TrackedArray } from "@ember-compat/tracked-built-ins";
import User from "discourse/models/user";
import DiscoursePostEventInvitee from "./discourse-post-event-invitee";
Expand All @@ -7,27 +8,22 @@ export default class DiscoursePostEventInvitees {
return new DiscoursePostEventInvitees(args);
}

@tracked _invitees;
@tracked _suggestedUsers;

constructor(args = {}) {
this.invitees = args.invitees;
this.suggestedUsers = args.meta?.suggested_users;
this.invitees = args.invitees || [];
this.suggestedUsers = args.meta?.suggested_users || [];
}

add(invitee) {
this.invitees.push(invitee);

const index = this.suggestedUsers.findIndex(
(su) => su.id === invitee.user.id
);
if (index > -1) {
this.suggestedUsers.splice(index, 1);
}
get invitees() {
return this._invitees;
}

remove(invitee) {
const index = this.invitees.findIndex((i) => i.user.id === invitee.user.id);
if (index > -1) {
this.invitees.splice(index, 1);
}
set invitees(invitees = []) {
this._invitees = new TrackedArray(
invitees.map((i) => DiscoursePostEventInvitee.create(i))
);
}

get suggestedUsers() {
Expand All @@ -36,17 +32,19 @@ export default class DiscoursePostEventInvitees {

set suggestedUsers(suggestedUsers = []) {
this._suggestedUsers = new TrackedArray(
suggestedUsers.map((i) => User.create(i))
suggestedUsers.map((su) => User.create(su))
);
}

get invitees() {
return this._invitees;
}
add(invitee) {
this.invitees.push(invitee);

set invitees(invitees = []) {
this._invitees = new TrackedArray(
invitees.map((i) => DiscoursePostEventInvitee.create(i))
this.suggestedUsers = this.suggestedUsers.filter(
(su) => su.id !== invitee.user.id
);
}

remove(invitee) {
this.invitees = this.invitees.filter((i) => i.user.id !== invitee.user.id);
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import Service from "@ember/service";
import { TrackedArray } from "@ember-compat/tracked-built-ins";
import { ajax } from "discourse/lib/ajax";
import DiscoursePostEventEvent from "discourse/plugins/discourse-calendar/discourse/models/discourse-post-event-event";
import DiscoursePostEventInvitee from "discourse/plugins/discourse-calendar/discourse/models/discourse-post-event-invitee";
Expand Down Expand Up @@ -63,8 +62,8 @@ export default class DiscoursePostEventApi extends Service {
async leaveEvent(event, invitee) {
await this.#deleteRequest(`/events/${event.id}/invitees/${invitee.id}`);

event.sampleInvitees = new TrackedArray(
event.sampleInvitees.filter((i) => i.id !== invitee.id)
event.sampleInvitees = event.sampleInvitees.filter(
(i) => i.id !== invitee.id
);

if (event.watchingInvitee?.id === invitee.id) {
Expand All @@ -81,10 +80,9 @@ export default class DiscoursePostEventApi extends Service {

if (!data.user_id) {
event.watchingInvitee = invitee;
event.sampleInvitees.push(event.watchingInvitee);
}

event.sampleInvitees.push(event.watchingInvitee);

event.stats = result.invitee.meta.event_stats;
event.shouldDisplayInvitees =
result.invitee.meta.event_should_display_invitees;
Expand Down

0 comments on commit 52ebb59

Please sign in to comment.