podchat
Version:
Javascript SDK to use POD's Chat Service
218 lines (216 loc) • 8.47 kB
JavaScript
"use strict";
var _interopRequireDefault = require("@babel/runtime/helpers/interopRequireDefault");
Object.defineProperty(exports, "__esModule", {
value: true
});
exports.ContactsList = void 0;
var _slicedToArray2 = _interopRequireDefault(require("@babel/runtime/helpers/slicedToArray"));
var _classCallCheck2 = _interopRequireDefault(require("@babel/runtime/helpers/classCallCheck"));
var _createClass2 = _interopRequireDefault(require("@babel/runtime/helpers/createClass"));
var _utility = _interopRequireDefault(require("../../utility/utility"));
function _classPrivateMethodInitSpec(obj, privateSet) { _checkPrivateRedeclaration(obj, privateSet); privateSet.add(obj); }
function _checkPrivateRedeclaration(obj, privateCollection) { if (privateCollection.has(obj)) { throw new TypeError("Cannot initialize the same private elements twice on an object"); } }
function _classPrivateMethodGet(receiver, privateSet, fn) { if (!privateSet.has(receiver)) { throw new TypeError("attempted to get private field on non-instance"); } return fn; }
var _checkIsSearchRequest = /*#__PURE__*/new WeakSet();
var _filterContactList = /*#__PURE__*/new WeakSet();
var _getResponse = /*#__PURE__*/new WeakSet();
var ContactsList = /*#__PURE__*/function () {
function ContactsList(app) {
(0, _classCallCheck2["default"])(this, ContactsList);
_classPrivateMethodInitSpec(this, _getResponse);
_classPrivateMethodInitSpec(this, _filterContactList);
_classPrivateMethodInitSpec(this, _checkIsSearchRequest);
this.loadAllData = false;
this._total_count = 0;
this._list = [];
}
(0, _createClass2["default"])(ContactsList, [{
key: "paginate",
value: function paginate(content, cache) {
if (!cache) {
return false;
}
var start = parseInt(content.offset);
var end = start + parseInt(content.size);
var contacts = [];
var checkExistOnlyCountAndOffset = Object.keys(content).some(function (key) {
return ["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 _classPrivateMethodGet(this, _getResponse, _getResponse2).call(this, contacts, start, end, true);
}
} else if (_classPrivateMethodGet(this, _checkIsSearchRequest, _checkIsSearchRequest2).call(this, content) && this.loadAllData) {
contacts = _classPrivateMethodGet(this, _filterContactList, _filterContactList2).call(this, content, start, end);
if (contacts.length > 0) {
return _classPrivateMethodGet(this, _getResponse, _getResponse2).call(this, contacts, start, end, end < contacts.length);
}
}
return false;
}
}, {
key: "get",
value: function get(id) {
return this._list.findIndex(function (item) {
return (item === null || item === void 0 ? void 0 : item.id) == id;
});
}
}, {
key: "getAll",
value: function getAll() {
return this._list;
}
}, {
key: "findOrCreate",
value: function findOrCreate(thread) {
var th = this.get(thread.id);
if (!th) {
//TODO: make sure we don't break unreadcount
th = this.save(thread);
}
return th;
}
}, {
key: "update",
value: function update(contactId) {
var obj = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : {};
var existedIndex = this.get(contactId);
if (existedIndex > -1) {
var contact = this._list[existedIndex];
Object.entries(obj).forEach(function (_ref) {
var _ref2 = (0, _slicedToArray2["default"])(_ref, 2),
key = _ref2[0],
value = _ref2[1];
contact[key] = value;
});
this._list[existedIndex] = contact;
}
}
}, {
key: "updateContactByUserId",
value: function updateContactByUserId(messageContent) {
var userId = messageContent.partner,
fullname = messageContent.title,
firstName = fullname.replace(/ .*/, ''),
lastName = fullname.replace(firstName, '');
var existedIndex = this._list.findIndex(function (item) {
return (item === null || item === void 0 ? void 0 : item.userId) == userId;
});
if (existedIndex > -1) {
var contact = this._list[existedIndex];
contact['firstName'] = firstName;
contact['lastName'] = lastName;
this._list[existedIndex] = contact;
}
}
// add or update thread
}, {
key: "save",
value: function save(contact, index) {
var existedIndex = this.get(contact.id);
if (index >= 0 && existedIndex === -1) {
this._list[index] = contact;
} else if (existedIndex > -1) {
this._list[existedIndex] = contact;
}
return contact;
}
}, {
key: "saveMany",
value: function saveMany(content, newContacts, count, offset) {
if (_classPrivateMethodGet(this, _checkIsSearchRequest, _checkIsSearchRequest2).call(this, content)) {
return;
}
var contacts = newContacts.contacts,
contentCount = newContacts.contentCount,
hasNext = newContacts.hasNext;
this._total_count = contentCount;
if (!hasNext) {
this.loadAllData = true;
}
if (Array.isArray(contacts)) {
count = parseInt(count);
offset = parseInt(offset);
var index = offset;
for (var item in contacts) {
this.save(contacts[item], index++);
}
for (var i = 0; i < this._list.length - 1; i++) {
if (this._list[i] == undefined) {
this._list[i] = null;
}
}
}
}
}, {
key: "remove",
value: function remove(id) {
var localThreadIndex = this.get(id);
if (localThreadIndex > -1) {
this._list.splice(localThreadIndex, 1);
}
return this._list;
}
}, {
key: "removeAll",
value: function removeAll() {
this._list = [];
}
}, {
key: "addOrUpdate",
value: function addOrUpdate(contact) {
var existedIndex = this.get(contact === null || contact === void 0 ? void 0 : contact.id);
if (existedIndex > -1) {
// update
this._list[existedIndex] = contact;
} else {
//Inserts at index (this._pinned_count )
this._list.splice(0, 0, contact);
}
}
}]);
return ContactsList;
}();
exports.ContactsList = ContactsList;
function _checkIsSearchRequest2(content) {
return Object.keys(content).some(function (key) {
return ["query", "username", "cellphoneNumber"].includes(key);
}) && Object.keys(content).length === 3;
}
function _filterContactList2(content, start, end) {
var contacts = [];
if (typeof content.query != "undefined") {
contacts = this._list.filter(function (items) {
return ((items === null || items === void 0 ? void 0 : items.firstName) + " " + (items === null || items === void 0 ? void 0 : items.lastName)).includes(content.query);
});
} else if (typeof content.username != "undefined") {
contacts = this._list.filter(function (items) {
var _items$linkedUser;
return items === null || items === void 0 ? void 0 : (_items$linkedUser = items.linkedUser) === null || _items$linkedUser === void 0 ? void 0 : _items$linkedUser.username.includes(content.username);
});
} else if (typeof content.cellphoneNumber != "undefined") {
contacts = this._list.filter(function (items) {
var _items$cellphoneNumbe;
return items === null || items === void 0 ? void 0 : (_items$cellphoneNumbe = items.cellphoneNumber) === null || _items$cellphoneNumbe === void 0 ? void 0 : _items$cellphoneNumbe.includes(content.cellphoneNumber);
});
}
return contacts.slice(start, end);
}
function _getResponse2(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["default"].generateUUID()
};
}