mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-12-28 14:38:52 +00:00
Can send text messages
This commit is contained in:
parent
e377d29096
commit
fef321f0ff
@ -202,6 +202,29 @@ export class ConversationsController {
|
||||
h.send(h.send(messages.map(e => this.ConversationMessageToAPI(e))));
|
||||
}
|
||||
|
||||
/**
|
||||
* Send a new message
|
||||
*
|
||||
* @param h Request handler
|
||||
*/
|
||||
public static async SendMessage(h: RequestHandler) {
|
||||
const convID = await this.GetPostConversationId("conversationID", h);
|
||||
const message = removeHTMLNodes(h.postString("message", 0));
|
||||
|
||||
if(message.length < 3)
|
||||
h.error(401, "Message is empty!");
|
||||
|
||||
// Send message
|
||||
await ConversationsHelper.SendMessage({
|
||||
userID: h.getUserId(),
|
||||
convID: convID,
|
||||
message: message,
|
||||
imagePath: ""
|
||||
});
|
||||
|
||||
h.success("Conversation message was sent!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return safely a conversation ID specified in a $_POST Request
|
||||
*
|
||||
|
@ -60,6 +60,8 @@ export const Routes : Route[] = [
|
||||
{path: "/conversations/refresh", cb: (h) => ConversationsController.RefreshList(h)},
|
||||
|
||||
{path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)},
|
||||
|
||||
{path: "/conversations/sendMessage", cb: (h) => ConversationsController.SendMessage(h)},
|
||||
|
||||
|
||||
// Search controller
|
||||
|
@ -6,15 +6,19 @@ import { pathUserData } from "../utils/UserDataUtils";
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
export interface ConversationMessageInfo {
|
||||
id: number,
|
||||
// Used directly when creating a new message
|
||||
export interface BaseConversationMessage {
|
||||
convID: number,
|
||||
userID: number,
|
||||
timeSent: number,
|
||||
imagePath: string,
|
||||
message: string
|
||||
}
|
||||
|
||||
export interface ConversationMessageInfo extends BaseConversationMessage {
|
||||
id: number,
|
||||
timeSent: number,
|
||||
}
|
||||
|
||||
export class ConversationMessage implements ConversationMessageInfo {
|
||||
public id: number;
|
||||
public convID: number;
|
||||
|
@ -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
|
||||
*
|
||||
|
@ -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) => {
|
||||
|
Loading…
Reference in New Issue
Block a user