Skip to content
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
21 changes: 21 additions & 0 deletions lib/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,24 @@ const CREDENTIALS_KEY_CLIENT_ID = 'clientId';
const CREDENTIALS_KEY_ACCESS_TOKEN = 'accessToken';
const CREDENTIALS_KEY_REFRESH_TOKEN = 'refreshToken';

function decodeJwtSubject(token) {
if (!token || typeof token !== 'string') {
return null;
}

const parts = token.split('.');
if (parts.length < 2) {
return null;
}

try {
const payload = JSON.parse(Buffer.from(parts[1], 'base64url').toString('utf8'));
return typeof payload.sub === 'string' ? payload.sub : null;
} catch {
return null;
}
}

/**
* AnyList class. There should be one
* instance per account.
Expand Down Expand Up @@ -134,6 +152,8 @@ class AnyList extends EventEmitter {
await this._fetchTokens();
}

this.uid = decodeJwtSubject(this.accessToken);

if (connectWebSocket) {
this._setupWebSocket();
}
Expand Down Expand Up @@ -164,6 +184,7 @@ class AnyList extends EventEmitter {

this.accessToken = result.access_token;
this.refreshToken = result.refresh_token;
this.uid = decodeJwtSubject(this.accessToken);
await this._storeCredentials();
} catch (error) {
if (error.response.statusCode !== 401) {
Expand Down
6 changes: 6 additions & 0 deletions lib/ingredient.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,12 @@ class Ingredient {
* @hideconstructor
*/
constructor(i, {client, protobuf, uid}) {
this._identifier = i.identifier;
this._rawIngredient = i.rawIngredient;
this._name = i.name;
this._quantity = i.quantity;
this._note = i.note;
this._isHeading = i.isHeading;
this._client = client;
this._protobuf = protobuf;
this._uid = uid;
Expand All @@ -28,19 +30,23 @@ class Ingredient {

toJSON() {
return {
identifier: this._identifier,
rawIngredient: this._rawIngredient,
name: this._name,
quantity: this._quantity,
note: this._note,
isHeading: this._isHeading,
};
}

_encode() {
return new this._protobuf.PBIngredient({
identifier: this._identifier,
name: this._name,
quantity: this._quantity,
rawIngredient: this._rawIngredient,
note: this._note,
isHeading: this._isHeading,
});
}

Expand Down
10 changes: 9 additions & 1 deletion lib/meal-planning-calendar-event.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,14 @@ const uuid = require('./uuid');
/// <reference path="./meal-planning-calendar-label.js" />
/// <reference path="./recipe.js" />

function formatLocalDate(date) {
return [
date.getFullYear(),
String(date.getMonth() + 1).padStart(2, '0'),
String(date.getDate()).padStart(2, '0'),
].join('-');
}

/**
* Meal Planning Calendar Event class.
* @class
Expand Down Expand Up @@ -70,7 +78,7 @@ class MealPlanningCalendarEvent {
identifier: this.identifier,
logicalTimestamp: this.logicalTimestamp,
calendarId: this._calendarId,
date: this.date.toISOString().slice(0, 10), // Only date, no time
date: formatLocalDate(this.date), // Only date, no time
title: this.title,
details: this.details,
recipeId: this.recipeId,
Expand Down
17 changes: 11 additions & 6 deletions lib/recipe.js
Original file line number Diff line number Diff line change
Expand Up @@ -60,10 +60,9 @@ class Recipe {
this.recipeDataId = recipeDataId;
}

_encode() {
return new this.protobuf.PBRecipe({
_encode(includeTimestamp = false) {
const payload = {
identifier: this.identifier,
timestamp: this.timestamp,
name: this.name,
note: this.note,
sourceName: this.sourceName,
Expand All @@ -81,7 +80,14 @@ class Recipe {
prepTime: this.prepTime,
servings: this.servings,
paprikaIdentifier: this.paprikaIdentifier,
});
};

if (includeTimestamp) {
payload.timestamp = this.timestamp;
payload.recipeDataId = this.recipeDataId;
}

return new this.protobuf.PBRecipe(payload);
}

/**
Expand All @@ -99,8 +105,7 @@ class Recipe {
userId: this.uid,
});
op.setRecipeDataId(this.recipeDataId);
op.setRecipe(this._encode());
op.setRecipeIds(this.recipeDataId);
op.setRecipe(this._encode(handlerId === 'remove-recipe'));
ops.setOperations(op);

const form = new FormData();
Expand Down