2020-12-07 08:01:52 +03:00
|
|
|
import { h } from '/js/web_modules/preact.js';
|
2020-10-05 04:43:31 +03:00
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-10-04 05:01:46 +03:00
|
|
|
const html = htm.bind(h);
|
2020-08-13 11:28:25 +03:00
|
|
|
|
2020-10-17 03:36:11 +03:00
|
|
|
import ChatMessageView from './chat-message-view.js';
|
|
|
|
|
2020-10-04 05:08:05 +03:00
|
|
|
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
2021-11-03 05:27:41 +03:00
|
|
|
import { checkIsModerator } from '../../utils/chat.js';
|
|
|
|
|
|
|
|
function SystemMessage(props) {
|
|
|
|
const { contents } = props;
|
|
|
|
return html`
|
2021-11-03 05:28:19 +03:00
|
|
|
<div
|
|
|
|
class="message message-name-change flex items-center justify-start p-3"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="message-content flex flex-row items-center justify-center text-sm w-full"
|
|
|
|
>
|
|
|
|
<div
|
2021-11-14 04:44:23 +03:00
|
|
|
class="text-gray-400 w-full text-center opacity-90 overflow-hidden break-words"
|
2021-11-03 05:28:19 +03:00
|
|
|
>
|
2021-11-03 05:27:41 +03:00
|
|
|
${contents}
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
2020-10-03 16:29:29 +03:00
|
|
|
|
2022-01-13 00:53:10 +03:00
|
|
|
function SingleFederatedUser(props) {
|
|
|
|
const { message } = props;
|
|
|
|
const { type, body, title, image, link } = message;
|
|
|
|
|
|
|
|
let icon = null;
|
|
|
|
switch (type) {
|
|
|
|
case SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_FOLLOW:
|
|
|
|
icon = '/img/follow.svg';
|
|
|
|
break;
|
|
|
|
case SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_LIKE:
|
|
|
|
icon = '/img/like.svg';
|
|
|
|
break;
|
|
|
|
case SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_REPOST:
|
|
|
|
icon = '/img/repost.svg';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return html`
|
|
|
|
<a
|
|
|
|
href=${link}
|
|
|
|
target="_blank"
|
|
|
|
class="hover:no-underline"
|
|
|
|
title="Visit profile"
|
|
|
|
>
|
|
|
|
<div
|
|
|
|
class="federated-action m-2 mt-3 bg-white flex items-center px-2 rounded-xl shadow border"
|
|
|
|
>
|
|
|
|
<div class="relative" style="top: -6px">
|
|
|
|
<img
|
|
|
|
src="${image || '/img/logo.svg'}"
|
|
|
|
style="max-width: unset"
|
|
|
|
class="rounded-full border border-slate-500 w-16"
|
|
|
|
/>
|
|
|
|
<span
|
2022-02-13 21:41:01 +03:00
|
|
|
style=${{ backgroundImage: `url(${icon})` }}
|
2022-01-13 00:53:10 +03:00
|
|
|
class="absolute h-6 w-6 rounded-full border-2 border-white action-icon"
|
|
|
|
></span>
|
|
|
|
</div>
|
2022-02-13 21:41:01 +03:00
|
|
|
<div class="px-4 py-2 min-w-0">
|
|
|
|
<div class="text-gray-500 text-sm hover:no-underline truncate">
|
2022-01-13 00:53:10 +03:00
|
|
|
${title}
|
2022-02-13 21:41:01 +03:00
|
|
|
</div>
|
2022-01-13 00:53:10 +03:00
|
|
|
<p
|
|
|
|
class=" text-gray-700 w-full text-base leading-6"
|
|
|
|
dangerouslySetInnerHTML=${{ __html: body }}
|
|
|
|
></p>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
</a>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
export default function Message(props) {
|
|
|
|
const { message } = props;
|
2021-11-03 05:27:41 +03:00
|
|
|
const { type, oldName, user, body } = message;
|
2021-07-20 05:23:06 +03:00
|
|
|
if (
|
|
|
|
type === SOCKET_MESSAGE_TYPES.CHAT ||
|
|
|
|
type === SOCKET_MESSAGE_TYPES.SYSTEM
|
|
|
|
) {
|
2020-12-07 08:01:52 +03:00
|
|
|
return html`<${ChatMessageView} ...${props} />`;
|
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.NAME_CHANGE) {
|
2021-12-12 03:44:39 +03:00
|
|
|
// User changed their name
|
2021-07-20 05:23:06 +03:00
|
|
|
const { displayName } = user;
|
2021-11-03 05:27:41 +03:00
|
|
|
const contents = html`
|
2021-12-12 03:44:39 +03:00
|
|
|
<div>
|
2021-11-03 05:27:41 +03:00
|
|
|
<span class="font-bold">${oldName}</span> is now known as ${' '}
|
|
|
|
<span class="font-bold">${displayName}</span>.
|
2021-12-12 03:44:39 +03:00
|
|
|
</div>
|
2021-07-20 05:23:06 +03:00
|
|
|
`;
|
2021-11-03 05:28:19 +03:00
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-02-19 10:05:52 +03:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.USER_JOINED) {
|
2022-03-07 07:09:55 +03:00
|
|
|
const { displayName, isBot } = user;
|
2022-01-13 00:53:10 +03:00
|
|
|
const isAuthorModerator = checkIsModerator(message);
|
|
|
|
const messageAuthorFlair = isAuthorModerator
|
|
|
|
? html`<img
|
|
|
|
title="Moderator"
|
|
|
|
class="inline-block mr-1 w-3 h-3"
|
|
|
|
src="/img/moderator-nobackground.svg"
|
|
|
|
/>`
|
|
|
|
: null;
|
2022-03-07 07:09:55 +03:00
|
|
|
|
2022-01-13 00:53:10 +03:00
|
|
|
const contents = html`<div>
|
|
|
|
<span class="font-bold">${messageAuthorFlair}${displayName}</span>
|
|
|
|
${' '}joined the chat.
|
|
|
|
</div>`;
|
2021-11-03 05:28:19 +03:00
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-02-19 10:05:52 +03:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.CHAT_ACTION) {
|
2021-11-03 05:27:41 +03:00
|
|
|
const contents = html`<span
|
|
|
|
dangerouslySetInnerHTML=${{ __html: body }}
|
|
|
|
></span>`;
|
2021-11-03 05:28:19 +03:00
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-07-20 05:22:29 +03:00
|
|
|
} else if (type === SOCKET_MESSAGE_TYPES.CONNECTED_USER_INFO) {
|
2021-11-03 05:27:41 +03:00
|
|
|
// moderator message
|
|
|
|
const isModerator = checkIsModerator(message);
|
|
|
|
if (isModerator) {
|
2021-11-14 04:03:17 +03:00
|
|
|
const contents = html`<div class="rounded-lg bg-gray-700 p-3">
|
|
|
|
<img src="/img/moderator.svg" class="moderator-flag" />You are now a
|
|
|
|
moderator.
|
|
|
|
</div>`;
|
2021-11-03 05:28:19 +03:00
|
|
|
return html`<${SystemMessage} contents=${contents} />`;
|
2021-11-03 05:27:41 +03:00
|
|
|
}
|
2022-01-13 00:53:10 +03:00
|
|
|
} else if (
|
|
|
|
type === SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_FOLLOW ||
|
|
|
|
SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_LIKE ||
|
|
|
|
SOCKET_MESSAGE_TYPES.FEDIVERSE_ENGAGEMENT_REPOST
|
|
|
|
) {
|
|
|
|
return html` <${SingleFederatedUser} message=${message} /> `;
|
2020-12-07 08:01:52 +03:00
|
|
|
} else {
|
2021-07-20 05:23:06 +03:00
|
|
|
console.log('Unknown message type:', type);
|
2020-10-14 02:45:52 +03:00
|
|
|
}
|
|
|
|
}
|