1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2024-12-28 14:38:52 +00:00

Send (create) a friendship request

This commit is contained in:
Pierre HUBERT 2019-12-31 10:47:55 +01:00
parent ca2e106852
commit fed23994fa
3 changed files with 46 additions and 0 deletions

View File

@ -88,6 +88,33 @@ export class FriendsController {
h.send(response);
}
/**
* Send a friendship request
*
* @param h Request handler
*/
public static async SendRequest(h: RequestHandler) {
const friendID = await h.postUserId("friendID");
if(friendID == h.getUserId())
h.error(401, "You can not become a friend of yourself!");
if(await FriendsHelper.AreFriend(h.getUserId(), friendID))
h.error(401, "You are already friend with this personn!");
// Check if a request is already in progress
if(await FriendsHelper.SentRequest(h.getUserId(), friendID)
|| await FriendsHelper.SentRequest(friendID, h.getUserId()))
h.error(401, "A friendship request is already in progress!");
// Send the request
await FriendsHelper.SendRequest(h.getUserId(), friendID);
// TODO : create the notification
h.success("Send (create) the friendship request");
}
/**
* Turn a friend object into an API entry
*

View File

@ -83,6 +83,8 @@ export const Routes : Route[] = [
{path: "/friends/getStatus", cb: (h) => FriendsController.GetStatus(h)},
{path: "/friends/sendRequest", cb: (h) => FriendsController.SendRequest(h)},
// Conversations controller
{path: "/conversations/create", cb: (h) => ConversationsController.CreateConversation(h)},

View File

@ -80,6 +80,23 @@ export class FriendsHelper {
return this.DbToFriend(result);
}
/**
* Send (create) a new membership request
*
* @param userID The user sending the request
* @param targetUser The ID of the target user
*/
public static async SendRequest(userID: number, targetUser: number) {
await DatabaseHelper.InsertRow(
FRIENDS_TABLE,
{
ID_personne: targetUser,
ID_amis: userID,
actif: 0
}
);
}
/**
* Check out whether a user sent a request to another friend
*