2017-06-16 09:42:28 +00:00
|
|
|
/**
|
|
|
|
* Conversations utilites
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
2020-04-09 06:45:03 +00:00
|
|
|
const ConversationsUtils = {
|
2017-06-16 09:42:28 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given conversation informations, returns its name
|
|
|
|
*
|
2021-03-05 14:26:45 +00:00
|
|
|
* @param {Conversation} info Conversation information
|
2017-06-16 09:42:28 +00:00
|
|
|
* @param {Function} afterName What to do once we got conversation name
|
|
|
|
* @return {Boolean} True for a success
|
|
|
|
*/
|
2021-03-05 14:26:45 +00:00
|
|
|
getName: function(info, afterName){
|
2017-06-16 09:42:28 +00:00
|
|
|
|
|
|
|
//Check if the conversation has a name or not
|
2021-03-05 14:26:45 +00:00
|
|
|
if(info.name && info.name.length > 0)
|
|
|
|
afterName(info.name);
|
2017-06-16 09:42:28 +00:00
|
|
|
else {
|
|
|
|
|
|
|
|
//Get informations about the first two members
|
|
|
|
var firstMembers = [];
|
|
|
|
|
|
|
|
//Retrieve IDs
|
2021-03-05 14:26:45 +00:00
|
|
|
for(o in info.members){
|
2017-06-16 09:42:28 +00:00
|
|
|
//Limit number to 2
|
2021-03-05 14:26:45 +00:00
|
|
|
if(firstMembers.length < 2){
|
|
|
|
//Exclude current user ID
|
|
|
|
if(info.members[o].user_id != userID())
|
|
|
|
firstMembers.push(info.members[o].user_id);
|
2017-06-16 09:42:28 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
//Get users informations
|
|
|
|
ComunicWeb.user.userInfos.getNames(firstMembers, function(usersName){
|
|
|
|
|
|
|
|
//For conversations with many members (more than 3 - we musn't forget current user)
|
2021-03-05 14:26:45 +00:00
|
|
|
if(info.members.length > 3)
|
2017-06-16 09:42:28 +00:00
|
|
|
usersName += ", ...";
|
|
|
|
|
|
|
|
//Peform next action now
|
|
|
|
afterName(usersName);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
//Success
|
|
|
|
return true;
|
2017-06-17 09:33:15 +00:00
|
|
|
},
|
2017-06-16 09:42:28 +00:00
|
|
|
|
2019-01-24 17:14:53 +00:00
|
|
|
/**
|
|
|
|
* Given a conversation ID, get its name
|
|
|
|
*
|
|
|
|
* @param {number} id The ID of the target conversation
|
|
|
|
* @param {function} onGotName Function called once we have got the name of the conversation
|
|
|
|
*/
|
|
|
|
getNameForID: function(id, onGotName){
|
|
|
|
|
|
|
|
ComunicWeb.components.conversations.interface.getInfosOne(id, function(info){
|
|
|
|
|
|
|
|
//Check if an error occurred
|
|
|
|
if(info.error)
|
|
|
|
return onGotName(false);
|
|
|
|
|
|
|
|
//Get and return the name of the conversation
|
|
|
|
ComunicWeb.components.conversations.utils.getName(info, onGotName);
|
|
|
|
|
|
|
|
});
|
|
|
|
|
|
|
|
},
|
|
|
|
|
2017-06-17 09:33:15 +00:00
|
|
|
/**
|
|
|
|
* Create and display a conversation creation / edition form
|
|
|
|
*
|
|
|
|
* @param {HTMLElement} target The target of the creation form
|
2020-04-25 16:20:29 +00:00
|
|
|
* @return {ConversationSettingsFormElements} Information about the form
|
2017-06-17 09:33:15 +00:00
|
|
|
*/
|
|
|
|
createConversationForm: function(target){
|
|
|
|
|
|
|
|
//Create form object
|
2020-04-25 16:20:29 +00:00
|
|
|
const form = {};
|
2017-06-17 09:33:15 +00:00
|
|
|
|
|
|
|
//Create and display conversation creation form
|
|
|
|
form.rootElem = createElem("div", target);
|
|
|
|
|
|
|
|
//Choose users
|
|
|
|
//Create select user element
|
|
|
|
form.usersElement = createFormGroup({
|
|
|
|
target: form.rootElem,
|
|
|
|
label: "Users",
|
|
|
|
multiple: true,
|
|
|
|
placeholder: "Select users",
|
|
|
|
type: "select2"});
|
|
|
|
|
|
|
|
//Initialize user selector
|
|
|
|
ComunicWeb.components.userSelect.init(form.usersElement);
|
|
|
|
|
|
|
|
|
2021-03-06 17:41:42 +00:00
|
|
|
// Conversation name
|
2017-06-17 09:33:15 +00:00
|
|
|
form.conversationNameInput = createFormGroup({
|
|
|
|
target: form.rootElem,
|
2021-03-06 17:41:42 +00:00
|
|
|
label: tr("Conversation name"),
|
|
|
|
placeholder: tr("Optional"),
|
|
|
|
type: "text"
|
|
|
|
});
|
|
|
|
|
|
|
|
// Conversation color
|
|
|
|
form.conversationColorInput = createFormGroup({
|
|
|
|
target: form.rootElem,
|
|
|
|
label: tr("Conversation color"),
|
|
|
|
placeholder: tr("Optional"),
|
|
|
|
type: "text"
|
|
|
|
});
|
|
|
|
$(form.conversationColorInput).colorpicker({format: "hex"})
|
2017-06-17 09:33:15 +00:00
|
|
|
|
2020-04-25 16:20:29 +00:00
|
|
|
// Follow discussion
|
2017-06-17 09:33:15 +00:00
|
|
|
form.followConversationInput = createFormGroup({
|
|
|
|
target: form.rootElem,
|
2021-03-06 17:41:42 +00:00
|
|
|
label: tr("Follow conversation"),
|
2017-06-17 09:33:15 +00:00
|
|
|
checked: true,
|
2021-03-06 17:41:42 +00:00
|
|
|
type: "checkbox"
|
|
|
|
});
|
2017-06-17 09:33:15 +00:00
|
|
|
|
2020-04-25 16:20:29 +00:00
|
|
|
// Allow all the members of the conversation to add other members
|
|
|
|
form.allowEveryoneToAddMembers = createFormGroup({
|
|
|
|
target: form.rootElem,
|
|
|
|
type: "checkbox",
|
|
|
|
checked: true,
|
|
|
|
label: "Allow everyone to add members"
|
|
|
|
});
|
|
|
|
|
2017-06-17 09:33:15 +00:00
|
|
|
//Create button
|
|
|
|
form.createButton = createElem2({
|
|
|
|
type: "button",
|
|
|
|
appendTo: form.rootElem,
|
|
|
|
class: "btn btn-primary",
|
|
|
|
innerHTML: "Create conversation"
|
|
|
|
});
|
|
|
|
form.createButton.style.width = "100%";
|
2017-06-16 09:42:28 +00:00
|
|
|
|
2017-06-17 09:33:15 +00:00
|
|
|
//Return result
|
|
|
|
return form;
|
|
|
|
},
|
2021-03-05 13:37:22 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of the users for a message
|
|
|
|
*
|
|
|
|
* @param {ConversationMessage} msg
|
|
|
|
*/
|
|
|
|
getUsersIDForMessage: function(msg) {
|
|
|
|
if (msg.user_id != null && msg.user_id > 0)
|
|
|
|
return [msg.user_id];
|
|
|
|
|
|
|
|
switch (msg.server_message.type) {
|
|
|
|
case "user_created_conv":
|
|
|
|
case "user_left":
|
|
|
|
return [msg.server_message.user_id];
|
|
|
|
|
|
|
|
case "user_added_another":
|
|
|
|
return [msg.server_message.user_who_added, msg.server_message.user_added];
|
|
|
|
|
|
|
|
case "user_removed_another":
|
|
|
|
return [msg.server_message.user_who_removed, msg.server_message.user_removed];
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the ID of the main user for a given message
|
|
|
|
*
|
|
|
|
* @param {ConversationMessage} msg
|
|
|
|
*/
|
|
|
|
getMainUserForMessage: function(msg) {
|
|
|
|
if (msg.user_id != null && msg.user_id > 0)
|
|
|
|
return msg.user_id;
|
|
|
|
|
|
|
|
switch (msg.server_message.type) {
|
|
|
|
case "user_created_conv":
|
|
|
|
case "user_left":
|
|
|
|
return msg.server_message.user_id;
|
|
|
|
|
|
|
|
case "user_added_another":
|
|
|
|
return msg.server_message.user_who_added;
|
|
|
|
|
|
|
|
case "user_removed_another":
|
|
|
|
return msg.server_message.user_who_removed;
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generate a message of the server
|
|
|
|
*
|
|
|
|
* @param {ConversationMessage} msg
|
|
|
|
* @param {UsersList} users
|
|
|
|
*/
|
|
|
|
getServerMessage: function(msg, users) {
|
|
|
|
if (msg.server_message == null)
|
|
|
|
return "";
|
|
|
|
|
|
|
|
switch (msg.server_message.type) {
|
|
|
|
case "user_created_conv":
|
|
|
|
return tr("%1% created the conversation", {
|
|
|
|
"1": users.get(msg.server_message.user_id).fullName
|
|
|
|
});
|
|
|
|
|
|
|
|
case "user_added_another":
|
|
|
|
return tr("%1% added %2% to the conversation", {
|
|
|
|
"1": users.get(msg.server_message.user_who_added).fullName,
|
|
|
|
"2": users.get(msg.server_message.user_added).fullName
|
|
|
|
})
|
|
|
|
|
|
|
|
case "user_left":
|
|
|
|
return tr("%1% left the conversation", {
|
|
|
|
"1": users.get(msg.server_message.user_id).fullName
|
|
|
|
});
|
|
|
|
|
|
|
|
case "user_removed_another":
|
|
|
|
return tr("%1% removed %2% from the conversation", {
|
|
|
|
"1": users.get(msg.server_message.user_who_removed).fullName,
|
|
|
|
"2": users.get(msg.server_message.user_removed).fullName
|
|
|
|
});
|
|
|
|
}
|
2021-03-06 14:04:10 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param {number} convID
|
|
|
|
* @param {HTMLInputElement} input
|
|
|
|
* @param {HTMLElement} target
|
|
|
|
*/
|
|
|
|
registerInputToSendFile: function(convID, fileInput, splashScreenTarget){
|
|
|
|
fileInput.addEventListener("change", async (e) => {
|
|
|
|
e.preventDefault();
|
|
|
|
|
|
|
|
let el;
|
|
|
|
|
|
|
|
try {
|
|
|
|
|
|
|
|
if(fileInput.files.length == 0)
|
|
|
|
return;
|
|
|
|
|
|
|
|
const file = fileInput.files[0];
|
|
|
|
|
|
|
|
if (ServerConfig.conf.allowed_conversation_files_type.indexOf(file.type) < 0) {
|
|
|
|
notify(tr("This file type is not allowed!"), "danger")
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
if (file.size > ServerConfig.conf.conversation_files_max_size) {
|
|
|
|
notify(tr("This file is too big (max file size: %1%)", {"1": fileSizeToHuman(ServerConfig.conf.conversation_files_max_size)}), "danger");
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
el = Page.showTransparentWaitSplashScreen(splashScreenTarget);
|
|
|
|
|
|
|
|
await ConversationsInterface.sendMessage(convID, null, fileInput);
|
|
|
|
}
|
|
|
|
|
|
|
|
catch(e) {
|
|
|
|
console.error(e);
|
|
|
|
notify(tr("Failed to send file!"), "danger");
|
|
|
|
}
|
|
|
|
|
|
|
|
el.remove();
|
|
|
|
|
|
|
|
});
|
2021-03-07 13:55:00 +00:00
|
|
|
},
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Upload a new conversation image
|
|
|
|
*/
|
|
|
|
uploadNewConversationImage: async function(convID) {
|
|
|
|
let input = document.createElement("input");
|
|
|
|
input.type = "file";
|
|
|
|
|
|
|
|
input.click();
|
|
|
|
|
|
|
|
// Wait for file
|
|
|
|
await new Promise((res, rej) => input.addEventListener("change", e => res()));
|
|
|
|
|
|
|
|
if(input.files.length == 0) return;
|
|
|
|
|
|
|
|
await ConversationsInterface.sendNewConversationImage(convID, input);
|
|
|
|
},
|
2020-04-09 06:45:03 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
ComunicWeb.components.conversations.utils = ConversationsUtils;
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get information about a conversation
|
|
|
|
*
|
|
|
|
* @param {Conversation} conv Information about the
|
|
|
|
* target conversation
|
|
|
|
*/
|
|
|
|
async function getConvName(conv) {
|
|
|
|
return new Promise((res, rej) => {
|
|
|
|
ConversationsUtils.getName(conv, (name) => res(name));
|
|
|
|
})
|
2017-06-16 09:42:28 +00:00
|
|
|
}
|