Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save SapphicFire/4b2a765042ee543d313977a58bfea375 to your computer and use it in GitHub Desktop.
Save SapphicFire/4b2a765042ee543d313977a58bfea375 to your computer and use it in GitHub Desktop.
SapphireNow: Ad-hoc Sidebar
var AdhocCollaborationGenerationUtil = Class.create();
AdhocCollaborationGenerationUtil.prototype = {
initialize: function() {
this.SIDEBAR_CHANNEL = gs.getProperty('sn_oe_sfs.sidebar_msg_channel','ea0cb816732110104a905ee515f6a7b5');
this.RECORD_CARD_JSON = JSON.parse(gs.getProperty('sn_oe_sfs.sidebar_record_obj'));
},
/**
* Creates a Sidebar conversation for a given task record, optionally adding users and groups
* @param {GlideRecord} task - Record provided for association. Should be a task/interaction or extended, but theoretically supports further
* @param {string} creator - Sys ID of user generating collab chat
* @param {string} [users] - Comma-separated list of users
* @param {string} [groups] - Comma-separated list of groups
* @param {string} [title] - Custom title for Sidebar conversation
* @returns
*/
generateConversation: function(task, creator, users, groups, title) {
// Declarations
var arrUsers = [], arrGroups = [], arrNames = [], objChat, objAssocRec;
// Do safety checks
if(!gs.nil(users)){
arrUsers = users.split(',');
}
if(!gs.nil(groups)){
arrGroups = groups.split(',');
}
if(gs.nil(title)){
title = task.short_description;
}
// Create key records
objChat = this.createConversation(title);
objAssocRec = this.createAssociatedRecord({table: task.getRecordClassName(), document: task.getUniqueValue()},objChat.sys_id);
this.addUserToConversation(creator,objAssocRec,objChat.sys_id,true);
var objMessage = this.createTaskCardMessage(task,objChat.sys_id);
// Process and add users, if provided
if(Array.isArray(arrUsers) && arrUsers.length > 0){
if(arrUsers.length == 1 && arrUsers[0].length < 32){
// Empty array
} else {
for(var user in arrUsers){
var objUsrTemp = this.addUserToConversation(arrUsers[user],objAssocRec,objChat.sys_id,false);
arrNames.push(objUsrTemp.name);
}
}
}
// Process and add groups, if provided
if(Array.isArray(arrGroups) && arrGroups.length > 0){
if(arrGroups.length == 1 && arrGroups[0].length < 32){
// Empty array
} else {
for(var grp in arrGroups){
var arrGrpTemp = this.addGroupToConversation(arrGroups[grp],objAssocRec,objChat.sys_id);
if(Array.isArray(arrGrpTemp) && arrGrpTemp.length > 0){
for(var iter in arrGrpTemp){
arrNames.push(arrGrpTemp[iter].name);
}
}
}
}
}
},
/**
* Adds the given user to the conversation
* @param {string} userId - Sys ID of a sys_user record
* @param {Object} objAssocRec - Object containing key associated record data
* @param {string} conversationId - Sys ID of the generated conversation
* @param {boolean} isCreator - True if you want this user to be listed as creator, else false
* @returns {Object} Object containing conversation member Sys ID and display name of the user
*/
addUserToConversation: function(userId, objAssocRec, conversationId, isCreator) {
var grUser = new global.GlideQuery('sys_user')
.get(userId, ['user_name','name'])
.get();
return this._addMemberToConversation(grUser, objAssocRec, conversationId, isCreator);
},
/**
* Adds members of a group to the conversation
* @param {string} groupId - Sys ID of a sys_user_group record
* @param {Object} objAssocRec - Object containing details of the associated record
* @param {string} conversationId - Sys ID of the generated conversation
* @returns {Object[]} Array of objects containing conversation member Sys IDs and display names
*/
addGroupToConversation: function(groupId, objAssocRec, conversationId) {
var arrMembers = [], members = this._getGroupMembers(groupId);
for(var member in members){
arrMembers.push(this._addMemberToConversation(members[member].user, objAssocRec, conversationId, false));
}
return arrMembers;
},
/**
* Looks up for existing, then adds to the conversation
* @param {Object} objUser - Object containing Sys ID, user_name, and Name of a sys_user record
* @param {Object} objAssocRec - Object containg the table, document, and Sys ID of the Conversation Associated Record
* @param {string} conversationId - Sys ID of the generated conversation
* @param {boolean} isCreator - True if member should be set as Creator, else false sets Collaborator
* @returns {Object} Object containing conversation member Sys ID and display name
*/
_addMemberToConversation: function(objUser, objAssocRec, conversationId, isCreator) {
var channelProfile = this._getChannelProfile(objUser);
var grChatMember = new global.GlideQuery('sys_cs_collab_member')
.getBy({
collab_chat: conversationId,
document: objAssocRec.document,
member: channelProfile.sys_id,
table: objAssocRec.table
},['sys_id'])
.orElse({
sys_id: '-1'
});
if(grChatMember.sys_id == '-1'){
grChatMember = this._createConversationMember(objUser, objAssocRec, conversationId, isCreator);
}
grChatMember.name = objUser.name;
return grChatMember;
},
/**
* Gets members of a group
* @param {string} groupId - Sys ID of a sys_user_group record
* @returns {Object[]} Array of objects containing group member Names, display names, and Sys IDs
*/
_getGroupMembers: function(groupId){
var arrGrMembers = [];
var grGrMembers = new global.GlideQuery('sys_user_grmember')
.where('group',groupId)
.where('user.active', true)
.select('user.name','user.user_name','user.sys_id')
.forEach(function (mem) {
arrGrMembers.push(mem);
});
return arrGrMembers;
},
/**
* Gets or creates channel profile for a user
* @param {Object} objUser - Object containing user record details
* @param {string} objUser.sys_id - Sys ID of the User record
* @returns {Object} Object containing channel profile Sys ID
*/
_getChannelProfile: function(objUser) {
var grProfile = new global.GlideQuery('sys_cs_channel_user_profile')
.getBy({
channel: this.SIDEBAR_CHANNEL,
active: true,
user_table: 'sys_user',
user_document: objUser.sys_id
},['sys_id'])
.orElse({
sys_id: '-1'
});
if(grProfile.sys_id == '-1'){
grProfile = this._createChannelProfile(objUser);
}
return grProfile;
},
/**
* Creates channel profile for a user
* @param {Object} objUser - Object containing user record details
* @param {string} objUser.sys_id - Sys ID of the User record
* @param {string} objUser.name - Display name of the User record
* @param {string} objUser.user_name - User ID of the User record
* @returns {Object} Object containing channel profile Sys ID
*/
_createChannelProfile: function(objUser) {
var grProfile = new global.GlideQuery('sys_cs_channel_user_profile')
.insert({
channel: this.SIDEBAR_CHANNEL,
active: true,
user_table: 'sys_user',
user_document: objUser.sys_id,
channel_user_id: objUser.user_name,
display_name: objUser.name
},['sys_id'])
.get();
return grProfile;
},
/**
* Creates conversation member for a user and conversation
* @param {Object} objUser - Object containing user record details
* @param {Object} objAssocRec - Object containg details of the Conversation Associated Record
* @param {string} objAssocRec.document - Sys ID of the record in the associated record
* @param {string} objAssocRec.table - Table name of the record in the associated record
* @param {string} conversationId - Sys ID of the generated conversation
* @param {boolean} isCreator - True if user should be Creator, else false for Collaborator
* @returns {Object} Object containing channel profile Sys ID
*/
_createConversationMember: function(objUser, objAssocRec, conversationId, isCreator) {
var channelProfile = this._getChannelProfile(objUser);
var grChatMember = new global.GlideQuery('sys_cs_collab_member')
.insert({
collab_chat: conversationId,
document: objAssocRec.document,
member: channelProfile.sys_id,
member_type: (isCreator ? 'creator' : 'collaborator'),
table: objAssocRec.table
},['sys_id'])
.get();
return grChatMember;
},
/**
* Create root Conversation record
* @param {string} title - Title of conversation
* @returns {Object} Object containing conversation Sys ID
*/
createConversation: function(title) {
var grConversation = new global.GlideQuery('sys_cs_collab_chat')
.insert({
state: 'open',
title: title
},['sys_id'])
.get();
return grConversation;
},
/**
* Create Conversation's Associated Record reference
* @param {Object} objRecord - Object containing details of source record
* @param {string} objRecord.table - Class name of source record
* @param {string} objRecord.document - Sys ID of source record
* @param {string} conversationId - Sys ID of conversation record
* @returns {Object} Object containing Associated record document, table, and Sys ID
*/
createAssociatedRecord: function(objRecord, conversationId) {
var grAssocRec = new global.GlideQuery('sys_cs_collab_record')
.insert({
collab_chat: conversationId,
table: objRecord.table,
document: objRecord.document
},['document','table','sys_id'])
.get();
return grAssocRec;
},
/**
* Create a message containing a CollabRecordCard for Sidebar
* @param {GlideRecord} objRecord - GlideRecord of the task requiring this message
* @param {string} conversationId - Sys ID of the sys_cs_collab_chat record
* @returns {Object} Object containing the Sys ID and status value of the message
*/
createTaskCardMessage: function(objRecord, conversationId) {
var objPayload = this.RECORD_CARD_JSON;
objPayload.uiMetadata.data.sys_id = objRecord.getUniqueValue();
objPayload.uiMetadata.data.table_name = objRecord.getRecordClassName();
var grMessageRec = new global.GlideQuery('sys_cs_collab_message')
.insert({
collab_chat: conversationId,
collab_chat_id: conversationId,
status: 'received',
message_type: 'rich',
payload: JSON.stringify(objPayload)
},['status','sys_id'])
.get();
return grMessageRec;
},
type: 'AdhocCollaborationGenerationUtil'
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment