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

Can delete a membership to a conversation

This commit is contained in:
Pierre HUBERT 2019-12-07 18:26:06 +01:00
parent aeb561bd41
commit fad098114a

View File

@ -278,6 +278,22 @@ export class ConversationsHelper {
})).map(m => this.DBToConversationMessage(convID, m));
}
/**
* Get all the messages of a single user for a conversation
*
* @param convID Target conversation ID
* @param userID Target user ID
*/
public static async GetUserMessagesForConversation(convID: number, userID: number): Promise<Array<ConversationMessage>> {
return (await DatabaseHelper.Query({
table: MESSAGES_TABLE,
where: {
conv_id: convID,
user_id: userID
}
})).map(m => this.DBToConversationMessage(convID, m));
}
/**
* Get older messages of a conversation
*
@ -464,11 +480,13 @@ export class ConversationsHelper {
* @param convID Target conversation ID
*/
public static async RemoveUserFromConversation(userID: number, convID: number) {
// Check whether the user is the owner of the conversation or not
if(await this.IsUserModerator(userID, convID))
await this.DeleteConversations(convID);
//else
// Delete the membersip fot
// TODO : implement
else
// Only delete the messages & membership of teh user
await this.DeleteMember(convID, userID);
}
@ -479,9 +497,8 @@ export class ConversationsHelper {
* @param convID The ID of the conversation to delete
*/
private static async DeleteConversations(convID: number) {
// Get all the messages of the conversations
// Get & delete all the messages of the conversations
const messages = await this.GetNewMessages(convID, 0);
for (const message of messages) {
await this.DeleteMessage(message);
}
@ -497,6 +514,23 @@ export class ConversationsHelper {
});
}
/**
* Delete a conversation membership
*
* @param convID Target conversation
* @param memberID Target user ID
*/
private static async DeleteMember(convID: number, memberID: number) {
// Get & delete all the messages of the member
const messages = await this.GetUserMessagesForConversation(convID, memberID);
for (const message of messages) {
await this.DeleteMessage(message);
}
// Delete membership
await this.RemoveMember(convID, memberID);
}
/**
* Delete a conversation message from the database
*