2022-08-18 18:38:42 +03:00
|
|
|
|
/**
|
2022-08-19 23:39:18 +03:00
|
|
|
|
* @typedef {object} AttributionResource
|
|
|
|
|
* @prop {string|null} id
|
2022-08-22 18:16:18 +03:00
|
|
|
|
* @prop {string|null} url (absolute URL)
|
2022-08-19 23:39:18 +03:00
|
|
|
|
* @prop {'page'|'post'|'author'|'tag'|'url'} type
|
|
|
|
|
* @prop {string|null} title
|
2022-08-18 18:38:42 +03:00
|
|
|
|
*/
|
|
|
|
|
|
2022-08-19 23:39:18 +03:00
|
|
|
|
class Attribution {
|
|
|
|
|
#urlTranslator;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @param {object} data
|
|
|
|
|
* @param {string|null} [data.id]
|
2022-08-22 18:16:18 +03:00
|
|
|
|
* @param {string|null} [data.url] Relative to subdirectory
|
2022-08-19 23:39:18 +03:00
|
|
|
|
* @param {'page'|'post'|'author'|'tag'|'url'} [data.type]
|
|
|
|
|
*/
|
|
|
|
|
constructor({id, url, type}, {urlTranslator}) {
|
|
|
|
|
this.id = id;
|
|
|
|
|
this.url = url;
|
|
|
|
|
this.type = type;
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* @private
|
|
|
|
|
*/
|
|
|
|
|
this.#urlTranslator = urlTranslator;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
2022-08-24 17:11:25 +03:00
|
|
|
|
* Converts the instance to a parsed instance with more information about the resource included.
|
2022-08-19 23:39:18 +03:00
|
|
|
|
* It does:
|
2022-08-24 17:11:25 +03:00
|
|
|
|
* - Uses the passed model and adds a title to the attribution
|
|
|
|
|
* - If the resource exists and has a new url, it updates the url if possible
|
2022-08-19 23:39:18 +03:00
|
|
|
|
* - Returns an absolute URL instead of a relative one
|
2022-08-24 17:11:25 +03:00
|
|
|
|
* @param {Object|null} [model] The Post/User/Tag model of the resource associated with this attribution
|
|
|
|
|
* @returns {AttributionResource}
|
2022-08-19 23:39:18 +03:00
|
|
|
|
*/
|
2022-08-24 17:11:25 +03:00
|
|
|
|
getResource(model) {
|
|
|
|
|
if (!this.id || this.type === 'url' || !this.type || !model) {
|
2022-08-19 23:39:18 +03:00
|
|
|
|
return {
|
|
|
|
|
id: null,
|
|
|
|
|
type: 'url',
|
2022-08-22 18:16:18 +03:00
|
|
|
|
url: this.#urlTranslator.relativeToAbsolute(this.url),
|
2022-08-25 15:51:38 +03:00
|
|
|
|
title: this.#urlTranslator.getUrlTitle(this.url)
|
2022-08-19 23:39:18 +03:00
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-24 17:11:25 +03:00
|
|
|
|
const updatedUrl = this.#urlTranslator.getUrlByResourceId(this.id, {absolute: true});
|
2022-08-24 17:11:25 +03:00
|
|
|
|
|
2022-08-24 18:01:47 +03:00
|
|
|
|
return {
|
2022-08-24 17:11:25 +03:00
|
|
|
|
id: model.id,
|
|
|
|
|
type: this.type,
|
|
|
|
|
url: updatedUrl,
|
2022-08-25 15:51:38 +03:00
|
|
|
|
title: model.get('title') ?? model.get('name') ?? this.#urlTranslator.getUrlTitle(this.url)
|
2022-08-24 18:01:47 +03:00
|
|
|
|
};
|
2022-08-24 17:11:25 +03:00
|
|
|
|
}
|
2022-08-24 17:11:25 +03:00
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Same as getResource, but fetches the model by ID instead of passing it as a parameter
|
|
|
|
|
*/
|
|
|
|
|
async fetchResource() {
|
|
|
|
|
if (!this.id || this.type === 'url' || !this.type) {
|
|
|
|
|
// No fetch required
|
|
|
|
|
return this.getResource();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Fetch model
|
|
|
|
|
const model = await this.#urlTranslator.getResourceById(this.id, this.type, {absolute: true});
|
|
|
|
|
return this.getResource(model);
|
|
|
|
|
}
|
2022-08-19 23:39:18 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
|
/**
|
|
|
|
|
* Convert a UrlHistory to an attribution object
|
|
|
|
|
*/
|
|
|
|
|
class AttributionBuilder {
|
|
|
|
|
/**
|
|
|
|
|
*/
|
|
|
|
|
constructor({urlTranslator}) {
|
|
|
|
|
this.urlTranslator = urlTranslator;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-19 23:39:18 +03:00
|
|
|
|
/**
|
|
|
|
|
* Creates an Attribution object with the dependencies injected
|
|
|
|
|
*/
|
|
|
|
|
build({id, url, type}) {
|
|
|
|
|
return new Attribution({
|
|
|
|
|
id,
|
|
|
|
|
url,
|
|
|
|
|
type
|
|
|
|
|
}, {urlTranslator: this.urlTranslator});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
|
/**
|
|
|
|
|
* Last Post Algorithm™️
|
|
|
|
|
* @param {UrlHistory} history
|
|
|
|
|
* @returns {Attribution}
|
|
|
|
|
*/
|
|
|
|
|
getAttribution(history) {
|
|
|
|
|
if (history.length === 0) {
|
2022-08-19 23:39:18 +03:00
|
|
|
|
return this.build({
|
2022-08-18 18:38:42 +03:00
|
|
|
|
id: null,
|
|
|
|
|
url: null,
|
|
|
|
|
type: null
|
2022-08-19 23:39:18 +03:00
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
|
}
|
|
|
|
|
|
2022-08-22 18:16:18 +03:00
|
|
|
|
// Convert history to subdirectory relative (instead of root relative)
|
|
|
|
|
// Note: this is ordered from recent to oldest!
|
|
|
|
|
const subdirectoryRelativeHistory = [];
|
|
|
|
|
for (const item of history) {
|
|
|
|
|
subdirectoryRelativeHistory.push({
|
|
|
|
|
...item,
|
|
|
|
|
path: this.urlTranslator.stripSubdirectoryFromPath(item.path)
|
|
|
|
|
});
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-18 18:38:42 +03:00
|
|
|
|
// TODO: if something is wrong with the attribution script, and it isn't loading
|
|
|
|
|
// we might get out of date URLs
|
|
|
|
|
// so we need to check the time of each item and ignore items that are older than 24u here!
|
|
|
|
|
|
|
|
|
|
// Start at the end. Return the first post we find
|
2022-08-22 18:16:18 +03:00
|
|
|
|
for (const item of subdirectoryRelativeHistory) {
|
2022-08-18 18:38:42 +03:00
|
|
|
|
const typeId = this.urlTranslator.getTypeAndId(item.path);
|
|
|
|
|
|
|
|
|
|
if (typeId && typeId.type === 'post') {
|
2022-08-19 23:39:18 +03:00
|
|
|
|
return this.build({
|
2022-08-18 18:38:42 +03:00
|
|
|
|
url: item.path,
|
|
|
|
|
...typeId
|
2022-08-19 23:39:18 +03:00
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No post found?
|
|
|
|
|
// Try page or tag or author
|
2022-08-22 18:16:18 +03:00
|
|
|
|
for (const item of subdirectoryRelativeHistory) {
|
2022-08-18 18:38:42 +03:00
|
|
|
|
const typeId = this.urlTranslator.getTypeAndId(item.path);
|
|
|
|
|
|
|
|
|
|
if (typeId) {
|
2022-08-19 23:39:18 +03:00
|
|
|
|
return this.build({
|
2022-08-18 18:38:42 +03:00
|
|
|
|
url: item.path,
|
|
|
|
|
...typeId
|
2022-08-19 23:39:18 +03:00
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Default to last URL
|
|
|
|
|
// In the future we might decide to exclude certain URLs, that can happen here
|
2022-08-19 23:39:18 +03:00
|
|
|
|
return this.build({
|
2022-08-18 18:38:42 +03:00
|
|
|
|
id: null,
|
2022-08-22 18:16:18 +03:00
|
|
|
|
url: subdirectoryRelativeHistory[0].path,
|
2022-08-18 18:38:42 +03:00
|
|
|
|
type: 'url'
|
2022-08-19 23:39:18 +03:00
|
|
|
|
});
|
2022-08-18 18:38:42 +03:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
module.exports = AttributionBuilder;
|