From 1cdf3c7097b0c9ae9846ce7dd2e4f92dd191ebc8 Mon Sep 17 00:00:00 2001 From: Laura Hausmann Date: Thu, 30 Nov 2023 18:53:13 +0100 Subject: [PATCH] [mastodon-client] Add basic support for push notifications --- packages/backend/src/db/postgre.ts | 2 + .../migration/1701371327908-add-masto-push.ts | 22 +++ .../src/models/entities/push-subscription.ts | 51 +++++++ packages/backend/src/models/index.ts | 2 + .../src/server/api/mastodon/endpoints/push.ts | 33 +++++ .../mastodon/entities/push_subscription.ts | 29 +++- .../src/server/api/mastodon/helpers/push.ts | 132 ++++++++++++++++++ .../backend/src/server/api/mastodon/index.ts | 2 + .../server/api/mastodon/middleware/auth.ts | 1 + .../src/server/api/mastodon/push/index.ts | 63 +++++++++ .../src/services/create-notification.ts | 2 + 11 files changed, 332 insertions(+), 7 deletions(-) create mode 100644 packages/backend/src/migration/1701371327908-add-masto-push.ts create mode 100644 packages/backend/src/models/entities/push-subscription.ts create mode 100644 packages/backend/src/server/api/mastodon/endpoints/push.ts create mode 100644 packages/backend/src/server/api/mastodon/helpers/push.ts create mode 100644 packages/backend/src/server/api/mastodon/push/index.ts diff --git a/packages/backend/src/db/postgre.ts b/packages/backend/src/db/postgre.ts index 3e3f3a289..c1aea3dc0 100644 --- a/packages/backend/src/db/postgre.ts +++ b/packages/backend/src/db/postgre.ts @@ -76,6 +76,7 @@ import { OAuthApp } from "@/models/entities/oauth-app.js"; import { OAuthToken } from "@/models/entities/oauth-token.js"; import { HtmlNoteCacheEntry } from "@/models/entities/html-note-cache-entry.js"; import { HtmlUserCacheEntry } from "@/models/entities/html-user-cache-entry.js"; +import { PushSubscription } from "@/models/entities/push-subscription.js"; const sqlLogger = dbLogger.createSubLogger("sql", "gray", false); class MyCustomLogger implements Logger { @@ -179,6 +180,7 @@ export const entities = [ OAuthToken, HtmlNoteCacheEntry, HtmlUserCacheEntry, + PushSubscription, ...charts, ]; diff --git a/packages/backend/src/migration/1701371327908-add-masto-push.ts b/packages/backend/src/migration/1701371327908-add-masto-push.ts new file mode 100644 index 000000000..995cfb3cd --- /dev/null +++ b/packages/backend/src/migration/1701371327908-add-masto-push.ts @@ -0,0 +1,22 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class AddMastoPush1701371327908 implements MigrationInterface { + name = 'AddMastoPush1701371327908' + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.query(`CREATE TABLE "push_subscription" ("id" character varying(32) NOT NULL, "createdAt" TIMESTAMP WITH TIME ZONE NOT NULL, "userId" character varying(32) NOT NULL, "tokenId" character varying(32) NOT NULL, "data" jsonb NOT NULL, "types" jsonb NOT NULL, "policy" character varying(32) NOT NULL, CONSTRAINT "PK_07fc861c0d2c38c1b830fb9cb5d" PRIMARY KEY ("id"))`); + await queryRunner.query(`CREATE INDEX "IDX_8a227cbc3dc43c0d56117ea156" ON "push_subscription" ("userId") `); + await queryRunner.query(`CREATE UNIQUE INDEX "IDX_e062a101e77e5992259b10b428" ON "push_subscription" ("tokenId") `); + await queryRunner.query(`ALTER TABLE "push_subscription" ADD CONSTRAINT "FK_8a227cbc3dc43c0d56117ea1563" FOREIGN KEY ("userId") REFERENCES "user"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + await queryRunner.query(`ALTER TABLE "push_subscription" ADD CONSTRAINT "FK_e062a101e77e5992259b10b4280" FOREIGN KEY ("tokenId") REFERENCES "oauth_token"("id") ON DELETE CASCADE ON UPDATE NO ACTION`); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.query(`ALTER TABLE "push_subscription" DROP CONSTRAINT "FK_e062a101e77e5992259b10b4280"`); + await queryRunner.query(`ALTER TABLE "push_subscription" DROP CONSTRAINT "FK_8a227cbc3dc43c0d56117ea1563"`); + await queryRunner.query(`DROP INDEX "public"."IDX_e062a101e77e5992259b10b428"`); + await queryRunner.query(`DROP INDEX "public"."IDX_8a227cbc3dc43c0d56117ea156"`); + await queryRunner.query(`DROP TABLE "push_subscription"`); + } + +} diff --git a/packages/backend/src/models/entities/push-subscription.ts b/packages/backend/src/models/entities/push-subscription.ts new file mode 100644 index 000000000..e1eef199d --- /dev/null +++ b/packages/backend/src/models/entities/push-subscription.ts @@ -0,0 +1,51 @@ +import { + PrimaryColumn, + Entity, + Index, + JoinColumn, + Column, + ManyToOne, +} from "typeorm"; +import { User } from "./user.js"; +import { id } from "../id.js"; +import { OAuthToken } from "@/models/entities/oauth-token.js"; + +@Entity() +export class PushSubscription { + @PrimaryColumn(id()) + public id: string; + + @Column("timestamp with time zone") + public createdAt: Date; + + @Index() + @Column(id()) + public userId: User["id"]; + + @ManyToOne((type) => User, { + onDelete: "CASCADE", + }) + @JoinColumn() + public user: User | null; + + @Index({ unique: true }) + @Column(id()) + public tokenId: OAuthToken["id"]; + + @ManyToOne((type) => OAuthToken, { + onDelete: "CASCADE", + }) + @JoinColumn() + public token: OAuthToken | null; + + @Column("jsonb") + public data: MastodonEntity.PushData; + + @Column("jsonb") + public types: MastodonEntity.PushTypes; + + @Column("varchar", { + length: 32, + }) + public policy: MastodonEntity.PushPolicy; +} diff --git a/packages/backend/src/models/index.ts b/packages/backend/src/models/index.ts index 2f229689b..8b0bc3ec8 100644 --- a/packages/backend/src/models/index.ts +++ b/packages/backend/src/models/index.ts @@ -70,6 +70,7 @@ import { OAuthToken } from "@/models/entities/oauth-token.js"; import { UserProfileRepository } from "@/models/repositories/user-profile.js"; import { HtmlNoteCacheEntry } from "@/models/entities/html-note-cache-entry.js"; import { HtmlUserCacheEntry } from "@/models/entities/html-user-cache-entry.js"; +import { PushSubscription } from "@/models/entities/push-subscription.js"; export const Announcements = db.getRepository(Announcement); export const AnnouncementReads = db.getRepository(AnnouncementRead); @@ -138,3 +139,4 @@ export const OAuthApps = db.getRepository(OAuthApp); export const OAuthTokens = db.getRepository(OAuthToken); export const HtmlUserCacheEntries = db.getRepository(HtmlUserCacheEntry); export const HtmlNoteCacheEntries = db.getRepository(HtmlNoteCacheEntry); +export const PushSubscriptions = db.getRepository(PushSubscription); diff --git a/packages/backend/src/server/api/mastodon/endpoints/push.ts b/packages/backend/src/server/api/mastodon/endpoints/push.ts new file mode 100644 index 000000000..3a2affd97 --- /dev/null +++ b/packages/backend/src/server/api/mastodon/endpoints/push.ts @@ -0,0 +1,33 @@ +import Router from "@koa/router"; +import { auth } from "@/server/api/mastodon/middleware/auth.js"; +import { PushHelpers } from "@/server/api/mastodon/helpers/push.js"; + +export function setupEndpointsPush(router: Router): void { + router.post("/v1/push/subscription", + auth(true, ['push']), + async (ctx) => { + ctx.body = await PushHelpers.subscribe(ctx); + } + ); + + router.get("/v1/push/subscription", + auth(true, ['push']), + async (ctx) => { + ctx.body = await PushHelpers.get(ctx); + } + ); + + router.put("/v1/push/subscription", + auth(true, ['push']), + async (ctx) => { + ctx.body = await PushHelpers.update(ctx); + } + ); + + router.delete("/v1/push/subscription", + auth(true, ['push']), + async (ctx) => { + ctx.body = await PushHelpers.unsubscribe(ctx); + } + ); +} diff --git a/packages/backend/src/server/api/mastodon/entities/push_subscription.ts b/packages/backend/src/server/api/mastodon/entities/push_subscription.ts index 379ecffc5..fe6d49669 100644 --- a/packages/backend/src/server/api/mastodon/entities/push_subscription.ts +++ b/packages/backend/src/server/api/mastodon/entities/push_subscription.ts @@ -1,16 +1,31 @@ namespace MastodonEntity { - export type Alerts = { - follow: boolean; - favourite: boolean; - mention: boolean; - reblog: boolean; - poll: boolean; + export type PushTypes = { + mention: boolean; + status: boolean; + reblog: boolean; + follow: boolean; + follow_request: boolean; + favourite: boolean; + poll: boolean; + update: boolean; }; + export type PushPolicy = 'all' | 'followed' | 'follower' | 'none'; + + export type PushType = 'mention' | 'status' | 'reblog' | 'follow' | 'follow_request' | 'favourite' | 'poll' | 'update'; + + export type PushData = { + endpoint: string; + keys: { + auth: string; + p256dh: string; + }; + }; + export type PushSubscription = { id: string; endpoint: string; server_key: string; - alerts: Alerts; + alerts: PushTypes; }; } diff --git a/packages/backend/src/server/api/mastodon/helpers/push.ts b/packages/backend/src/server/api/mastodon/helpers/push.ts new file mode 100644 index 000000000..d826d190f --- /dev/null +++ b/packages/backend/src/server/api/mastodon/helpers/push.ts @@ -0,0 +1,132 @@ +import { MastoContext } from "@/server/api/mastodon/index.js"; +import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js"; +import { PushSubscriptions } from "@/models/index.js"; +import { genId } from "@/misc/gen-id.js"; +import { ILocalUser } from "@/models/entities/user.js"; +import { OAuthToken } from "@/models/entities/oauth-token.js"; +import { fetchMeta } from "@/misc/fetch-meta.js"; +import { PushSubscription } from "@/models/entities/push-subscription.js"; + +export class PushHelpers { + public static async subscribe(ctx: MastoContext): Promise { + const body = ctx.request.body as any; + + // This is undocumented on https://docs.joinmastodon.org/methods/push, + // but some apps appear to use query parameters instead of form data, so we need to normalize things + + if (typeof body['subscription[endpoint]'] === "string") { + body.subscription = { + endpoint: body['subscription[endpoint]'], + keys: { + p256dh: body['subscription[keys][p256dh]'], + auth: body['subscription[keys][auth]'], + }, + }; + + body.data = { + policy: body['policy'], + alerts: { + mention: body['data[alerts][mention]'], + status: body['data[alerts][status]'], + reblog: body['data[alerts][reblog]'], + follow: body['data[alerts][follow]'], + follow_request: body['data[alerts][follow_request]'], + favourite: body['data[alerts][favourite]'], + poll: body['data[alerts][poll]'], + update: body['data[alerts][update]'], + }, + }; + } + + if (typeof body?.subscription?.endpoint !== "string" || typeof body?.subscription?.keys?.p256dh !== "string" || typeof body?.subscription?.keys?.auth !== "string") + throw new MastoApiError(400, "Required parameters are missing or empty"); + + const data = body.subscription as MastodonEntity.PushData; + const user = ctx.user as ILocalUser; + const token = ctx.token as OAuthToken; + + const types: MastodonEntity.PushTypes = { + mention: !!body?.data?.alerts?.mention, + status: !!body?.data?.alerts?.status, + reblog: !!body?.data?.alerts?.reblog, + follow: !!body?.data?.alerts?.follow, + follow_request: !!body?.data?.alerts?.follow_request, + favourite: !!body?.data?.alerts?.favourite, + poll: !!body?.data?.alerts?.poll, + update: !!body?.data?.alerts?.update, + }; + + const policy: MastodonEntity.PushPolicy = ['all', 'followed', 'follower', 'none'].includes(body?.data?.policy) + ? body?.data?.policy + : 'all'; + + const subscription: Partial = { + id: genId(), + createdAt: new Date(), + data: data, + types: types, + policy: policy, + userId: user.id, + tokenId: token.id, + }; + + await PushSubscriptions.upsert(subscription, ['tokenId']); + + return { + id: subscription.id!, + alerts: types, + endpoint: data.endpoint, + server_key: await fetchMeta().then(meta => meta.swPublicKey), + } + } + + public static async get(ctx: MastoContext): Promise { + const token = ctx.token as OAuthToken; + const subscription = await PushSubscriptions.findOneBy({ tokenId: token.id }); + if (!subscription) throw new MastoApiError(404); + + return { + id: subscription.id, + alerts: subscription.types, + endpoint: subscription.data.endpoint, + server_key: await fetchMeta().then(meta => meta.swPublicKey), + } + } + + public static async update(ctx: MastoContext): Promise { + const body = ctx.request.body as any; + const token = ctx.token as OAuthToken; + + const types: MastodonEntity.PushTypes = { + mention: !!body?.data?.alerts?.mention, + status: !!body?.data?.alerts?.status, + reblog: !!body?.data?.alerts?.reblog, + follow: !!body?.data?.alerts?.follow, + follow_request: !!body?.data?.alerts?.follow_request, + favourite: !!body?.data?.alerts?.favourite, + poll: !!body?.data?.alerts?.poll, + update: !!body?.data?.alerts?.update, + }; + + const policy: MastodonEntity.PushPolicy = ['all', 'followed', 'follower', 'none'].includes(body?.data?.policy) + ? body?.data?.policy + : 'all'; + + const updates: Partial = { + types: types, + policy: policy, + }; + + const res = await PushSubscriptions.update({ tokenId: token.id }, updates); + if (!res.affected) throw new MastoApiError(404); + + return this.get(ctx); + } + + public static async unsubscribe(ctx: MastoContext): Promise<{}> { + const token = ctx.token as OAuthToken; + const res = await PushSubscriptions.delete({ tokenId: token.id }); + if (!res.affected) throw new MastoApiError(404); + return {}; + } +} diff --git a/packages/backend/src/server/api/mastodon/index.ts b/packages/backend/src/server/api/mastodon/index.ts index 365e6abad..eb04d9f8e 100644 --- a/packages/backend/src/server/api/mastodon/index.ts +++ b/packages/backend/src/server/api/mastodon/index.ts @@ -21,6 +21,7 @@ import { SetHeadersMiddleware } from "@/server/api/mastodon/middleware/set-heade import { UserHelpers } from "@/server/api/mastodon/helpers/user.js"; import { ILocalUser } from "@/models/entities/user.js"; import { setupEndpointsStreaming } from "@/server/api/mastodon/endpoints/streaming.js"; +import { setupEndpointsPush } from "@/server/api/mastodon/endpoints/push.js"; export const logger = apiLogger.createSubLogger("mastodon"); export type MastoContext = RouterContext & DefaultContext; @@ -38,6 +39,7 @@ export function setupMastodonApi(router: Router): void { setupEndpointsMedia(router); setupEndpointsList(router); setupEndpointsMisc(router); + setupEndpointsPush(router); } function setupMiddleware(router: Router): void { diff --git a/packages/backend/src/server/api/mastodon/middleware/auth.ts b/packages/backend/src/server/api/mastodon/middleware/auth.ts index 8b97c6da4..153e8bdc0 100644 --- a/packages/backend/src/server/api/mastodon/middleware/auth.ts +++ b/packages/backend/src/server/api/mastodon/middleware/auth.ts @@ -11,6 +11,7 @@ export async function AuthMiddleware(ctx: MastoContext, next: () => Promise ctx.appId = token?.appId; ctx.user = token?.user ?? null as ILocalUser | null; + ctx.token = token ?? null; ctx.scopes = token?.scopes ?? [] as string[]; await next(); diff --git a/packages/backend/src/server/api/mastodon/push/index.ts b/packages/backend/src/server/api/mastodon/push/index.ts new file mode 100644 index 000000000..417e217ca --- /dev/null +++ b/packages/backend/src/server/api/mastodon/push/index.ts @@ -0,0 +1,63 @@ +import { fetchMeta } from "@/misc/fetch-meta.js"; +import config from "@/config/index.js"; +import push from "web-push"; +import { PushSubscriptions } from "@/models/index.js"; +import { Notification } from "@/models/entities/notification.js"; +import { notificationTypes } from "@/types.js"; + +export class MastodonPushHandler { + public static async sendPushNotification(n: Notification) { + const id = n.id; + const userId = n.notifieeId; + const type = this.encodeType(n.type); + if (type === null) return; + + const meta = await fetchMeta(); + push.setVapidDetails(config.url, meta.swPublicKey, meta.swPrivateKey); + + for (const subscription of await PushSubscriptions.find({ where: { userId: userId }, relations: ['token'] })) { + if (!subscription.types[type]) continue; + + //FIXME: respect subscription.policy + + const data = { + access_token: subscription.token?.token, + title: meta.name ?? "Iceshrimp", + body: "You have unread notifications", + notification_id: id, + notification_type: type, + }; + + push.sendNotification(subscription.data, JSON.stringify(data), { proxy: config.proxy, contentEncoding: "aesgcm" }) + .catch((err: any) => { + if (err.statusCode === 410) { + PushSubscriptions.delete({ id: subscription.id }); + } + }); + } + } + + public static encodeType(type: typeof notificationTypes[number]): MastodonEntity.PushType | null { + switch(type) { + case "follow": + return "follow"; + case "mention": + case "reply": + return "mention"; + case "renote": + case "quote": + return "reblog"; + case "reaction": + return "favourite"; + case "pollEnded": + return "poll"; + case "receiveFollowRequest": + return "follow_request"; + case "followRequestAccepted": + case "groupInvited": + case "pollVote": + case "app": + return null; + } + } +} diff --git a/packages/backend/src/services/create-notification.ts b/packages/backend/src/services/create-notification.ts index 9bfd1a7bf..21943c537 100644 --- a/packages/backend/src/services/create-notification.ts +++ b/packages/backend/src/services/create-notification.ts @@ -13,6 +13,7 @@ import type { User } from "@/models/entities/user.js"; import type { Notification } from "@/models/entities/notification.js"; import { sendEmailNotification } from "./send-email-notification.js"; import { shouldSilenceInstance } from "@/misc/should-block-instance.js"; +import { MastodonPushHandler } from "@/server/api/mastodon/push/index.js"; export async function createNotification( notifieeId: User["id"], @@ -75,6 +76,7 @@ export async function createNotification( // Publish notification event publishMainStream(notifieeId, "notification", packed); + MastodonPushHandler.sendPushNotification(notification); // 2秒経っても(今回作成した)通知が既読にならなかったら「未読の通知がありますよ」イベントを発行する setTimeout(async () => {