import { TemplateResult, html, nothing } from "lit";
import {
AppWithSourceMetadataType,
EntityType,
EntityTypeString,
LinkType,
MetadataSourceType,
} from "../types/app.types";
import { NotificationType } from "../types/notifications.types";
import { WyLinkEventType } from "../types/notifications.events";
import { msg, str } from "@lit/localize";
import { type WeavyType } from "../client/weavy";
import { AppTypeGuids, AgentAppTypeGuids } from "../classes/weavy-type-component";
import { NamedEvent } from "../types/generic.types";
import { unsafeHTML } from "lit/directives/unsafe-html.js";
import { UnknownApp } from "../classes/weavy-app-component";
export function getEntityChain(entity?: EntityType) {
const chain = [entity];
while (entity) {
if (entity.parent) {
entity = entity.parent;
chain.push(entity);
} else {
break;
}
}
return chain;
}
export function isEntityMatch(entity: EntityType, type: EntityTypeString, item?: { id: number }) {
return entity.type === type && (!item || entity.id === item.id);
}
export function isEntityChainMatch(entity: EntityType, type: EntityTypeString, item?: { id: number }) {
const chain = getEntityChain(entity);
return chain.some((chainEntity) => chainEntity && isEntityMatch(chainEntity, type, item));
}
export function getEntityChainMatch(entity: EntityType, type: EntityTypeString, item?: { id: number }) {
const chain = getEntityChain(entity);
return chain.find((chainEntity) => chainEntity && isEntityMatch(chainEntity, type, item));
}
export function hasEntityChildType(
entity: EntityType,
type: EntityTypeString,
item: { id: number },
childType: EntityTypeString,
) {
// Looking at child to be able to check parent
const childMatch = getEntityChainMatch(entity, childType);
if (childMatch && childMatch.parent) {
return isEntityChainMatch(childMatch.parent, type, item);
} else {
return false;
}
}
export function getEntityChildType(
entity: EntityType,
type: EntityTypeString,
item: { id: number },
childType: EntityTypeString,
) {
// Looking at child to be able to check parent
const childMatch = getEntityChainMatch(entity, childType);
if (childMatch && childMatch.parent && isEntityChainMatch(childMatch.parent, type, item)) {
return childMatch;
} else {
return undefined;
}
}
export function dispatchRawLinkEvent(target: EventTarget, link: LinkType) {
const event: WyLinkEventType = new (CustomEvent as NamedEvent)("wy-link", {
bubbles: true,
composed: true,
cancelable: true,
detail: {
link,
app_type: (link?.app?.type && AppTypeGuids.get(link.app.type)) || UnknownApp,
},
});
return target.dispatchEvent(event);
}
export async function dispatchLinkEvent(
target: EventTarget,
weavy: WeavyType | undefined,
notification: NotificationType,
) {
let appMetadata: MetadataSourceType | undefined;
if (weavy && notification?.link?.app?.id) {
// HACK: fetch app metadata to get source info used for navigation context
const response = await weavy.fetch(`/api/apps/${notification.link.app.id}`);
if (response.ok) {
appMetadata = ((await response.json()) as AppWithSourceMetadataType).metadata;
}
}
const event: WyLinkEventType = new (CustomEvent as NamedEvent)("wy-link", {
bubbles: true,
composed: true,
cancelable: true,
detail: {
link: notification.link
? {
...notification?.link,
// HACK: pass along the agent uid so uikit can match the correct agent chat
agent:
notification.actor.is_agent &&
notification.link?.app?.type &&
AgentAppTypeGuids.has(notification.link.app.type)
? notification.actor.uid
: undefined,
}
: undefined,
metadata: notification.metadata,
app_type: (notification?.link?.app?.type && AppTypeGuids.get(notification.link.app.type)) || UnknownApp,
source_name: appMetadata?.source_name,
source_url: appMetadata?.source_url,
source_data: appMetadata?.source_data,
},
});
return target.dispatchEvent(event);
}
export function getNotificationText(notification: NotificationType): {
title: string;
titleHtml: TemplateResult | typeof nothing;
detail?: string;
} {
const args = notification.args;
switch (notification.template) {
case "**{0}** added *{1}* to **{2}**": {
const [name, fileName, appName] = args;
return {
title: msg(str`${name} added ${fileName} to ${appName}`),
titleHtml: msg(html`${name} added ${fileName} to ${appName}`),
};
}
case "**{0}** edited *{1}*": {
const [name, fileName] = args;
return {
title: msg(str`${name} edited ${fileName}`),
titleHtml: msg(html`${name} edited ${fileName}`),
};
}
case '**{0}** commented on *{1}*: "{2}"': {
const [name, fileName, comment] = args;
return {
title: msg(str`${name} commented on ${fileName}`),
titleHtml: msg(html`${name} commented on ${fileName}`),
detail: comment,
};
}
case '**{0}** commented on **{1}**: "{2}"': {
const [name, appName, comment] = args;
return {
title: msg(str`${name} commented on ${appName}`),
titleHtml: msg(html`${name} commented on ${appName}`),
detail: comment,
};
}
case '**{0}** replied to your post: "{1}"': {
const [name, comment] = args;
return {
title: msg(str`${name} replied to your post`),
titleHtml: msg(html`${name} replied to your post`),
detail: comment,
};
}
case '**{0}** replied to a post: "{1}"': {
const [name, comment] = args;
return {
title: msg(str`${name} replied to a post`),
titleHtml: msg(html`${name} replied to a post`),
detail: comment,
};
}
case '**{0}** mentioned you in a comment: "{1}"': {
const [name, comment] = args;
return {
title: msg(str`${name} mentioned you in a comment`),
titleHtml: msg(html`${name} mentioned you in a comment`),
detail: comment,
};
}
case '**{0}** posted in **{1}**: "{2}"': {
const [name, appName, post] = args;
return {
title: msg(str`${name} posted in ${appName}`),
titleHtml: msg(html`${name} posted in ${appName}`),
detail: post,
};
}
case '**{0}** mentioned you in a post: "{1}"': {
const [name, post] = args;
return {
title: msg(str`${name} mentioned you in a post`),
titleHtml: msg(html`${name} mentioned you in a post`),
detail: post,
};
}
case 'New message from **{0}**: "{1}"': {
const [name, message] = args;
return {
title: msg(str`New message from ${name}`),
titleHtml: msg(html`New message from ${name}`),
detail: message,
};
}
case '**{0}** sent a message in **{1}**: "{2}"': {
const [name, appName, message] = args;
return {
title: msg(str`${name} sent a message in ${appName}`),
titleHtml: msg(html`${name} sent a message in ${appName}`),
detail: message,
};
}
case '**{0}** mentioned you in a message: "{1}"': {
const [name, message] = args;
return {
title: msg(str`${name} mentioned you in a message`),
titleHtml: msg(html`${name} mentioned you in a message`),
detail: message,
};
}
case '**{0}** edited a post: "{1}"': {
const [name, post] = args;
return {
title: msg(str`${name} edited a post`),
titleHtml: msg(html`${name} edited a post`),
detail: post,
};
}
case "**{0}** liked *{1}*": {
const [name, fileName] = args;
return {
title: msg(str`${name} liked ${fileName}`),
titleHtml: msg(html`${name} liked ${fileName}`),
};
}
case "**{0}** reacted {2} to *{1}*": {
const [name, reaction, fileName] = args;
return {
title: msg(str`${name} reacted ${reaction} to ${fileName}`),
titleHtml: msg(html`${name} reacted ${reaction} to ${fileName}`),
};
}
case '**{0}** liked your comment: "{1}"': {
const [name, comment] = args;
return {
title: msg(str`${name} liked your comment`),
titleHtml: msg(html`${name} liked your comment`),
detail: comment,
};
}
case '**{0}** reacted {1} to your comment: "{2}"': {
const [name, reaction, comment] = args;
return {
title: msg(str`${name} reacted ${reaction} to your comment`),
titleHtml: msg(html`${name} reacted ${reaction} to your comment`),
detail: comment,
};
}
case '**{0}** liked your message: "{1}"': {
const [name, message] = args;
return {
title: msg(str`${name} liked your message`),
titleHtml: msg(html`${name} liked your message`),
detail: message,
};
}
case '**{0}** reacted {1} to your message: "{2}"': {
const [name, reaction, message] = args;
return {
title: msg(str`${name} reacted ${reaction} to your message`),
titleHtml: msg(html`${name} reacted ${reaction} to your message`),
detail: message,
};
}
case '**{0}** liked your post: "{1}"': {
const [name, post] = args;
return {
title: msg(str`${name} liked your post`),
titleHtml: msg(html`${name} liked your post`),
detail: post,
};
}
case '**{0}** reacted {1} to your post: "{2}"': {
const [name, reaction, post] = args;
return {
title: msg(str`${name} reacted ${reaction} to your post`),
titleHtml: msg(html`${name} reacted ${reaction} to your post`),
detail: post,
};
}
case "**{0}** voted on your poll": {
const [name] = args;
return {
title: msg(str`${name} voted on your poll`),
titleHtml: msg(html`${name} voted on your poll`),
};
}
case "**{0}** and **{1}** voted on your poll": {
const [name1, name2] = args;
return {
title: msg(str`${name1} and ${name2} voted on your poll`),
titleHtml: msg(html`${name1} and ${name2} voted on your poll`),
};
}
case "**{0}**, **{1}** and **{2}** voted on your poll": {
const [name1, name2, name3] = args;
return {
title: msg(str`${name1}, ${name2} and ${name3} voted on your poll`),
titleHtml: msg(
html`${name1}, ${name2} and ${name3} voted on your poll`,
),
};
}
case "**{0}** and {1} others voted on your poll": {
const [name, count] = args;
return {
title: msg(str`${name} and ${count} others voted on your poll`),
titleHtml: msg(html`${name} and ${count} others voted on your poll`),
};
}
default: {
return {
title: notification.plain,
titleHtml: html`${unsafeHTML(notification.html)}`,
};
// console.error(`Notification template not found! '${notification.template}'`);
// return { title: "", titleHtml: nothing };
}
}
}