-
Notifications
You must be signed in to change notification settings - Fork 194
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
🛠 Implement endpoint to get message file and other fixes (#2416)
* Fix medias migration script * Fix support for images preview * Implement new messages-files service * Implemented the controllers * Add some comments * Fix delay requests with async calls * Maybe fixed open on desktop app * Fix linter
- Loading branch information
1 parent
b53199a
commit f4b3fb2
Showing
21 changed files
with
276 additions
and
324 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
125 changes: 125 additions & 0 deletions
125
twake/backend/node/src/services/messages/services/messages-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
import { Channel } from "../../../services/channels/entities"; | ||
import { fileIsMedia } from "../../../services/files/utils"; | ||
import { UserObject } from "../../../services/user/web/types"; | ||
import { formatUser } from "../../../utils/users"; | ||
import { Initializable } from "../../../core/platform/framework"; | ||
import Repository from "../../../core/platform/services/database/services/orm/repository/repository"; | ||
import gr from "../../../services/global-resolver"; | ||
import { MessageFileRef, TYPE as TYPERef } from "../entities/message-file-refs"; | ||
import { MessageFile, TYPE } from "../entities/message-files"; | ||
import { Message } from "../entities/messages"; | ||
|
||
export class MessagesFilesService implements Initializable { | ||
version: "1"; | ||
msgFilesRepository: Repository<MessageFile>; | ||
msgFilesRefRepository: Repository<MessageFileRef>; | ||
|
||
constructor() {} | ||
|
||
async init() { | ||
this.msgFilesRepository = await gr.database.getRepository(TYPE, MessageFile); | ||
this.msgFilesRefRepository = await gr.database.getRepository(TYPERef, MessageFileRef); | ||
return this; | ||
} | ||
|
||
/** | ||
* Delete a message file and test this files belongs to the right user | ||
* @param message_id | ||
* @param id | ||
* @param user_id | ||
* @returns | ||
*/ | ||
async deleteMessageFile(message_id: string, id: string, user_id: string) { | ||
const msgFile = await this.getMessageFile(message_id, id); | ||
if (!msgFile) return null; | ||
|
||
if (msgFile.message.user_id !== user_id) return null; | ||
|
||
await this.msgFilesRepository.remove(msgFile); | ||
|
||
for (const target_type of ["channel_media", "channel_file", "channel"]) { | ||
const ref = await this.msgFilesRefRepository.findOne({ | ||
target_type, | ||
company_id: msgFile.channel.company_id, | ||
target_id: msgFile.channel.id, | ||
id: msgFile.id, | ||
}); | ||
if (ref) await this.msgFilesRefRepository.remove(ref); | ||
} | ||
|
||
return msgFile; | ||
} | ||
|
||
/** | ||
* Get a message file and returns more contextual data | ||
* @param message_id | ||
* @param id | ||
* @returns | ||
*/ | ||
async getMessageFile( | ||
message_id: string, | ||
id: string, | ||
): Promise< | ||
MessageFile & { | ||
user: UserObject; | ||
message: Message; | ||
channel: Channel; | ||
navigation: { next: string; previous: string }; | ||
} | ||
> { | ||
const msgFile = await this.msgFilesRepository.findOne({ message_id, id }); | ||
if (!msgFile) return null; | ||
|
||
const message = await gr.services.messages.messages.get({ | ||
thread_id: msgFile.thread_id, | ||
id: message_id, | ||
}); | ||
const channel = await gr.services.channels.channels.get({ | ||
company_id: message.cache.company_id, | ||
workspace_id: message.cache.workspace_id, | ||
id: message.cache.channel_id, | ||
}); | ||
const user = await formatUser(await gr.services.users.get({ id: message.user_id })); | ||
|
||
const navigationPk = { | ||
target_type: fileIsMedia(msgFile) ? "channel_media" : "channel_file", | ||
company_id: channel.company_id, | ||
target_id: channel.id, | ||
}; | ||
const next = ( | ||
await this.msgFilesRefRepository.find(navigationPk, { | ||
pagination: { | ||
page_token: null, | ||
limitStr: "2", | ||
reversed: true, | ||
}, | ||
$gte: [["id", msgFile.id]], | ||
}) | ||
) | ||
.getEntities() | ||
.filter(a => a.id !== msgFile.id)?.[0]?.id; | ||
const previous = ( | ||
await this.msgFilesRefRepository.find(navigationPk, { | ||
pagination: { | ||
page_token: null, | ||
limitStr: "2", | ||
reversed: false, | ||
}, | ||
$gte: [["id", msgFile.id]], | ||
}) | ||
) | ||
.getEntities() | ||
.filter(a => a.id !== msgFile.id)?.[0]?.id; | ||
|
||
return { | ||
...msgFile, | ||
user, | ||
message, | ||
channel, | ||
navigation: { | ||
next, | ||
previous, | ||
}, | ||
}; | ||
} | ||
} |
52 changes: 52 additions & 0 deletions
52
twake/backend/node/src/services/messages/web/controllers/messages-files.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
import gr from "../../../global-resolver"; | ||
import { FastifyRequest, FastifyReply } from "fastify"; | ||
|
||
export class MessagesFilesController { | ||
async deleteMessageFile( | ||
request: FastifyRequest<{ | ||
Params: { | ||
company_id: string; | ||
message_id: string; | ||
id: string; | ||
}; | ||
}>, | ||
reply: FastifyReply, | ||
) { | ||
const user = request.currentUser; | ||
const resp = await gr.services.messages.messagesFiles.deleteMessageFile( | ||
request.params.message_id, | ||
request.params.id, | ||
user.id, | ||
); | ||
|
||
if (!resp) reply.code(404).send(); | ||
|
||
reply.send(resp); | ||
} | ||
|
||
async getMessageFile( | ||
request: FastifyRequest<{ | ||
Params: { | ||
company_id: string; | ||
message_id: string; | ||
id: string; | ||
}; | ||
}>, | ||
reply: FastifyReply, | ||
) { | ||
const user = request.currentUser; | ||
const resp = await gr.services.messages.messagesFiles.getMessageFile( | ||
request.params.message_id, | ||
request.params.id, | ||
); | ||
|
||
if (!resp) reply.code(404).send(); | ||
|
||
//Check user has access to this file (check access to channel) | ||
if (!(await gr.services.channels.members.getChannelMember(user, resp.channel))) { | ||
reply.code(403).send(); | ||
} | ||
|
||
reply.send(resp); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.