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

Removed BB dep from url service #14939

Merged
merged 2 commits into from
Aug 30, 2022
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
107 changes: 48 additions & 59 deletions ghost/core/core/server/services/url/Resources.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
const _ = require('lodash');
const Promise = require('bluebird');
const debug = require('@tryghost/debug')('services:url:resources');
const Resource = require('./Resource');
const config = require('../../../shared/config');
Expand Down Expand Up @@ -65,7 +64,7 @@ class Resources {
/**
* @description Helper function to initialize data fetching.
*/
fetchResources() {
async fetchResources() {
const ops = [];
debug('fetchResources');

Expand All @@ -77,7 +76,7 @@ class Resources {
ops.push(this._fetch(resourceConfig));
});

return Promise.all(ops);
await Promise.all(ops);
}

/**
Expand All @@ -95,28 +94,28 @@ class Resources {
* 3 event listeners connected to "_onResourceUpdated" handler and it's 'tag.edited', 'tag.attached', 'tag.detached' events
* 1 event listener connected to "_onResourceRemoved" handler and it's 'tag.deleted' event
*/
initEvenListeners() {
initEventListeners() {
_.each(this.resourcesConfig, (resourceConfig) => {
this.data[resourceConfig.type] = [];

this._listenOn(resourceConfig.events.add, (model) => {
return this._onResourceAdded.bind(this)(resourceConfig.type, model);
this._listenOn(resourceConfig.events.add, async (model) => {
await this._onResourceAdded(resourceConfig.type, model);
});

if (_.isArray(resourceConfig.events.update)) {
resourceConfig.events.update.forEach((event) => {
this._listenOn(event, (model) => {
return this._onResourceUpdated.bind(this)(resourceConfig.type, model);
this._listenOn(event, async (model) => {
await this._onResourceUpdated(resourceConfig.type, model);
});
});
} else {
this._listenOn(resourceConfig.events.update, (model) => {
return this._onResourceUpdated.bind(this)(resourceConfig.type, model);
this._listenOn(resourceConfig.events.update, async (model) => {
await this._onResourceUpdated(resourceConfig.type, model);
});
}

this._listenOn(resourceConfig.events.remove, (model) => {
return this._onResourceRemoved.bind(this)(resourceConfig.type, model);
this._onResourceRemoved(resourceConfig.type, model);
});
});
}
Expand Down Expand Up @@ -240,10 +239,9 @@ class Resources {
*
* @param {String} type (post,user...)
* @param {Bookshelf-Model} model
* @returns {Promise}
* @private
*/
_onResourceAdded(type, model) {
async _onResourceAdded(type, model) {
debug('_onResourceAdded', type);

const resourceConfig = _.find(this.resourcesConfig, {type: type});
Expand All @@ -267,27 +265,23 @@ class Resources {
}
});
} else {
return Promise.resolve()
.then(() => {
return this._fetchSingle(resourceConfig, model.id);
})
.then(([dbResource]) => {
if (dbResource) {
const resource = new Resource(type, dbResource);
const [dbResource] = await this._fetchSingle(resourceConfig, model.id);

debug('_onResourceAdded', type);
this.data[type].push(resource);
if (dbResource) {
const resource = new Resource(type, dbResource);

this.queue.start({
event: 'added',
action: 'added:' + model.id,
eventData: {
id: model.id,
type: type
}
});
debug('_onResourceAdded', type);
this.data[type].push(resource);

this.queue.start({
event: 'added',
action: 'added:' + model.id,
eventData: {
id: model.id,
type: type
}
});
}
}
}

Expand All @@ -309,10 +303,9 @@ class Resources {
*
* @param {String} type (post,user...)
* @param {Bookshelf-Model} model
* @returns {Promise}
* @private
*/
_onResourceUpdated(type, model) {
async _onResourceUpdated(type, model) {
debug('_onResourceUpdated', type);

const resourceConfig = _.find(this.resourcesConfig, {type: type});
Expand Down Expand Up @@ -346,34 +339,30 @@ class Resources {
return true;
});
} else {
return Promise.resolve()
.then(() => {
return this._fetchSingle(resourceConfig, model.id);
})
.then(([dbResource]) => {
const resource = this.data[type].find(r => (r.data.id === model.id));

// CASE: cached resource exists, API conditions matched with the data in the db
if (resource && dbResource) {
resource.update(dbResource);

// CASE: pretend it was added
if (!resource.isReserved()) {
this.queue.start({
event: 'added',
action: 'added:' + dbResource.id,
eventData: {
id: dbResource.id,
type: type
}
});
const [dbResource] = await this._fetchSingle(resourceConfig, model.id);

const resource = this.data[type].find(r => (r.data.id === model.id));

// CASE: cached resource exists, API conditions matched with the data in the db
if (resource && dbResource) {
resource.update(dbResource);

// CASE: pretend it was added
if (!resource.isReserved()) {
this.queue.start({
event: 'added',
action: 'added:' + dbResource.id,
eventData: {
id: dbResource.id,
type: type
}
} else if (!resource && dbResource) {
this._onResourceAdded(type, model);
} else if (resource && !dbResource) {
this._onResourceRemoved(type, model);
}
});
});
}
} else if (!resource && dbResource) {
await this._onResourceAdded(type, model);
} else if (resource && !dbResource) {
await this._onResourceRemoved(type, model);
}
}
}

Expand Down
4 changes: 2 additions & 2 deletions ghost/core/core/server/services/url/UrlService.js
Original file line number Diff line number Diff line change
Expand Up @@ -322,12 +322,12 @@ class UrlService {
this.urls.urls = persistedUrls;
this.resources.data = persistedResources;
this.resources.initResourceConfig();
this.resources.initEvenListeners();
this.resources.initEventListeners();

this._onQueueEnded('init');
} else {
this.resources.initResourceConfig();
this.resources.initEvenListeners();
this.resources.initEventListeners();
await this.resources.fetchResources();
// CASE: all resources are fetched, start the queue
this.queue.start({
Expand Down