hippofish/packages/backend/src/server/api/StreamingApiServerService.ts

126 lines
3.8 KiB
TypeScript
Raw Normal View History

2022-09-17 20:27:08 +02:00
import { EventEmitter } from 'events';
import { Inject, Injectable } from '@nestjs/common';
import Redis from 'ioredis';
import * as websocket from 'websocket';
import { DI } from '@/di-symbols.js';
import type { UsersRepository, BlockingsRepository, ChannelFollowingsRepository, FollowingsRepository, MutingsRepository, UserProfilesRepository, RenoteMutingsRepository } from '@/models/index.js';
2022-09-20 22:33:11 +02:00
import type { Config } from '@/config.js';
2022-09-17 20:27:08 +02:00
import { NoteReadService } from '@/core/NoteReadService.js';
import { GlobalEventService } from '@/core/GlobalEventService.js';
import { NotificationService } from '@/core/NotificationService.js';
import { bindThis } from '@/decorators.js';
2023-04-05 03:21:10 +02:00
import { CacheService } from '@/core/CacheService.js';
2022-09-17 20:27:08 +02:00
import { AuthenticateService } from './AuthenticateService.js';
import MainStreamConnection from './stream/index.js';
import { ChannelsService } from './stream/ChannelsService.js';
import type { ParsedUrlQuery } from 'querystring';
import type * as http from 'node:http';
@Injectable()
export class StreamingApiServerService {
constructor(
@Inject(DI.config)
private config: Config,
@Inject(DI.redisForSub)
private redisForSub: Redis.Redis,
2022-09-17 20:27:08 +02:00
@Inject(DI.usersRepository)
private usersRepository: UsersRepository,
@Inject(DI.followingsRepository)
private followingsRepository: FollowingsRepository,
@Inject(DI.mutingsRepository)
private mutingsRepository: MutingsRepository,
@Inject(DI.renoteMutingsRepository)
private renoteMutingsRepository: RenoteMutingsRepository,
2022-09-17 20:27:08 +02:00
@Inject(DI.blockingsRepository)
private blockingsRepository: BlockingsRepository,
@Inject(DI.channelFollowingsRepository)
private channelFollowingsRepository: ChannelFollowingsRepository,
@Inject(DI.userProfilesRepository)
private userProfilesRepository: UserProfilesRepository,
2023-04-05 03:21:10 +02:00
private cacheService: CacheService,
2022-09-17 20:27:08 +02:00
private noteReadService: NoteReadService,
private authenticateService: AuthenticateService,
private channelsService: ChannelsService,
private notificationService: NotificationService,
) {
}
@bindThis
2022-09-17 20:27:08 +02:00
public attachStreamingApi(server: http.Server) {
// Init websocket server
const ws = new websocket.server({
httpServer: server,
});
ws.on('request', async (request) => {
const q = request.resourceURL.query as ParsedUrlQuery;
// TODO: トークンが間違ってるなどしてauthenticateに失敗したら
// コネクション切断するなりエラーメッセージ返すなりする
// (現状はエラーがキャッチされておらずサーバーのログに流れて邪魔なので)
const [user, miapp] = await this.authenticateService.authenticate(q.i as string);
if (user?.isSuspended) {
request.reject(400);
return;
}
const ev = new EventEmitter();
2022-09-23 23:45:44 +02:00
async function onRedisMessage(_: string, data: string): Promise<void> {
2022-09-17 20:27:08 +02:00
const parsed = JSON.parse(data);
ev.emit(parsed.channel, parsed.message);
}
this.redisForSub.on('message', onRedisMessage);
2022-09-17 20:27:08 +02:00
const main = new MainStreamConnection(
this.channelsService,
this.noteReadService,
this.notificationService,
2023-04-05 03:21:10 +02:00
this.cacheService,
ev, user, miapp,
2022-09-17 20:27:08 +02:00
);
2023-04-05 03:21:10 +02:00
await main.init();
const connection = request.accept();
main.init2(connection);
2022-09-17 20:27:08 +02:00
const intervalId = user ? setInterval(() => {
this.usersRepository.update(user.id, {
lastActiveDate: new Date(),
});
}, 1000 * 60 * 5) : null;
if (user) {
this.usersRepository.update(user.id, {
lastActiveDate: new Date(),
});
}
connection.once('close', () => {
ev.removeAllListeners();
main.dispose();
this.redisForSub.off('message', onRedisMessage);
2022-09-17 20:27:08 +02:00
if (intervalId) clearInterval(intervalId);
});
connection.on('message', async (data) => {
if (data.type === 'utf8' && data.utf8Data === 'ping') {
connection.send('pong');
}
});
});
}
}