refactor (backend): remove publishUserListStream

This commit is contained in:
naskya 2024-07-25 07:29:21 +09:00
parent 7d2e98963d
commit 0ba868cc3c
No known key found for this signature in database
GPG key ID: 712D413B3A9FED5C
8 changed files with 25 additions and 80 deletions

View file

@ -32,9 +32,6 @@ pub enum Stream {
},
Notes,
NoteEdit,
UserList {
list_id: String,
},
Main {
user_id: String,
},
@ -90,7 +87,6 @@ pub async fn publish_to_stream(
Stream::Note { note_id } => format!("noteStream:{note_id}"),
Stream::NoteEdit => format!("noteUpdatesStream"),
Stream::Notes => "notesStream".to_owned(),
Stream::UserList { list_id } => format!("userListStream:{list_id}"),
Stream::Main { user_id } => format!("mainStream:{user_id}"),
Stream::Drive { user_id } => format!("driveStream:{user_id}"),
Stream::Antenna { antenna_id } => format!("antennaStream:{antenna_id}"),

View file

@ -1,8 +1,6 @@
import { publishUserListStream } from "@/services/stream.js";
import { UserLists, UserListJoinings, Users } from "@/models/index.js";
import define from "@/server/api/define.js";
import { ApiError } from "@/server/api/error.js";
import { getUser } from "@/server/api/common/getters.js";
export const meta = {
tags: ["lists", "users"],
@ -38,25 +36,23 @@ export const paramDef = {
} as const;
export default define(meta, paramDef, async (ps, me) => {
// Fetch the list
const userList = await UserLists.findOneBy({
id: ps.listId,
userId: me.id,
});
const [userExists, listExists] = await Promise.all([
Users.existsBy({
id: ps.userId,
}),
UserLists.existsBy({
id: ps.listId,
userId: me.id,
}),
]);
if (userList == null) {
if (!userExists) {
throw new ApiError(meta.errors.noSuchUser);
}
if (!listExists) {
throw new ApiError(meta.errors.noSuchList);
}
// Fetch the user
const user = await getUser(ps.userId).catch((e) => {
if (e.id === "15348ddd-432d-49c2-8a5a-8069753becff")
throw new ApiError(meta.errors.noSuchUser);
throw e;
});
// Pull the user
await UserListJoinings.delete({ userListId: userList.id, userId: user.id });
publishUserListStream(userList.id, "userRemoved", await Users.pack(user));
// Remove the user from the list
await UserListJoinings.delete({ userListId: ps.listId, userId: ps.userId });
});

View file

@ -12,8 +12,6 @@ import { pushUserToUserList } from "@/services/user-list/push.js";
import { genId } from "backend-rs";
import { MastoApiError } from "@/server/api/mastodon/middleware/catch-errors.js";
import type { MastoContext } from "@/server/api/mastodon/index.js";
import { pullUserFromUserList } from "@/services/user-list/pull.js";
import { publishUserEvent } from "@/services/stream.js";
export class ListHelpers {
public static async getLists(
@ -123,14 +121,12 @@ export class ListHelpers {
throw Error("Cant add users youre not following to list");
}
const exist = await UserListJoinings.exists({
where: {
userListId: list.id,
userId: user.id,
},
const exists = await UserListJoinings.existsBy({
userListId: list.id,
userId: user.id,
});
if (exist) continue;
if (exists) continue;
await pushUserToUserList(user, list);
}
}
@ -144,15 +140,13 @@ export class ListHelpers {
if (localUser.id !== list.userId)
throw new Error("List is not owned by user");
for (const user of usersToRemove) {
const exist = await UserListJoinings.exists({
where: {
userListId: list.id,
userId: user.id,
},
const exists = await UserListJoinings.existsBy({
userListId: list.id,
userId: user.id,
});
if (!exist) continue;
await pullUserFromUserList(user, list);
if (!exists) continue;
await UserListJoinings.delete({ userListId: list.id, userId: user.id });
}
}

View file

@ -29,12 +29,10 @@ export default class extends Channel {
if (!exists) return;
// Subscribe stream
this.subscriber.on(`userListStream:${this.listId}`, this.send);
this.subscriber.on("notesStream", this.onNote);
this.updateListUsers();
this.listUsersClock = setInterval(this.updateListUsers, 5000);
this.listUsersClock = setInterval(this.updateListUsers, 10000);
}
private async updateListUsers() {
@ -65,7 +63,6 @@ export default class extends Channel {
public dispose() {
// Unsubscribe events
this.subscriber.off(`userListStream:${this.listId}`, this.send);
this.subscriber.off("notesStream", this.onNote);
clearInterval(this.listUsersClock);

View file

@ -7,7 +7,6 @@ import type { Note } from "@/models/entities/note.js";
import type { Antenna } from "@/models/entities/antenna.js";
import type { DriveFile } from "@/models/entities/drive-file.js";
import type { DriveFolder } from "@/models/entities/drive-folder.js";
import type { UserList } from "@/models/entities/user-list.js";
import type { MessagingMessage } from "@/models/entities/messaging-message.js";
import type { UserGroup } from "@/models/entities/user-group.js";
import type { AbuseUserReport } from "@/models/entities/abuse-user-report.js";
@ -165,11 +164,6 @@ export interface ChannelStreamTypes {
typing: User["id"];
}
export interface UserListStreamTypes {
userAdded: Packed<"User">;
userRemoved: Packed<"User">;
}
export interface AntennaStreamTypes {
note: Note;
}
@ -242,10 +236,6 @@ export type StreamMessages = {
name: `channelStream:${Channel["id"]}`;
payload: EventUnionFromDictionary<ChannelStreamTypes>;
};
userList: {
name: `userListStream:${UserList["id"]}`;
payload: EventUnionFromDictionary<UserListStreamTypes>;
};
antenna: {
name: `antennaStream:${Antenna["id"]}`;
payload: EventUnionFromDictionary<AntennaStreamTypes>;

View file

@ -19,7 +19,6 @@ import type {
// MessagingIndexStreamTypes,
// MessagingStreamTypes,
NoteStreamTypes,
UserListStreamTypes,
UserStreamTypes,
// NoteUpdatesStreamTypes,
} from "@/server/api/stream/types.js";
@ -134,18 +133,6 @@ class Publisher {
// );
// };
public publishUserListStream = <K extends keyof UserListStreamTypes>(
listId: UserList["id"],
type: K,
value?: UserListStreamTypes[K],
): void => {
this.publish(
`userListStream:${listId}`,
type,
typeof value === "undefined" ? null : value,
);
};
/* ported to backend-rs */
// public publishAntennaStream = <K extends keyof AntennaStreamTypes>(
// antennaId: Antenna["id"],
@ -235,7 +222,6 @@ export const publishNoteStream = publisher.publishNoteStream;
// export const publishNotesStream = publisher.publishNotesStream;
// export const publishNoteUpdatesStream = publisher.publishNoteUpdatesStream;
// export const publishChannelStream = publisher.publishChannelStream;
export const publishUserListStream = publisher.publishUserListStream;
// export const publishAntennaStream = publisher.publishAntennaStream;
// export const publishMessagingStream = publisher.publishMessagingStream;
// export const publishGroupMessagingStream = publisher.publishGroupMessagingStream;

View file

@ -1,11 +0,0 @@
import { publishUserListStream } from "@/services/stream.js";
import type { User } from "@/models/entities/user.js";
import type { UserList } from "@/models/entities/user-list.js";
import { UserListJoinings, Users } from "@/models/index.js";
export async function pullUserFromUserList(target: User, list: UserList) {
await UserListJoinings.delete({ userListId: list.id, userId: target.id });
const packed = await Users.pack(target);
publishUserListStream(list.id, "userRemoved", packed);
}

View file

@ -1,4 +1,3 @@
import { publishUserListStream } from "@/services/stream.js";
import type { User } from "@/models/entities/user.js";
import type { UserList } from "@/models/entities/user-list.js";
import { UserListJoinings, Users } from "@/models/index.js";
@ -17,8 +16,6 @@ export async function pushUserToUserList(target: User, list: UserList) {
userListId: list.id,
} as UserListJoining);
publishUserListStream(list.id, "userAdded", await Users.pack(target));
// このインスタンス内にこのリモートユーザーをフォローしているユーザーがいなくても投稿を受け取るためにダミーのユーザーがフォローしたということにする
if (Users.isRemoteUser(target)) {
const proxy = await fetchProxyAccount();