Compare commits

...

2 Commits

Author SHA1 Message Date
Laura Hausmann c60ff90557
[mastodon-client] Switch to numeric notification ids
/ test-build (push) Successful in 1m3s Details
This appears to be required for push notification handling in most apps
2023-12-04 16:45:47 +01:00
Laura Hausmann 1cdf3c7097
[mastodon-client] Add basic support for push notifications 2023-12-04 16:45:11 +01:00
16 changed files with 367 additions and 11 deletions

View File

@ -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,
];

View File

@ -0,0 +1,22 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddMastoPush1701371327908 implements MigrationInterface {
name = 'AddMastoPush1701371327908'
public async up(queryRunner: QueryRunner): Promise<void> {
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<void> {
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"`);
}
}

View File

@ -0,0 +1,15 @@
import { MigrationInterface, QueryRunner } from "typeorm";
export class AddNotificationMastoId1701546940248 implements MigrationInterface {
name = 'AddNotificationMastoId1701546940248'
public async up(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`ALTER TABLE "notification" ADD "mastoId" SERIAL NOT NULL`);
await queryRunner.query(`CREATE UNIQUE INDEX "IDX_0ee0c7254e5612a8129251997e" ON "notification" ("mastoId") `);
}
public async down(queryRunner: QueryRunner): Promise<void> {
await queryRunner.query(`DROP INDEX "public"."IDX_0ee0c7254e5612a8129251997e"`);
await queryRunner.query(`ALTER TABLE "notification" DROP COLUMN "mastoId"`);
}
}

View File

@ -4,7 +4,7 @@ import {
JoinColumn,
ManyToOne,
Column,
PrimaryColumn,
PrimaryColumn, Generated,
} from "typeorm";
import { User } from "./user.js";
import { id } from "../id.js";
@ -181,4 +181,9 @@ export class Notification {
})
@JoinColumn()
public appAccessToken: AccessToken | null;
@Index({ unique: true })
@Column()
@Generated("increment")
public mastoId: number;
}

View File

@ -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;
}

View File

@ -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);

View File

@ -24,7 +24,7 @@ export class NotificationConverter {
: UserConverter.encode(localUser, ctx);
let result = {
id: notification.id,
id: notification.mastoId.toString(),
account: account,
created_at: notification.createdAt.toISOString(),
type: this.encodeNotificationType(notification.type),

View File

@ -20,7 +20,7 @@ export function setupEndpointsNotifications(router: Router): void {
auth(true, ['read:notifications']),
filterContext('notifications'),
async (ctx) => {
const notification = await NotificationHelpers.getNotificationOr404(ctx.params.id, ctx);
const notification = await NotificationHelpers.getPushNotificationOr404(Number.parseInt(ctx.params.id), ctx);
ctx.body = await NotificationConverter.encode(notification, ctx);
}
);
@ -36,7 +36,7 @@ export function setupEndpointsNotifications(router: Router): void {
router.post("/v1/notifications/:id/dismiss",
auth(true, ['write:notifications']),
async (ctx) => {
const notification = await NotificationHelpers.getNotificationOr404(ctx.params.id, ctx);
const notification = await NotificationHelpers.getPushNotificationOr404(Number.parseInt(ctx.params.id), ctx);
await NotificationHelpers.dismissNotification(notification.id, ctx);
ctx.body = {};
}

View File

@ -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);
}
);
}

View File

@ -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;
};
}

View File

@ -51,6 +51,18 @@ export class NotificationHelpers {
});
}
public static async getPushNotification(id: number, ctx: MastoContext): Promise<Notification | null> {
const user = ctx.user as ILocalUser;
return Notifications.findOneBy({ mastoId: id, notifieeId: user.id });
}
public static async getPushNotificationOr404(id: number, ctx: MastoContext): Promise<Notification> {
return this.getPushNotification(id, ctx).then(p => {
if (p) return p;
throw new MastoApiError(404);
});
}
public static async dismissNotification(id: string, ctx: MastoContext): Promise<void> {
const user = ctx.user as ILocalUser;
await Notifications.update({ id: id, notifieeId: user.id }, { isRead: true });

View File

@ -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<MastodonEntity.PushSubscription> {
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<PushSubscription> = {
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<MastodonEntity.PushSubscription> {
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<MastodonEntity.PushSubscription> {
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<PushSubscription> = {
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 {};
}
}

View File

@ -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 {

View File

@ -11,6 +11,7 @@ export async function AuthMiddleware(ctx: MastoContext, next: () => Promise<any>
ctx.appId = token?.appId;
ctx.user = token?.user ?? null as ILocalUser | null;
ctx.token = token ?? null;
ctx.scopes = token?.scopes ?? [] as string[];
await next();

View File

@ -0,0 +1,62 @@
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 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: n.mastoId,
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;
}
}
}

View File

@ -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 () => {