1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-11-22 21:39:22 +00:00

Can create conversations

This commit is contained in:
Pierre HUBERT 2019-11-30 10:11:51 +01:00
parent b6739473a2
commit 3db123cd1e
7 changed files with 134 additions and 6 deletions

View File

@ -1,6 +1,7 @@
import { RequestHandler } from "../entities/RequestHandler"; import { RequestHandler } from "../entities/RequestHandler";
import { ConversationsHelper } from "../helpers/ConversationsHelper"; import { ConversationsHelper } from "../helpers/ConversationsHelper";
import { Conversation } from "../entities/Conversation"; import { Conversation, BaseConversation } from "../entities/Conversation";
import { UserHelper } from "../helpers/UserHelper";
/** /**
* Conversations controller * Conversations controller
@ -10,6 +11,42 @@ import { Conversation } from "../entities/Conversation";
export class ConversationsController { 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 * Get the list of conversations of the user
* *

View File

@ -48,6 +48,8 @@ export const Routes : Route[] = [
// Conversations controller // Conversations controller
{path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)},
{path: "/conversations/getList", cb: (h) => ConversationsController.GetList(h)}, {path: "/conversations/getList", cb: (h) => ConversationsController.GetList(h)},
{path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)}, {path: "/conversations/getInfoOne", cb: (h) => ConversationsController.GetInfoSingle(h)},

View File

@ -4,13 +4,16 @@
* @author Pierre HUBERT * @author Pierre HUBERT
*/ */
export interface Conversation { export interface BaseConversation {
id: number,
ownerID: number, ownerID: number,
name: string, name: string,
lastActive: number,
timeCreate: number,
following: boolean, following: boolean,
sawLastMessage: boolean,
members: Array<number> members: Array<number>
} }
export interface Conversation extends BaseConversation {
id: number,
lastActive: number,
timeCreate: number,
sawLastMessage: boolean,
}

View File

@ -113,6 +113,20 @@ export class RequestHandler {
return list; 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 * Validate API tokens

View File

@ -1,5 +1,6 @@
import { Conversation } from "../entities/Conversation"; import { Conversation, BaseConversation } from "../entities/Conversation";
import { DatabaseHelper } from "./DatabaseHelper"; import { DatabaseHelper } from "./DatabaseHelper";
import { time } from "../utils/DateUtils";
/** /**
* Conversations helper * Conversations helper
@ -13,6 +14,53 @@ const MESSAGES_TABLE = "comunic_conversations_messages";
export class ConversationsHelper { export class ConversationsHelper {
/**
* Create a new conversation
*
* @param conv Information about the conversation to create
*/
public static async Create(conv : BaseConversation) : Promise<number> {
// 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 * Get the list of conversations of the user
* *

View File

@ -32,6 +32,20 @@ export class UserHelper {
return this.DbToUser(result); 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<boolean> {
return await DatabaseHelper.Count({
table: TABLE_NAME,
where: {
ID: id
}
}) > 0;
}
/** /**
* Search for user in the database * Search for user in the database
* *

View File

@ -22,3 +22,13 @@ export function checkMail(emailAddress: string): boolean {
export function fixEncoding(input : string) : string { export function fixEncoding(input : string) : string {
return Buffer.from(input, "latin1").toString("utf-8"); 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, "&lt;")
.replace(/>/g, "&gt;");
}