2022-08-17 14:50:30 +03:00
|
|
|
import Helper from '@ember/component/helper';
|
2023-07-24 17:01:02 +03:00
|
|
|
import config from 'ghost-admin/config/environment';
|
2022-08-17 14:50:30 +03:00
|
|
|
import {inject as service} from '@ember/service';
|
2022-08-16 13:40:33 +03:00
|
|
|
|
2022-09-06 13:48:45 +03:00
|
|
|
export default class ParseHistoryEvent extends Helper {
|
2022-08-31 16:55:14 +03:00
|
|
|
@service ghostPaths;
|
2022-08-17 14:50:30 +03:00
|
|
|
|
|
|
|
compute([ev]) {
|
|
|
|
const action = getAction(ev);
|
|
|
|
const actionIcon = getActionIcon(ev);
|
2022-08-23 15:11:24 +03:00
|
|
|
const contextResource = getContextResource(ev);
|
2022-08-23 18:37:09 +03:00
|
|
|
const linkTarget = getLinkTarget(ev);
|
2022-08-17 14:50:30 +03:00
|
|
|
|
2022-08-31 16:55:14 +03:00
|
|
|
const actor = getActor(ev);
|
|
|
|
const actorLinkTarget = getActorLinkTarget(ev);
|
|
|
|
|
2023-07-24 17:01:02 +03:00
|
|
|
const assetRoot = (config.cdnUrl ? '' : this.ghostPaths.assetRoot.replace(/\/$/, ''));
|
2022-08-31 16:55:14 +03:00
|
|
|
const actorIcon = getActorIcon(ev, assetRoot);
|
|
|
|
|
2022-08-17 14:50:30 +03:00
|
|
|
return {
|
2022-08-23 15:11:24 +03:00
|
|
|
contextResource,
|
2022-08-23 18:37:09 +03:00
|
|
|
linkTarget,
|
2022-08-17 14:50:30 +03:00
|
|
|
actionIcon,
|
|
|
|
action,
|
2022-08-31 16:55:14 +03:00
|
|
|
actor,
|
|
|
|
actorIcon,
|
|
|
|
actorLinkTarget,
|
2023-05-05 10:45:36 +03:00
|
|
|
original: ev,
|
|
|
|
isBulkAction: !!ev.context.count
|
2022-08-17 14:50:30 +03:00
|
|
|
};
|
|
|
|
}
|
2022-08-16 13:40:33 +03:00
|
|
|
}
|
|
|
|
|
2022-08-31 16:55:14 +03:00
|
|
|
function getActor(ev) {
|
|
|
|
if (!ev.actor.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return ev.actor;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActorIcon(ev, assetRoot) {
|
2023-07-24 17:01:02 +03:00
|
|
|
const defaultImage = `/img/user-image.png`;
|
|
|
|
let defaultImageUrl = `${assetRoot}${defaultImage}`;
|
2022-09-01 18:58:07 +03:00
|
|
|
|
2023-07-24 17:01:02 +03:00
|
|
|
if (!ev.actor.id || !ev.actor.image) {
|
|
|
|
return defaultImageUrl;
|
2022-08-31 16:55:14 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
return ev.actor.image;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getActorLinkTarget(ev) {
|
|
|
|
const actor = getActor(ev);
|
|
|
|
if (!actor) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (ev.actor_type) {
|
|
|
|
case 'integration':
|
|
|
|
if (!actor.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'settings.integration',
|
|
|
|
models: [actor.id]
|
|
|
|
};
|
|
|
|
case 'user':
|
|
|
|
if (!actor.slug) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'settings.staff.user',
|
|
|
|
models: [actor.slug]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-23 18:37:09 +03:00
|
|
|
function getLinkTarget(ev) {
|
|
|
|
let resourceType = ev.resource_type;
|
|
|
|
|
|
|
|
if (ev.event !== 'deleted') {
|
|
|
|
switch (ev.resource_type) {
|
|
|
|
case 'page':
|
|
|
|
case 'post':
|
2023-05-05 10:45:36 +03:00
|
|
|
if (!ev.resource || !ev.resource.id) {
|
2022-08-23 18:37:09 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (resourceType === 'post') {
|
|
|
|
if (ev.context?.type) {
|
|
|
|
resourceType = ev.context?.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'editor.edit',
|
|
|
|
models: [resourceType, ev.resource.id]
|
|
|
|
};
|
2022-08-23 19:06:12 +03:00
|
|
|
case 'integration':
|
2023-05-05 10:45:36 +03:00
|
|
|
if (!ev.resource || !ev.resource.id) {
|
2022-08-23 19:06:12 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'settings.integration',
|
|
|
|
models: [ev.resource.id]
|
|
|
|
};
|
2022-08-24 14:24:13 +03:00
|
|
|
case 'offer':
|
2023-05-05 10:45:36 +03:00
|
|
|
if (!ev.resource || !ev.resource.id) {
|
2022-08-24 14:24:13 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'offer',
|
|
|
|
models: [ev.resource.id]
|
|
|
|
};
|
2022-08-23 18:37:09 +03:00
|
|
|
case 'tag':
|
2023-05-05 10:45:36 +03:00
|
|
|
if (!ev.resource || !ev.resource.slug) {
|
2022-08-23 18:37:09 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'tag',
|
|
|
|
models: [ev.resource.slug]
|
|
|
|
};
|
2022-09-05 17:20:26 +03:00
|
|
|
case 'product':
|
|
|
|
return {
|
|
|
|
route: 'settings.membership',
|
|
|
|
models: null
|
|
|
|
};
|
2022-08-23 18:37:09 +03:00
|
|
|
case 'user':
|
2023-05-05 10:45:36 +03:00
|
|
|
if (!ev.resource || !ev.resource.slug) {
|
2022-08-23 18:37:09 +03:00
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'settings.staff.user',
|
|
|
|
models: [ev.resource.slug]
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
2022-08-16 13:40:33 +03:00
|
|
|
function getActionIcon(ev) {
|
|
|
|
switch (ev.event) {
|
|
|
|
case 'added':
|
2022-09-02 18:56:02 +03:00
|
|
|
return 'plus-large';
|
2022-08-16 13:40:33 +03:00
|
|
|
case 'edited':
|
2022-08-22 20:14:17 +03:00
|
|
|
return 'pen';
|
2022-08-16 13:40:33 +03:00
|
|
|
case 'deleted':
|
2022-08-22 20:14:17 +03:00
|
|
|
return 'trash';
|
2022-08-16 13:40:33 +03:00
|
|
|
}
|
2022-08-17 15:54:54 +03:00
|
|
|
|
|
|
|
return 'info';
|
2022-08-16 13:40:33 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getAction(ev) {
|
2022-08-22 16:30:13 +03:00
|
|
|
let resourceType = ev.resource_type;
|
|
|
|
|
|
|
|
if (resourceType === 'api_key') {
|
|
|
|
resourceType = 'API key';
|
2022-08-22 22:12:32 +03:00
|
|
|
} else if (resourceType === 'setting') {
|
|
|
|
resourceType = 'settings';
|
2022-09-05 17:09:10 +03:00
|
|
|
} else if (resourceType === 'product') {
|
|
|
|
resourceType = 'tier';
|
2022-09-06 13:48:45 +03:00
|
|
|
}
|
2022-08-22 16:30:13 +03:00
|
|
|
|
2022-08-23 18:37:09 +03:00
|
|
|
// Because a `page` and `post` both use the same model, we store the
|
|
|
|
// actual type in the context, so let's check if that exists
|
|
|
|
if (resourceType === 'post') {
|
|
|
|
if (ev.context?.type) {
|
|
|
|
resourceType = ev.context?.type;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-05-05 10:45:36 +03:00
|
|
|
let action = ev.event;
|
|
|
|
|
|
|
|
if (ev.event === 'edited') {
|
|
|
|
if (ev.context.action_name) {
|
|
|
|
action = ev.context.action_name;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ev.context.count && ev.context.count > 1) {
|
|
|
|
return `${ev.context.count} ${resourceType}s ${action}`;
|
|
|
|
}
|
|
|
|
|
|
|
|
return `${resourceType} ${action}`;
|
2022-08-16 13:40:33 +03:00
|
|
|
}
|
2022-08-23 15:11:24 +03:00
|
|
|
|
|
|
|
function getContextResource(ev) {
|
|
|
|
if (ev.resource_type === 'setting') {
|
|
|
|
if (ev.context?.group && ev.context?.key) {
|
|
|
|
return {
|
|
|
|
first: ev.context.group,
|
|
|
|
second: ev.context.key
|
|
|
|
};
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return null;
|
|
|
|
}
|