chamesu
Version:
This package contains all packages of the chat application.
66 lines (57 loc) • 2.24 kB
text/typescript
/*
* Copyright (c) 2022.
* Author Peter Placzek (tada5hi)
* For the full copyright and license information,
* view the LICENSE file that was distributed with this source code.
*/
import {consumeMessageQueue, handleMessageQueueChannel} from "../modules/message-queue";
import {ImageResizeResponse} from "../components/image/resize-image";
import {getCustomRepository} from "typeorm";
import {UserRepository} from "@chamesu/common/domains/auth/user/repository";
import {QueueMessage} from "../modules/message-queue/type";
function createImageAggregatorHandlers() {
return {
resizeFailed: async(message: QueueMessage) => {
console.log('Resize failed' + message.data.resultMessage);
},
resized: async (message: QueueMessage) => {
const sizes : ImageResizeResponse[] = message.data.resultSizes;
const type : string = message.data.type;
switch (type) {
case "user-avatar":
case "user-cover":
const userId : number = message.data.id;
const repository = getCustomRepository(UserRepository);
const user = await repository.findOne(userId);
if(type === 'user-avatar') {
for(let i=0; i<sizes.length; i++) {
user.avatar[sizes[i].name] = sizes[i].file;
}
} else {
for(let i=0; i<sizes.length; i++) {
user.cover[sizes[i].name] = sizes[i].file;
}
}
await repository.save(user);
break;
}
}
}
}
export function buildImageAggregator() {
const handlers = createImageAggregatorHandlers();
function start() {
return consumeMessageQueue('image.event', ((async (channel, msg) => {
try {
await handleMessageQueueChannel(channel, handlers, msg);
await channel.ack(msg);
} catch (e) {
console.log(e);
await channel.reject(msg, false);
}
})));
}
return {
start
}
}