Skip to content

Commit

Permalink
feat: add controller to update social data
Browse files Browse the repository at this point in the history
  • Loading branch information
Matheus Paice committed May 6, 2024
1 parent 49dea44 commit 1750468
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 3 deletions.
4 changes: 2 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "vtru-studio-websocket",
"version": "1.1.38",
"version": "1.1.39",
"description": "",
"main": "index.js",
"scripts": {
Expand Down
2 changes: 2 additions & 0 deletions src/controllers/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import debug from 'debug';
import * as preSignedURL from './preSignedURL';
import * as notify from './notify';
import * as userSocialAvatar from './userSocialAvatar';
import { getConnection } from '../services';

const logger = debug('controllers');
Expand All @@ -10,6 +11,7 @@ export const controllersStart = async () => {

await preSignedURL.start();
await notify.start();
await userSocialAvatar.start();
};

controllersStart().catch((error) => {
Expand Down
78 changes: 78 additions & 0 deletions src/controllers/userSocialAvatar/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
import debug from 'debug';
import { nanoid } from 'nanoid';

import { RABBITMQ_EXCHANGE_CREATORS } from '../../constants';
import { disconnect, getChannel } from '../../services/rabbitmq';
import { captureException } from '../../services/sentry';
import { io } from '../../services';
import { AvatarEnvelope } from './types';

const logger = debug('controllers:userSocialAvatar');

const uniqueId = nanoid();

export const start = async () => {
const channel = await getChannel();

if (!channel) {
logger('Channel not available');
process.exit(1);
}
channel.on('close', () => {
logger('Channel closed');
process.exit(1);
});
channel.on('error', (error) => {
logger('Error occurred in channel:', error);
process.exit(1);
});

logger('Channel controller userSocialAvatar started');

const logQueue = `${RABBITMQ_EXCHANGE_CREATORS}.social.${uniqueId}`;
logger('logQueue', logQueue);
channel.assertExchange(RABBITMQ_EXCHANGE_CREATORS, 'topic', {
durable: true,
});
channel.assertQueue(logQueue, { durable: false });
channel.bindQueue(logQueue, RABBITMQ_EXCHANGE_CREATORS, 'userSocialAvatar');
channel.consume(logQueue, async (message) => {
if (!message) return;

try {
const parsedMessage = JSON.parse(
message.content.toString().trim()
) as AvatarEnvelope;

logger(parsedMessage);

const sockets = await io.sockets.in('creators').fetchSockets();

sockets.forEach((socket) => {
if (socket.data.id === parsedMessage.creatorId) {
socket.emit('userSocialAvatar', {
social: {
type: parsedMessage.type,
name: parsedMessage.name,
avatar: parsedMessage.avatar,
},
});
}
});

channel.ack(message);
return;
} catch (parsingError) {
captureException(parsingError);
}
channel.nack(message);
});

process.once('SIGINT', async () => {
logger(`Deleting queue ${logQueue}`);
await channel.deleteQueue(logQueue);

// disconnect from RabbitMQ
await disconnect();
});
};
6 changes: 6 additions & 0 deletions src/controllers/userSocialAvatar/types.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
export interface AvatarEnvelope {
creatorId: string;
type: string;
name: string;
avatar: string;
}

0 comments on commit 1750468

Please sign in to comment.