UNPKG

podchat-browser

Version:

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

198 lines (151 loc) 5.6 kB
import Utility from "../../utility/utility"; class ContactsList { constructor(app) { this.loadAllData = false this._total_count = 0 this._list = [] } #checkIsSearchRequest(content) { return Object.keys(content).some(key => ["query", "username", "cellphoneNumber"].includes(key)) && Object.keys(content).length === 3 } #filterContactList(content, start, end) { let contacts = [] if (typeof content.query != "undefined") { contacts = this._list.filter(items => (items?.firstName + " " + items?.lastName).includes(content.query)) } else if (typeof content.username != "undefined") { contacts = this._list.filter(items => items?.linkedUser?.username.includes(content.username)) } else if (typeof content.cellphoneNumber != "undefined") { contacts = this._list.filter(items => items?.cellphoneNumber?.includes(content.cellphoneNumber)) } return contacts.slice(start, end); } #getResponse(contacts, start, end, hasNext) { return { typeCode: "default", ownerId: undefined, hasError: false, cache: true, errorMessage: "", errorCode: "", result: { hasNext: hasNext, nextOffset: end, contentCount: this._total_count, contacts: contacts }, uniqueId: Utility.generateUUID() } } paginate(content, cache) { if (!cache) { return false } const start = parseInt(content.offset); const end = start + parseInt(content.size); let contacts = []; const checkExistOnlyCountAndOffset = Object.keys(content).some(key => ["size", "offset"].includes(key)) && Object.keys(content).length === 2; if (checkExistOnlyCountAndOffset) { contacts = this._list.slice(start, end); if (contacts.length >= content.size && !contacts.includes(null)) { return this.#getResponse(contacts, start, end, true); } } else if (this.#checkIsSearchRequest(content) && this.loadAllData) { contacts = this.#filterContactList(content, start, end); if (contacts.length > 0) { return this.#getResponse(contacts, start, end, end < contacts.length) } } return false } get(id) { return this._list.findIndex(item => item?.id == id); } getAll() { return this._list; } findOrCreate(thread) { let th = this.get(thread.id); if (!th) { //TODO: make sure we don't break unreadcount th = this.save(thread); } return th; } update(contactId, obj = {}) { const existedIndex = this.get(contactId); if (existedIndex > -1) { let contact = this._list[existedIndex] Object.entries(obj).forEach(([key, value]) => { contact[key] = value }); this._list[existedIndex] = contact; } } updateContactByUserId(messageContent) { let userId = messageContent.partner, fullname = messageContent.title, firstName = fullname.replace(/ .*/, ''), lastName = fullname.replace(firstName, ''); const existedIndex = this._list.findIndex(item => item?.userId == userId); if (existedIndex > -1) { let contact = this._list[existedIndex] contact['firstName'] = firstName contact['lastName'] = lastName this._list[existedIndex] = contact; } } // add or update thread save(contact, index) { const existedIndex = this.get(contact.id); if (index >= 0 && existedIndex === -1) { this._list[index] = contact; } else if (existedIndex > -1) { this._list[existedIndex] = contact; } return contact; } saveMany(content,newContacts, count, offset) { if(this.#checkIsSearchRequest(content)){ return; } const {contacts, contentCount, hasNext} = newContacts this._total_count = contentCount if (!hasNext) { this.loadAllData = true } if (Array.isArray(contacts)) { count = parseInt(count); offset = parseInt(offset); let index = offset; for (let item in contacts) { this.save(contacts[item], index++) } for (let i = 0; i < this._list.length - 1; i++) { if (this._list[i] == undefined) { this._list[i] = null } } } } remove(id) { let localThreadIndex = this.get(id); if (localThreadIndex > -1) { this._list.splice(localThreadIndex, 1); } return this._list } removeAll() { this._list = []; } addOrUpdate(contact) { const existedIndex = this.get(contact?.id); if (existedIndex > -1) { // update this._list[existedIndex] = contact } else { //Inserts at index (this._pinned_count ) this._list.splice(0, 0, contact) } } } export {ContactsList}