Skip to content

Commit

Permalink
DEV: support changes in user bookmarks in core (#146)
Browse files Browse the repository at this point in the history
This PR aim to support changes that'll be made by discourse/discourse#14257 That PR moves the method for loading bookmarks from the model to the route. There is a failed test in that PR that shows that it breaks the discourse-encrypt plugin.

The changes supported in backward compatible manner:

- we're merging this PR firstly
- then we can merge discourse/discourse#14257
- after that, we can remove code that modifies the bookmark model from Discourse Encrypt
  • Loading branch information
AndrewPrigorshnev committed Oct 1, 2021
1 parent 621fe04 commit 1fbec31
Showing 1 changed file with 59 additions and 36 deletions.
95 changes: 59 additions & 36 deletions assets/javascripts/discourse/initializers/fetch-encrypt-keys.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,43 @@ import { Promise } from "rsvp";

const CACHE_KEY = "discourse-encrypt-bookmark-cache";

function addEncryptedBookmarksFromCache(session, response, query, username) {
if (!query) {
saveBookmarksResponse(session, response);
return response;
}

let cachePromise = Promise.resolve();
if (!session.get(CACHE_KEY)) {
const url = `/u/${username}/bookmarks.json`;
cachePromise = ajax(url).then((resp) =>
saveBookmarksResponse(session, resp)
);
}

return cachePromise.then(() => {
const bookmarkIds = new Set();
response?.user_bookmark_list?.bookmarks?.forEach((bookmark) => {
bookmarkIds.add(bookmark.id);
});

const cache = session.get(CACHE_KEY);
cache.forEach((bookmark) => {
if (bookmark.title.toLowerCase().includes(query.toLowerCase())) {
if (!response?.user_bookmark_list?.bookmarks) {
response = { user_bookmark_list: { bookmarks: [] } };
}

if (!bookmarkIds.has(bookmark.id)) {
bookmarkIds.add(bookmark.id);
response.user_bookmark_list.bookmarks.push(bookmark);
}
}
});
return response;
});
}

function saveBookmarksResponse(session, response) {
if (!response?.user_bookmark_list?.bookmarks) {
return Promise.resolve();
Expand Down Expand Up @@ -127,47 +164,33 @@ export default {
},
});

// this can be removed in v2.8.0.beta7
api.modifyClass("model:bookmark", {
pluginId: "fetch-encrypt-keys",

loadItems(params) {
return this._super(...arguments).then((response) => {
if (!params.q) {
saveBookmarksResponse(session, response);
return response;
}

let cachePromise = Promise.resolve();
if (!session.get(CACHE_KEY)) {
const url = `/u/${currentUser.username}/bookmarks.json`;
cachePromise = ajax(url).then((resp) =>
saveBookmarksResponse(session, resp)
);
}

return cachePromise.then(() => {
const bookmarkIds = new Set();
response?.user_bookmark_list?.bookmarks?.forEach((bookmark) => {
bookmarkIds.add(bookmark.id);
});

const cache = session.get(CACHE_KEY);
cache.forEach((bookmark) => {
if (
bookmark.title.toLowerCase().includes(params.q.toLowerCase())
) {
if (!response?.user_bookmark_list?.bookmarks) {
response = { user_bookmark_list: { bookmarks: [] } };
}

if (!bookmarkIds.has(bookmark.id)) {
bookmarkIds.add(bookmark.id);
response.user_bookmark_list.bookmarks.push(bookmark);
}
}
});
return response;
});
return addEncryptedBookmarksFromCache(
session,
response,
params.q,
currentUser.username
);
});
},
});

api.modifyClass("route:user-activity-bookmarks", {
pluginId: "fetch-encrypt-keys",

_loadBookmarks(params) {
return this._super(...arguments).then((response) => {
return addEncryptedBookmarksFromCache(
session,
response,
params.q,
currentUser.username
);
});
},
});
Expand Down

0 comments on commit 1fbec31

Please sign in to comment.