Skip to content

Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.

🔊 Record why organizer notification emails are skipped (#3408)

Luke Vella8ca016e7447f
diff --git a/apps/web/src/app/api/unsubscribe/[token]/route.ts b/apps/web/src/app/api/unsubscribe/[token]/route.ts
index 21c1146ce..6df041f79 100644
--- a/apps/web/src/app/api/unsubscribe/[token]/route.ts
+++ b/apps/web/src/app/api/unsubscribe/[token]/route.ts
@@ -1,5 +1,6 @@
-import { NextResponse } from "next/server";
+import { after, NextResponse } from "next/server";
import { unsubscribeWithToken } from "@/features/notifications/mutations";
+import { flushPostHog, track } from "@/lib/posthog";
type Context = { params: Promise<{ token: string }> };
@@ -16,6 +17,22 @@ export async function POST(_request: Request, { params }: Context) {
return NextResponse.json({ error: "Invalid token" }, { status: 400 });
}
+ if (result.ok) {
+ track(
+ { id: result.target.userId, isGuest: false },
+ {
+ event: "poll_notification:mute_update",
+ properties: {
+ poll_id: result.target.pollId,
+ muted: true,
+ source: "one_click",
+ },
+ groups: { poll: result.target.pollId },
+ },
+ );
+ after(() => flushPostHog());
+ }
+
// An item that no longer exists (or changed owner) has nothing left to
// mute; the client's intent is satisfied either way.
return NextResponse.json({ ok: true });
diff --git a/apps/web/src/features/notifications/actions.ts b/apps/web/src/features/notifications/actions.ts
index 7a1a54456..c3bfd1db1 100644
--- a/apps/web/src/features/notifications/actions.ts
+++ b/apps/web/src/features/notifications/actions.ts
@@ -41,6 +41,21 @@ export const unsubscribeWithTokenAction = actionClient
.action(async ({ parsedInput }) => {
const result = await unsubscribeWithToken({ token: parsedInput.token });
+ if (result.ok) {
+ track(
+ { id: result.target.userId, isGuest: false },
+ {
+ event: "poll_notification:mute_update",
+ properties: {
+ poll_id: result.target.pollId,
+ muted: true,
+ source: "unsubscribe_page",
+ },
+ groups: { poll: result.target.pollId },
+ },
+ );
+ }
+
refresh();
return result;
diff --git a/apps/web/src/features/notifications/data.ts b/apps/web/src/features/notifications/data.ts
index c0e8a9ef8..168ac5323 100644
--- a/apps/web/src/features/notifications/data.ts
+++ b/apps/web/src/features/notifications/data.ts
@@ -32,10 +32,19 @@ type NotificationRecipient = {
locale: string | null;
};
+export type NotificationSkipReason =
+ | "no_creator"
+ | "poll_deleted"
+ | "triggered_by_creator"
+ | "poll_muted"
+ | "creator_is_guest"
+ | "not_effective_member"
+ | "preference_off";
+
/**
* Get the poll creator if they should receive a notification for this event.
- * Returns null if the creator has notifications disabled or is the one who
- * triggered the event.
+ * Otherwise returns why not, so a skipped notification can be traced after
+ * the fact.
*/
export async function getNotificationRecipient({
pollId,
@@ -45,41 +54,51 @@ export async function getNotificationRecipient({
pollId: string;
type: ActivityEventType;
excludeUserId: string;
-}): Promise<NotificationRecipient | null> {
+}): Promise<
+ | { ok: true; recipient: NotificationRecipient }
+ | { ok: false; reason: NotificationSkipReason }
+> {
const poll = await prisma.poll.findUnique({
where: { id: pollId },
select: { userId: true, spaceId: true, muted: true, deleted: true },
});
- if (
- !poll?.userId ||
- poll.deleted ||
- poll.userId === excludeUserId ||
- poll.muted
- ) {
- return null;
+ if (!poll?.userId) {
+ return { ok: false, reason: "no_creator" };
+ }
+
+ if (poll.deleted) {
+ return { ok: false, reason: "poll_deleted" };
+ }
+
+ if (poll.userId === excludeUserId) {
+ return { ok: false, reason: "triggered_by_creator" };
+ }
+
+ if (poll.muted) {
+ return { ok: false, reason: "poll_muted" };
}
const creator = await prisma.user.findUnique({
- where: {
- id: poll.userId,
- isAnonymous: false,
- // A poll in a space notifies its creator only while they remain an
- // effective member of it. Once they have left, the poll is the
- // space's and its responses are none of their business.
- ...(poll.spaceId && {
- memberOf: {
- some: {
- spaceId: poll.spaceId,
- ...effectiveSpaceMemberWhere({ userId: poll.userId }),
- },
- },
- }),
- },
+ where: { id: poll.userId },
select: {
id: true,
email: true,
locale: true,
+ isAnonymous: true,
+ // A poll in a space notifies its creator only while they remain an
+ // effective member of it. Once they have left, the poll is the
+ // space's and its responses are none of their business.
+ memberOf: poll.spaceId
+ ? {
+ where: {
+ spaceId: poll.spaceId,
+ ...effectiveSpaceMemberWhere({ userId: poll.userId }),
+ },
+ select: { id: true },
+ take: 1,
+ }
+ : false,
notificationPreferences: {
select: { prefs: true },
},
@@ -87,19 +106,30 @@ export async function getNotificationRecipient({
});
if (!creator) {
- return null;
+ return { ok: false, reason: "no_creator" };
+ }
+
+ if (creator.isAnonymous) {
+ return { ok: false, reason: "creator_is_guest" };
+ }
+
+ if (poll.spaceId && creator.memberOf.length === 0) {
+ return { ok: false, reason: "not_effective_member" };
}
const prefs = parsePrefs(creator.notificationPreferences?.prefs);
if (!prefs[type]) {
- return null;
+ return { ok: false, reason: "preference_off" };
}
return {
- id: creator.id,
- email: creator.email,
- locale: creator.locale,
+ ok: true,
+ recipient: {
+ id: creator.id,
+ email: creator.email,
+ locale: creator.locale,
+ },
};
}
diff --git a/apps/web/src/features/poll/actions.ts b/apps/web/src/features/poll/actions.ts
index 3b7ba6abf..965f7f91b 100644
--- a/apps/web/src/features/poll/actions.ts
+++ b/apps/web/src/features/poll/actions.ts
@@ -4,7 +4,7 @@ import { refresh } from "next/cache";
import { setPollMuted } from "@/features/poll/mutations";
import { setPollMutedSchema } from "@/features/poll/schema";
-import { identifyGroup } from "@/lib/posthog";
+import { identifyGroup, track } from "@/lib/posthog";
import { authActionClient } from "@/lib/safe-action/server";
export const setPollMutedAction = authActionClient
@@ -20,6 +20,11 @@ export const setPollMutedAction = authActionClient
});
if (result.ok) {
+ track(ctx.user, {
+ event: "poll_notification:mute_update",
+ properties: { poll_id: pollId, muted, source: "app" },
+ groups: { poll: pollId },
+ });
identifyGroup({
groupType: "poll",
groupKey: pollId,
diff --git a/apps/web/src/features/poll/comment/actions.ts b/apps/web/src/features/poll/comment/actions.ts
index df1331c7a..5a7c3000f 100644
--- a/apps/web/src/features/poll/comment/actions.ts
+++ b/apps/web/src/features/poll/comment/actions.ts
@@ -16,7 +16,7 @@ import {
} from "@/features/poll/comment/schema";
import { hasPollAdminAccess } from "@/features/poll/data";
import { AppError } from "@/lib/errors/app-error";
-import { track } from "@/lib/posthog";
+import { flushPostHog, track, trackSystemEvent } from "@/lib/posthog";
import {
anyUserActionClient,
createRateLimitMiddleware,
@@ -38,16 +38,31 @@ async function sendNewCommentNotificationEmail({
excludeUserId: string;
}) {
try {
- const recipient = await getNotificationRecipient({
+ const result = await getNotificationRecipient({
pollId,
type: "poll.comment.added",
excludeUserId,
});
- if (!recipient) {
+ if (!result.ok) {
+ logger.info(
+ { pollId, reason: result.reason },
+ "Skipped new comment notification email",
+ );
+ trackSystemEvent({
+ event: "poll_notification:email_skip",
+ properties: {
+ poll_id: pollId,
+ type: "poll.comment.added",
+ reason: result.reason,
+ },
+ });
+ await flushPostHog();
return;
}
+ const { recipient } = result;
+
const unsubscribeToken = createUnsubscribeToken({
target: { kind: "poll", pollId, userId: recipient.id },
secret: env.SECRET_PASSWORD,
diff --git a/apps/web/src/features/poll/participant/actions.ts b/apps/web/src/features/poll/participant/actions.ts
index 8e76ca04b..c5b517196 100644
--- a/apps/web/src/features/poll/participant/actions.ts
+++ b/apps/web/src/features/poll/participant/actions.ts
@@ -29,7 +29,7 @@ import {
} from "@/features/poll/participant/schema";
import { getLocale } from "@/i18n/server/get-locale";
import { AppError } from "@/lib/errors/app-error";
-import { track } from "@/lib/posthog";
+import { flushPostHog, track, trackSystemEvent } from "@/lib/posthog";
import {
anyUserActionClient,
createRateLimitMiddleware,
@@ -119,16 +119,31 @@ async function sendNewResponseNotificationEmail({
excludeUserId: string;
}) {
try {
- const recipient = await getNotificationRecipient({
+ const result = await getNotificationRecipient({
pollId,
type: "poll.response.submitted",
excludeUserId,
});
- if (!recipient) {
+ if (!result.ok) {
+ logger.info(
+ { pollId, reason: result.reason },
+ "Skipped new response notification email",
+ );
+ trackSystemEvent({
+ event: "poll_notification:email_skip",
+ properties: {
+ poll_id: pollId,
+ type: "poll.response.submitted",
+ reason: result.reason,
+ },
+ });
+ await flushPostHog();
return;
}
+ const { recipient } = result;
+
const unsubscribeToken = createUnsubscribeToken({
target: { kind: "poll", pollId, userId: recipient.id },
secret: env.SECRET_PASSWORD,