monkeeShark/src/server/api/stream/hybrid-timeline.ts

47 lines
1.2 KiB
TypeScript
Raw Normal View History

2018-07-11 00:36:30 +00:00
import * as websocket from 'websocket';
2018-07-29 22:20:27 +00:00
import Xev from 'xev';
2018-07-11 00:36:30 +00:00
import { IUser } from '../../../models/user';
import Mute from '../../../models/mute';
import { pack } from '../../../models/note';
export default async function(
request: websocket.request,
connection: websocket.connection,
2018-07-29 22:20:27 +00:00
subscriber: Xev,
2018-07-11 00:36:30 +00:00
user: IUser
) {
// Subscribe stream
2018-07-29 22:20:27 +00:00
subscriber.on('hybrid-timeline', onEvent);
subscriber.on(`hybrid-timeline:${user._id}`, onEvent);
2018-07-11 00:36:30 +00:00
const mute = await Mute.find({ muterId: user._id });
const mutedUserIds = mute.map(m => m.muteeId.toString());
2018-07-29 22:20:27 +00:00
async function onEvent(note: any) {
2018-07-11 00:36:30 +00:00
//#region 流れてきたNoteがミュートしているユーザーが関わるものだったら無視する
if (mutedUserIds.indexOf(note.userId) != -1) {
return;
}
if (note.reply != null && mutedUserIds.indexOf(note.reply.userId) != -1) {
return;
}
if (note.renote != null && mutedUserIds.indexOf(note.renote.userId) != -1) {
return;
}
//#endregion
// Renoteなら再pack
if (note.renoteId != null) {
note.renote = await pack(note.renoteId, user, {
detail: true
});
}
connection.send(JSON.stringify({
type: 'note',
body: note
}));
2018-07-29 22:20:27 +00:00
}
2018-07-11 00:36:30 +00:00
}