diff --git a/.github/workflows/docker-dev-build.yml b/.github/workflows/docker-dev-build.yml index d82b8f6185..60f0342999 100644 --- a/.github/workflows/docker-dev-build.yml +++ b/.github/workflows/docker-dev-build.yml @@ -34,7 +34,7 @@ jobs: run: | npm run build - name: ↗️ Upload build artifact - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: static path: front/build @@ -64,7 +64,7 @@ jobs: with: version: latest - name: ↙️ Download build artifact - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: static path: static diff --git a/.github/workflows/docker-master-test.yml b/.github/workflows/docker-master-test.yml index e3e174f4ee..07087243c8 100644 --- a/.github/workflows/docker-master-test.yml +++ b/.github/workflows/docker-master-test.yml @@ -62,7 +62,7 @@ jobs: env: RELATIVE_CI_KEY: ${{ secrets.RELATIVE_CI_KEY }} - name: ↗️ Upload build artifact - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: static path: front/build diff --git a/.github/workflows/docker-pr-build.yml b/.github/workflows/docker-pr-build.yml index 99021bdbb8..cccb00d5d1 100644 --- a/.github/workflows/docker-pr-build.yml +++ b/.github/workflows/docker-pr-build.yml @@ -144,11 +144,11 @@ jobs: run: | npm run build-with-stats - name: ↗️ Upload webpack stats artifact - uses: relative-ci/agent-upload-artifact-action@v1 + uses: relative-ci/agent-upload-artifact-action@v2 with: webpackStatsFile: ./front/stats.json - name: ↗️ Upload build artifact - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: static path: front/build @@ -168,7 +168,7 @@ jobs: with: version: latest - name: ↙️ Download build artifact - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: static path: static diff --git a/.github/workflows/docker-release-build.yml b/.github/workflows/docker-release-build.yml index 6d71cbbf04..852495f396 100644 --- a/.github/workflows/docker-release-build.yml +++ b/.github/workflows/docker-release-build.yml @@ -114,7 +114,7 @@ jobs: run: | npm run build - name: ↗️ Upload build artifact - uses: actions/upload-artifact@v3 + uses: actions/upload-artifact@v4 with: name: static path: front/build @@ -148,7 +148,7 @@ jobs: with: version: v0.9.1 - name: ↙️ Download build artifact - uses: actions/download-artifact@v3 + uses: actions/download-artifact@v4 with: name: static path: static diff --git a/server/config/scheduler-jobs.js b/server/config/scheduler-jobs.js index 0823d4683e..04f6c37a55 100644 --- a/server/config/scheduler-jobs.js +++ b/server/config/scheduler-jobs.js @@ -16,6 +16,11 @@ const jobs = [ rule: '0 0 22 * * *', // every day at 22:00 event: EVENTS.JOB.PURGE_OLD_JOBS, }, + { + name: 'daily-purge-of-old-messages', + rule: '0 0 23 * * *', // every day at 23:00 + event: EVENTS.MESSAGE.PURGE_OLD_MESSAGES, + }, { name: 'check-device-batteries', rule: '0 0 9 * * 6', // At 09:00 AM, only on Saturday diff --git a/server/lib/message/index.js b/server/lib/message/index.js index d836f3dfb4..ef351bdf60 100644 --- a/server/lib/message/index.js +++ b/server/lib/message/index.js @@ -2,9 +2,11 @@ const { EVENTS } = require('../../utils/constants'); const { create } = require('./message.create'); const { get } = require('./message.get'); const { reply } = require('./message.reply'); +const { purge } = require('./message.purge'); const { handleEvent } = require('./message.handleEvent'); const { replyByIntent } = require('./message.replyByIntent'); const { sendToUser } = require('./message.sendToUser'); +const { eventFunctionWrapper } = require('../../utils/functionsWrapper'); const MessageHandler = function MessageHandler(event, brain, service, state, variable) { this.event = event; @@ -12,13 +14,15 @@ const MessageHandler = function MessageHandler(event, brain, service, state, var this.service = service; this.state = state; this.variable = variable; - event.on(EVENTS.MESSAGE.NEW, (message) => this.handleEvent(message)); + this.event.on(EVENTS.MESSAGE.NEW, (message) => this.handleEvent(message)); + this.event.on(EVENTS.MESSAGE.PURGE_OLD_MESSAGES, eventFunctionWrapper(this.purge.bind(this))); }; MessageHandler.prototype.create = create; MessageHandler.prototype.get = get; MessageHandler.prototype.handleEvent = handleEvent; MessageHandler.prototype.reply = reply; +MessageHandler.prototype.purge = purge; MessageHandler.prototype.replyByIntent = replyByIntent; MessageHandler.prototype.sendToUser = sendToUser; diff --git a/server/lib/message/message.purge.js b/server/lib/message/message.purge.js new file mode 100644 index 0000000000..396e755fb3 --- /dev/null +++ b/server/lib/message/message.purge.js @@ -0,0 +1,29 @@ +const { Op } = require('sequelize'); +const db = require('../../models'); +const logger = require('../../utils/logger'); + +const DAYS_TO_KEEP = 15; + +/** + * @public + * @description Purge. + * @returns {Promise} Resolve. + * @example + * gladys.message.purge(); + */ +async function purge() { + const deleteBeforeDate = new Date(new Date().getTime() - DAYS_TO_KEEP * 24 * 60 * 60 * 1000); + logger.info(`Deleting all messages created before = ${deleteBeforeDate}`); + await db.Message.destroy({ + where: { + created_at: { + [Op.lte]: deleteBeforeDate, + }, + }, + }); + logger.info('Messages purged!'); +} + +module.exports = { + purge, +}; diff --git a/server/test/lib/message/message.purge.test.js b/server/test/lib/message/message.purge.test.js new file mode 100644 index 0000000000..3514110145 --- /dev/null +++ b/server/test/lib/message/message.purge.test.js @@ -0,0 +1,41 @@ +const { expect } = require('chai'); +const EventEmitter = require('events'); +const db = require('../../../models'); +const MessageHandler = require('../../../lib/message'); + +describe('message.purge', () => { + const eventEmitter = new EventEmitter(); + const messageHandler = new MessageHandler(eventEmitter); + it('should purge messages', async () => { + await db.Message.truncate(); + await db.Message.create({ + id: '2e3dccb0-fe8e-4e26-96c7-13041a1a3852', + sender_id: '0cd30aef-9c4e-4a23-88e3-3547971296e5', + receiver_id: null, + file: null, + text: 'This is an old message', + is_read: true, + created_at: new Date('2019-02-12T07:49:07.556Z'), + }); + await db.Message.create({ + id: 'b9a395df-d1d6-4905-a29f-2f110e028ea5', + sender_id: '0cd30aef-9c4e-4a23-88e3-3547971296e5', + receiver_id: null, + file: null, + text: 'this is a recent message', + is_read: true, + created_at: new Date(), + }); + await messageHandler.purge(); + const rows = await db.Message.findAll({ + attributes: ['id', 'text'], + raw: true, + }); + expect(rows).to.deep.equal([ + { + id: 'b9a395df-d1d6-4905-a29f-2f110e028ea5', + text: 'this is a recent message', + }, + ]); + }); +}); diff --git a/server/utils/constants.js b/server/utils/constants.js index 95eae8e42a..9e8a7aa702 100644 --- a/server/utils/constants.js +++ b/server/utils/constants.js @@ -241,6 +241,7 @@ const EVENTS = { MESSAGE: { NEW: 'message.new', NEW_FOR_OPEN_AI: 'message.new-for-open-ai', + PURGE_OLD_MESSAGES: 'message.purge-old-messages', }, SYSTEM: { DOWNLOAD_UPGRADE: 'system.download-upgrade',