Removed notification endpoint

This commit is contained in:
Natty 2024-02-28 17:24:08 +01:00
parent 786f3626af
commit da6de92b5a
Signed by: natty
GPG Key ID: BF6CB659ADEE60EC
3 changed files with 1 additions and 220 deletions

View File

@ -1,13 +1,10 @@
import {In} from "typeorm";
import {Notification} from "@/models/entities/notification.js";
import {awaitAll} from "@/prelude/await-all.js";
import type {Packed} from "@/misc/schema.js";
import type {Note} from "@/models/entities/note.js";
import type {NoteReaction} from "@/models/entities/note-reaction.js";
import type {User} from "@/models/entities/user.js";
import {aggregateNoteEmojis, prefetchEmojis} from "@/misc/populate-emojis.js";
import {db} from "@/db/postgre.js";
import {AccessTokens, NoteReactions, Users,} from "../index.js";
import {AccessTokens, Users,} from "../index.js";
import {getNote} from "@/server/api/common/getters.js";
export const NotificationRepository = db.getRepository(Notification).extend({
@ -85,42 +82,4 @@ export const NotificationRepository = db.getRepository(Notification).extend({
: {}),
});
},
async packMany(notifications: Notification[], meId: User["id"]) {
if (notifications.length === 0) return [];
const notes = notifications
.filter((x) => x.note != null)
.map((x) => x.note!);
const noteIds = notes.map((n) => n.id);
const myReactionsMap = new Map<Note["id"], NoteReaction | null>();
const renoteIds = notes
.filter((n) => n.renoteId != null)
.map((n) => n.renoteId!);
const targets = [...noteIds, ...renoteIds];
const myReactions = await NoteReactions.findBy({
userId: meId,
noteId: In(targets),
});
for (const target of targets) {
myReactionsMap.set(
target,
myReactions.find((reaction) => reaction.noteId === target) || null,
);
}
await prefetchEmojis(aggregateNoteEmojis(notes));
const results = await Promise.all(
notifications.map((x) =>
this.pack(x, {
_hintForEachNotes_: {
myReactions: myReactionsMap,
},
}).catch((e) => null),
),
);
return results.filter((x) => x != null);
},
});

View File

@ -171,7 +171,6 @@ import * as ep___i_importBlocking from "./endpoints/i/import-blocking.js";
import * as ep___i_importFollowing from "./endpoints/i/import-following.js";
import * as ep___i_importMuting from "./endpoints/i/import-muting.js";
import * as ep___i_importUserLists from "./endpoints/i/import-user-lists.js";
import * as ep___i_notifications from "./endpoints/i/notifications.js";
import * as ep___i_pin from "./endpoints/i/pin.js";
import * as ep___i_readAllUnreadNotes from "./endpoints/i/read-all-unread-notes.js";
import * as ep___i_readAnnouncement from "./endpoints/i/read-announcement.js";
@ -463,7 +462,6 @@ const eps = [
["i/import-following", ep___i_importFollowing],
["i/import-muting", ep___i_importMuting],
["i/import-user-lists", ep___i_importUserLists],
["i/notifications", ep___i_notifications],
["i/pin", ep___i_pin],
["i/read-all-unread-notes", ep___i_readAllUnreadNotes],
["i/read-announcement", ep___i_readAnnouncement],

View File

@ -1,176 +0,0 @@
import {Brackets} from "typeorm";
import {Followings, Mutings, Notifications, UserProfiles, Users,} from "@/models/index.js";
import {notificationTypes} from "@/types.js";
import read from "@/services/note/read.js";
import {readNotification} from "../../common/read-notification.js";
import define from "../../define.js";
import {makePaginationQuery} from "../../common/make-pagination-query.js";
export const meta = {
tags: ["account", "notifications"],
requireCredential: true,
limit: {
duration: 60000,
max: 15,
},
kind: "read:notifications",
res: {
type: "array",
optional: false,
nullable: false,
items: {
type: "object",
optional: false,
nullable: false,
ref: "Notification",
},
},
} as const;
export const paramDef = {
type: "object",
properties: {
limit: { type: "integer", minimum: 1, maximum: 100, default: 10 },
sinceId: { type: "string", format: "misskey:id" },
untilId: { type: "string", format: "misskey:id" },
sinceDate: { type: "integer" },
untilDate: { type: "integer" },
following: { type: "boolean", default: false },
unreadOnly: { type: "boolean", default: false },
markAsRead: { type: "boolean", default: true },
includeTypes: {
type: "array",
items: {
type: "string",
enum: notificationTypes,
},
},
excludeTypes: {
type: "array",
items: {
type: "string",
enum: notificationTypes,
},
},
},
required: [],
} as const;
export default define(meta, paramDef, async (ps, user) => {
// includeTypes が空の場合はクエリしない
if (ps.includeTypes && ps.includeTypes.length === 0) {
return [];
}
// excludeTypes に全指定されている場合はクエリしない
if (notificationTypes.every((type) => ps.excludeTypes?.includes(type))) {
return [];
}
const followingQuery = Followings.createQueryBuilder("following")
.select("following.followeeId")
.where("following.followerId = :followerId", { followerId: user.id });
const mutingQuery = Mutings.createQueryBuilder("muting")
.select("muting.muteeId")
.where("muting.muterId = :muterId", { muterId: user.id });
const mutingInstanceQuery = UserProfiles.createQueryBuilder("user_profile")
.select("user_profile.mutedInstances")
.where("user_profile.userId = :muterId", { muterId: user.id });
const suspendedQuery = Users.createQueryBuilder("users")
.select("users.id")
.where("users.isSuspended = TRUE");
const query = makePaginationQuery(
Notifications.createQueryBuilder("notification"),
ps.sinceId,
ps.untilId,
ps.sinceDate,
ps.untilDate
)
.andWhere("notification.notifieeId = :meId", { meId: user.id })
.leftJoinAndSelect("notification.notifier", "notifier")
.leftJoinAndSelect("notification.note", "note")
.leftJoinAndSelect("note.user", "user")
.leftJoinAndSelect("note.reply", "reply")
.leftJoinAndSelect("note.renote", "renote")
.leftJoinAndSelect("reply.user", "replyUser")
.leftJoinAndSelect("renote.user", "renoteUser");
// muted users
query.andWhere(
new Brackets((qb) => {
qb.where(
`notification.notifierId NOT IN (${mutingQuery.getQuery()})`,
).orWhere("notification.notifierId IS NULL");
}),
);
query.setParameters(mutingQuery.getParameters());
// muted instances
query.andWhere(
new Brackets((qb) => {
qb.andWhere("notifier.host IS NULL").orWhere(
`NOT (( ${mutingInstanceQuery.getQuery()} )::jsonb ? notifier.host)`,
);
}),
);
query.setParameters(mutingInstanceQuery.getParameters());
// suspended users
query.andWhere(
new Brackets((qb) => {
qb.where(
`notification.notifierId NOT IN (${suspendedQuery.getQuery()})`,
).orWhere("notification.notifierId IS NULL");
}),
);
if (ps.following) {
query.andWhere(
`((notification.notifierId IN (${followingQuery.getQuery()})) OR (notification.notifierId = :meId))`,
{ meId: user.id },
);
query.setParameters(followingQuery.getParameters());
}
if (ps.includeTypes && ps.includeTypes.length > 0) {
query.andWhere("notification.type IN (:...includeTypes)", {
includeTypes: ps.includeTypes,
});
} else if (ps.excludeTypes && ps.excludeTypes.length > 0) {
query.andWhere("notification.type NOT IN (:...excludeTypes)", {
excludeTypes: ps.excludeTypes,
});
}
if (ps.unreadOnly) {
query.andWhere("notification.isRead = false");
}
const notifications = await query.take(ps.limit).getMany();
// Mark all as read
if (notifications.length > 0 && ps.markAsRead) {
readNotification(
user.id,
notifications.map((x) => x.id),
);
}
const notes = notifications
.filter((notification) =>
["mention", "reply", "quote"].includes(notification.type),
)
.map((notification) => notification.note!);
if (notes.length > 0) {
read(user.id, notes);
}
return await Notifications.packMany(notifications, user.id);
});