diff --git a/packages/chat/package.json b/packages/chat/package.json index 241d19d..8fce2fe 100644 --- a/packages/chat/package.json +++ b/packages/chat/package.json @@ -23,6 +23,7 @@ "@latus/core": "^1.0.0", "@latus/db": "^1.0.0", "@latus/governor": "^1.0.0", + "@latus/redis": "^1.0.0", "@latus/socket": "^1.0.0", "@reddichat/core": "^1.0.0", "@reddichat/state": "^1.0.0", diff --git a/packages/chat/src/client/index.js b/packages/chat/src/client/index.js index f29fc25..3870cbb 100644 --- a/packages/chat/src/client/index.js +++ b/packages/chat/src/client/index.js @@ -2,6 +2,7 @@ import Activity from '../packets/activity'; import Join from '../packets/join'; import Leave from '../packets/leave'; import Message from '../packets/message'; +import MessageSiteBan from '../packets/message-site-ban'; import chat from './state'; @@ -14,6 +15,7 @@ export default { Join: Join(latus), Leave: Leave(latus), Message: Message(latus), + MessageSiteBan: MessageSiteBan(latus), }), // eslint-disable-next-line global-require '@reddichat/state/effects': (latus) => require('./effects').default(latus), diff --git a/packages/chat/src/index.js b/packages/chat/src/index.js index 9a94082..0da52c8 100644 --- a/packages/chat/src/index.js +++ b/packages/chat/src/index.js @@ -13,6 +13,7 @@ import Activity from './packets/activity.server'; import Join from './packets/join.server'; import Leave from './packets/leave.server'; import Message from './packets/message.server'; +import MessageSiteBan from './packets/message-site-ban.server'; import ensureCanonical from './ensure-canonical'; import defaultState, {channelsToHydrate} from './state'; @@ -60,6 +61,7 @@ export default { Join: Join(latus), Leave: Leave(latus), Message: Message(latus), + MessageSiteBan: MessageSiteBan(latus), }), '@latus/socket/connect': async (socket, latus) => { const {req} = socket; diff --git a/packages/chat/src/packets/message-site-ban.js b/packages/chat/src/packets/message-site-ban.js new file mode 100644 index 0000000..2ada7be --- /dev/null +++ b/packages/chat/src/packets/message-site-ban.js @@ -0,0 +1,20 @@ +import {Packet, ValidationError} from '@latus/socket/packets'; +import {validate} from 'uuid'; + +export default () => class MessageSiteBan extends Packet { + + static get data() { + return 'string'; + } + + static async validate({data: uuid}, {req}) { + const {user} = req; + if (!user || !user.isAdmin) { + throw new ValidationError({code: 400, reason: 'unauthorized'}); + } + if (!validate(uuid)) { + throw new ValidationError({code: 400, reason: 'malformed'}); + } + } + +}; diff --git a/packages/chat/src/packets/message-site-ban.server.js b/packages/chat/src/packets/message-site-ban.server.js new file mode 100644 index 0000000..4363700 --- /dev/null +++ b/packages/chat/src/packets/message-site-ban.server.js @@ -0,0 +1,29 @@ +import {promisify} from 'util'; + +import {ModelMap} from '@latus/db'; +import {createClient, keys} from '@latus/redis'; + +import MessageSiteBan from './message-site-ban'; + +export default (latus) => class MessageSiteBanServer extends MessageSiteBan() { + + static async respond({data: uuid}, socket) { + const {Ban} = ModelMap(latus); + const redisClient = createClient(latus); + const mget = promisify(redisClient.mget.bind(redisClient)); + const messageKeys = await keys(redisClient, `*:messages:${uuid}`); + const messages = 0 === messageKeys.length + ? [] + : (await mget(messageKeys)) + .map((reply) => JSON.parse(reply)); + const promises = messages.map(async ({ip, owner, socket: socketId}) => { + await Ban.create({ + ip, + ...(owner ? {userId: owner} : {}), + }); + socket.to(socketId).send(['Refresh']); + }); + return Promise.all(promises); + } + +}; diff --git a/packages/chat/yarn.lock b/packages/chat/yarn.lock index 240b96b..c8095dc 100644 --- a/packages/chat/yarn.lock +++ b/packages/chat/yarn.lock @@ -990,15 +990,15 @@ "@latus/redis@^1.0.0": version "1.0.0" - resolved "https://npm.i12e.cha0s.io/@latus%2fredis/-/redis-1.0.0.tgz#b54c176a02481f8f43ea383d2df665c1aa365885" - integrity sha512-HSQPI57K7RZMGfehsMSRvvAY+paJ4gxeRNpcQhMhLmWH8ShartGSR7/vPEJjpdvGcSgucYb97KM913oAgDDG5g== + resolved "https://npm.i12e.cha0s.io/@latus%2fredis/-/redis-1.0.0.tgz#c029ffeac5e349f00cdb9a4f7a62e096164fbca5" + integrity sha512-0c9Ap1GO3WhGGPpOYsPzgoCyGMmlu0GFk+4vfSZ+L414FyY4RvjgcWXQjATVPfOHBPG2Vm+zKxHyyZt0no6E4w== dependencies: connect-redis "^5.0.0" debug "4.3.1" express-session "^1.17.1" mkdirp "^1.0.4" redis "^3.0.2" - socket.io-redis "^6.0.1" + socket.io-redis "5.3.0" "@latus/socket@^1.0.0": version "1.0.0" @@ -3116,6 +3116,11 @@ dottie@^2.0.0: resolved "https://npm.i12e.cha0s.io/dottie/-/dottie-2.0.2.tgz#cc91c0726ce3a054ebf11c55fbc92a7f266dd154" integrity sha512-fmrwR04lsniq/uSr8yikThDTrM7epXHBAAjH9TbeH3rEA8tdCO7mRzB9hdmdGyJCxF8KERo9CITcm3kGuoyMhg== +double-ended-queue@^2.1.0-0: + version "2.1.0-0" + resolved "https://npm.i12e.cha0s.io/double-ended-queue/-/double-ended-queue-2.1.0-0.tgz#103d3527fd31528f40188130c841efdd78264e5c" + integrity sha1-ED01J/0xUo9AGIEwyEHv3XgmTlw= + duplexify@^3.4.2, duplexify@^3.6.0: version "3.7.1" resolved "https://npm.i12e.cha0s.io/duplexify/-/duplexify-3.7.1.tgz#2a4df5317f6ccfd91f86d6fd25d8d8a103b88309" @@ -5875,6 +5880,11 @@ normalize-url@1.9.1: query-string "^4.1.0" sort-keys "^1.0.0" +notepack.io@~2.1.2: + version "2.1.3" + resolved "https://npm.i12e.cha0s.io/notepack.io/-/notepack.io-2.1.3.tgz#cc904045c751b1a27b2dcfd838d81d0bf3ced923" + integrity sha512-AgSt+cP5XMooho1Ppn8NB3FFaVWefV+qZoZncYTUSch2GAEwlYLcIIbT5YVkMlFeNHnfwOvc4HDlbvrB5BRxXA== + notepack.io@~2.2.0: version "2.2.0" resolved "https://npm.i12e.cha0s.io/notepack.io/-/notepack.io-2.2.0.tgz#d7ea71d1cb90094f88c6f3c8d84277c2d0cd101c" @@ -6916,7 +6926,7 @@ redent@^1.0.0: indent-string "^2.1.0" strip-indent "^1.0.1" -redis-commands@^1.5.0: +redis-commands@^1.2.0, redis-commands@^1.5.0: version "1.6.0" resolved "https://npm.i12e.cha0s.io/redis-commands/-/redis-commands-1.6.0.tgz#36d4ca42ae9ed29815cdb30ad9f97982eba1ce23" integrity sha512-2jnZ0IkjZxvguITjFTrGiLyzQZcTvaw8DAaCXxZq/dsHXz7KfMQ3OUJy7Tz9vnRtZRVz6VRCPDvruvU8Ts44wQ== @@ -6926,6 +6936,11 @@ redis-errors@^1.0.0, redis-errors@^1.2.0: resolved "https://npm.i12e.cha0s.io/redis-errors/-/redis-errors-1.2.0.tgz#eb62d2adb15e4eaf4610c04afe1529384250abad" integrity sha1-62LSrbFeTq9GEMBK/hUpOEJQq60= +redis-parser@^2.6.0: + version "2.6.0" + resolved "https://npm.i12e.cha0s.io/redis-parser/-/redis-parser-2.6.0.tgz#52ed09dacac108f1a631c07e9b69941e7a19504b" + integrity sha1-Uu0J2srBCPGmMcB+m2mUHnoZUEs= + redis-parser@^3.0.0: version "3.0.0" resolved "https://npm.i12e.cha0s.io/redis-parser/-/redis-parser-3.0.0.tgz#b66d828cdcafe6b4b8a428a7def4c6bcac31c8b4" @@ -6943,6 +6958,15 @@ redis@^3.0.0, redis@^3.0.2: redis-errors "^1.2.0" redis-parser "^3.0.0" +redis@~2.8.0: + version "2.8.0" + resolved "https://npm.i12e.cha0s.io/redis/-/redis-2.8.0.tgz#202288e3f58c49f6079d97af7a10e1303ae14b02" + integrity sha512-M1OkonEQwtRmZv4tEWF2VgpG0JWJ8Fv1PhlgT5+B+uNq2cA3Rt1Yt/ryoR+vQNOQcIEgdCdfH0jr3bDpihAw1A== + dependencies: + double-ended-queue "^2.1.0-0" + redis-commands "^1.2.0" + redis-parser "^2.6.0" + redux-thunk@^2.3.0: version "2.3.0" resolved "https://npm.i12e.cha0s.io/redux-thunk/-/redux-thunk-2.3.0.tgz#51c2c19a185ed5187aaa9a2d08b666d0d6467622" @@ -7580,6 +7604,17 @@ socket.io-parser@~3.4.0: debug "~4.1.0" isarray "2.0.1" +socket.io-redis@5.3.0: + version "5.3.0" + resolved "https://npm.i12e.cha0s.io/socket.io-redis/-/socket.io-redis-5.3.0.tgz#d01716d2813c25b76918cd704e1cf540b2f3985a" + integrity sha512-w2EqyGdw3oXzd1MY4sVIg2rYVooDI5sSwel8DOt38sTgaJuuXQSC847x38FvLSn2Rt6MAcdLhiNw/FqjzeC4RQ== + dependencies: + debug "~4.1.0" + notepack.io "~2.1.2" + redis "~2.8.0" + socket.io-adapter "~1.1.0" + uid2 "0.0.3" + socket.io-redis@^6.0.1: version "6.0.1" resolved "https://npm.i12e.cha0s.io/socket.io-redis/-/socket.io-redis-6.0.1.tgz#0d6c82bd6e0dcbb0d70dcbc57f0c3269e6e53594"