|
|
@@ -1,6 +1,6 @@
|
|
|
import { Inject, Injectable, Logger, OnModuleInit, forwardRef } from '@nestjs/common'
|
|
|
import { InjectRepository } from '@nestjs/typeorm'
|
|
|
-import { In, MoreThan, Repository } from 'typeorm'
|
|
|
+import { In, Like, MoreThan, Repository } from 'typeorm'
|
|
|
import { Danmu } from './entities/danmu.enitity'
|
|
|
import axios from 'axios'
|
|
|
import { Client } from 'tmi.js'
|
|
|
@@ -9,6 +9,9 @@ import { StreamPlatform } from '../common/enums/stream-platform.enum'
|
|
|
import { DanmuUser } from './entities/danmu-user.entity'
|
|
|
import { RoomService } from '../room/room.service'
|
|
|
import { Room } from '../room/entities/room.entity'
|
|
|
+import { mkdtempSync, rmSync } from 'fs'
|
|
|
+import { FileService } from 'src/file/file.service'
|
|
|
+import path = require('path')
|
|
|
|
|
|
@Injectable()
|
|
|
export class DanmuService implements OnModuleInit {
|
|
|
@@ -23,7 +26,8 @@ export class DanmuService implements OnModuleInit {
|
|
|
@InjectRepository(DanmuUser)
|
|
|
private readonly danmuUserRepository: Repository<DanmuUser>,
|
|
|
@Inject(forwardRef(() => RoomService))
|
|
|
- private readonly roomService: RoomService
|
|
|
+ private readonly roomService: RoomService,
|
|
|
+ private readonly fileService: FileService
|
|
|
) {}
|
|
|
|
|
|
async onModuleInit() {
|
|
|
@@ -119,6 +123,7 @@ export class DanmuService implements OnModuleInit {
|
|
|
danmuUserId: danmuUser.id,
|
|
|
channel,
|
|
|
platformRoomId: context['room-id'],
|
|
|
+ platformUserId: platformUserId,
|
|
|
content: msg,
|
|
|
roomId: room.id,
|
|
|
gameId: room.currentGameId
|
|
|
@@ -166,18 +171,46 @@ export class DanmuService implements OnModuleInit {
|
|
|
})
|
|
|
}
|
|
|
|
|
|
- async pickAsCharactor(gameId: number, startTime: Date) {
|
|
|
- const res = await this.danmuRepository
|
|
|
- .createQueryBuilder('danmu')
|
|
|
- .select()
|
|
|
- .where('danmu.gameId = :gameId', { gameId })
|
|
|
- .andWhere('danmu.createdAt > :startTime', { startTime })
|
|
|
- .andWhere('danmu.content like :charactor', { charactor: '扮演%' })
|
|
|
- .getRawMany()
|
|
|
+ async pickCharactor(gameId: number, startTime: Date) {
|
|
|
+ const res = await this.danmuRepository.find({
|
|
|
+ where: {
|
|
|
+ gameId,
|
|
|
+ createdAt: MoreThan(startTime),
|
|
|
+ content: Like('我是%')
|
|
|
+ }
|
|
|
+ })
|
|
|
if (res.length === 0) {
|
|
|
return null
|
|
|
}
|
|
|
const index = Math.floor(Math.random() * res.length)
|
|
|
return res[index]
|
|
|
}
|
|
|
+
|
|
|
+ async findDanmuUser(danmuUserId: number) {
|
|
|
+ const danmuUser = await this.danmuUserRepository.findOneBy({
|
|
|
+ id: danmuUserId
|
|
|
+ })
|
|
|
+ const {
|
|
|
+ data: {
|
|
|
+ data: [user]
|
|
|
+ }
|
|
|
+ } = await axios.get('https://api.twitch.tv/helix/users', {
|
|
|
+ headers: {
|
|
|
+ Authorization: `Bearer ${await this.getTwitchAppAccessToken()}`,
|
|
|
+ 'Client-ID': process.env.TWITCH_CLIENT_ID
|
|
|
+ },
|
|
|
+ params: {
|
|
|
+ id: danmuUser.platformUserId
|
|
|
+ }
|
|
|
+ })
|
|
|
+ const { data } = await axios.get(user.profile_image_url, { responseType: 'arraybuffer' })
|
|
|
+ danmuUser.avatar = (
|
|
|
+ await this.fileService.uploadBuffer(
|
|
|
+ data,
|
|
|
+ `avatar/${danmuUser.platform}/${danmuUser.platformUserId}.${path.extname(user.profile_image_url)}`
|
|
|
+ )
|
|
|
+ ).url
|
|
|
+ danmuUser.name = user.display_name
|
|
|
+ return await this.danmuUserRepository.save(danmuUser)
|
|
|
+ }
|
|
|
}
|