UNPKG

podchat-browser

Version:

Javascript SDK to use POD's Chat Service - Browser Only

262 lines (213 loc) 7.38 kB
import Utility from "../../../utility/utility"; import Archive from './archive'; import Pin from './pin'; import Mute from './mute'; import UnRead from "./unRead"; import Validation from "./validation"; class Thread { constructor(app) { this._app = app; this._list = { main: [], archive: [], } this._params = {} this.archive = new Archive(this) this.pin = new Pin(this) this.mute = new Mute(this) this.unRead = new UnRead(this) this.validation = new Validation(this) } getThreadItemById(threadId, repo = 'main') { return this.getAll(repo).find(thread => thread?.id === threadId) || null; } getThreadResponse(threads) { const start = parseInt(this._params.offset) const end = start + parseInt(this._params.count) var result = {hasNext: true, nextOffset: end, contentCount: undefined, threads: threads} return { typeCode: "default", ownerId: undefined, hasError: false, cache: false, errorMessage: "", errorCode: "", result: result, uniqueId: Utility.generateUUID() }; } getThreadByIds(ids, cache) { let threads = [] if (typeof ids !== "undefined" && cache && ids?.length > 0) { ids.forEach(i => { let index = this.findIndex(i); if (index > -1) { threads.push(this.getAll()[index]); } }); } if (threads.length === 0) { return false } return { typeCode: "default", ownerId: undefined, hasError: false, cache: false, errorMessage: "", errorCode: "", result: {hasNext: true, nextOffset: null, contentCount: undefined, threads: threads}, uniqueId: Utility.generateUUID() }; } getCacheData(params) { params.typeCode = params.typeCode ? params.typeCode : this._app.typeCodes.getActiveTypeCode().typeCode; this._params = params; if (!this.validation.checkParameterIsValid(params)) { return false; } const start = parseInt(params.offset) const end = start + parseInt(params.count) const threads = this.getAll().slice(start, end); if (this.validation.checkDataIsValid(params, threads)) { return this.getThreadResponse(threads); } return false } get(id) { return this.getAll()[this.findIndex(id)]; } getAll(repo = 'main') { if (repo === 'archive') { return this._list.archive; } return this.sortPinMessage() } findIndex(threadId, repo = 'main') { return this.getAll(repo).findIndex(item => item?.id == threadId); } updateOrSave(threadId, message) { const repo = message?.archiveThread ? 'archive' : 'main'; const index = this.length(repo); const update = this.update(threadId, message) if (!update) { this.save(message, index) } } length(repo = 'main') { return this._list[repo]?.length; } update(threadId, obj = {}) { const repo = obj?.archiveThread ? 'archive' : 'main'; const index = this.findIndex(threadId, repo); if (index > -1) { this._list[repo][index] = {...this._list[repo][index], ...obj}; return true; } return false; } changeTitle(threadId, title) { this.update(threadId, {title: title}) } handleNewMessage(threadId, message) { delete message.unreadCount; if(message?.lastMessageVO?.mentioned){ this.updateMentioned(threadId,true) } this.updateDelivered(threadId,false) this.updateOrSave(threadId, message) } updateSeen(threadId, value) { this.update(threadId, {unreadCount: value ?? 0}) } updateMentioned(threadId,value) { //todo we need handle archived thread const repo = 'main'; const index = this.findIndex(threadId); if (index > -1) { this._list[repo][index]['mentioned'] = value; return true; } return false; } updateDelivered(threadId,value) { //todo we need handle archived thread const repo = 'main'; const index = this.findIndex(threadId); if (index > -1) { this._list[repo][index]['lastMessageVO'] = {...this._list[repo][index]['lastMessageVO'],...{delivered:value,seen:value}}; return true; } return false; } putItemAfterLastPinThread(item) { const index = this.getAll().filter(item => item?.pin === true).length ?? 0 this.getAll().splice(index, 0, item); } // add or update thread save(thread, index) { const repo = thread.archiveThread ? 'archive' : 'main' const existedIndex = this.findIndex(thread.id, repo); if (index >= 0 && existedIndex === -1) { this.getAll(repo)[index] = thread; } else if (existedIndex > -1) { this.getAll(repo)[existedIndex] = thread; } return thread; } saveMany(newThreads, params) { if (!this.validation.checkTypeCodeIsValid(params.typeCode)) { return; } if (Array.isArray(newThreads)) { let index = parseInt(this._params.offset); for (let item in newThreads) { this.save(newThreads[item], index++) } this._list.main = [...this.getAll()].map(item => item === undefined ? null : item); } } remove(id) { let localThreadIndex = this.findIndex(id); if (localThreadIndex > -1) { this.getAll().splice(localThreadIndex, 1); } return this.getAll() } removeAll() { this._list.main = []; this._list.archive = []; } addAfterPinMessageOrUpdate(thread) { const existedIndex = this.findIndex(thread?.id); if (existedIndex > -1) { // update this.getAll()[existedIndex] = thread } else { //Inserts at index (this._pinned_count ) this.getAll().splice(this.pinedMessageCount(), 0, thread) } } sortPinMessage() { return this._list.main.sort((a, b) => { if (b?.pin || a?.pin) { return b?.pin - a?.pin } if (b?.time < a?.time) { return -1 } if (b?.time > a?.time) { return 1 } return 0 }) } pinedMessageCount() { return this.getAll().filter(data => data.pin === true).length } getLastMessageItem(threadId) { const thread= this.getAll().find(thread => thread?.id === threadId) || {} return thread?.lastMessageVO || {}; } } export {Thread}