desc logic import { getTime } from 'date-fns' ; import { _authUser } from "../utils/utils" ; import { getHash , totalOnline } from "../redis/redis" ; import { flipPage , getCount , insertOne } from "../mongo/curd" ; import { _pushSuccess } from "../app" ; desc export const talkIn = async ( socket , sid , data , channel , eventName ) => { await _authUser ( socket , sid , data , channel , eventName , 'noAuth' ) ; const redisObj = await getHash ( sid ) || { } ; const talkInObj = { sid , name : redisObj ? redisObj . name : "" , avatarUrl : redisObj ? redisObj . avatar_url : "" , isAdmin : redisObj ? redisObj . isAdmin : false , message : data , channel : channel , eventName , createTimestamp : getTime ( new Date ( ) ) } ; const { _id } = await insertOne ( talkInObj , 'barrages' ) ; talkInObj . _id = _id ; await _pushSuccess ( 'broadcast' , 'talk' , talkInObj , 'talking' ) ; } ; desc sid export const getBarrageList = async ( io , socket , eventName ) => { const { id } = socket ; await socket . on ( eventName , async ( data ) => { const { page = 1 , size = 10 } = data || { } ; const theCount = await getCount ( { } , 'barrages' ) ; const countDivideTen = Math . ceil ( theCount / size ) || 1 ; let pushData = [ ] ; if ( page <= countDivideTen ) { pushData = await flipPage ( 'barrages' , page * size - size , size , [ 'avatarUrl' , 'name' , 'message' ] ) ; } await io . of ( '/broadcast' ) . to ( id ) . emit ( 'getTalk' , { list : pushData || [ ] , count : countDivideTen } ) ; } ) ; } ;