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

Can get older messages

This commit is contained in:
Pierre HUBERT 2019-12-07 17:08:53 +01:00
parent 4ade176434
commit ae88b7a979
3 changed files with 43 additions and 0 deletions

View File

@ -264,6 +264,25 @@ export class ConversationsController {
h.success("Conversation message was sent!");
}
/**
* Get the older messages of a conversation
*
* @param h Request handler
*/
public static async GetOlderMessages(h: RequestHandler) {
const convID = await this.GetPostConversationId("conversationID", h);
const maxID = h.postInt("oldest_message_id") - 1;
let limit = h.postInt("limit");
if(limit < 1) limit = 1;
else if(limit > 30) limit = 30;
// Get the list of messages
const messages = await ConversationsHelper.GetOlderMessage(convID, maxID, limit);
h.send(messages.map(e => this.ConversationMessageToAPI(e)));
}
/**
* Get and return safely a conversation ID specified in a $_POST Request
*

View File

@ -64,6 +64,8 @@ export const Routes : Route[] = [
{path: "/conversations/refresh_single", cb: (h) => ConversationsController.RefreshSingleConversation(h)},
{path: "/conversations/sendMessage", cb: (h) => ConversationsController.SendMessage(h)},
{path: "/conversations/get_older_messages", cb: (h) => ConversationsController.GetOlderMessages(h)},
// Search controller

View File

@ -276,6 +276,28 @@ export class ConversationsHelper {
})).map(m => this.DBToConversationMessage(convID, m));
}
/**
* Get older messages of a conversation
*
* @param convID ID of the target conversation
* @param startID ID from which the research should start
* @param limit Maximum number of messages to get
* @return The list of messages
*/
public static async GetOlderMessage(convID: number, startID: number, limit: number) : Promise<Array<ConversationMessage>> {
return (await DatabaseHelper.Query({
table: MESSAGES_TABLE,
where: {
conv_id: convID,
},
customWhere: "ID <= ?",
customWhereArgs: [startID.toString()],
order: "id DESC",
limit: limit
}))
.map(m => this.DBToConversationMessage(convID, m)).reverse();
}
/**
* Mark the user has seen the last messages of the conversation
*