feat: send email for published posts
This commit is contained in:
@@ -10,6 +10,9 @@ GOOGLE_CLIENT_SECRET=
|
||||
|
||||
AUTH_COOKIE_NAME=app.session-token
|
||||
|
||||
# openssl rand -base64 32
|
||||
ENCRYPT_SECRET=uIeqyCW1E5PbK2ztsc+UXcPwsZ0L/Z4k08ArOSaKpHHFViRbdnFJcQ==
|
||||
|
||||
OUR_DOMAIN=localhost:3000
|
||||
NEXT_PUBLIC_OUR_DOMAIN=$OUR_DOMAIN
|
||||
|
||||
|
||||
+3
-2
@@ -16,8 +16,9 @@
|
||||
"@codemirror/language": "^0.20.0",
|
||||
"@codemirror/state": "^0.20.0",
|
||||
"@codemirror/view": "^0.20.3",
|
||||
"@hapi/iron": "^6.0.0",
|
||||
"@headlessui/react": "^1.6.0",
|
||||
"@prisma/client": "^3.13.0",
|
||||
"@prisma/client": "^3.14.0",
|
||||
"@trpc/client": "^9.23.2",
|
||||
"@trpc/next": "^9.23.2",
|
||||
"@trpc/react": "^9.23.2",
|
||||
@@ -61,7 +62,7 @@
|
||||
"eslint": "^8.15.0",
|
||||
"eslint-config-next": "^12.1.6",
|
||||
"postcss-import": "^14.1.0",
|
||||
"prisma": "^3.13.0",
|
||||
"prisma": "^3.14.0",
|
||||
"tailwindcss": "^3.0.24",
|
||||
"typescript": "^4.5.5"
|
||||
},
|
||||
|
||||
Generated
+54
-20
@@ -7,8 +7,9 @@ specifiers:
|
||||
'@codemirror/language': ^0.20.0
|
||||
'@codemirror/state': ^0.20.0
|
||||
'@codemirror/view': ^0.20.3
|
||||
'@hapi/iron': ^6.0.0
|
||||
'@headlessui/react': ^1.6.0
|
||||
'@prisma/client': ^3.13.0
|
||||
'@prisma/client': ^3.14.0
|
||||
'@trpc/client': ^9.23.2
|
||||
'@trpc/next': ^9.23.2
|
||||
'@trpc/react': ^9.23.2
|
||||
@@ -41,7 +42,7 @@ specifiers:
|
||||
next: ^12.1.6
|
||||
pica: ^9.0.1
|
||||
postcss-import: ^14.1.0
|
||||
prisma: ^3.13.0
|
||||
prisma: ^3.14.0
|
||||
react: ^18.1.0
|
||||
react-avatar-editor: ^12.0.0
|
||||
react-dom: ^18.1.0
|
||||
@@ -61,8 +62,9 @@ dependencies:
|
||||
'@codemirror/language': 0.20.0
|
||||
'@codemirror/state': 0.20.0
|
||||
'@codemirror/view': 0.20.4
|
||||
'@hapi/iron': 6.0.0
|
||||
'@headlessui/react': 1.6.1_ef5jwxihqo6n7gxfmzogljlgcm
|
||||
'@prisma/client': 3.13.0_prisma@3.13.0
|
||||
'@prisma/client': 3.14.0_prisma@3.14.0
|
||||
'@trpc/client': 9.23.2_@trpc+server@9.23.2
|
||||
'@trpc/next': 9.23.2_ekhln2aut74ytaacni4ejzffly
|
||||
'@trpc/react': 9.23.2_ztktndqynogm35aelp247cafku
|
||||
@@ -106,7 +108,7 @@ devDependencies:
|
||||
eslint: 8.15.0
|
||||
eslint-config-next: 12.1.6_6vdljae35l7howjttefzzsim3e
|
||||
postcss-import: 14.1.0
|
||||
prisma: 3.13.0
|
||||
prisma: 3.14.0
|
||||
tailwindcss: 3.0.24
|
||||
typescript: 4.6.4
|
||||
|
||||
@@ -1256,6 +1258,43 @@ packages:
|
||||
- supports-color
|
||||
dev: true
|
||||
|
||||
/@hapi/b64/5.0.0:
|
||||
resolution: {integrity: sha512-ngu0tSEmrezoiIaNGG6rRvKOUkUuDdf4XTPnONHGYfSGRmDqPZX5oJL6HAdKTo1UQHECbdB4OzhWrfgVppjHUw==}
|
||||
dependencies:
|
||||
'@hapi/hoek': 9.3.0
|
||||
dev: false
|
||||
|
||||
/@hapi/boom/9.1.4:
|
||||
resolution: {integrity: sha512-Ls1oH8jaN1vNsqcaHVYJrKmgMcKsC1wcp8bujvXrHaAqD2iDYq3HoOwsxwo09Cuda5R5nC0o0IxlrlTuvPuzSw==}
|
||||
dependencies:
|
||||
'@hapi/hoek': 9.3.0
|
||||
dev: false
|
||||
|
||||
/@hapi/bourne/2.1.0:
|
||||
resolution: {integrity: sha512-i1BpaNDVLJdRBEKeJWkVO6tYX6DMFBuwMhSuWqLsY4ufeTKGVuV5rBsUhxPayXqnnWHgXUAmWK16H/ykO5Wj4Q==}
|
||||
dev: false
|
||||
|
||||
/@hapi/cryptiles/5.1.0:
|
||||
resolution: {integrity: sha512-fo9+d1Ba5/FIoMySfMqPBR/7Pa29J2RsiPrl7bkwo5W5o+AN1dAYQRi4SPrPwwVxVGKjgLOEWrsvt1BonJSfLA==}
|
||||
engines: {node: '>=12.0.0'}
|
||||
dependencies:
|
||||
'@hapi/boom': 9.1.4
|
||||
dev: false
|
||||
|
||||
/@hapi/hoek/9.3.0:
|
||||
resolution: {integrity: sha512-/c6rf4UJlmHlC9b5BaNvzAcFv7HZ2QHaV0D4/HNlBdvFnvQq8RI4kYdhyPCl7Xj+oWvTWQ8ujhqS53LIgAe6KQ==}
|
||||
dev: false
|
||||
|
||||
/@hapi/iron/6.0.0:
|
||||
resolution: {integrity: sha512-zvGvWDufiTGpTJPG1Y/McN8UqWBu0k/xs/7l++HVU535NLHXsHhy54cfEMdW7EjwKfbBfM9Xy25FmTiobb7Hvw==}
|
||||
dependencies:
|
||||
'@hapi/b64': 5.0.0
|
||||
'@hapi/boom': 9.1.4
|
||||
'@hapi/bourne': 2.1.0
|
||||
'@hapi/cryptiles': 5.1.0
|
||||
'@hapi/hoek': 9.3.0
|
||||
dev: false
|
||||
|
||||
/@headlessui/react/1.6.1_ef5jwxihqo6n7gxfmzogljlgcm:
|
||||
resolution: {integrity: sha512-gMd6uIs1U4Oz718Z5gFoV0o/vD43/4zvbyiJN9Dt7PK9Ubxn+TmJwTmYwyNJc5KxxU1t0CmgTNgwZX9+4NjCnQ==}
|
||||
engines: {node: '>=10'}
|
||||
@@ -1482,8 +1521,8 @@ packages:
|
||||
fastq: 1.13.0
|
||||
dev: true
|
||||
|
||||
/@prisma/client/3.13.0_prisma@3.13.0:
|
||||
resolution: {integrity: sha512-lnEA2tTyVbO5mS1ehmHJQKBDiKB8shaR6s3azwj3Azfi5XHIfnqmkolLCvUeFYnkDCNVzGXJpUgKwQt/UOOYVQ==}
|
||||
/@prisma/client/3.14.0_prisma@3.14.0:
|
||||
resolution: {integrity: sha512-atb41UpgTR1MCst0VIbiHTMw8lmXnwUvE1KyUCAkq08+wJyjRE78Due+nSf+7uwqQn+fBFYVmoojtinhlLOSaA==}
|
||||
engines: {node: '>=12.6'}
|
||||
requiresBuild: true
|
||||
peerDependencies:
|
||||
@@ -1492,16 +1531,16 @@ packages:
|
||||
prisma:
|
||||
optional: true
|
||||
dependencies:
|
||||
'@prisma/engines-version': 3.13.0-17.efdf9b1183dddfd4258cd181a72125755215ab7b
|
||||
prisma: 3.13.0
|
||||
'@prisma/engines-version': 3.14.0-36.2b0c12756921c891fec4f68d9444e18c7d5d4a6a
|
||||
prisma: 3.14.0
|
||||
dev: false
|
||||
|
||||
/@prisma/engines-version/3.13.0-17.efdf9b1183dddfd4258cd181a72125755215ab7b:
|
||||
resolution: {integrity: sha512-TGp9rvgJIKo8NgvAHSwOosbut9mTA7VC6/rpQI9gh+ySSRjdQFhbGyNUiOcQrlI9Ob2DWeO7y4HEnhdKxYiECg==}
|
||||
/@prisma/engines-version/3.14.0-36.2b0c12756921c891fec4f68d9444e18c7d5d4a6a:
|
||||
resolution: {integrity: sha512-D+yHzq4a2r2Rrd0ZOW/mTZbgDIkUkD8ofKgusEI1xPiZz60Daks+UM7Me2ty5FzH3p/TgyhBpRrfIHx+ha20RQ==}
|
||||
dev: false
|
||||
|
||||
/@prisma/engines/3.13.0-17.efdf9b1183dddfd4258cd181a72125755215ab7b:
|
||||
resolution: {integrity: sha512-Ip9CcCeUocH61eXu4BUGpvl5KleQyhcUVLpWCv+0ZmDv44bFaDpREqjGHHdRupvPN/ugB6gTlD9b9ewdj02yVA==}
|
||||
/@prisma/engines/3.14.0-36.2b0c12756921c891fec4f68d9444e18c7d5d4a6a:
|
||||
resolution: {integrity: sha512-LwZvI3FY6f43xFjQNRuE10JM5R8vJzFTSmbV9X0Wuhv9kscLkjRlZt0BEoiHmO+2HA3B3xxbMfB5du7ZoSFXGg==}
|
||||
requiresBuild: true
|
||||
dev: true
|
||||
|
||||
@@ -3556,14 +3595,13 @@ packages:
|
||||
engines: {node: '>= 0.8.0'}
|
||||
dev: true
|
||||
|
||||
/prisma/3.13.0:
|
||||
resolution: {integrity: sha512-oO1auBnBtieGdiN+57IgsA9Vr7Sy4HkILi1KSaUG4mpKfEbnkTGnLOxAqjLed+K2nsG/GtE1tJBtB7JxN1a78Q==}
|
||||
/prisma/3.14.0:
|
||||
resolution: {integrity: sha512-l9MOgNCn/paDE+i1K2fp9NZ+Du4trzPTJsGkaQHVBufTGqzoYHuNk8JfzXuIn0Gte6/ZjyKj652Jq/Lc1tp2yw==}
|
||||
engines: {node: '>=12.6'}
|
||||
hasBin: true
|
||||
requiresBuild: true
|
||||
dependencies:
|
||||
'@prisma/engines': 3.13.0-17.efdf9b1183dddfd4258cd181a72125755215ab7b
|
||||
ts-pattern: 4.0.2
|
||||
'@prisma/engines': 3.14.0-36.2b0c12756921c891fec4f68d9444e18c7d5d4a6a
|
||||
dev: true
|
||||
|
||||
/prop-types/15.8.1:
|
||||
@@ -3942,10 +3980,6 @@ packages:
|
||||
is-number: 7.0.0
|
||||
dev: true
|
||||
|
||||
/ts-pattern/4.0.2:
|
||||
resolution: {integrity: sha512-eHqR/7A6fcw05vCOfnL6RwgGJbVi9G/YHTdYdjYmElhDdJ1SMn7pWs+6+YuxygaFwQS/g+cIDlu+UD8IVpur1A==}
|
||||
dev: true
|
||||
|
||||
/tsconfig-paths/3.14.1:
|
||||
resolution: {integrity: sha512-fxDhWnFSLt3VuTwtvJt5fpwxBHg5AdKWMsgcPOOIilyjymcYVZoCQF8fvFRezCNfblEXmi+PcM1eYHeOAgXCOQ==}
|
||||
dependencies:
|
||||
|
||||
+16
-15
@@ -101,21 +101,22 @@ enum PageType {
|
||||
}
|
||||
|
||||
model Page {
|
||||
id String @id @default(uuid())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
contentUpdatedAt DateTime @default(now())
|
||||
publishedAt DateTime @default(now())
|
||||
published Boolean @default(false)
|
||||
type PageType @default(POST)
|
||||
title String
|
||||
content String
|
||||
excerpt String
|
||||
format String @default("markdown")
|
||||
slug String
|
||||
site Site @relation(name: "SitePages", fields: [siteId], references: [id], onDelete: Cascade)
|
||||
siteId String
|
||||
deletedAt DateTime?
|
||||
id String @id @default(uuid())
|
||||
createdAt DateTime @default(now())
|
||||
updatedAt DateTime @updatedAt
|
||||
contentUpdatedAt DateTime @default(now())
|
||||
publishedAt DateTime @default(now())
|
||||
published Boolean @default(false)
|
||||
type PageType @default(POST)
|
||||
title String
|
||||
content String
|
||||
excerpt String
|
||||
format String @default("markdown")
|
||||
slug String
|
||||
site Site @relation(name: "SitePages", fields: [siteId], references: [id], onDelete: Cascade)
|
||||
siteId String
|
||||
deletedAt DateTime?
|
||||
subscribersNotifiedAt DateTime?
|
||||
|
||||
@@unique([siteId, slug])
|
||||
@@map("pages")
|
||||
|
||||
@@ -6,7 +6,7 @@ export const Button: React.FC<
|
||||
isLoading?: boolean
|
||||
isBlock?: boolean
|
||||
variant?: "primary" | "secondary" | "text"
|
||||
variantColor?: "green" | "red"
|
||||
variantColor?: "green" | "red" | "gray"
|
||||
size?: "sm"
|
||||
rounded?: "full" | "lg"
|
||||
}
|
||||
|
||||
@@ -76,10 +76,15 @@
|
||||
@apply bg-red-500 hover:bg-red-600 focus:bg-red-700;
|
||||
}
|
||||
|
||||
.button.is-gray {
|
||||
--shadow-color-button: theme("colors.gray.700");
|
||||
@apply bg-gray-500 hover:bg-gray-600 focus:bg-gray-700;
|
||||
}
|
||||
|
||||
.button.is-sm {
|
||||
@apply h-7;
|
||||
@apply px-2;
|
||||
@apply text-sm;
|
||||
@apply text-xs;
|
||||
}
|
||||
|
||||
.input {
|
||||
|
||||
@@ -6,5 +6,6 @@ export const S3_ENDPOINT = process.env.S3_ENDPOINT
|
||||
export const MAILGUN_APIKEY = process.env.MAILGUN_APIKEY
|
||||
export const MAILGUN_DOMAIN = process.env.MAILGUN_DOMAIN
|
||||
export const MAILGUN_EU = process.env.MAILGUN_EU
|
||||
export const ENCRYPT_SECRET = process.env.ENCRYPT_SECRET
|
||||
|
||||
export const AUTH_COOKIE_NAME = process.env.AUTH_COOKIE_NAME
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { IS_PROD } from "./constants"
|
||||
|
||||
export const APP_NAME = process.env.NEXT_PUBLIC_APP_NAME
|
||||
export const OUR_DOMAIN = process.env.NEXT_PUBLIC_OUR_DOMAIN
|
||||
export const S3_CDN_PREFIX = process.env.NEXT_PUBLIC_S3_CDN_PREFIX
|
||||
export const SITE_URL = `${IS_PROD ? "https" : "http"}://${OUR_DOMAIN}`
|
||||
|
||||
@@ -25,6 +25,10 @@ type Action =
|
||||
type: "can-update-site"
|
||||
site: Site
|
||||
}
|
||||
| {
|
||||
type: "can-notify-site-subscribers"
|
||||
site: Site
|
||||
}
|
||||
export const createGate = <TRequiredAuth extends boolean | undefined>({
|
||||
user,
|
||||
}: {
|
||||
@@ -49,6 +53,14 @@ export const createGate = <TRequiredAuth extends boolean | undefined>({
|
||||
: AuthUser | null | undefined
|
||||
},
|
||||
|
||||
hasRoles(siteId: string, roles: MembershipRole[]) {
|
||||
return isSiteMember(siteId, roles)
|
||||
},
|
||||
|
||||
isOwnerOrAdmin(siteId: string) {
|
||||
return this.hasRoles(siteId, [MembershipRole.OWNER, MembershipRole.ADMIN])
|
||||
},
|
||||
|
||||
allows(action: Action): boolean {
|
||||
if (action.type === "can-delete-page") {
|
||||
return isSiteMember(action.siteId, [
|
||||
@@ -96,6 +108,13 @@ export const createGate = <TRequiredAuth extends boolean | undefined>({
|
||||
])
|
||||
}
|
||||
|
||||
if (action.type === "can-notify-site-subscribers") {
|
||||
return isSiteMember(action.site.id, [
|
||||
MembershipRole.ADMIN,
|
||||
MembershipRole.OWNER,
|
||||
])
|
||||
}
|
||||
|
||||
return false
|
||||
},
|
||||
permissionError(message = "not allowed") {
|
||||
|
||||
+76
-12
@@ -1,12 +1,20 @@
|
||||
import Mailgun from "mailgun.js"
|
||||
import FormData from "form-data"
|
||||
import { singleton } from "./singleton.server"
|
||||
import { MAILGUN_APIKEY, MAILGUN_DOMAIN, MAILGUN_EU } from "~/lib/env.server"
|
||||
import {
|
||||
ENCRYPT_SECRET,
|
||||
MAILGUN_APIKEY,
|
||||
MAILGUN_DOMAIN,
|
||||
MAILGUN_EU,
|
||||
} from "~/lib/env.server"
|
||||
import type { MailgunMessageData } from "mailgun.js/interfaces/Messages"
|
||||
import { IS_PROD } from "./constants"
|
||||
import { APP_NAME, OUR_DOMAIN } from "./env"
|
||||
import { SubscribeFormData } from "./types"
|
||||
import { APP_NAME, OUR_DOMAIN, SITE_URL } from "./env"
|
||||
import { PostOnArchivesPage, SubscribeFormData } from "./types"
|
||||
import { getSite } from "~/models/site.model"
|
||||
import { Site, User } from "@prisma/client"
|
||||
import { nanoid } from "nanoid"
|
||||
import Iron from "@hapi/iron"
|
||||
|
||||
const getClient = () =>
|
||||
singleton("mailgun", () => {
|
||||
@@ -19,6 +27,18 @@ const getClient = () =>
|
||||
return client
|
||||
})
|
||||
|
||||
const sendEmail = async (message: MailgunMessageData) => {
|
||||
console.log(message)
|
||||
|
||||
if (!IS_PROD) {
|
||||
return
|
||||
}
|
||||
|
||||
const client = getClient()
|
||||
|
||||
await client.messages.create(MAILGUN_DOMAIN, message).then(console.log)
|
||||
}
|
||||
|
||||
export const sendLoginEmail = async (payload: {
|
||||
token: string
|
||||
email: string
|
||||
@@ -64,13 +84,57 @@ export const sendLoginEmail = async (payload: {
|
||||
html,
|
||||
}
|
||||
|
||||
console.log(message)
|
||||
|
||||
if (!IS_PROD) {
|
||||
return
|
||||
}
|
||||
|
||||
const client = getClient()
|
||||
|
||||
await client.messages.create(MAILGUN_DOMAIN, message).then(console.log)
|
||||
await sendEmail(message)
|
||||
}
|
||||
|
||||
export const sendEmailForNewPost = async (payload: {
|
||||
post: { slug: string; title: string; content: string }
|
||||
site: Site
|
||||
subscribers: { id: string; email: string }[]
|
||||
}) => {
|
||||
try {
|
||||
const from = `${payload.site.name} <updates@${payload.site.subdomain}.${OUR_DOMAIN}>`
|
||||
const subject = `${payload.post.title} - ${payload.site.name}`
|
||||
const html = `
|
||||
|
||||
<h2>${payload.post.title}</h2>
|
||||
<div>
|
||||
${payload.post.content}
|
||||
</div>
|
||||
<p>
|
||||
<a href="${SITE_URL}/api/unsubscribe?token=%recipient.unsubscribeToken%">Unsubscribe (no login required)</a>
|
||||
</p>`
|
||||
|
||||
const recipientVariables: {
|
||||
[email in string]: { unsubscribeToken: string }
|
||||
} = {}
|
||||
|
||||
await Promise.allSettled(
|
||||
payload.subscribers.map(async (sub) => {
|
||||
const unsubscribeToken = await Iron.seal(
|
||||
{
|
||||
userId: sub.id,
|
||||
siteId: payload.site.id,
|
||||
},
|
||||
ENCRYPT_SECRET,
|
||||
Iron.defaults
|
||||
)
|
||||
recipientVariables[sub.email] = {
|
||||
unsubscribeToken,
|
||||
}
|
||||
})
|
||||
)
|
||||
|
||||
const message = {
|
||||
from,
|
||||
subject,
|
||||
html,
|
||||
to: Object.keys(recipientVariables),
|
||||
"recipient-variables": JSON.stringify(recipientVariables),
|
||||
}
|
||||
|
||||
await sendEmail(message)
|
||||
} catch (error) {
|
||||
console.error(error)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,6 @@
|
||||
import { IS_PROD } from "./constants"
|
||||
import { OUR_DOMAIN } from "./env"
|
||||
|
||||
type Truthy<T> = T extends false | "" | 0 | null | undefined ? never : T // from lodash
|
||||
|
||||
export function truthy<T>(value: T): value is Truthy<T> {
|
||||
|
||||
@@ -1,6 +1,8 @@
|
||||
import { MembershipRole } from "@prisma/client"
|
||||
import { nanoid } from "nanoid"
|
||||
import { prisma, Prisma } from "~/lib/db.server"
|
||||
import { type Gate } from "~/lib/gate.server"
|
||||
import { sendEmailForNewPost } from "~/lib/mailgun.server"
|
||||
import { renderPageContent } from "~/lib/markdown.server"
|
||||
import { notFound } from "~/lib/server-side-props"
|
||||
import { PageVisibilityEnum } from "~/lib/types"
|
||||
@@ -107,6 +109,18 @@ export async function createOrUpdatePage(
|
||||
type: input.isPost ? "POST" : "PAGE",
|
||||
},
|
||||
})
|
||||
|
||||
if (
|
||||
input.isPost &&
|
||||
!page.subscribersNotifiedAt &&
|
||||
page.published &&
|
||||
page.publishedAt <= new Date()
|
||||
) {
|
||||
await notifySubscribersForNewPost(gate, {
|
||||
pageId: page.id,
|
||||
})
|
||||
}
|
||||
|
||||
return { page: updated }
|
||||
}
|
||||
|
||||
@@ -209,7 +223,7 @@ export async function getPage(
|
||||
input: {
|
||||
/** page slug or id, `site` is needed when `page` is a slug */
|
||||
page: string
|
||||
site: string
|
||||
site?: string
|
||||
renderContent?: boolean
|
||||
}
|
||||
) {
|
||||
@@ -221,7 +235,7 @@ export async function getPage(
|
||||
|
||||
const isPageUUID = isUUID(input.page)
|
||||
if (!isPageUUID && !input.site) {
|
||||
throw new Error("missing input site")
|
||||
throw new Error("input.site is required because input.page is a slug")
|
||||
}
|
||||
|
||||
const page = isPageUUID
|
||||
@@ -251,3 +265,50 @@ export async function getPage(
|
||||
|
||||
return page
|
||||
}
|
||||
|
||||
export const notifySubscribersForNewPost = async (
|
||||
gate: Gate,
|
||||
input: {
|
||||
pageId: string
|
||||
}
|
||||
) => {
|
||||
const page = await getPage(gate, { page: input.pageId, renderContent: true })
|
||||
const site = await getSite(page.siteId)
|
||||
|
||||
if (!gate.allows({ type: "can-notify-site-subscribers", site })) {
|
||||
throw gate.permissionError()
|
||||
}
|
||||
|
||||
if (page.subscribersNotifiedAt) {
|
||||
throw new Error("You have already notified subscribers for this post")
|
||||
}
|
||||
|
||||
const subscribers = await prisma.membership.findMany({
|
||||
where: {
|
||||
role: MembershipRole.SUBSCRIBER,
|
||||
siteId: site.id,
|
||||
},
|
||||
include: {
|
||||
user: true,
|
||||
},
|
||||
})
|
||||
|
||||
if (subscribers.length === 0) return
|
||||
|
||||
await prisma.page.update({
|
||||
where: {
|
||||
id: page.id,
|
||||
},
|
||||
data: {
|
||||
subscribersNotifiedAt: new Date(),
|
||||
},
|
||||
})
|
||||
|
||||
sendEmailForNewPost({
|
||||
post: page,
|
||||
site,
|
||||
subscribers: subscribers
|
||||
.filter((sub) => (sub.config as Prisma.JsonObject).email)
|
||||
.map((sub) => sub.user),
|
||||
})
|
||||
}
|
||||
|
||||
@@ -0,0 +1,32 @@
|
||||
import { NextApiHandler } from "next"
|
||||
import Iron from "@hapi/iron"
|
||||
import { ENCRYPT_SECRET } from "~/lib/env.server"
|
||||
import { unsubscribeFromSite } from "~/models/site.model"
|
||||
import { prisma } from "~/lib/db.server"
|
||||
import { createGate } from "~/lib/gate.server"
|
||||
|
||||
const handler: NextApiHandler = async (req, res) => {
|
||||
const token = req.query.token as string
|
||||
const payload: {
|
||||
siteId: string
|
||||
userId: string
|
||||
} = await Iron.unseal(token, ENCRYPT_SECRET, Iron.defaults)
|
||||
|
||||
const user = await prisma.user.findUnique({
|
||||
where: {
|
||||
id: payload.userId,
|
||||
},
|
||||
include: {
|
||||
memberships: true,
|
||||
},
|
||||
})
|
||||
if (!user) return res.send("invalid token")
|
||||
|
||||
const gate = createGate({ user })
|
||||
|
||||
await unsubscribeFromSite(gate, { siteId: payload.siteId })
|
||||
|
||||
res.send("unsubscribed!")
|
||||
}
|
||||
|
||||
export default handler
|
||||
@@ -234,20 +234,22 @@ export default function SubdomainEditor() {
|
||||
</div>
|
||||
</div>
|
||||
<div className="h-full overflow-auto flex-shrink-0 w-[280px] border-l bg-gray-50 p-5">
|
||||
<Input
|
||||
name="slug"
|
||||
value={values.slug}
|
||||
label="Page slug"
|
||||
id="slug"
|
||||
isBlock
|
||||
onChange={(e) => updateValue("slug", e.target.value)}
|
||||
/>
|
||||
{values.slug && (
|
||||
<div className="text-xs text-gray-400 mt-1">
|
||||
This {isPost ? "post" : "page"} will be accessible at{" "}
|
||||
{getSiteLink({ subdomain })}/{values.slug}
|
||||
</div>
|
||||
)}
|
||||
<div>
|
||||
<Input
|
||||
name="slug"
|
||||
value={values.slug}
|
||||
label="Page slug"
|
||||
id="slug"
|
||||
isBlock
|
||||
onChange={(e) => updateValue("slug", e.target.value)}
|
||||
/>
|
||||
{values.slug && (
|
||||
<div className="text-xs text-gray-400 mt-1">
|
||||
This {isPost ? "post" : "page"} will be accessible at{" "}
|
||||
{getSiteLink({ subdomain })}/{values.slug}
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</DashboardMain>
|
||||
|
||||
@@ -14,9 +14,26 @@ import {
|
||||
getPage,
|
||||
deletePage,
|
||||
getPagesBySite,
|
||||
notifySubscribersForNewPost,
|
||||
} from "~/models/page.model"
|
||||
|
||||
export const siteRouter = createRouter()
|
||||
.query("subscribersNotifiedAt", {
|
||||
input: z.object({
|
||||
pageId: z.string(),
|
||||
}),
|
||||
output: z
|
||||
.date()
|
||||
.transform((v) => v.toISOString())
|
||||
.nullable(),
|
||||
async resolve({ input, ctx }) {
|
||||
const page = await getPage(ctx.gate, { page: input.pageId })
|
||||
if (!ctx.gate.isOwnerOrAdmin(page.siteId)) {
|
||||
throw ctx.gate.permissionError()
|
||||
}
|
||||
return page.subscribersNotifiedAt
|
||||
},
|
||||
})
|
||||
.query("subscription", {
|
||||
input: z.object({
|
||||
site: z.string(),
|
||||
@@ -183,3 +200,13 @@ export const siteRouter = createRouter()
|
||||
await unsubscribeFromSite(ctx.gate, input)
|
||||
},
|
||||
})
|
||||
.mutation("notifySubscribersForNewPost", {
|
||||
input: z.object({
|
||||
pageId: z.string(),
|
||||
}),
|
||||
async resolve({ ctx, input }) {
|
||||
await notifySubscribersForNewPost(ctx.gate, {
|
||||
pageId: input.pageId,
|
||||
})
|
||||
},
|
||||
})
|
||||
|
||||
Vendored
+1
@@ -15,6 +15,7 @@ declare interface ServerEnv {
|
||||
S3_CDN_PREFIX: string
|
||||
FLY_REGION?: string
|
||||
PRIMARY_REGION?: string
|
||||
ENCRYPT_SECRET: string
|
||||
}
|
||||
|
||||
declare interface BrowserEnv {
|
||||
|
||||
Reference in New Issue
Block a user