-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #3 from Vitruveo/main
Release 08/05
- Loading branch information
Showing
5 changed files
with
89 additions
and
3 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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(); | ||
}); | ||
}; |
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,6 @@ | ||
export interface AvatarEnvelope { | ||
creatorId: string; | ||
type: string; | ||
name: string; | ||
avatar: string; | ||
} |