2019-11-30 10:11:51 +01:00
|
|
|
import { Conversation, BaseConversation } from "../entities/Conversation";
|
2019-11-23 18:41:13 +01:00
|
|
|
import { DatabaseHelper } from "./DatabaseHelper";
|
2019-11-30 10:11:51 +01:00
|
|
|
import { time } from "../utils/DateUtils";
|
2019-11-23 18:41:13 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Conversations helper
|
|
|
|
*
|
|
|
|
* @author Pierre HUBERT
|
|
|
|
*/
|
|
|
|
|
|
|
|
const LIST_TABLE = "comunic_conversations_list";
|
|
|
|
const USERS_TABLE = "comunic_conversations_users";
|
|
|
|
const MESSAGES_TABLE = "comunic_conversations_messages";
|
|
|
|
|
|
|
|
export class ConversationsHelper {
|
|
|
|
|
2019-11-30 10:11:51 +01:00
|
|
|
/**
|
|
|
|
* 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
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:41:13 +01:00
|
|
|
/**
|
|
|
|
* Get the list of conversations of the user
|
|
|
|
*
|
|
|
|
* @param userID Target user ID
|
|
|
|
*/
|
|
|
|
public static async GetListUser(userID: number) : Promise<Array<Conversation>> {
|
|
|
|
|
|
|
|
// Fetch the list of conversations
|
|
|
|
const result = await DatabaseHelper.Query({
|
|
|
|
fields: [
|
|
|
|
"*",
|
|
|
|
"l.id as id",
|
|
|
|
"l.user_id as owner_id" // The field conflits with user.user_id
|
|
|
|
],
|
|
|
|
table: LIST_TABLE + " l",
|
|
|
|
joins: [
|
|
|
|
|
|
|
|
// Joins with conversation members table
|
|
|
|
{
|
|
|
|
table: USERS_TABLE + " u",
|
|
|
|
condition: "l.id = u.conv_id"
|
|
|
|
}
|
|
|
|
|
|
|
|
],
|
|
|
|
where: {
|
|
|
|
"u.user_id": userID
|
|
|
|
},
|
|
|
|
order: "l.last_active DESC"
|
|
|
|
});
|
|
|
|
|
|
|
|
const list = [];
|
|
|
|
for (const el of result) {
|
|
|
|
list.push(await this.DBToConversationInfo(el));
|
|
|
|
}
|
|
|
|
return list;
|
|
|
|
}
|
|
|
|
|
2019-11-23 20:53:13 +01:00
|
|
|
/**
|
|
|
|
* Get information about a single conversation
|
|
|
|
*
|
|
|
|
* @param convID The ID of the conversation to get
|
|
|
|
*/
|
2019-11-23 20:54:35 +01:00
|
|
|
public static async GetSingle(convID : number, userID: number) : Promise<Conversation | null> {
|
2019-11-23 20:53:13 +01:00
|
|
|
const result = await DatabaseHelper.QueryRow({
|
|
|
|
fields: [
|
|
|
|
"*",
|
|
|
|
"l.id as id",
|
|
|
|
"l.user_id as owner_id",
|
|
|
|
],
|
|
|
|
table: LIST_TABLE + " l",
|
|
|
|
joins: [
|
|
|
|
// Joins with conversation members table
|
|
|
|
{
|
|
|
|
table: USERS_TABLE + " u",
|
|
|
|
condition: "l.id = u.conv_id"
|
|
|
|
}
|
|
|
|
],
|
|
|
|
where: {
|
|
|
|
"l.id": convID,
|
2019-11-23 20:54:35 +01:00
|
|
|
"u.user_id": userID
|
2019-11-23 20:53:13 +01:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
if(!result)
|
|
|
|
return null;
|
|
|
|
|
|
|
|
return await this.DBToConversationInfo(result);
|
|
|
|
}
|
2019-11-23 18:41:13 +01:00
|
|
|
|
2019-11-23 19:31:48 +01:00
|
|
|
/**
|
|
|
|
* Check out whether a user is the member of a conversation or not
|
|
|
|
*
|
|
|
|
* @param userID Target user ID
|
|
|
|
* @param convID Target conversation
|
|
|
|
*/
|
|
|
|
public static async DoesUsersBelongsTo(userID: number, convID: number) : Promise<boolean> {
|
|
|
|
return await DatabaseHelper.Count({
|
|
|
|
table: USERS_TABLE,
|
|
|
|
where: {
|
|
|
|
conv_id: convID,
|
|
|
|
user_id: userID
|
|
|
|
}
|
|
|
|
}) == 1;
|
|
|
|
}
|
|
|
|
|
2019-11-30 12:24:19 +01:00
|
|
|
/**
|
|
|
|
* Update following state of the conversation
|
|
|
|
*
|
|
|
|
* @param userID User to update
|
|
|
|
* @param convID Target conversation ID
|
|
|
|
* @param following New status
|
|
|
|
*/
|
|
|
|
public static async SetFollowing(userID: number, convID: number, following: boolean) {
|
|
|
|
await DatabaseHelper.UpdateRows({
|
|
|
|
table: USERS_TABLE,
|
|
|
|
set: {
|
|
|
|
"following": following ? 1 : 0
|
|
|
|
},
|
|
|
|
where: {
|
|
|
|
"conv_id": convID,
|
|
|
|
"user_id": userID
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2019-11-23 18:41:13 +01:00
|
|
|
/**
|
|
|
|
* Get the list of members of a conversation
|
|
|
|
*
|
|
|
|
* @param convID The ID of the target conversation
|
|
|
|
*/
|
|
|
|
private static async GetConversationMembers(convID : number): Promise<Array<number>> {
|
|
|
|
const result = await DatabaseHelper.Query({
|
|
|
|
table: USERS_TABLE,
|
|
|
|
where: {
|
|
|
|
"conv_id": convID
|
|
|
|
},
|
|
|
|
fields: ["user_id"]
|
|
|
|
});
|
|
|
|
|
|
|
|
return result.map((e) => e.user_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Turn a database entry into a conversation object
|
|
|
|
*
|
|
|
|
* @param row
|
|
|
|
*/
|
|
|
|
private static async DBToConversationInfo(row: any) : Promise<Conversation> {
|
|
|
|
return {
|
|
|
|
id: row.id,
|
|
|
|
ownerID: row.owner_id,
|
|
|
|
name: row.name,
|
|
|
|
lastActive: row.last_active,
|
|
|
|
timeCreate: row.time_add,
|
|
|
|
following: row.following,
|
2019-11-23 20:53:13 +01:00
|
|
|
sawLastMessage: row.saw_last_message == 1,
|
2019-11-23 18:41:13 +01:00
|
|
|
members: await this.GetConversationMembers(row.id)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|