mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-26 07:19:23 +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))));
|
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
|
* Get and return safely a conversation ID specified in a $_POST Request
|
||||||
*
|
*
|
||||||
|
@ -61,6 +61,8 @@ export const Routes : Route[] = [
|
|||||||
|
|
||||||
{path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)},
|
{path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)},
|
||||||
|
|
||||||
|
{path: "/conversations/sendMessage", cb: (h) => ConversationsController.SendMessage(h)},
|
||||||
|
|
||||||
|
|
||||||
// Search controller
|
// Search controller
|
||||||
{path: "/search/user", cb: (h) => SearchController.SearchUser(h)},
|
{path: "/search/user", cb: (h) => SearchController.SearchUser(h)},
|
||||||
|
@ -6,15 +6,19 @@ import { pathUserData } from "../utils/UserDataUtils";
|
|||||||
* @author Pierre HUBERT
|
* @author Pierre HUBERT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
export interface ConversationMessageInfo {
|
// Used directly when creating a new message
|
||||||
id: number,
|
export interface BaseConversationMessage {
|
||||||
convID: number,
|
convID: number,
|
||||||
userID: number,
|
userID: number,
|
||||||
timeSent: number,
|
|
||||||
imagePath: string,
|
imagePath: string,
|
||||||
message: string
|
message: string
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface ConversationMessageInfo extends BaseConversationMessage {
|
||||||
|
id: number,
|
||||||
|
timeSent: number,
|
||||||
|
}
|
||||||
|
|
||||||
export class ConversationMessage implements ConversationMessageInfo {
|
export class ConversationMessage implements ConversationMessageInfo {
|
||||||
public id: number;
|
public id: number;
|
||||||
public convID: number;
|
public convID: number;
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import { Conversation, BaseConversation } from "../entities/Conversation";
|
import { Conversation, BaseConversation } from "../entities/Conversation";
|
||||||
import { DatabaseHelper } from "./DatabaseHelper";
|
import { DatabaseHelper } from "./DatabaseHelper";
|
||||||
import { time } from "../utils/DateUtils";
|
import { time } from "../utils/DateUtils";
|
||||||
import { ConversationMessage } from "../entities/ConversationMessage";
|
import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conversations helper
|
* 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
|
* Get the list of members of a conversation
|
||||||
*
|
*
|
||||||
|
@ -26,6 +26,8 @@ export interface QueryInformation {
|
|||||||
export interface UpdateInformation {
|
export interface UpdateInformation {
|
||||||
table: string,
|
table: string,
|
||||||
set: Object,
|
set: Object,
|
||||||
|
customWhere ?: string,
|
||||||
|
customWhereArgs ?: Array<string>,
|
||||||
where ?: Object,
|
where ?: Object,
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -227,9 +229,23 @@ export class DatabaseHelper {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
else
|
|
||||||
|
// Security : block unconditionned updates
|
||||||
|
else if(!info.customWhere)
|
||||||
throw Error("Error : Updates without conditions are blocked for security!");
|
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
|
// Execute request
|
||||||
return await new Promise((resolve, reject) => {
|
return await new Promise((resolve, reject) => {
|
||||||
this.connection.query(sql, args, (err, results, f) => {
|
this.connection.query(sql, args, (err, results, f) => {
|
||||||
|
Loading…
Reference in New Issue
Block a user