2023-01-13 05:40:33 +01: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 {
|
2021-10-20 18:04:10 +02:00
|
|
|
StreamChannels,
|
|
|
|
AdminStreamTypes,
|
|
|
|
AntennaStreamTypes,
|
|
|
|
BroadcastTypes,
|
|
|
|
ChannelStreamTypes,
|
|
|
|
DriveStreamTypes,
|
|
|
|
GroupMessagingStreamTypes,
|
|
|
|
InternalStreamTypes,
|
|
|
|
MainStreamTypes,
|
|
|
|
MessagingIndexStreamTypes,
|
|
|
|
MessagingStreamTypes,
|
|
|
|
NoteStreamTypes,
|
|
|
|
UserListStreamTypes,
|
2021-12-09 15:58:30 +01:00
|
|
|
UserStreamTypes,
|
2023-01-13 05:40:33 +01:00
|
|
|
} from "@/server/api/stream/types.js";
|
2017-03-20 05:54:59 +01:00
|
|
|
|
2018-09-11 19:48:19 +02:00
|
|
|
class Publisher {
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2018-07-11 02:36:30 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public publishInternalEvent = <K extends keyof InternalStreamTypes>(
|
|
|
|
type: K,
|
|
|
|
value?: InternalStreamTypes[K],
|
|
|
|
): void => {
|
|
|
|
this.publish("internal", type, typeof value === "undefined" ? null : value);
|
2021-11-12 02:52:10 +01:00
|
|
|
};
|
2021-03-23 07:06:56 +01:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2021-03-21 07:14:03 +01:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public publishBroadcastStream = <K extends keyof BroadcastTypes>(
|
|
|
|
type: K,
|
|
|
|
value?: BroadcastTypes[K],
|
|
|
|
): void => {
|
|
|
|
this.publish(
|
|
|
|
"broadcast",
|
|
|
|
type,
|
|
|
|
typeof value === "undefined" ? null : value,
|
|
|
|
);
|
2021-11-12 02:52:10 +01:00
|
|
|
};
|
2020-04-02 15:17:17 +02:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2018-04-17 07:52:28 +02:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2017-05-24 13:50:17 +02:00
|
|
|
|
2023-01-13 05:40:33 +01:00
|
|
|
public publishNoteStream = <K extends keyof NoteStreamTypes>(
|
|
|
|
noteId: Note["id"],
|
|
|
|
type: K,
|
|
|
|
value?: NoteStreamTypes[K],
|
|
|
|
): void => {
|
2018-10-07 04:06:17 +02:00
|
|
|
this.publish(`noteStream:${noteId}`, type, {
|
|
|
|
id: noteId,
|
2021-12-09 15:58:30 +01:00
|
|
|
body: value,
|
2018-10-07 04:06:17 +02:00
|
|
|
});
|
2021-11-12 02:52:10 +01:00
|
|
|
};
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2021-02-20 12:20:05 +01:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2016-12-28 23:49:51 +01:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2020-01-29 20:37:25 +01:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2018-09-11 19:48:19 +02:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2019-05-18 13:36:33 +02:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2018-09-11 19:48:19 +02:00
|
|
|
|
2022-07-25 18:15:21 +02:00
|
|
|
public publishNotesStream = (note: Note): void => {
|
2023-01-13 05:40:33 +01:00
|
|
|
this.publish("notesStream", null, note);
|
2021-11-12 02:52:10 +01:00
|
|
|
};
|
2018-11-03 03:38:00 +01:00
|
|
|
|
2023-01-13 05:40:33 +01: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-12 02:52:10 +01:00
|
|
|
};
|
2018-07-30 00:20:27 +02:00
|
|
|
}
|
2018-09-11 19:48:19 +02:00
|
|
|
|
|
|
|
const publisher = new Publisher();
|
|
|
|
|
|
|
|
export default publisher;
|
|
|
|
|
2021-03-23 07:06:56 +01:00
|
|
|
export const publishInternalEvent = publisher.publishInternalEvent;
|
2021-03-21 07:14:03 +01:00
|
|
|
export const publishUserEvent = publisher.publishUserEvent;
|
2020-04-02 15:17:17 +02:00
|
|
|
export const publishBroadcastStream = publisher.publishBroadcastStream;
|
2018-10-07 04:06:17 +02:00
|
|
|
export const publishMainStream = publisher.publishMainStream;
|
2018-09-11 19:48:19 +02:00
|
|
|
export const publishDriveStream = publisher.publishDriveStream;
|
|
|
|
export const publishNoteStream = publisher.publishNoteStream;
|
2019-04-07 14:50:36 +02:00
|
|
|
export const publishNotesStream = publisher.publishNotesStream;
|
2021-02-20 12:20:05 +01:00
|
|
|
export const publishChannelStream = publisher.publishChannelStream;
|
2018-09-11 19:48:19 +02:00
|
|
|
export const publishUserListStream = publisher.publishUserListStream;
|
2020-01-29 20:37:25 +01:00
|
|
|
export const publishAntennaStream = publisher.publishAntennaStream;
|
2018-09-11 19:48:19 +02:00
|
|
|
export const publishMessagingStream = publisher.publishMessagingStream;
|
2023-01-13 05:40:33 +01:00
|
|
|
export const publishGroupMessagingStream =
|
|
|
|
publisher.publishGroupMessagingStream;
|
|
|
|
export const publishMessagingIndexStream =
|
|
|
|
publisher.publishMessagingIndexStream;
|
2019-01-27 06:55:02 +01:00
|
|
|
export const publishAdminStream = publisher.publishAdminStream;
|