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
* /
2021-03-09 16:06:35 +00:00
registerInputToSendFile : function ( convID , fileInput , progressTarget ) {
2021-03-06 14:04:10 +00:00
fileInput . addEventListener ( "change" , async ( e ) => {
e . preventDefault ( ) ;
let el ;
try {
if ( fileInput . files . length == 0 )
return ;
const file = fileInput . files [ 0 ] ;
2021-03-12 15:07:16 +00:00
if ( ServerConfig . conf . conversations _policy . allowed _conversation _files _type . indexOf ( file . type ) < 0 ) {
2021-03-06 14:04:10 +00:00
notify ( tr ( "This file type is not allowed!" ) , "danger" )
return ;
}
2021-03-12 15:07:16 +00:00
if ( file . size > ServerConfig . conf . conversations _policy . conversation _files _max _size ) {
notify ( tr ( "This file is too big (max file size: %1%)" , { "1" : fileSizeToHuman ( ServerConfig . conf . conversations _policy . conversation _files _max _size ) } ) , "danger" ) ;
2021-03-06 14:04:10 +00:00
return ;
}
2021-03-09 16:06:35 +00:00
el = new FileUploadProgress ( progressTarget ) ;
2021-03-06 14:04:10 +00:00
2021-03-09 16:06:35 +00:00
await ConversationsInterface . sendMessage ( convID , null , fileInput , ( progress ) => el . setProgress ( progress ) ) ;
2021-03-06 14:04:10 +00:00
}
catch ( e ) {
console . error ( e ) ;
notify ( tr ( "Failed to send file!" ) , "danger" ) ;
}
2021-03-09 16:06:35 +00:00
el . remove ( )
2021-03-06 14:04:10 +00:00
} ) ;
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 ) ;
} ,
2021-03-08 17:09:56 +00:00
/ * *
* Automatically listen to change events of an input
* to notify other users current user is writing a message
*
* @ param { HTMLInputElement } input
* @ param { number } convID
* /
listenToInputChangeEvents : async function ( input , convID ) {
let last _update = 0 ;
input . addEventListener ( "keyup" , e => {
if ( input . value == "" )
return ;
const t = ComunicDate . time ( ) ;
2021-03-12 15:07:16 +00:00
if ( t - last _update < ServerConfig . conf . conversations _policy . conversation _writing _event _interval )
2021-03-08 17:09:56 +00:00
return ;
last _update = t ;
2021-03-08 17:16:45 +00:00
ws ( "conversations/is_writing" , { convID : convID } ) ;
2021-03-08 17:09:56 +00:00
} ) ;
} ,
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
}