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

Add Meal Planning Calendar Events #39

Merged
merged 3 commits into from
Dec 13, 2023
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
35 changes: 32 additions & 3 deletions lib/index.js
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
const EventEmitter = require('events');
const crypto = require('crypto');
const fs = require('fs');
const os = require("os");
const os = require('os');
const path = require('path');
const {Buffer} = require('buffer');
const got = require('got');
Expand All @@ -15,6 +15,8 @@ const Item = require('./item');
const uuid = require('./uuid');
const Recipe = require('./recipe');
const RecipeCollection = require('./recipe-collection');
const MealPlanningCalendarEvent = require('./meal-planning-calendar-event');
const MealPlanningCalendarEventLabel = require('./meal-planning-calendar-label');

const CREDENTIALS_KEY_CLIENT_ID = 'clientId';
const CREDENTIALS_KEY_ACCESS_TOKEN = 'accessToken';
Expand Down Expand Up @@ -55,7 +57,7 @@ class AnyList extends EventEmitter {
const url = response.request.options.url.href;
console.error(`Endpoint ${url} returned uncaught status code ${response.statusCode}`);
return error;
}
},
],
},
});
Expand Down Expand Up @@ -100,7 +102,7 @@ class AnyList extends EventEmitter {
const url = response.request.options.url.href;
console.error(`Endpoint ${url} returned uncaught status code ${response.statusCode}`);
return error;
}
},
],
},
});
Expand Down Expand Up @@ -270,6 +272,7 @@ class AnyList extends EventEmitter {
}
});

// eslint-disable-next-line arrow-parens
this.ws.addEventListener('error', async (error) => {
console.error(`Disconnected from websocket: ${error.message}`);
await this._refreshTokens();
Expand Down Expand Up @@ -323,6 +326,32 @@ class AnyList extends EventEmitter {
return this.lists.find(l => l.name === name);
}

/**
* Load all meal planning calendar events from account into memory.
* @return {Promise<MealPlanningCalendarEvent[]>} events
*/
async getMealPlanningCalendarEvents() {
const result = await this.client.post('data/user-data/get');

const decoded = this.protobuf.PBUserDataResponse.decode(result.body);

this.mealPlanningCalendarEvents = decoded.mealPlanningCalendarResponse.events.map(event => new MealPlanningCalendarEvent(event, this));

// Map and assign labels
this.mealPlanningCalendarEventLabels = decoded.mealPlanningCalendarResponse.labels.map(label => new MealPlanningCalendarEventLabel(label));
for (const event of this.mealPlanningCalendarEvents) {
event.label = this.mealPlanningCalendarEventLabels.find(label => label.identifier === event.labelId);
}

// Map and assign recipies
this.recipes = decoded.recipeDataResponse.recipes.map(recipe => new Recipe(recipe, this));
for (const event of this.mealPlanningCalendarEvents) {
event.recipe = this.recipes.find(recipe => recipe.identifier === event.recipeId);
}

return this.mealPlanningCalendarEvents;
}

/**
* Get the recently added items for a list
* @param {string} listId list ID
Expand Down
49 changes: 49 additions & 0 deletions lib/meal-planning-calendar-event.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/// <reference path="./meal-planning-calendar-label.js" />
/// <reference path="./recipe.js" />

/**
* Meal Planning Calendar Event class.
* @class
*
* @param {object} event event
* @param {object[]} labels labels
* @param {object} context context
*
* @property {string} identifier
* @property {string} calendarId
* @property {Date} date
* @property {string=} details
* @property {string=} labelId
* @property {MealPlanningCalendarEventLabel=} label
* @property {number=} logicalTimestamp
* @property {number=} orderAddedSortIndex
* @property {string=} recipeId
* @property {Recipe=} recipe
* @property {number=} recipeScaleFactor
* @property {string=} title
*/
class MealPlanningCalendarEvent {
/**
* @hideconstructor
*/
constructor(event, {client, protobuf, uid}) {
this.identifier = event.identifier;
this.calendarId = event.calendarId;
this.date = new Date(event.date);
this.details = event.details;
this.labelId = event.labelId;
this.logicalTimestamp = event.logicalTimestamp;
this.orderAddedSortIndex = event.orderAddedSortIndex;
this.recipeId = event.recipeId;
this.recipeScaleFactor = event.recipeScaleFactor;
this.title = event.title;
this.recipe = null;
this.label = null;

this.client = client;
this.protobuf = protobuf;
this.uid = uid;
jchadwick marked this conversation as resolved.
Show resolved Hide resolved
}
}

module.exports = MealPlanningCalendarEvent;
29 changes: 29 additions & 0 deletions lib/meal-planning-calendar-label.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
/**
* Meal Planning Calendar Event Label class.
* @class
*
* @param {object} label label
*
* @property {string} identifier
* @property {string} calendarId
* @property {string} hexColor
* @property {number} logicalTimestamp
* @property {string} name
* @property {number} sortIndex
*
*/
class MealPlanningCalendarEventLabel {
/**
* @hideconstructor
*/
constructor(label) {
this.identifier = label.identifier;
this.calendarId = label.calendarId;
this.hexColor = label.hexColor;
this.logicalTimestamp = label.logicalTimestamp;
this.name = label.name;
this.sortIndex = label.sortIndex;
}
}

module.exports = MealPlanningCalendarEventLabel;