-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: store messages in redis-stack instead of db
- Loading branch information
Showing
21 changed files
with
494 additions
and
375 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
This file was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
FROM node:20-alpine as builder | ||
WORKDIR /app | ||
RUN npm i -g pnpm@8 | ||
|
||
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc ./ | ||
COPY apps/chat-messages-store /app/apps/chat-messages-store | ||
COPY libs/config /app/libs/config | ||
COPY libs/bus-core /app/libs/bus-core | ||
|
||
RUN pnpm prune --prod | ||
|
||
FROM node:20-alpine as node_prod_base | ||
WORKDIR /app | ||
RUN apk add wget && \ | ||
wget -q -t3 'https://packages.doppler.com/public/cli/rsa.8004D9FF50437357.key' -O /etc/apk/keys/cli@doppler-8004D9FF50437357.rsa.pub && \ | ||
echo 'https://packages.doppler.com/public/cli/alpine/any-version/main' | tee -a /etc/apk/repositories && \ | ||
apk add doppler && apk del wget && \ | ||
rm -rf /var/cache/apk/* | ||
COPY package.json pnpm-lock.yaml pnpm-workspace.yaml .npmrc docker-entrypoint.sh ./ | ||
RUN chmod +x docker-entrypoint.sh | ||
RUN npm i -g pnpm@8 | ||
ENTRYPOINT ["/app/docker-entrypoint.sh"] | ||
|
||
FROM node_prod_base | ||
WORKDIR /app | ||
COPY --from=builder /app /app | ||
CMD ["pnpm", "--filter=@twir/chat-messages-store", "start"] |
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,19 @@ | ||
{ | ||
"name": "@twir/chat-messages-store", | ||
"main": "src/index.js", | ||
"type": "module", | ||
"scripts": { | ||
"start": "node src/index.js", | ||
"dev": "node --watch --watch-preserve-output src/index.js" | ||
}, | ||
"keywords": [], | ||
"author": "", | ||
"license": "ISC", | ||
"dependencies": { | ||
"@twir/bus-core": "workspace:*", | ||
"@twir/config": "workspace:*", | ||
"nats": "2.22.0", | ||
"redis": "4.6.13", | ||
"redis-om": "0.4.3" | ||
} | ||
} |
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,84 @@ | ||
import { config } from '@twir/config' | ||
import { JSONCodec, connect as natsConnect } from 'nats' | ||
import { createClient } from 'redis' | ||
import { EntityId, Repository, Schema } from 'redis-om' | ||
|
||
const redisClient = createClient({ | ||
url: config.REDIS_URL, | ||
}) | ||
await redisClient.connect() | ||
|
||
const schema = new Schema('chat-messages-store:messages', { | ||
message_id: { type: 'string' }, | ||
channel_id: { type: 'string' }, | ||
user_id: { type: 'string' }, | ||
user_login: { type: 'string' }, | ||
text: { type: 'text' }, | ||
can_be_deleted: { type: 'boolean' }, | ||
created_at: { type: 'string' }, | ||
}) | ||
|
||
const repository = new Repository(schema, redisClient) | ||
await repository.createIndex() | ||
|
||
const sc = JSONCodec() | ||
const nc = await natsConnect({ | ||
servers: config.NATS_URL, | ||
}) | ||
|
||
const chatMessagesSub = nc.subscribe( | ||
'chat.messages', | ||
{ | ||
queue: 'chat-messages-store', | ||
}, | ||
) | ||
const chatMessagesStorePub = nc.subscribe( | ||
'chat_messages_store.get_by_text_for_delete', | ||
{ queue: 'chat-messages-store' }, | ||
) | ||
|
||
const ignoredBadges = ['broadcaster', 'moderator', 'vip', 'subscriber']; | ||
|
||
(async () => { | ||
for await (const m of chatMessagesSub) { | ||
const data = sc.decode(m.data) | ||
|
||
const canBeDeleted = !data.badges.some(b => ignoredBadges.includes(b.set_id)) | ||
|
||
const entity = await repository.save({ | ||
message_id: data.message_id, | ||
channel_id: data.broadcaster_user_id, | ||
user_id: data.chatter_user_id, | ||
user_login: data.chatter_user_login, | ||
text: data.message.text, | ||
can_be_deleted: canBeDeleted, | ||
created_at: new Date().toISOString(), | ||
}) | ||
await repository.expire(entity[EntityId], 60 * 60) | ||
} | ||
})(); | ||
|
||
(async () => { | ||
for await (const m of chatMessagesStorePub) { | ||
const data = sc.decode(m.data) | ||
if (!data.channel_id || !data.text) { | ||
m.respond(sc.encode({ messages: [] })) | ||
continue | ||
} | ||
|
||
const messages = await repository | ||
.search() | ||
.where('channel_id').equal(data.channel_id) | ||
.and('can_be_deleted').equal(true) | ||
.and('text').match(`*${data.text}*`) | ||
.return.all() | ||
|
||
m.respond(sc.encode({ | ||
messages, | ||
})) | ||
} | ||
|
||
// eslint-disable-next-line style/semi | ||
})(); | ||
|
||
console.info('[chat-messages-store] is running') |
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.