Rallly is an open-source scheduling and collaboration tool designed to make organizing events and meetings easier.
TypeScript i18nextnext-authnextjspostgresqlprismareact-emailt3-stacktailwindcsstrpcturborepotypescriptzod
🐛 Gate space branding and attribution removal on the resolved tier (#3157)
Luke Vellad374ed482678
diff --git a/apps/web/src/app/[locale]/(optional-space)/new/page.tsx b/apps/web/src/app/[locale]/(optional-space)/new/page.tsxindex bee968bb5..971aeba2e 100644--- a/apps/web/src/app/[locale]/(optional-space)/new/page.tsx+++ b/apps/web/src/app/[locale]/(optional-space)/new/page.tsx@@ -14,6 +14,7 @@ import { BrandStyle } from "@/features/branding/components/brand-style";import { loadInstancePolicy } from "@/features/instance-policy/loaders";import { CreatePoll } from "@/features/poll/components/create-poll";import { getActiveSpaceForUser } from "@/features/space/data";+import { isSpaceBrandingActive } from "@/features/space/utils";import { Trans } from "@/i18n/client";import { getTranslation } from "@/i18n/server";import { getSession } from "@/lib/auth";@@ -30,9 +31,11 @@ export default async function Page() {}const primaryColor =- space?.showBranding &&- space.primaryColor &&- (await loadInstancePolicy()).spaceBrandingAllowed+ space?.primaryColor &&+ isSpaceBrandingActive({+ ...space,+ spaceBrandingAllowed: (await loadInstancePolicy()).spaceBrandingAllowed,+ })? space.primaryColor: null;diff --git a/apps/web/src/emails/branding.ts b/apps/web/src/emails/branding.tsindex 9aa7105b6..bc945f054 100644--- a/apps/web/src/emails/branding.ts+++ b/apps/web/src/emails/branding.ts@@ -2,6 +2,11 @@ import type { EmailBranding } from "@rallly/emails";import { getInstanceBrandingConfig } from "@/features/branding/data";import { getInstancePolicy } from "@/features/instance-policy/data";+import type { SpaceTier } from "@/features/space/schema";+import {+ isSpaceAttributionHidden,+ isSpaceBrandingActive,+} from "@/features/space/utils";import { resolveStorageUrl } from "@/lib/storage/resolve-storage-url";/**@@ -23,6 +28,7 @@ export async function getInstanceBranding(): Promise<EmailBranding> {* space's logo/color applied when the space has custom branding enabled.*/export async function getSpaceBranding(space: {+ tier: SpaceTier;showBranding: boolean;hideAttribution: boolean;primaryColor: string | null;@@ -36,8 +42,8 @@ export async function getSpaceBranding(space: {// reflected in the instance brandinghideAttribution:instance.hideAttribution ||- (spaceAttributionConfigurable && space.hideAttribution),- ...(space.showBranding && spaceBrandingAllowed+ isSpaceAttributionHidden({ ...space, spaceAttributionConfigurable }),+ ...(isSpaceBrandingActive({ ...space, spaceBrandingAllowed })? {primaryColor: space.primaryColor ?? instance.primaryColor,logoUrl: space.imagediff --git a/apps/web/src/features/scheduled-event/data.ts b/apps/web/src/features/scheduled-event/data.tsindex c4da09716..a1f605c36 100644--- a/apps/web/src/features/scheduled-event/data.ts+++ b/apps/web/src/features/scheduled-event/data.ts@@ -87,6 +87,7 @@ export function getScheduledEventRsvpEmailData(eventId: string) {user: { select: { name: true, email: true } },space: {select: {+ tier: true,showBranding: true,hideAttribution: true,primaryColor: true,diff --git a/apps/web/src/features/space/client.tsx b/apps/web/src/features/space/client.tsxindex 39db83f11..41a8783fc 100644--- a/apps/web/src/features/space/client.tsx+++ b/apps/web/src/features/space/client.tsx@@ -5,6 +5,7 @@ import { getPrimaryColorVars } from "@/features/branding/utils";import { useInstancePolicy } from "@/features/instance-policy/client";import { defineAbilityForMember } from "@/features/space/member/ability";import type { SpaceDTO } from "@/features/space/types";+import { isSpaceBrandingActive } from "@/features/space/utils";import { useAuthedUser } from "@/features/user/client";import { defineAbilityForSpace } from "./ability";@@ -46,7 +47,8 @@ export function SpaceProvider({const { spaceBrandingAllowed } = useInstancePolicy();const primaryColorVars =- spaceBrandingAllowed && space.showBranding && space.primaryColor+ isSpaceBrandingActive({ ...space, spaceBrandingAllowed }) &&+ space.primaryColor? getPrimaryColorVars(space.primaryColor): null;diff --git a/apps/web/src/features/space/data.ts b/apps/web/src/features/space/data.tsindex 769b24673..acab9b6df 100644--- a/apps/web/src/features/space/data.ts+++ b/apps/web/src/features/space/data.ts@@ -12,7 +12,11 @@ import { cached_getInstanceLicense } from "@/features/licensing/data";import type { LicenseType } from "@/features/licensing/schema";import type { MemberDTO } from "@/features/space/member/types";import { effectiveSpaceMemberWhere } from "@/features/space/member/utils";-import { createSpaceDTO, fromDBRole } from "@/features/space/utils";+import {+ createSpaceDTO,+ fromDBRole,+ isSpaceBrandingActive,+} from "@/features/space/utils";import { AppError } from "@/lib/errors/app-error";const logger = createLogger("space/data");@@ -171,6 +175,7 @@ export async function getSpaceBranding(spaceId: string) {select: {name: true,image: true,+ tier: true,showBranding: true,primaryColor: true,},@@ -182,7 +187,7 @@ export async function getSpaceBranding(spaceId: string) {const { spaceBrandingAllowed } = await getInstancePolicy();- if (!spaceBrandingAllowed) {+ if (!isSpaceBrandingActive({ ...space, spaceBrandingAllowed })) {return { ...space, showBranding: false, primaryColor: null };}diff --git a/apps/web/src/features/space/utils.test.ts b/apps/web/src/features/space/utils.test.tsindex 8bcda32a8..71c8ae332 100644--- a/apps/web/src/features/space/utils.test.ts+++ b/apps/web/src/features/space/utils.test.ts@@ -4,6 +4,8 @@ import {createSpaceContentScope,createSpaceDTO,inferIndustry,+ isSpaceAttributionHidden,+ isSpaceBrandingActive,} from "@/features/space/utils";const spaceId = "space-1" as AuthorizedSpaceId;@@ -28,6 +30,90 @@ describe("createSpaceContentScope", () => {});});+describe("isSpaceBrandingActive", () => {+ it("applies branding for a pro space that turned it on", () => {+ expect(+ isSpaceBrandingActive({+ tier: "pro",+ showBranding: true,+ spaceBrandingAllowed: true,+ }),+ ).toBe(true);+ });++ it("ignores a stored colour once the space is on hobby", () => {+ expect(+ isSpaceBrandingActive({+ tier: "hobby",+ showBranding: true,+ spaceBrandingAllowed: true,+ }),+ ).toBe(false);+ });++ it("respects the switch being off", () => {+ expect(+ isSpaceBrandingActive({+ tier: "pro",+ showBranding: false,+ spaceBrandingAllowed: true,+ }),+ ).toBe(false);+ });++ it("defers to instance policy", () => {+ expect(+ isSpaceBrandingActive({+ tier: "pro",+ showBranding: true,+ spaceBrandingAllowed: false,+ }),+ ).toBe(false);+ });+});++describe("isSpaceAttributionHidden", () => {+ it("hides attribution for a pro space that turned it on", () => {+ expect(+ isSpaceAttributionHidden({+ tier: "pro",+ hideAttribution: true,+ spaceAttributionConfigurable: true,+ }),+ ).toBe(true);+ });++ it("ignores the stored setting once the space is on hobby", () => {+ expect(+ isSpaceAttributionHidden({+ tier: "hobby",+ hideAttribution: true,+ spaceAttributionConfigurable: true,+ }),+ ).toBe(false);+ });++ it("respects the switch being off", () => {+ expect(+ isSpaceAttributionHidden({+ tier: "pro",+ hideAttribution: false,+ spaceAttributionConfigurable: true,+ }),+ ).toBe(false);+ });++ it("defers to instance policy", () => {+ expect(+ isSpaceAttributionHidden({+ tier: "pro",+ hideAttribution: true,+ spaceAttributionConfigurable: false,+ }),+ ).toBe(false);+ });+});+describe("inferIndustry", () => {it("infers education from an academic domain", () => {expect(inferIndustry({ email: "[email protected]" })).toBe("education");diff --git a/apps/web/src/features/space/utils.ts b/apps/web/src/features/space/utils.tsindex 59c5b9f1d..fbe4c887a 100644--- a/apps/web/src/features/space/utils.ts+++ b/apps/web/src/features/space/utils.ts@@ -9,7 +9,7 @@ import {industryDomainRules,industryKeywordRules,} from "@/features/space/constants";-import type { MemberRole } from "@/features/space/schema";+import type { MemberRole, SpaceTier } from "@/features/space/schema";import type {AuthorizedSpaceId,SpaceContentScope,@@ -73,6 +73,48 @@ export function createSpaceDTO({};}+/**+ * Whether a space's stored colour and logo are applied wherever the space is+ * shown (app chrome, poll pages, emails). Custom branding is a paid feature:+ * the setting may stay on after a downgrade so it comes back on upgrade, but+ * the stored values must not render until then. Accepts the stored tier;+ * resolving is idempotent, so an already resolved DTO tier is fine too.+ */+export function isSpaceBrandingActive({+ tier,+ showBranding,+ spaceBrandingAllowed,+}: {+ tier: SpaceTier;+ showBranding: boolean;+ spaceBrandingAllowed: boolean;+}) {+ return (+ spaceBrandingAllowed && showBranding && resolveSpaceTier(tier) === "pro"+ );+}++/**+ * Whether the "Powered by" credit is dropped from a space's polls and emails.+ * Same shape as branding: paid, and the stored setting survives a downgrade+ * without taking effect until the space is pro again.+ */+export function isSpaceAttributionHidden({+ tier,+ hideAttribution,+ spaceAttributionConfigurable,+}: {+ tier: SpaceTier;+ hideAttribution: boolean;+ spaceAttributionConfigurable: boolean;+}) {+ return (+ spaceAttributionConfigurable &&+ hideAttribution &&+ resolveSpaceTier(tier) === "pro"+ );+}+/*** The visibility scope space-scoped content reads must apply for this* member. One rule for all content types and all roles: in an unshareddiff --git a/apps/web/src/trpc/routers/polls.ts b/apps/web/src/trpc/routers/polls.tsindex 3a5469abb..e32148b03 100644--- a/apps/web/src/trpc/routers/polls.ts+++ b/apps/web/src/trpc/routers/polls.ts@@ -19,6 +19,10 @@ import {import { MAX_POLL_DESCRIPTION_LENGTH } from "@/features/poll/schema";import { formatEventDateTime } from "@/features/scheduled-event/utils";import { getActiveSpaceForUser } from "@/features/space/data";+import {+ isSpaceAttributionHidden,+ isSpaceBrandingActive,+} from "@/features/space/utils";import { dayjs } from "@/lib/dayjs";import { identifyGroup, track } from "@/lib/posthog";import { createIcsEvent } from "@/lib/utils/ics";@@ -813,18 +817,21 @@ export const polls = router({const { spaceBrandingAllowed, spaceAttributionConfigurable } =await getInstancePolicy();+ const brandingActive = res.space+ ? isSpaceBrandingActive({ ...res.space, spaceBrandingAllowed })+ : false;return {...res,space: res.space? {...res.space,- showBranding: res.space.showBranding && spaceBrandingAllowed,- primaryColor: spaceBrandingAllowed- ? res.space.primaryColor- : null,- hideAttribution:- spaceAttributionConfigurable && res.space.hideAttribution,+ showBranding: brandingActive,+ primaryColor: brandingActive ? res.space.primaryColor : null,+ hideAttribution: isSpaceAttributionHidden({+ ...res.space,+ spaceAttributionConfigurable,+ }),}: null,canManage,@@ -865,6 +872,7 @@ export const polls = router({hideParticipants: true,space: {select: {+ tier: true,showBranding: true,hideAttribution: true,primaryColor: true,diff --git a/apps/web/tests/space-branding.spec.ts b/apps/web/tests/space-branding.spec.tsnew file mode 100644index 000000000..0355cf989--- /dev/null+++ b/apps/web/tests/space-branding.spec.ts@@ -0,0 +1,95 @@+import type { Page } from "@playwright/test";+import { expect, test } from "@playwright/test";+import { prisma } from "@rallly/database";+import { createTestPoll, createUserInDb } from "./test-utils";++const HOST_EMAIL = "[email protected]";+const POLL_ID = "space-branding-poll";+const STORED_COLOR = "#dc2626";++async function readPrimaryVar(page: Page) {+ await page.goto(`/invite/${POLL_ID}`);+ await expect(+ page.getByRole("heading", { name: "Space branding poll" }),+ ).toBeVisible();+ return page.evaluate(() =>+ getComputedStyle(document.documentElement).getPropertyValue("--primary"),+ );+}++test.describe("space branding on the invite page", () => {+ let spaceId: string;+ let defaultPrimary: string;++ test.beforeAll(async ({ browser }) => {+ await prisma.user.deleteMany({ where: { email: HOST_EMAIL } });+ const host = await createUserInDb({+ email: HOST_EMAIL,+ name: "Space branding host",+ });+ const space = await prisma.space.findFirstOrThrow({+ where: { ownerId: host.id },+ });+ spaceId = space.id;+ await createTestPoll({+ id: POLL_ID,+ title: "Space branding poll",+ userId: host.id,+ spaceId,+ updatedAt: new Date(),+ hasFutureOptions: true,+ });++ const page = await browser.newPage();+ defaultPrimary = await readPrimaryVar(page);+ await page.close();+ });++ test.afterAll(async () => {+ await prisma.user.deleteMany({ where: { email: HOST_EMAIL } });+ });++ test("pro space with branding on renders its colour", async ({ page }) => {+ await prisma.space.update({+ where: { id: spaceId },+ data: { tier: "pro", showBranding: true, primaryColor: STORED_COLOR },+ });++ expect(await readPrimaryVar(page)).not.toBe(defaultPrimary);+ });++ test("hobby space with a stored colour renders the default primary", async ({+ page,+ }) => {+ await prisma.space.update({+ where: { id: spaceId },+ data: { tier: "hobby", showBranding: true, primaryColor: STORED_COLOR },+ });++ expect(await readPrimaryVar(page)).toBe(defaultPrimary);+ });++ test("pro space with attribution removed hides the footer credit", async ({+ page,+ }) => {+ await prisma.space.update({+ where: { id: spaceId },+ data: { tier: "pro", hideAttribution: true },+ });++ await readPrimaryVar(page);+ await expect(page.getByText("Powered by")).toBeHidden();+ });++ test("hobby space with attribution removed still shows the footer credit", async ({+ page,+ }) => {+ await prisma.space.update({+ where: { id: spaceId },+ data: { tier: "hobby", hideAttribution: true },+ });++ await readPrimaryVar(page);+ await expect(page.getByText("Powered by")).toBeVisible();+ });+});