Skip to content

Commit

Permalink
Merge pull request #17049 from davelopez/23.1_fix_broadcast_filtering…
Browse files Browse the repository at this point in the history
…_for_admins

[23.1] Fix filtering of active broadcasts for admins
  • Loading branch information
dannon authored Nov 20, 2023
2 parents e5d37d4 + 8d514b1 commit ddd2157
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,26 @@ import { type BroadcastNotification, useBroadcastsStore } from "@/stores/broadca
const localVue = getLocalVue(true);

const now = new Date();
const inTwoMonths = new Date(now.setMonth(now.getMonth() + 2));
const inTwoMonths = new Date(new Date(now).setMonth(now.getMonth() + 2));

function generateBroadcastNotification(id: string): BroadcastNotification {
/** API date-time does not have timezone indicator and it's always UTC. */
function toApiDate(date: Date): string {
return date.toISOString().replace("Z", "");
}

function generateBroadcastNotification(
id: string,
publicationTime?: Date,
expirationTime?: Date
): BroadcastNotification {
const publication_time = publicationTime ? toApiDate(publicationTime) : toApiDate(now);
const expiration_time = expirationTime ? toApiDate(expirationTime) : toApiDate(inTwoMonths);
return {
id: id,
create_time: now.toISOString(),
update_time: now.toISOString(),
publication_time: now.toISOString(),
expiration_time: inTwoMonths.toISOString(),
create_time: toApiDate(now),
update_time: toApiDate(now),
publication_time,
expiration_time,
source: "testing",
variant: "info",
content: {
Expand Down Expand Up @@ -78,4 +89,20 @@ describe("BroadcastsOverlay.vue", () => {
expect(wrapper.findAll(".broadcast-message")).toHaveLength(1);
expect(wrapper.find(".broadcast-message").text()).toContain("Test message 2");
});

it("should not render the broadcast when it has expired", async () => {
const expiredBroadcast = generateBroadcastNotification("expired", undefined, new Date(now));
const wrapper = await mountBroadcastsOverlayWith([expiredBroadcast]);

expect(wrapper.exists()).toBe(true);
expect(wrapper.html()).toBe("");
});

it("should not render the broadcast when it has not been published yet", async () => {
const unpublishedBroadcast = generateBroadcastNotification("unpublished", new Date(inTwoMonths));
const wrapper = await mountBroadcastsOverlayWith([unpublishedBroadcast]);

expect(wrapper.exists()).toBe(true);
expect(wrapper.html()).toBe("");
});
});
16 changes: 15 additions & 1 deletion client/src/stores/broadcastsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ export const useBroadcastsStore = defineStore(
const dismissedBroadcasts = ref<{ [key: string]: Expirable }>({});

const activeBroadcasts = computed(() => {
return broadcasts.value.filter((b) => !dismissedBroadcasts.value[b.id]);
return broadcasts.value.filter(isActive);
});

async function loadBroadcasts() {
Expand All @@ -40,6 +40,14 @@ export const useBroadcastsStore = defineStore(
Vue.set(dismissedBroadcasts.value, broadcast.id, { expiration_time: broadcast.expiration_time });
}

function isActive(broadcast: BroadcastNotification) {
return (
!dismissedBroadcasts.value[broadcast.id] &&
!hasExpired(broadcast.expiration_time) &&
hasBeenPublished(broadcast)
);
}

function hasExpired(expirationTimeStr?: string) {
if (!expirationTimeStr) {
return false;
Expand All @@ -49,6 +57,12 @@ export const useBroadcastsStore = defineStore(
return now > expirationTime;
}

function hasBeenPublished(broadcast: BroadcastNotification) {
const publicationTime = new Date(`${broadcast.publication_time}Z`);
const now = new Date();
return now >= publicationTime;
}

function clearExpiredDismissedBroadcasts() {
for (const key in dismissedBroadcasts.value) {
if (hasExpired(dismissedBroadcasts.value[key]?.expiration_time)) {
Expand Down

0 comments on commit ddd2157

Please sign in to comment.