import { Team } from '../models/team' ; interface FloodMessage { _id : string ; isFile : boolean ; timestamp : number ; userID : string ; } interface FloodControl { teams ? : { [ key ] : { channels ? : { [ key ] : { threads ? : { [ key ] : { messages ? : FloodMessage [ ] ; } ; } ; } ; } ; } ; } ; } const floodControl = { } ; const mainThreadName = 'main' ; const floodSeconds = parseInt ( process . env . FLOOD_CONTROL_SECONDS || '30' , 10 ) ; export function initFloodFeature ( ) { } function createObjects ( { teamID , channelID , threadID , } ) { const _threadID = threadID || mainThreadName ; floodControl . teams = floodControl . teams || { } ; floodControl . teams [ teamID ] = floodControl . teams [ teamID ] || { } ; const teamTree = floodControl . teams [ teamID ] ; teamTree . channels = teamTree . channels || { } ; teamTree . channels [ channelID ] = teamTree . channels [ channelID ] || { } ; const channelTree = teamTree . channels [ channelID ] ; channelTree . threads = channelTree . threads || { } ; channelTree . threads [ _threadID ] = channelTree . threads [ _threadID ] || { } ; const threadTree = channelTree . threads [ _threadID ] ; threadTree . messages = threadTree . messages || [ ] ; return floodControl ; } export function removeMessage ( { teamID , channelID , threadID , messageID , } ) { const _threadID = threadID || mainThreadName ; const flood = createObjects ( { teamID , channelID , threadID : _threadID } ) ; let messages = flood . teams ! [ teamID ] . channels ! [ channelID ] . threads ! [ _threadID ] . messages ! ; messages = messages . filter ( message => message . _id !== messageID ) ; flood . teams ! [ teamID ] . channels ! [ channelID ] . threads ! [ _threadID ] . messages = messages ; } export function registerMessage ( { teamID , channelID , threadID , messageID , userID , timestamp , isFile , } ) { const _threadID = threadID || mainThreadName ; const flood = createObjects ( { teamID , channelID , threadID : _threadID } ) ; flood . teams ! [ teamID ] . channels ! [ channelID ] . threads ! [ _threadID ] . messages ! . push ( { _id : messageID , isFile , timestamp , userID , } ) ; } export function registerTemporaryMessage ( { teamID , channelID , threadID , messageID , userID , timestamp , isFile = false , cleanAfterSeconds = floodSeconds , } ) { const _threadID = threadID || mainThreadName ; registerMessage ( { teamID , channelID , threadID : _threadID , messageID , userID , timestamp , isFile } ) ; setTimeout ( ( ) => { removeMessage ( { teamID , channelID , threadID : _threadID , messageID } ) ; } , cleanAfterSeconds * 1000 ) ; } export function isUserFloodingThread ( { teamID , channelID , threadID , userID , numberOfMessagesAllowed = 1 , } ) { const _threadID = threadID || mainThreadName ; const flood = createObjects ( { teamID , channelID , threadID : _threadID } ) ; const _messages = flood . teams ! [ teamID ] . channels ! [ channelID ] . threads ! [ _threadID ] . messages ! ; const normalMessages = _messages . filter ( message => message . userID === userID && ! message . isFile ) ; const fileMessages = _messages . filter ( message => message . userID === userID && ! ! message . isFile ) ; return ( normalMessages . length > numberOfMessagesAllowed || fileMessages . length > numberOfMessagesAllowed || normalMessages . length + fileMessages . length > numberOfMessagesAllowed + 1 ) ; }