mirror of
https://gitlab.com/comunic/comunicapiv2
synced 2024-11-22 05:19:22 +00:00
Can open new conversations
This commit is contained in:
parent
abd6514b59
commit
46f6e15b02
@ -3,6 +3,7 @@ import { ConversationsHelper } from "../helpers/ConversationsHelper";
|
||||
import { Conversation, BaseConversation } from "../entities/Conversation";
|
||||
import { UserHelper } from "../helpers/UserHelper";
|
||||
import { removeHTMLNodes } from "../utils/StringUtils";
|
||||
import { ConversationMessage } from "../entities/ConversationMessage";
|
||||
|
||||
/**
|
||||
* Conversations controller
|
||||
@ -120,6 +121,33 @@ export class ConversationsController {
|
||||
h.success("Conversation information successfully updated!");
|
||||
}
|
||||
|
||||
/**
|
||||
* Refresh current user conversations
|
||||
*
|
||||
* @param h Request handler
|
||||
*/
|
||||
public static async RefreshList(h: RequestHandler) {
|
||||
|
||||
const list = {};
|
||||
|
||||
// Check for new conversations
|
||||
if(h.hasPostParameter("newConversations")) {
|
||||
for(const convID of h.postNumbersSet("newConversations")) {
|
||||
if(!ConversationsHelper.DoesUsersBelongsTo(h.getUserId(), convID))
|
||||
h.error(401, "You are not allowed to fetch the messages of this conversation ("+convID+")!");
|
||||
|
||||
list["conversation-" + convID] = (await ConversationsHelper.GetLastMessages(convID, 10))
|
||||
.map(e => this.ConversationMessageToAPI(e));
|
||||
|
||||
// TODO : mark the user has seen the messages
|
||||
}
|
||||
}
|
||||
|
||||
// TODO : Check for refresh on some conversations
|
||||
|
||||
h.send(list);
|
||||
}
|
||||
|
||||
/**
|
||||
* Get and return safely a conversation ID specified in a $_POST Request
|
||||
*
|
||||
@ -152,4 +180,19 @@ export class ConversationsController {
|
||||
members: [...c.members]
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a conversation message into an API object
|
||||
*
|
||||
* @param c Information about the conversation
|
||||
*/
|
||||
private static ConversationMessageToAPI(c: ConversationMessage) : any {
|
||||
return {
|
||||
ID: c.id,
|
||||
ID_user: c.userID,
|
||||
time_insert: c.timeSent,
|
||||
message: c.message,
|
||||
image_path: c.hasImage ? c.imageURL : null
|
||||
};
|
||||
}
|
||||
}
|
@ -57,6 +57,9 @@ export const Routes : Route[] = [
|
||||
|
||||
{path: "/conversations/updateSettings", cb: (h) => ConversationsController.UpdateSettings(h)},
|
||||
|
||||
{path: "/conversations/refresh", cb: (h) => ConversationsController.RefreshList(h)},
|
||||
|
||||
|
||||
// Search controller
|
||||
{path: "/search/user", cb: (h) => SearchController.SearchUser(h)},
|
||||
{path: "/user/search", cb: (h) => SearchController.SearchUser(h)}, // Legacy
|
||||
|
42
src/entities/ConversationMessage.ts
Normal file
42
src/entities/ConversationMessage.ts
Normal file
@ -0,0 +1,42 @@
|
||||
import { pathUserData } from "../utils/UserDataUtils";
|
||||
|
||||
/**
|
||||
* Single conversation message
|
||||
*
|
||||
* @author Pierre HUBERT
|
||||
*/
|
||||
|
||||
export interface ConversationMessageInfo {
|
||||
id: number,
|
||||
convID: number,
|
||||
userID: number,
|
||||
timeSent: number,
|
||||
imagePath: string,
|
||||
message: string
|
||||
}
|
||||
|
||||
export class ConversationMessage implements ConversationMessageInfo {
|
||||
public id: number;
|
||||
public convID: number;
|
||||
public userID: number;
|
||||
public timeSent: number;
|
||||
public imagePath: string;
|
||||
public message: string;
|
||||
|
||||
constructor(info: ConversationMessageInfo) {
|
||||
this.id = info.id;
|
||||
this.convID = info.convID;
|
||||
this.userID = info.userID;
|
||||
this.timeSent = info.timeSent;
|
||||
this.imagePath = info.imagePath;
|
||||
this.message = info.message;
|
||||
}
|
||||
|
||||
get hasImage() : boolean {
|
||||
return this.imagePath.length > 1;
|
||||
}
|
||||
|
||||
get imageURL() : string {
|
||||
return pathUserData(this.imagePath);
|
||||
}
|
||||
}
|
@ -1,6 +1,7 @@
|
||||
import { Conversation, BaseConversation } from "../entities/Conversation";
|
||||
import { DatabaseHelper } from "./DatabaseHelper";
|
||||
import { time } from "../utils/DateUtils";
|
||||
import { ConversationMessage } from "../entities/ConversationMessage";
|
||||
|
||||
/**
|
||||
* Conversations helper
|
||||
@ -240,6 +241,23 @@ export class ConversationsHelper {
|
||||
}) == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the last messages of a conversation
|
||||
*
|
||||
* @param convID Target conversation ID
|
||||
* @param numberOfMessages The maximum number of messages to return
|
||||
*/
|
||||
public static async GetLastMessages(convID: number, numberOfMessages: number) : Promise<Array<ConversationMessage>> {
|
||||
return (await DatabaseHelper.Query({
|
||||
table: MESSAGES_TABLE,
|
||||
where: {
|
||||
conv_id: convID
|
||||
},
|
||||
limit: numberOfMessages,
|
||||
order: "id DESC"
|
||||
})).map(m => this.DBToConversationMessage(convID, m));
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the list of members of a conversation
|
||||
*
|
||||
@ -274,4 +292,22 @@ export class ConversationsHelper {
|
||||
members: await this.GetConversationMembers(row.id)
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Turn a database entry into a conversation message
|
||||
*
|
||||
* @param convID The ID of the conversation the message belongs to
|
||||
* @param row Row to convert
|
||||
* @return Generated conversation message
|
||||
*/
|
||||
private static DBToConversationMessage(convID: number, row: any) : ConversationMessage {
|
||||
return new ConversationMessage({
|
||||
id: row.id,
|
||||
convID: convID,
|
||||
userID: row.user_id,
|
||||
timeSent: row.time_insert,
|
||||
imagePath: row.image_path ? row.image_path : "",
|
||||
message: row.message ? row.message : ""
|
||||
});
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user