mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 05:19:22 +00:00
Can refresh conversation messages
This commit is contained in:
parent
c9f25a46ae
commit
2d29db4900
@ -132,7 +132,7 @@ export class ConversationsController {
|
||||
|
||||
// Check for new conversations
|
||||
if(h.hasPostParameter("newConversations")) {
|
||||
for(const convID of h.postNumbersSet("newConversations")) {
|
||||
for(const convID of h.postNumbersSet("newConversations", 0)) {
|
||||
if(!ConversationsHelper.DoesUsersBelongsTo(h.getUserId(), convID))
|
||||
h.error(401, "You are not allowed to fetch the messages of this conversation ("+convID+")!");
|
||||
|
||||
@ -144,7 +144,37 @@ export class ConversationsController {
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : Check for refresh on some conversations
|
||||
// Check for refresh on some conversations
|
||||
if(h.hasPostParameter("toRefresh")) {
|
||||
const toRefresh = h.postJSON("toRefresh");
|
||||
|
||||
for (const key in toRefresh) {
|
||||
if (toRefresh.hasOwnProperty(key)) {
|
||||
const element = toRefresh[key];
|
||||
|
||||
// Extract conversation ID
|
||||
if(!key.startsWith("conversation-"))
|
||||
h.error(400, "Entries of 'toRefresh' should start with 'conversation-' !");
|
||||
const convID = Number.parseInt(key.replace("conversation-", ""));
|
||||
|
||||
// Extract last message ID
|
||||
if(!element.hasOwnProperty("last_message_id"))
|
||||
h.error(400, "Missing last_message_id for conversation " + convID + "!");
|
||||
const lastMessageID = Number.parseInt(element.last_message_id);
|
||||
|
||||
// Check user rights
|
||||
if(!ConversationsHelper.DoesUsersBelongsTo(h.getUserId(), convID))
|
||||
h.error(401, "You are not allowed to fetch the messages of this conversation ("+convID+")!");
|
||||
|
||||
// Get the messages
|
||||
list["conversation-" + convID] = (await ConversationsHelper.GetNewMessages(convID, lastMessageID))
|
||||
.map(e => this.ConversationMessageToAPI(e));
|
||||
|
||||
// Mark the user has seen the messages
|
||||
await ConversationsHelper.MarkUserSeen(convID, h.getUserId());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
h.send(list);
|
||||
}
|
||||
|
@ -103,9 +103,10 @@ export class RequestHandler {
|
||||
* Get a list of integeres included in the request
|
||||
*
|
||||
* @param name The name of the post field
|
||||
* @param minEntries Specify the minimum number of entries required
|
||||
*/
|
||||
public postNumbersList(name: string) : Array<number> {
|
||||
const param = this.postString(name);
|
||||
public postNumbersList(name: string, minEntries : number = 1) : Array<number> {
|
||||
const param = this.postString(name, minEntries < 1 ? 0 : minEntries, minEntries > 0);
|
||||
let list = [];
|
||||
for (const el of param.split(",")) {
|
||||
|
||||
@ -119,6 +120,9 @@ export class RequestHandler {
|
||||
|
||||
}
|
||||
|
||||
if(list.length < minEntries)
|
||||
this.error(400, "Not enough entries in '" + name + "'!")
|
||||
|
||||
return list;
|
||||
}
|
||||
|
||||
@ -126,9 +130,29 @@ export class RequestHandler {
|
||||
* Turn a list of string into a Set object
|
||||
*
|
||||
* @param name Name of POST field
|
||||
* @param minEntries Minimum number of entries to specify
|
||||
*/
|
||||
public postNumbersSet(name : string) : Set<number> {
|
||||
return new Set(this.postNumbersList(name));
|
||||
public postNumbersSet(name : string, minEntries : number = 1) : Set<number> {
|
||||
return new Set(this.postNumbersList(name, minEntries));
|
||||
}
|
||||
|
||||
/**
|
||||
* Attempt to decode JSON included in a POST request
|
||||
*
|
||||
* @param name Name of POST field
|
||||
*/
|
||||
public postJSON(name: string) : any {
|
||||
const src = this.getPostParam(name);
|
||||
|
||||
if(src == undefined)
|
||||
this.error(400, "Missing JSON '" + name + "' in the request!");
|
||||
|
||||
try {
|
||||
const response = JSON.parse(src);
|
||||
return response;
|
||||
} catch(e) {
|
||||
this.error(500, "'" + name + "' is not a valid JSON !");
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -255,6 +255,24 @@ export class ConversationsHelper {
|
||||
},
|
||||
limit: numberOfMessages,
|
||||
order: "id DESC"
|
||||
})).map(m => this.DBToConversationMessage(convID, m)).reverse();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the new messages of a conversation
|
||||
*
|
||||
* @param convID Target conversation ID
|
||||
* @param lastMessageID The ID of the last known message
|
||||
*/
|
||||
public static async GetNewMessages(convID: number, lastMessageID: number): Promise<Array<ConversationMessage>> {
|
||||
return (await DatabaseHelper.Query({
|
||||
table: MESSAGES_TABLE,
|
||||
where: {
|
||||
conv_id: convID
|
||||
},
|
||||
customWhere: "ID > ?",
|
||||
customWhereArgs: [lastMessageID.toString()],
|
||||
order: "id"
|
||||
})).map(m => this.DBToConversationMessage(convID, m));
|
||||
}
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user