diff --git a/src/controllers/ConversationsController.ts b/src/controllers/ConversationsController.ts index 365db81..b9fe686 100644 --- a/src/controllers/ConversationsController.ts +++ b/src/controllers/ConversationsController.ts @@ -1,6 +1,7 @@ import { RequestHandler } from "../entities/RequestHandler"; import { ConversationsHelper } from "../helpers/ConversationsHelper"; -import { Conversation } from "../entities/Conversation"; +import { Conversation, BaseConversation } from "../entities/Conversation"; +import { UserHelper } from "../helpers/UserHelper"; /** * Conversations controller @@ -10,6 +11,42 @@ import { Conversation } from "../entities/Conversation"; export class ConversationsController { + /** + * Create a new conversation + * + * @param h Request handler + */ + public static async CreateConversation(h : RequestHandler) { + + const name = h.postString("name"); + + const members = h.postNumbersList("users"); + + // 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(), + name: name == "false" ? "" : name, + following: h.postBool("follow"), + members: members, + } + + const convID = await ConversationsHelper.Create(conv); + + // Success + h.send({ + conversationID: convID, + success: "The conversation was successfully created!" + }) + } + /** * Get the list of conversations of the user * diff --git a/src/controllers/Routes.ts b/src/controllers/Routes.ts index 4b01640..0b503bd 100644 --- a/src/controllers/Routes.ts +++ b/src/controllers/Routes.ts @@ -48,6 +48,8 @@ export const Routes : Route[] = [ // Conversations controller + {path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)}, + {path: "/conversations/getList", cb: (h) => ConversationsController.GetList(h)}, {path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, diff --git a/src/entities/Conversation.ts b/src/entities/Conversation.ts index 3dd3b15..da871f3 100644 --- a/src/entities/Conversation.ts +++ b/src/entities/Conversation.ts @@ -4,13 +4,16 @@ * @author Pierre HUBERT */ -export interface Conversation { - id: number, +export interface BaseConversation { ownerID: number, name: string, + following: boolean, + members: Array +} + +export interface Conversation extends BaseConversation { + id: number, lastActive: number, timeCreate: number, - following: boolean, sawLastMessage: boolean, - members: Array } \ No newline at end of file diff --git a/src/entities/RequestHandler.ts b/src/entities/RequestHandler.ts index 0e394f2..b19a861 100644 --- a/src/entities/RequestHandler.ts +++ b/src/entities/RequestHandler.ts @@ -113,6 +113,20 @@ export class RequestHandler { return list; } + /** + * Get a boolean included in the request + * + * @param name The name of the POST field + */ + public postBool(name: string) : boolean { + const param = this.getPostParam(name); + + if(param == undefined) + this.error(400, "Missing boolean '" + name + "' in the request!"); + + return param === "true" || param === true; + } + /** * Validate API tokens diff --git a/src/helpers/ConversationsHelper.ts b/src/helpers/ConversationsHelper.ts index be7cfd2..5db04b2 100644 --- a/src/helpers/ConversationsHelper.ts +++ b/src/helpers/ConversationsHelper.ts @@ -1,5 +1,6 @@ -import { Conversation } from "../entities/Conversation"; +import { Conversation, BaseConversation } from "../entities/Conversation"; import { DatabaseHelper } from "./DatabaseHelper"; +import { time } from "../utils/DateUtils"; /** * Conversations helper @@ -13,6 +14,53 @@ const MESSAGES_TABLE = "comunic_conversations_messages"; export class ConversationsHelper { + /** + * Create a new conversation + * + * @param conv Information about the conversation to create + */ + public static async Create(conv : BaseConversation) : Promise { + + // Create the conversation in the main table + const convID = await DatabaseHelper.InsertRow(LIST_TABLE, { + "user_id": conv.ownerID, + "name": conv.name, + "last_active": time(), + "creation_time": time() + }); + + // Add the members to the conversation + for (const userID of conv.members) { + await this.AddMember( + convID, + userID, + conv.ownerID == userID ? conv.following : true); + } + + return convID; + } + + /** + * Add a member to a conversation + * + * @param convID Conversation ID + * @param userID User ID + * @param following Specify whether the user is following + * the conversation or not + */ + private static async AddMember(convID : number, userID: number, following : boolean = true) { + await DatabaseHelper.InsertRow( + USERS_TABLE, + { + "conv_id": convID, + "user_id": userID, + "time_add": time(), + "following": following ? 1 : 0, + "saw_last_message": 1 + } + ); + } + /** * Get the list of conversations of the user * diff --git a/src/helpers/UserHelper.ts b/src/helpers/UserHelper.ts index 66f67e6..b3cb93a 100644 --- a/src/helpers/UserHelper.ts +++ b/src/helpers/UserHelper.ts @@ -32,6 +32,20 @@ export class UserHelper { return this.DbToUser(result); } + /** + * Check out whether a user exists or not + * + * @param id The ID of the user to check + */ + public static async Exists(id: number) : Promise { + return await DatabaseHelper.Count({ + table: TABLE_NAME, + where: { + ID: id + } + }) > 0; + } + /** * Search for user in the database * diff --git a/src/utils/StringUtils.ts b/src/utils/StringUtils.ts index 2578168..ec2dd03 100644 --- a/src/utils/StringUtils.ts +++ b/src/utils/StringUtils.ts @@ -21,4 +21,14 @@ export function checkMail(emailAddress: string): boolean { */ export function fixEncoding(input : string) : string { return Buffer.from(input, "latin1").toString("utf-8"); +} + +/** + * Remove HTML markup codes in a string (<, >) + * + * @param input String to change + */ +export function removeHTMLNodes(input : string) : string { + return input.replace(//g, ">"); } \ No newline at end of file