SmartIT_Extensions/BMC/smart-it-full-helix/components/chat/chat-window-vo.js

190 lines
7.6 KiB
JavaScript

"use strict";
/**
* Value object for asset item.
*
* @constructor
*/
function ChatWindowVO() {
this.id = new Date().getTime() + '_roomDummy@conf.host';
// database fields
this.room = {};
this.parent = null;
this.inviterJID = null;
this.participants = {};
this.roster = null;
this.messages = [];
this.isOpened = false;
this.isLoading = true;
this.conversationThumbnail = "";
this.roomOwner = null;
}
// inherit BaseVO
ChatWindowVO.prototype = new BaseVO();
// correct the constructor pointer
ChatWindowVO.prototype.constructor = ChatWindowVO;
/**
* @override
* @return {Array}
*/
ChatWindowVO.prototype.getProps = function () {
return ["id", "room", "parent", "inviterJID", "participants", "roster", "messages", "isOpened", "isLoading"];
};
/**
* @override
*/
ChatWindowVO.prototype.postBuild = function () {
this.id = this.room.name;
if (this.room) {
this.participants = _.clone(this.room.roster);
this.roomOwner = _.find(this.participants, { affiliation: 'owner' });
}
};
ChatWindowVO.prototype.getUserThumbnailForChatList = function () {
if (this.conversationThumbnail) {
var lastMessage = this.getLastMessageInConversation();
var systemUserNick = this.room.nick, systemUser = this.participants[systemUserNick], systemUserId = systemUser ? systemUser.jid.split("@")[0] : "";
if (lastMessage && lastMessage.author && lastMessage.author.thumbnail && (lastMessage.author.jid != systemUserId)) {
this.conversationThumbnail = lastMessage.author.thumbnail;
}
return this.conversationThumbnail;
}
var roomOwner = this.roomOwner || _.find(this.room.roster, { affiliation: 'owner' });
if (roomOwner && roomOwner.jid && this.participants) {
var ownerUserId = Strophe.getNodeFromJid(roomOwner.jid), currentUserId = this.room.client._connection.authcid;
if (ownerUserId != currentUserId) {
this.conversationThumbnail = this.roster[ownerUserId] ? this.roster[ownerUserId].thumbnail : "";
}
else {
var usersWithThumbnails = _.filter(this.roster, function (user) {
return !!(user.thumbnail && (user.jid != currentUserId));
});
var roomParticipant = _.sample(usersWithThumbnails || []);
if (roomParticipant && roomParticipant.jid != currentUserId) {
this.conversationThumbnail = roomParticipant.thumbnail;
}
}
}
return this.conversationThumbnail;
};
ChatWindowVO.prototype.toggleChatWindow = function () {
this.isOpened = !this.isOpened;
};
ChatWindowVO.prototype.generateConversationSummary = function () {
var self = this, summary, participantList = [];
var userCount = _.size(this.participants);
if (userCount > 1) {
_.each(this.participants, function (user) {
if (user.nick && Strophe.unescapeNode(user.nick) == self.room.nick)
return;
var userId = user.jid ? Strophe.getNodeFromJid(user.jid) : angular.noop(), userProfile = userId && self.roster ? self.roster[userId] : angular.noop();
if (userProfile) {
participantList.push(userProfile.firstName);
}
else {
var userName = user.nick ? user.nick.split("_")[0] : angular.noop();
if (userName)
participantList.push(userName);
}
});
if (participantList.length > 0) {
if (participantList.length <= 3) {
summary = participantList.join(", ");
}
else {
var selectedPraticipants = _.sample(participantList, 3);
summary = selectedPraticipants.join(", ") + " ...";
}
}
}
if (this.parent) {
if (this.parent.ticketType && this.parent.ticketType == EntityVO.TYPE_ASSET) {
summary = (summary || "") + " (" + this.parent.name + ")";
}
else {
summary = (summary || "") + " (" + this.parent.displayId + ")";
}
}
return summary;
};
ChatWindowVO.prototype.getLastMessageText = function () {
var message = this.getLastMessageInConversation();
if (message.hasOwnProperty("text") && (message.text.startsWith(EntityVO.CHATOPS_DETAILS) ||
message.text.startsWith(EntityVO.CHATOPS_INCIDENTDETAILS) || message.text.startsWith(EntityVO.CHATOPS_KNOWLEDGE) ||
message.text.startsWith(EntityVO.CHATOPS_INCIDENT) || message.text.startsWith(EntityVO.CHATOPS_LINKKNOWLEDGE)
|| message.text.startsWith(EntityVO.CHATOPS_SWARM) || message.text.startsWith(EntityVO.CHATOPS_TOOLS)
|| message.text.startsWith(EntityVO.CHATOPS_ERROR))) {
return "";
}
return message ? message.text : "";
};
ChatWindowVO.prototype.generateChatTopic = function () {
if (this.parent) {
var topicParts = [];
if (this.parent.ticketType && this.parent.ticketType == EntityVO.TYPE_ASSET) {
topicParts = [EntityVO.TYPE_ASSET.toUpperCase(), this.parent.name];
}
else {
if (window.isRtl) {
topicParts = [this.parent.summary, this.parent.displayId];
}
else {
topicParts = [this.parent.displayId, this.parent.summary];
}
}
return topicParts.join(": ");
}
};
ChatWindowVO.prototype.getLastMessageInConversation = function () {
var lastMessageIndex = _.findLastIndex(this.messages, { type: 'chat' });
return (lastMessageIndex > 0) ? this.messages[lastMessageIndex] : "";
};
ChatWindowVO.prototype.getTimeSinceLastMessage = function () {
var lastMessage = this.getLastMessageInConversation();
if (lastMessage) {
var messageCreate = lastMessage.created;
return moment(messageCreate).fromNow();
}
};
ChatWindowVO.prototype.getParticipantJIDsArray = function () {
var JIDsList = [];
_.each(this.room.roster, function (user) {
var jid = Strophe.getNodeFromJid(user.jid);
JIDsList.push(jid);
});
return JIDsList;
};
ChatWindowVO.prototype.generateRosterSummary = function () {
var self = this;
if (this.participants) {
var participantNamesList = [], participantsCount = 0, userName;
//Defect fix #SW00488104
try {
_.each(this.room.userSessions, function (activeUsersArray) {
if (activeUsersArray.length > 0)
participantsCount++;
});
}
catch (e) {
participantsCount = _.size(this.participants);
}
_.each(this.participants, function (user, nick) {
//TODO: new xmppRoom has nick prop whatever was send by the code(e.g. Cal'syee_Neramani). But join room stanza has escaped nick value. So user joins room using escaped nick value(e.g. Cal\27syee_Neramani). This will cause issues when nick will be compared. So nick should be Strophe.unescapeNode prior to comparison
if (Strophe.unescapeNode(nick) == self.room.nick || !self.room.roster[nick])
return;
var userJID = user.jid.split("@")[0], userProfile = self.roster[userJID] || {};
if (participantsCount <= 3) {
userName = userProfile.firstName ? userProfile.firstName + " " + userProfile.lastName : nick.replace("_", " ");
}
else {
userName = userProfile.firstName ? userProfile.firstName : nick.split("_")[0];
}
;
participantNamesList.push(userName);
});
if (participantNamesList.length) {
return participantNamesList.join(", ");
}
}
return "";
};