mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 13:29:22 +00:00
Can get the list of unread conversations
This commit is contained in:
parent
94600597b4
commit
83e61f49ca
@ -4,6 +4,7 @@ import { Conversation, BaseConversation } from "../entities/Conversation";
|
|||||||
import { UserHelper } from "../helpers/UserHelper";
|
import { UserHelper } from "../helpers/UserHelper";
|
||||||
import { removeHTMLNodes } from "../utils/StringUtils";
|
import { removeHTMLNodes } from "../utils/StringUtils";
|
||||||
import { ConversationMessage } from "../entities/ConversationMessage";
|
import { ConversationMessage } from "../entities/ConversationMessage";
|
||||||
|
import { UnreadConversation } from "../entities/UnreadConversation";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conversations controller
|
* Conversations controller
|
||||||
@ -294,6 +295,17 @@ export class ConversationsController {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of unread conversations of the user
|
||||||
|
*
|
||||||
|
* @param h Request handler
|
||||||
|
*/
|
||||||
|
public static async GetListUnread(h: RequestHandler) {
|
||||||
|
const list = await ConversationsHelper.GetListUnread(h.getUserId());
|
||||||
|
|
||||||
|
h.send(list.map(e => this.UnreadConversationToAPI(e)));
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get and return safely a conversation ID specified in a $_POST Request
|
* Get and return safely a conversation ID specified in a $_POST Request
|
||||||
*
|
*
|
||||||
@ -341,4 +353,19 @@ export class ConversationsController {
|
|||||||
image_path: c.hasImage ? c.imageURL : null
|
image_path: c.hasImage ? c.imageURL : null
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Turn an UnreadConversation object into an API entry
|
||||||
|
*
|
||||||
|
* @param c Target conversation
|
||||||
|
*/
|
||||||
|
private static UnreadConversationToAPI(c: UnreadConversation): any {
|
||||||
|
return {
|
||||||
|
id: c.id,
|
||||||
|
conv_name: c.name,
|
||||||
|
last_active: c.lastActive,
|
||||||
|
userID: c.userID,
|
||||||
|
message: c.message
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
@ -67,7 +67,9 @@ export const Routes : Route[] = [
|
|||||||
|
|
||||||
{path: "/conversations/get_older_messages", cb: (h) => ConversationsController.GetOlderMessages(h)},
|
{path: "/conversations/get_older_messages", cb: (h) => ConversationsController.GetOlderMessages(h)},
|
||||||
|
|
||||||
{path: "/conversations/get_number_unread", cb: (h) => ConversationsController.CountUnreadForUser(h)}
|
{path: "/conversations/get_number_unread", cb: (h) => ConversationsController.CountUnreadForUser(h)},
|
||||||
|
|
||||||
|
{path: "/conversations/get_list_unread", cb: (h) => ConversationsController.GetListUnread(h)},
|
||||||
|
|
||||||
|
|
||||||
// Search controller
|
// Search controller
|
||||||
|
13
src/entities/UnreadConversation.ts
Normal file
13
src/entities/UnreadConversation.ts
Normal file
@ -0,0 +1,13 @@
|
|||||||
|
/**
|
||||||
|
* Single unread conversation information
|
||||||
|
*
|
||||||
|
* @author Pierre HUBERT
|
||||||
|
*/
|
||||||
|
|
||||||
|
export interface UnreadConversation {
|
||||||
|
id: number,
|
||||||
|
name: string,
|
||||||
|
lastActive: number,
|
||||||
|
userID: number,
|
||||||
|
message: string
|
||||||
|
}
|
@ -2,6 +2,7 @@ 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, BaseConversationMessage } from "../entities/ConversationMessage";
|
import { ConversationMessage, BaseConversationMessage } from "../entities/ConversationMessage";
|
||||||
|
import { UnreadConversation } from "../entities/UnreadConversation";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Conversations helper
|
* Conversations helper
|
||||||
@ -409,6 +410,52 @@ export class ConversationsHelper {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the list of unread conversations of the user
|
||||||
|
*
|
||||||
|
* @param userID Target user ID
|
||||||
|
*/
|
||||||
|
public static async GetListUnread(userID: number) : Promise<Array<UnreadConversation>> {
|
||||||
|
return (await DatabaseHelper.Query({
|
||||||
|
table: USERS_TABLE,
|
||||||
|
tableAlias: "users",
|
||||||
|
|
||||||
|
joins: [
|
||||||
|
|
||||||
|
// Join with conversations list table
|
||||||
|
{
|
||||||
|
table: LIST_TABLE,
|
||||||
|
tableAlias: "list",
|
||||||
|
condition: "users.conv_id = list.id"
|
||||||
|
},
|
||||||
|
|
||||||
|
// Join with message table to get the latest message
|
||||||
|
{
|
||||||
|
table: MESSAGES_TABLE,
|
||||||
|
tableAlias: "messages",
|
||||||
|
condition: "messages.conv_id = users.conv_id"
|
||||||
|
}
|
||||||
|
],
|
||||||
|
|
||||||
|
where: {
|
||||||
|
"users.user_id": userID,
|
||||||
|
"users.following": 1,
|
||||||
|
"users.saw_last_message": 0,
|
||||||
|
},
|
||||||
|
|
||||||
|
customWhere: "list.last_active = messages.time_insert",
|
||||||
|
|
||||||
|
order: "list.last_active DESC"
|
||||||
|
|
||||||
|
})).map(m => <UnreadConversation>{
|
||||||
|
id: m.conv_id,
|
||||||
|
name: m.name,
|
||||||
|
lastActive: m.last_active,
|
||||||
|
userID: m.user_id,
|
||||||
|
message: m.message,
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Get the list of members of a conversation
|
* Get the list of members of a conversation
|
||||||
*
|
*
|
||||||
|
Loading…
Reference in New Issue
Block a user