1
0
mirror of https://gitlab.com/comunic/comunicapiv2 synced 2025-06-20 00:25:17 +00:00

Can send text messages

This commit is contained in:
2019-11-30 17:13:36 +01:00
parent e377d29096
commit fef321f0ff
5 changed files with 97 additions and 5 deletions

View File

@ -1,7 +1,7 @@
import { Conversation, BaseConversation } from "../entities/Conversation";
import { DatabaseHelper } from "./DatabaseHelper";
import { time } from "../utils/DateUtils";
import { ConversationMessage } from "../entities/ConversationMessage";
import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage";
/**
* Conversations helper
@ -295,6 +295,53 @@ export class ConversationsHelper {
});
}
/**
* Insert a new message into the database
*
* @param message The message to insert
*/
public static async SendMessage(message: BaseConversationMessage) {
const t = time();
// Insert the message in the database
await DatabaseHelper.InsertRow(
MESSAGES_TABLE,
{
conv_id: message.convID,
user_id: message.userID,
time_insert: t,
message: message.message,
image_path: message.imagePath
}
);
// Update the last activity of the conversation
await DatabaseHelper.UpdateRows({
table: LIST_TABLE,
where: {
id: message.convID
},
set: {
last_active: t,
}
});
// Mark all the user of the conversations as unread, except current user
await DatabaseHelper.UpdateRows({
table: USERS_TABLE,
where: {
conv_id: message.convID
},
customWhere: "user_id != ?",
customWhereArgs: [message.userID.toString()],
set: {
saw_last_message: 0
}
});
}
/**
* Get the list of members of a conversation
*

View File

@ -26,6 +26,8 @@ export interface QueryInformation {
export interface UpdateInformation {
table: string,
set: Object,
customWhere ?: string,
customWhereArgs ?: Array<string>,
where ?: Object,
}
@ -227,8 +229,22 @@ export class DatabaseHelper {
}
}
}
else
// Security : block unconditionned updates
else if(!info.customWhere)
throw Error("Error : Updates without conditions are blocked for security!");
// Process custom conditions
if(info.customWhere) {
if(info.where)
sql += " AND (" + info.customWhere + ") ";
else
sql += " WHERE " + info.customWhere + " ";
if(info.customWhereArgs)
info.customWhereArgs.forEach(e => args.push(e));
}
// Execute request
return await new Promise((resolve, reject) => {