add main stream

This commit is contained in:
tamaina 2021-09-06 00:55:40 +09:00
parent 08576c49de
commit 06c0373be9
3 changed files with 55 additions and 27 deletions

View file

@ -11,35 +11,34 @@ export default class extends Channel {
public async init(params: any) {
// Subscribe main stream channel
this.subscriber.on(`mainStream:${this.user!.id}`, async data => {
const { type } = data;
let { body } = data;
switch (type) {
switch (data.type) {
case 'notification': {
if (this.muting.has(body.userId)) return;
if (body.note && body.note.isHidden) {
const note = await Notes.pack(body.note.id, this.user, {
if (data.body.userId && this.muting.has(data.body.userId)) return;
// ????
if (data.body.note && data.body.note.isHidden) {
const note = await Notes.pack(data.body.note.id, this.user, {
detail: true
});
this.connection.cacheNote(note);
body.note = note;
data.body.note = note;
}
break;
}
case 'mention': {
if (this.muting.has(body.userId)) return;
if (body.isHidden) {
const note = await Notes.pack(body.id, this.user, {
if (this.muting.has(data.body.userId)) return;
if (data.body.isHidden) {
const note = await Notes.pack(data.body.id, this.user, {
detail: true
});
this.connection.cacheNote(note);
body = note;
data.body = note;
}
break;
}
}
this.send(type, body);
this.send(data.type, data.body);
});
}
}

View file

@ -15,7 +15,7 @@ import { UserProfile } from '@/models/entities/user-profile';
import { publishChannelStream, publishGroupMessagingStream, publishMessagingStream } from '@/services/stream';
import { UserGroup } from '@/models/entities/user-group';
import { PackedNote } from '@/models/repositories/note';
import { StreamEventEmitter, UserEvent } from './types';
import { StreamEventEmitter, UserEvents } from './types';
/**
* Main stream connection
@ -65,7 +65,7 @@ export default class Connection {
}
@autobind
private onUserEvent(ev: UserEvent) { // { type, body }と展開すると型も展開されてしまう
private onUserEvent(ev: UserEvents) { // { type, body }と展開すると型も展開されてしまう
switch (ev.type) {
case 'follow':
this.following.add(ev.body.id);

View file

@ -5,6 +5,8 @@ import StreamTypes from 'misskey-js/built/streaming.types';
import { Channel } from '@/models/entities/channel';
import { UserProfile } from '@/models/entities/user-profile';
import { PackedUser } from '@/models/repositories/user';
import { PackedNotification } from '@/models/repositories/notification';
import { PackedNote } from '@/models/repositories/note';
type Payload<T extends (payload: any) => void> = T extends (payload: infer P) => void ? P : never;
@ -14,11 +16,12 @@ type EventUnionFromDictionary<
U = { [K in keyof T]: { type: K; body: T[K]} }
> = U[keyof U];
export type BroadcastStream<T extends keyof StreamTypes.BroadcasrEvents> = {
name: 'broadcast';
type: T;
body: Payload<StreamTypes.BroadcasrEvents[T]>;
};
type EventUnionFromMkJSTypes<
T extends { [key: string]: ((payload: any) => void) | (() => void) },
U = { [K in keyof T]: { type: K; body: Payload<T[K]>} }
> = U[keyof U]
export type BroadcastStream = EventUnionFromMkJSTypes<StreamTypes.BroadcasrEvents>;
export interface UserEventTypes {
terminate: {};
@ -31,19 +34,45 @@ export interface UserEventTypes {
unfollow: PackedUser;
userAdded: PackedUser;
};
// UserList userRemoved: PackedUser;
export type UserEventName = `user:${User['id']}`;
export type UserEvent = EventUnionFromDictionary<UserEventTypes>;
export type UserEvents = EventUnionFromDictionary<UserEventTypes>;
export interface mainStreamTypes {
notification: PackedNotification;
mention: PackedNote;
reply: PackedNote;
renote: PackedNote;
follow: PackedUser;
followed: PackedUser;
unfollow: PackedUser;
meUpdated: PackedUser;
pageEvent: Payload<StreamTypes.Channels['main']['events']['pageEvent']>;
urlUploadFinished: Payload<StreamTypes.Channels['main']['events']['urlUploadFinished']>;
readAllNotifications: never;
unreadNotification: never;
unreadMention: never;
readAllUnreadMentions: never;
unreadSpecifiedNote: never;
readAllUnreadSpecifiedNotes: never;
readAllMessagingMessages: never;
unreadMessagingMessage: never;
readAllAntennas: never;
unreadAntenna: never;
readAllAnnouncements: never;
readAllChannels: never;
unreadChannel: never;
myTokenRegenerated: never;
};
export type mainStreamName = `mainStream:${User['id']}`;
export type mainStreams = EventUnionFromDictionary<mainStreamTypes>;
interface StreamEvents {
'broadcast': <T extends keyof StreamTypes.BroadcasrEvents>(e: BroadcastStream<T>) => void;
'broadcast': (e: BroadcastStream) => void;
}
interface AuthenticatedStreamEvents {
[key: UserEventName]: (e: UserEvent) => void;
[key: `mainStream:${User['id']}`]: (e: { type: string; body: any }) => void;
[key: UserEventName]: (e: UserEvents) => void;
[key: mainStreamName]: (e: mainStreams) => void;
[key: `driveStream:${User['id']}`]: (e: { type: string; body: any }) => void;
}