mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2025-06-20 08:35:17 +00:00
Can create conversations
This commit is contained in:
@ -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)},
|
||||
|
Reference in New Issue
Block a user