2022-08-17 14:50:30 +03:00
|
|
|
import Helper from '@ember/component/helper';
|
|
|
|
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);
|
|
|
|
|
|
|
|
const assetRoot = this.ghostPaths.assetRoot.replace(/\/$/, '');
|
|
|
|
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,
|
2022-08-17 14:50:30 +03:00
|
|
|
original: ev
|
|
|
|
};
|
|
|
|
}
|
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) {
|
2022-09-01 18:58:07 +03:00
|
|
|
const defaultImage = `${assetRoot}/img/user-image.png`;
|
|
|
|
|
2022-08-31 16:55:14 +03:00
|
|
|
if (!ev.actor.id) {
|
2022-09-01 18:58:07 +03:00
|
|
|
return defaultImage;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!ev.actor.image) {
|
|
|
|
return defaultImage;
|
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':
|
|
|
|
if (!ev.resource.id) {
|
|
|
|
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':
|
|
|
|
if (!ev.resource.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'settings.integration',
|
|
|
|
models: [ev.resource.id]
|
|
|
|
};
|
2022-08-24 14:24:13 +03:00
|
|
|
case 'offer':
|
|
|
|
if (!ev.resource.id) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
route: 'offer',
|
|
|
|
models: [ev.resource.id]
|
|
|
|
};
|
2022-08-23 18:37:09 +03:00
|
|
|
case 'tag':
|
|
|
|
if (!ev.resource.slug) {
|
|
|
|
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':
|
|
|
|
if (!ev.resource.slug) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-25 14:06:53 +03:00
|
|
|
return `${resourceType} ${ev.event}`;
|
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;
|
|
|
|
}
|