jormungandr-bite/packages/backend/src/services/stream.ts

227 lines
5.5 KiB
TypeScript
Raw Normal View History

2023-01-12 21:40:33 -07:00
import { redisClient } from "../db/redis.js";
import type { User } from "@/models/entities/user.js";
import type { Note } from "@/models/entities/note.js";
import type { UserList } from "@/models/entities/user-list.js";
import type { UserGroup } from "@/models/entities/user-group.js";
import config from "@/config/index.js";
import type { Antenna } from "@/models/entities/antenna.js";
import type { Channel } from "@/models/entities/channel.js";
import type {
refactor: publishHogeStreamとStreamのEventEmitterに型定義する (#7769) * wip * wip * wip * :v: * add main stream * packedNotificationSchemaを更新 * read:gallery, write:gallery, read:gallery-likes, write:gallery-likesに翻訳を追加 * fix * ok * add header, choice, invitation * add header, choice, invitation * test * fix * fix * yatta * remove no longer needed "as PackedUser/PackedNote" * clean up * add simple-schema * fix lint * fix lint * wip * wip! * wip * fix * wip * wip * :v: * 送信側に型エラーがないことを3回確認した * :v: * wip * update typescript * define items in full Schema * edit comment * edit comment * edit comment * Update src/prelude/types.ts Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com> * https://github.com/misskey-dev/misskey/pull/7769#discussion_r703058458 * user packとnote packの型不整合を修正 * revert https://github.com/misskey-dev/misskey/pull/7772#discussion_r706627736 * revert https://github.com/misskey-dev/misskey/pull/7772#discussion_r706627736 * user packとnote packの型不整合を修正 * add prelude/types.ts * emoji * signin * game * matching * clean up * ev => data * refactor * clean up * add type * antenna * channel * fix * add Packed type * add PackedRef * fix lint * add emoji schema * add reversiGame * add reversiMatching * remove signin schema (use Signin entity) * add schemas refs, fix Packed type * wip PackedHoge => Packed<'Hoge'> * add Packed type * note-reaction * user * user-group * user-list * note * app, messaging-message * notification * drive-file * drive-folder * following * muting * blocking * hashtag * page * app (with modifying schema) * import user? * channel * antenna * clip * gallery-post * emoji * Packed * reversi-matching * update stream.ts * https://github.com/misskey-dev/misskey/pull/7769#issuecomment-917542339 * fix lint * clean up? * add changelog * add changelog * add changelog * fix: アンテナが既読にならないのを修正 * revert fix * https://github.com/misskey-dev/misskey/pull/7769#discussion_r711474875 * spec => payload * edit commetn Co-authored-by: Acid Chicken (硫酸鶏) <root@acid-chicken.com>
2021-10-20 10:04:10 -06:00
StreamChannels,
AdminStreamTypes,
AntennaStreamTypes,
BroadcastTypes,
ChannelStreamTypes,
DriveStreamTypes,
GroupMessagingStreamTypes,
InternalStreamTypes,
MainStreamTypes,
MessagingIndexStreamTypes,
MessagingStreamTypes,
NoteStreamTypes,
UserListStreamTypes,
2021-12-09 07:58:30 -07:00
UserStreamTypes,
2023-01-12 21:40:33 -07:00
} from "@/server/api/stream/types.js";
2017-03-19 22:54:59 -06:00
2018-09-11 11:48:19 -06:00
class Publisher {
2023-01-12 21:40:33 -07:00
private publish = (
channel: StreamChannels,
type: string | null,
value?: any,
): void => {
const message =
type == null
? value
: value == null
? { type: type, body: null }
: { type: type, body: value };
redisClient.publish(
config.host,
JSON.stringify({
channel: channel,
message: message,
}),
);
2021-11-11 18:52:10 -07:00
};
2018-07-10 18:36:30 -06:00
2023-01-12 21:40:33 -07:00
public publishInternalEvent = <K extends keyof InternalStreamTypes>(
type: K,
value?: InternalStreamTypes[K],
): void => {
this.publish("internal", type, typeof value === "undefined" ? null : value);
2021-11-11 18:52:10 -07:00
};
2021-03-23 00:06:56 -06:00
2023-01-12 21:40:33 -07:00
public publishUserEvent = <K extends keyof UserStreamTypes>(
userId: User["id"],
type: K,
value?: UserStreamTypes[K],
): void => {
this.publish(
`user:${userId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2023-01-12 21:40:33 -07:00
public publishBroadcastStream = <K extends keyof BroadcastTypes>(
type: K,
value?: BroadcastTypes[K],
): void => {
this.publish(
"broadcast",
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2020-04-02 07:17:17 -06:00
2023-01-12 21:40:33 -07:00
public publishMainStream = <K extends keyof MainStreamTypes>(
userId: User["id"],
type: K,
value?: MainStreamTypes[K],
): void => {
this.publish(
`mainStream:${userId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2023-01-12 21:40:33 -07:00
public publishDriveStream = <K extends keyof DriveStreamTypes>(
userId: User["id"],
type: K,
value?: DriveStreamTypes[K],
): void => {
this.publish(
`driveStream:${userId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2017-05-24 05:50:17 -06:00
2023-01-12 21:40:33 -07:00
public publishNoteStream = <K extends keyof NoteStreamTypes>(
noteId: Note["id"],
type: K,
value?: NoteStreamTypes[K],
): void => {
this.publish(`noteStream:${noteId}`, type, {
id: noteId,
2021-12-09 07:58:30 -07:00
body: value,
});
2021-11-11 18:52:10 -07:00
};
2016-12-28 15:49:51 -07:00
2023-01-12 21:40:33 -07:00
public publishChannelStream = <K extends keyof ChannelStreamTypes>(
channelId: Channel["id"],
type: K,
value?: ChannelStreamTypes[K],
): void => {
this.publish(
`channelStream:${channelId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2023-01-12 21:40:33 -07:00
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,
);
2021-11-11 18:52:10 -07:00
};
2016-12-28 15:49:51 -07:00
2023-01-12 21:40:33 -07:00
public publishAntennaStream = <K extends keyof AntennaStreamTypes>(
antennaId: Antenna["id"],
type: K,
value?: AntennaStreamTypes[K],
): void => {
this.publish(
`antennaStream:${antennaId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2023-01-12 21:40:33 -07:00
public publishMessagingStream = <K extends keyof MessagingStreamTypes>(
userId: User["id"],
otherpartyId: User["id"],
type: K,
value?: MessagingStreamTypes[K],
): void => {
this.publish(
`messagingStream:${userId}-${otherpartyId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2018-09-11 11:48:19 -06:00
2023-01-12 21:40:33 -07:00
public publishGroupMessagingStream = <
K extends keyof GroupMessagingStreamTypes,
>(
groupId: UserGroup["id"],
type: K,
value?: GroupMessagingStreamTypes[K],
): void => {
this.publish(
`messagingStream:${groupId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2019-05-18 05:36:33 -06:00
2023-01-12 21:40:33 -07:00
public publishMessagingIndexStream = <
K extends keyof MessagingIndexStreamTypes,
>(
userId: User["id"],
type: K,
value?: MessagingIndexStreamTypes[K],
): void => {
this.publish(
`messagingIndexStream:${userId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2018-09-11 11:48:19 -06:00
public publishNotesStream = (note: Note): void => {
2023-01-12 21:40:33 -07:00
this.publish("notesStream", null, note);
2021-11-11 18:52:10 -07:00
};
2018-11-02 20:38:00 -06:00
2023-01-12 21:40:33 -07:00
public publishAdminStream = <K extends keyof AdminStreamTypes>(
userId: User["id"],
type: K,
value?: AdminStreamTypes[K],
): void => {
this.publish(
`adminStream:${userId}`,
type,
typeof value === "undefined" ? null : value,
);
2021-11-11 18:52:10 -07:00
};
2018-07-29 16:20:27 -06:00
}
2018-09-11 11:48:19 -06:00
const publisher = new Publisher();
export default publisher;
2021-03-23 00:06:56 -06:00
export const publishInternalEvent = publisher.publishInternalEvent;
export const publishUserEvent = publisher.publishUserEvent;
2020-04-02 07:17:17 -06:00
export const publishBroadcastStream = publisher.publishBroadcastStream;
export const publishMainStream = publisher.publishMainStream;
2018-09-11 11:48:19 -06:00
export const publishDriveStream = publisher.publishDriveStream;
export const publishNoteStream = publisher.publishNoteStream;
Use PostgreSQL instead of MongoDB (#4572) * wip * Update note.ts * Update timeline.ts * Update core.ts * wip * Update generate-visibility-query.ts * wip * wip * wip * wip * wip * Update global-timeline.ts * wip * wip * wip * Update vote.ts * wip * wip * Update create.ts * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * wip * Update files.ts * wip * wip * Update CONTRIBUTING.md * wip * wip * wip * wip * wip * wip * wip * wip * Update read-notification.ts * wip * wip * wip * wip * wip * wip * wip * Update cancel.ts * wip * wip * wip * Update show.ts * wip * wip * Update gen-id.ts * Update create.ts * Update id.ts * wip * wip * wip * wip * wip * wip * wip * Docker: Update files about Docker (#4599) * Docker: Use cache if files used by `yarn install` was not updated This patch reduces the number of times to installing node_modules. For example, `yarn install` step will be skipped when only ".config/default.yml" is updated. * Docker: Migrate MongoDB to Postgresql Misskey uses Postgresql as a database instead of Mongodb since version 11. * Docker: Uncomment about data persistence This patch will save a lot of databases. * wip * wip * wip * Update activitypub.ts * wip * wip * wip * Update logs.ts * wip * Update drive-file.ts * Update register.ts * wip * wip * Update mentions.ts * wip * wip * wip * Update recommendation.ts * wip * Update index.ts * wip * Update recommendation.ts * Doc: Update docker.ja.md and docker.en.md (#1) (#4608) Update how to set up misskey. * wip * :v: * wip * Update note.ts * Update postgre.ts * wip * wip * wip * wip * Update add-file.ts * wip * wip * wip * Clean up * Update logs.ts * wip * :pizza: * wip * Ad notes * wip * Update api-visibility.ts * Update note.ts * Update add-file.ts * tests * tests * Update postgre.ts * Update utils.ts * wip * wip * Refactor * wip * Refactor * wip * wip * Update show-users.ts * Update update-instance.ts * wip * Update feed.ts * Update outbox.ts * Update outbox.ts * Update user.ts * wip * Update list.ts * Update update-hashtag.ts * wip * Update update-hashtag.ts * Refactor * Update update.ts * wip * wip * :v: * clean up * docs * Update push.ts * wip * Update api.ts * wip * :v: * Update make-pagination-query.ts * :v: * Delete hashtags.ts * Update instances.ts * Update instances.ts * Update create.ts * Update search.ts * Update reversi-game.ts * Update signup.ts * Update user.ts * id * Update example.yml * :art: * objectid * fix * reversi * reversi * Fix bug of chart engine * Add test of chart engine * Improve test * Better testing * Improve chart engine * Refactor * Add test of chart engine * Refactor * Add chart test * Fix bug * コミットし忘れ * Refactoring * :v: * Add tests * Add test * Extarct note tests * Refactor * 存在しないユーザーにメンションできなくなっていた問題を修正 * Fix bug * Update update-meta.ts * Fix bug * Update mention.vue * Fix bug * Update meta.ts * Update CONTRIBUTING.md * Fix bug * Fix bug * Fix bug * Clean up * Clean up * Update notification.ts * Clean up * Add mute tests * Add test * Refactor * Add test * Fix test * Refactor * Refactor * Add tests * Update utils.ts * Update utils.ts * Fix test * Update package.json * Update update.ts * Update manifest.ts * Fix bug * Fix bug * Add test * :art: * Update endpoint permissions * Updaye permisison * Update person.ts #4299 * データベースと同期しないように * Fix bug * Fix bug * Update reversi-game.ts * Use a feature of Node v11.7.0 to extract a public key (#4644) * wip * wip * :v: * Refactoring #1540 * test * test * test * test * test * test * test * Fix bug * Fix test * :sushi: * wip * #4471 * Add test for #4335 * Refactor * Fix test * Add tests * :clock4: * Fix bug * Add test * Add test * rename * Fix bug
2019-04-07 06:50:36 -06:00
export const publishNotesStream = publisher.publishNotesStream;
export const publishChannelStream = publisher.publishChannelStream;
2018-09-11 11:48:19 -06:00
export const publishUserListStream = publisher.publishUserListStream;
export const publishAntennaStream = publisher.publishAntennaStream;
2018-09-11 11:48:19 -06:00
export const publishMessagingStream = publisher.publishMessagingStream;
2023-01-12 21:40:33 -07:00
export const publishGroupMessagingStream =
publisher.publishGroupMessagingStream;
export const publishMessagingIndexStream =
publisher.publishMessagingIndexStream;
export const publishAdminStream = publisher.publishAdminStream;