2019-11-23 17:41:13 +00:00
|
|
|
import { RequestHandler } from "../entities/RequestHandler";
|
|
|
|
import { ConversationsHelper } from "../helpers/ConversationsHelper";
|
2019-11-30 09:11:51 +00:00
|
|
|
import { Conversation, BaseConversation } from "../entities/Conversation";
|
|
|
|
import { UserHelper } from "../helpers/UserHelper";
|
2019-11-30 13:03:53 +00:00
|
|
|
import { removeHTMLNodes } from "../utils/StringUtils";
|
2019-11-23 17:41:13 +00:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Conversations controller
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
export class ConversationsController {
|
|
|
|
|
2019-11-30 09:11:51 +00:00
|
|
|
/**
|
|
|
|
* Create a new conversation
|
|
|
|
*
|
|
|
|
* @param h Request handler
|
|
|
|
*/
|
|
|
|
public static async CreateConversation(h : RequestHandler) {
|
|
|
|
|
|
|
|
const name = h.postString("name");
|
|
|
|
|
2019-11-30 13:09:05 +00:00
|
|
|
const members = [...new Set(h.postNumbersList("users"))];
|
2019-11-30 09:11:51 +00:00
|
|
|
|
|
|
|
// Check if the users exists
|
|
|
|
for (const userID of members) {
|
|
|
|
if(!await UserHelper.Exists(userID))
|
|
|
|
h.error(404, "User " + userID + " not found!");
|
|
|
|
}
|
|
|
|
|
|
|
|
if(!members.includes(h.getUserId()))
|
|
|
|
members.push(h.getUserId());
|
|
|
|
|
|
|
|
const conv : BaseConversation = {
|
|
|
|
ownerID: h.getUserId(),
|
2019-11-30 13:03:53 +00:00
|
|
|
name: name == "false" ? "" : removeHTMLNodes(name),
|
2019-11-30 09:11:51 +00:00
|
|
|
following: h.postBool("follow"),
|
|
|
|
members: members,
|
|
|
|
}
|
|
|
|
|
|
|
|
const convID = await ConversationsHelper.Create(conv);
|
|
|
|
|
|
|
|
// Success
|
|
|
|
h.send({
|
|
|
|
conversationID: convID,
|
|
|
|
success: "The conversation was successfully created!"
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:41:13 +00:00
|
|
|
/**
|
|
|
|
* Get the list of conversations of the user
|
|
|
|
*
|
|
|
|
* @param handler
|
|
|
|
*/
|
|
|
|
public static async GetList(handler: RequestHandler) {
|
|
|
|
const list = await ConversationsHelper.GetListUser(handler.getUserId());
|
|
|
|
handler.send(list.map(c => this.ConversationToAPI(c)));
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:53:13 +00:00
|
|
|
/**
|
|
|
|
* Get information about a single conversation
|
|
|
|
*
|
|
|
|
* @param handler
|
|
|
|
*/
|
|
|
|
public static async GetInfoSingle(handler: RequestHandler) {
|
|
|
|
const conversationID = await this.GetPostConversationId("conversationID", handler);
|
2019-11-23 19:54:35 +00:00
|
|
|
const conv = await ConversationsHelper.GetSingle(conversationID, handler.getUserId());
|
2019-11-23 19:53:13 +00:00
|
|
|
|
|
|
|
if(!conv)
|
|
|
|
throw Error("Could not get information about the conversation (but it should have been found though) !!");
|
|
|
|
|
|
|
|
handler.send(this.ConversationToAPI(conv));
|
|
|
|
}
|
|
|
|
|
2019-11-30 11:24:19 +00:00
|
|
|
/**
|
|
|
|
* Update conversation settings
|
|
|
|
*
|
|
|
|
* @param handler Request handler
|
|
|
|
*/
|
|
|
|
public static async UpdateSettings(handler: RequestHandler) : Promise<void> {
|
|
|
|
const convID = await this.GetPostConversationId("conversationID", handler);
|
|
|
|
|
|
|
|
// Update following state, if required
|
|
|
|
if(handler.hasPostParameter("following")) {
|
|
|
|
await ConversationsHelper.SetFollowing(
|
|
|
|
handler.getUserId(),
|
|
|
|
convID,
|
|
|
|
handler.postBool("following")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-30 13:25:06 +00:00
|
|
|
// Change moderator settings
|
|
|
|
if(handler.hasPostParameter("members") || handler.hasPostParameter("name")) {
|
|
|
|
|
|
|
|
// Check if user is the moderator of the conversation
|
|
|
|
if(!await ConversationsHelper.IsUserModerator(handler.getUserId(), convID))
|
|
|
|
handler.error(401, "You are not allowed to perform changes on this conversation !");
|
|
|
|
|
|
|
|
|
|
|
|
// Update conversation name (if required)
|
|
|
|
if(handler.hasPostParameter("name")) {
|
|
|
|
const name = handler.postString("name");
|
|
|
|
await ConversationsHelper.SetName(convID, name == "false" ? "" : removeHTMLNodes(name));
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2019-11-30 11:24:19 +00:00
|
|
|
|
|
|
|
handler.success("Conversation information successfully updated!");
|
|
|
|
}
|
|
|
|
|
2019-11-23 19:53:13 +00:00
|
|
|
/**
|
|
|
|
* Get and return safely a conversation ID specified in a $_POST Request
|
|
|
|
*
|
|
|
|
* @param name The name of the POST field containing the ID of the conversation
|
|
|
|
* @param handler
|
|
|
|
*/
|
|
|
|
private static async GetPostConversationId(name : string, handler: RequestHandler) : Promise<number> {
|
|
|
|
const convID = handler.postInt(name);
|
|
|
|
|
|
|
|
// Check out whether the user belongs to the conversation or not
|
|
|
|
if(!await ConversationsHelper.DoesUsersBelongsTo(handler.getUserId(), convID))
|
|
|
|
handler.error(401, "You are not allowed to perform queries on this conversation!");
|
|
|
|
|
|
|
|
return convID;
|
|
|
|
}
|
|
|
|
|
2019-11-23 17:41:13 +00:00
|
|
|
/**
|
|
|
|
* Turn a conversation object into an API entry
|
|
|
|
*
|
|
|
|
* @param c
|
|
|
|
*/
|
|
|
|
private static ConversationToAPI(c : Conversation) : any {
|
|
|
|
return {
|
|
|
|
ID: c.id,
|
|
|
|
ID_owner: c.ownerID,
|
|
|
|
last_active: c.lastActive,
|
|
|
|
name: c.name.length > 0 ? c.name : false,
|
|
|
|
following: c.following ? 1 : 0,
|
|
|
|
saw_last_message: c.sawLastMessage ? 1 : 0,
|
|
|
|
members: c.members
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|