import StorageProvider from '../types/StorageProvider' ; import Message from '../types/Message' ; import Room from '../types/Room' ; import { Db , MongoClient } from 'mongodb' ; import IgnoreChatInfo from '../types/IgnoreChatInfo' ; export default class MongoStorageProvider implements StorageProvider { id : string | number ; connStr : string ; mdb : Db ; constructor ( connStr , id ) { this . id = id ; this . connStr = connStr ; } removeIgnoredChat ( id ) { return this . mdb . collection ( 'ignoredChats' ) . deleteOne ( { id } ) ; } async getAllRooms ( ) { try { return await this . mdb . collection ( 'rooms' ) . find ( { } , { sort : [ [ 'utime' , - 1 ] ] } ) . toArray ( ) ; } catch ( e ) { return [ ] ; } } async connect ( ) { const dba = await MongoClient . connect ( this . connStr ) ; this . mdb = dba . db ( 'eqq' + this . id ) ; await this . mdb . collection ( 'rooms' ) . createIndex ( 'roomId' , { background : true , unique : true , } ) ; await this . mdb . collection ( 'rooms' ) . createIndex ( { utime : - 1 } , { background : true , } ) ; const rooms = await this . getAllRooms ( ) ; for ( const i of rooms ) { await this . mdb . collection ( 'msg' + i . roomId ) . createIndex ( { time : - 1 } , { background : true , } ) ; } await this . mdb . collection ( 'ignoredChats' ) . createIndex ( 'id' , { background : true , unique : true , } ) ; } async addMessage ( roomId , message ) { try { return await this . mdb . collection ( 'msg' + roomId ) . insertOne ( message as object ) ; } catch ( e ) { } } async addRoom ( room ) { try { return await this . mdb . collection ( 'rooms' ) . insertOne ( room ) ; } catch ( e ) { } } async updateMessage ( roomId , messageId , message ) { try { return await this . mdb . collection ( 'msg' + roomId ) . updateOne ( { _id : messageId } , { $set : message } ) ; } catch ( e ) { } } async fetchMessages ( roomId , skip , limit ) { const arr = await this . mdb . collection < any > ( 'msg' + roomId ) . find ( { } , { sort : [ [ 'time' , - 1 ] ] , skip , limit , } ) . toArray ( ) ; return arr . reverse ( ) ; } async removeRoom ( roomId ) { try { return await this . mdb . collection ( 'rooms' ) . findOneAndDelete ( { roomId : roomId } ) ; } catch ( e ) { } } async updateRoom ( roomId , room ) { try { return await this . mdb . collection ( 'rooms' ) . updateOne ( { roomId : roomId } , { $set : room } ) ; } catch ( e ) { } } getMessage ( roomId , messageId ) { return this . mdb . collection < any > ( 'msg' + roomId ) . findOne ( { _id : messageId } ) ; } async addMessages ( roomId , messages ) { try { return await this . mdb . collection ( 'msg' + roomId ) . insertMany ( messages as object [ ] , { ordered : false } ) ; } catch ( e ) { return e ; } } getRoom ( roomId ) { return this . mdb . collection < any > ( 'rooms' ) . findOne ( { roomId } ) ; } getUnreadCount ( priority ) { const unreadRooms = this . mdb . collection ( 'rooms' ) . find ( { unreadCount : { $gt : 0 , } , priority : { $gte : priority , } , } ) ; return unreadRooms . count ( ) ; } getFirstUnreadRoom ( priority ) { return this . mdb . collection < any > ( 'rooms' ) . findOne ( { unreadCount : { $gt : 0 , } , priority : { $gte : priority , } , } ) ; } addIgnoredChat ( info ) { return this . mdb . collection ( 'ignoredChats' ) . insertOne ( info ) ; } getIgnoredChats ( ) { return this . mdb . collection ( 'ignoredChats' ) . find ( ) . toArray ( ) ; } async isChatIgnored ( id ) { return ! ! ( await this . mdb . collection ( 'ignoredChats' ) . findOne ( { id } ) ) ; } }