Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[pull] master from GladysAssistant:master #196

Merged
merged 4 commits into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions .github/workflows/docker-dev-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/docker-master-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions .github/workflows/docker-pr-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
4 changes: 2 additions & 2 deletions .github/workflows/docker-release-build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
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": "gladys",
"version": "4.45.0",
"version": "4.45.1",
"description": "A privacy-first, open-source home assistant",
"main": "index.js",
"engines": {
Expand Down
1 change: 1 addition & 0 deletions server/api/controllers/user.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@ module.exports = function UserController(gladys) {
* @apiGroup User
*/
async function updateMySelf(req, res, next) {
delete req.body.role;
const newUser = await gladys.user.update(req.user.id, req.body);
res.json(newUser);
}
Expand Down
5 changes: 5 additions & 0 deletions server/config/scheduler-jobs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
28 changes: 21 additions & 7 deletions server/lib/calendar/calendar.destroyEvents.js
Original file line number Diff line number Diff line change
@@ -1,17 +1,31 @@
const { Op } = require('sequelize');
const db = require('../../models');

/**
* @description Delete events from a calendar.
* @param {string} calendarId - Calendar id to empty.
* @param {object} options - Options of the query.
* @example
* gladys.calendar.destroyEvents('0dc03aef-4a23-9c4e-88e3-5437971269e5');
* gladys.calendar.destroyEvents('0dc03aef-4a23-9c4e-88e3-5437971269e5', {url: '/calendar/event.ics'});
*/
async function destroyEvents(calendarId) {
await db.CalendarEvent.destroy({
where: {
calendar_id: calendarId,
},
});
async function destroyEvents(calendarId, options = {}) {
const where = {
calendar_id: calendarId,
};

if (options.url) {
where.url = {
[Op.eq]: options.url,
};
}

if (options.from) {
where.start = {
[Op.gte]: new Date(options.from),
};
}

await db.CalendarEvent.destroy({ where });
}

module.exports = {
Expand Down
6 changes: 5 additions & 1 deletion server/lib/message/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,27 @@ 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;
this.brain = brain;
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;

Expand Down
29 changes: 29 additions & 0 deletions server/lib/message/message.purge.js
Original file line number Diff line number Diff line change
@@ -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,
};
14 changes: 14 additions & 0 deletions server/services/caldav/lib/calendar/calendar.syncUserCalendars.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
const Promise = require('bluebird');
const get = require('get-value');
const logger = require('../../../../utils/logger');
const { ServiceNotConfiguredError, NotFoundError } = require('../../../../utils/coreErrors');

Expand Down Expand Up @@ -108,6 +109,19 @@ async function syncUserCalendars(userId) {
throw new NotFoundError('CALDAV_FAILED_REQUEST_EVENTS');
}

await Promise.map(
jsonEvents,
async (jsonEvent) => {
if (get(jsonEvent, 'rrule.options.until') && jsonEvent.href) {
await this.gladys.calendar.destroyEvents(calendarToUpdate.id, {
url: jsonEvent.href,
from: get(jsonEvent, 'rrule.options.until'),
});
}
},
{ concurrency: 5 },
);

const formatedEvents = this.formatEvents(jsonEvents, calendarToUpdate);

let insertedOrUpdatedEvent = 0;
Expand Down
33 changes: 33 additions & 0 deletions server/services/caldav/package-lock.json

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

1 change: 1 addition & 0 deletions server/services/caldav/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"bluebird": "^3.7.0",
"dav-request": "^1.8.0",
"dayjs": "^1.11.10",
"get-value": "^3.0.1",
"ical": "^0.8.0",
"xmldom": "^0.6.0"
}
Expand Down
24 changes: 23 additions & 1 deletion server/test/lib/calendar/calendar.event.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,15 +52,37 @@ describe('calendar.destroy', () => {
});

describe('calendar.destroyEvents', () => {
const calendar = new Calendar();
it("should destroy all calendar's event", async () => {
const calendar = new Calendar();
await calendar.destroyEvents('07ec2599-3221-4d6c-ac56-41443973201b');
const allCalendarEvents = await calendar.getEvents(
'0cd30aef-9c4e-4a23-88e3-3547971296e5',
'07ec2599-3221-4d6c-ac56-41443973201b',
);
assert.deepEqual(allCalendarEvents, []);
});

it("should destroy calendar's events by url", async () => {
const calendar = new Calendar();
await calendar.destroyEvents('07ec2599-3221-4d6c-ac56-41443973201b', {
url: '/remote.php/dav/calendars/tony/personal/eee42d70-24f2-4c18-949d-822f3f72594c.ics',
});
const allCalendarEvents = await calendar.getEvents(
'0cd30aef-9c4e-4a23-88e3-3547971296e5',
'07ec2599-3221-4d6c-ac56-41443973201b',
);
expect(allCalendarEvents.length).eq(1);
});

it("should destroy calendar's events starting after date", async () => {
const calendar = new Calendar();
await calendar.destroyEvents('07ec2599-3221-4d6c-ac56-41443973201b', { from: '2019-03-10 07:49:07.556 +00:00' });
const allCalendarEvents = await calendar.getEvents(
'0cd30aef-9c4e-4a23-88e3-3547971296e5',
'07ec2599-3221-4d6c-ac56-41443973201b',
);
expect(allCalendarEvents.length).eq(1);
});
});

describe('calendar.getEvents', () => {
Expand Down
41 changes: 41 additions & 0 deletions server/test/lib/message/message.purge.test.js
Original file line number Diff line number Diff line change
@@ -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',
},
]);
});
});
Loading
Loading