This commit is contained in:
tamaina
2021-09-05 23:47:41 +09:00
parent 65d9c304df
commit 987c98faec
4 changed files with 61 additions and 4 deletions

View File

@@ -15,6 +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';
/**
* Main stream connection
@@ -28,7 +29,7 @@ export default class Connection {
public followingChannels: Set<ChannelModel['id']> = new Set();
public token?: AccessToken;
private wsConnection: websocket.connection;
public subscriber: EventEmitter;
public subscriber: StreamEventEmitter;
private channels: Channel[] = [];
private subscribingNotes: any = {};
private cachedNotes: PackedNote[] = [];
@@ -57,14 +58,14 @@ export default class Connection {
this.updateFollowingChannels();
this.updateUserProfile();
this.subscriber.on(`user:${this.user.id}`, ({ type, body }) => {
this.onUserEvent(type, body);
this.subscriber.on(`user:${this.user.id}`, (ev) => {
this.onUserEvent(ev);
});
}
}
@autobind
private onUserEvent(type: string, body: any) {
private onUserEvent({ type, body }: UserEvent) {
switch (type) {
case 'follow':
this.following.add(body.id);

View File

@@ -0,0 +1,50 @@
import { User } from '@/models/entities/user';
import { EventEmitter } from 'events';
import Emitter from 'strict-event-emitter-types';
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';
type Payload<T extends (payload: any) => void> = T extends (payload: infer P) => void ? P : never;
// https://stackoverflow.com/questions/49311989/can-i-infer-the-type-of-a-value-using-extends-keyof-type
type EventUnionFromDictionary<
T extends object,
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]>;
};
export interface UserEventTypes {
terminate: {};
followChannel: Channel;
unfollowChannel: Channel;
updateUserProfile: UserProfile;
mute: User;
unmute: User;
follow: PackedUser;
unfollow: PackedUser;
userAdded: PackedUser;
};
// UserList userRemoved: PackedUser;
export type UserEventName = `user:${User['id']}`;
export type UserEvent = EventUnionFromDictionary<UserEventTypes>;
interface StreamEvents {
'broadcast': <T extends keyof StreamTypes.BroadcasrEvents>(e: BroadcastStream<T>) => void;
}
interface AuthenticatedStreamEvents {
[key: UserEventName]: <T extends keyof UserEventTypes>(e: UserEvent<T>) => void;
[key: `mainStream:${User['id']}`]: (e: { type: string; body: any }) => void;
[key: `driveStream:${User['id']}`]: (e: { type: string; body: any }) => void;
}
export type StreamEventEmitter = Emitter<EventEmitter, AuthenticatedStreamEvents & StreamEvents>;