mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Can create conversations
This commit is contained in:
parent
b6739473a2
commit
3db123cd1e
@ -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
|
||||
*
|
||||
|
@ -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)},
|
||||
|
@ -4,13 +4,16 @@
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
export interface Conversation {
|
||||
id: number,
|
||||
export interface BaseConversation {
|
||||
ownerID: number,
|
||||
name: string,
|
||||
following: boolean,
|
||||
members: Array<number>
|
||||
}
|
||||
|
||||
export interface Conversation extends BaseConversation {
|
||||
id: number,
|
||||
lastActive: number,
|
||||
timeCreate: number,
|
||||
following: boolean,
|
||||
sawLastMessage: boolean,
|
||||
members: Array<number>
|
||||
}
|
@ -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
|
||||
|
@ -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<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
|
||||
*
|
||||
|
@ -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<boolean> {
|
||||
return await DatabaseHelper.Count({
|
||||
table: TABLE_NAME,
|
||||
where: {
|
||||
ID: id
|
||||
}
|
||||
}) > 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* Search for user in the database
|
||||
*
|
||||
|
@ -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, "<")
|
||||
.replace(/>/g, ">");
|
||||
}
|
Loading…
Reference in New Issue
Block a user