2020-10-17 03:50:00 +03:00
|
|
|
import { h, Component } from '/js/web_modules/preact.js';
|
|
|
|
import htm from '/js/web_modules/htm.js';
|
2020-11-18 04:29:03 +03:00
|
|
|
import Mark from '/js/web_modules/markjs/dist/mark.es6.min.js';
|
2020-10-17 03:50:00 +03:00
|
|
|
const html = htm.bind(h);
|
|
|
|
|
|
|
|
import {
|
2021-07-20 05:22:29 +03:00
|
|
|
messageBubbleColorForHue,
|
|
|
|
textColorForHue,
|
2020-10-17 03:50:00 +03:00
|
|
|
} from '../../utils/user-colors.js';
|
2021-12-12 03:03:24 +03:00
|
|
|
import { convertToText, checkIsModerator } from '../../utils/chat.js';
|
2020-10-17 03:50:00 +03:00
|
|
|
import { SOCKET_MESSAGE_TYPES } from '../../utils/websocket.js';
|
2021-06-20 20:49:16 +03:00
|
|
|
import { getDiffInDaysFromNow } from '../../utils/helpers.js';
|
2021-11-03 05:27:41 +03:00
|
|
|
import ModeratorActions from './moderator-actions.js';
|
2020-10-17 03:50:00 +03:00
|
|
|
|
|
|
|
export default class ChatMessageView extends Component {
|
2020-12-07 08:01:52 +03:00
|
|
|
constructor(props) {
|
|
|
|
super(props);
|
|
|
|
this.state = {
|
|
|
|
formattedMessage: '',
|
2021-11-03 05:27:41 +03:00
|
|
|
moderatorMenuOpen: false,
|
2020-12-07 08:01:52 +03:00
|
|
|
};
|
|
|
|
}
|
|
|
|
|
|
|
|
shouldComponentUpdate(nextProps, nextState) {
|
|
|
|
const { formattedMessage } = this.state;
|
|
|
|
const { formattedMessage: nextFormattedMessage } = nextState;
|
|
|
|
|
2021-12-19 10:32:33 +03:00
|
|
|
return (
|
|
|
|
formattedMessage !== nextFormattedMessage ||
|
|
|
|
(!this.props.isModerator && nextProps.isModerator)
|
|
|
|
);
|
2020-12-07 08:01:52 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
async componentDidMount() {
|
2020-10-17 03:50:00 +03:00
|
|
|
const { message, username } = this.props;
|
2021-07-20 05:22:29 +03:00
|
|
|
const { body } = message;
|
2021-07-20 05:23:06 +03:00
|
|
|
|
2020-12-07 08:01:52 +03:00
|
|
|
if (message && username) {
|
|
|
|
const formattedMessage = await formatMessageText(body, username);
|
|
|
|
this.setState({
|
|
|
|
formattedMessage,
|
|
|
|
});
|
2020-11-18 04:29:03 +03:00
|
|
|
}
|
2020-12-07 08:01:52 +03:00
|
|
|
}
|
2020-11-18 04:29:03 +03:00
|
|
|
render() {
|
2021-11-03 05:27:41 +03:00
|
|
|
const { message, isModerator, accessToken } = this.props;
|
2021-07-20 05:22:29 +03:00
|
|
|
const { user, timestamp } = message;
|
2022-01-13 07:23:01 +03:00
|
|
|
|
|
|
|
if (!user) {
|
|
|
|
return null;
|
|
|
|
}
|
2022-04-22 00:55:26 +03:00
|
|
|
|
|
|
|
const { displayName, displayColor, createdAt, isBot, authenticated } = user;
|
2021-12-12 03:03:24 +03:00
|
|
|
const isAuthorModerator = checkIsModerator(message);
|
2021-11-03 05:27:41 +03:00
|
|
|
|
2021-11-03 05:28:19 +03:00
|
|
|
const isMessageModeratable =
|
|
|
|
isModerator && message.type === SOCKET_MESSAGE_TYPES.CHAT;
|
2020-11-18 04:29:03 +03:00
|
|
|
|
|
|
|
const { formattedMessage } = this.state;
|
|
|
|
if (!formattedMessage) {
|
2020-12-07 08:01:52 +03:00
|
|
|
return null;
|
2020-11-18 04:29:03 +03:00
|
|
|
}
|
2021-07-20 05:22:29 +03:00
|
|
|
const formattedTimestamp = `Sent at ${formatTimestamp(timestamp)}`;
|
2021-07-26 08:41:48 +03:00
|
|
|
const userMetadata = createdAt
|
|
|
|
? `${displayName} first joined ${formatTimestamp(createdAt)}`
|
|
|
|
: null;
|
2020-10-17 03:50:00 +03:00
|
|
|
|
|
|
|
const isSystemMessage = message.type === SOCKET_MESSAGE_TYPES.SYSTEM;
|
2020-10-18 05:21:43 +03:00
|
|
|
|
|
|
|
const authorTextColor = isSystemMessage
|
|
|
|
? { color: '#fff' }
|
2021-07-20 05:22:29 +03:00
|
|
|
: { color: textColorForHue(displayColor) };
|
2020-10-17 03:50:00 +03:00
|
|
|
const backgroundStyle = isSystemMessage
|
|
|
|
? { backgroundColor: '#667eea' }
|
2021-07-20 05:22:29 +03:00
|
|
|
: { backgroundColor: messageBubbleColorForHue(displayColor) };
|
2020-10-18 05:21:43 +03:00
|
|
|
const messageClassString = isSystemMessage
|
2021-11-03 05:27:41 +03:00
|
|
|
? 'message flex flex-row items-start p-4 m-2 rounded-lg shadow-l border-solid border-indigo-700 border-2 border-opacity-60 text-l'
|
2021-11-03 05:28:19 +03:00
|
|
|
: `message relative flex flex-row items-start p-3 m-3 rounded-lg shadow-s text-sm ${
|
|
|
|
isMessageModeratable ? 'moderatable' : ''
|
|
|
|
}`;
|
2020-10-17 03:50:00 +03:00
|
|
|
|
2022-04-22 00:55:26 +03:00
|
|
|
const isModeratorFlair = isAuthorModerator
|
2021-12-10 01:06:48 +03:00
|
|
|
? html`<img
|
|
|
|
class="flair"
|
|
|
|
title="Moderator"
|
|
|
|
src="/img/moderator-nobackground.svg"
|
|
|
|
/>`
|
|
|
|
: null;
|
|
|
|
|
2022-03-07 07:09:55 +03:00
|
|
|
const isBotFlair = isBot
|
|
|
|
? html`<img
|
|
|
|
title="Bot"
|
|
|
|
class="inline-block mr-1 w-4 h-4 relative"
|
|
|
|
style=${{ bottom: '1px' }}
|
|
|
|
src="/img/bot.svg"
|
|
|
|
/>`
|
|
|
|
: null;
|
|
|
|
|
2022-04-22 00:55:26 +03:00
|
|
|
const authorAuthenticatedFlair = authenticated
|
|
|
|
? html`<img
|
|
|
|
class="flair"
|
|
|
|
title="Authenticated"
|
|
|
|
src="/img/authenticated.svg"
|
|
|
|
/>`
|
|
|
|
: null;
|
|
|
|
|
2020-10-17 03:50:00 +03:00
|
|
|
return html`
|
|
|
|
<div
|
|
|
|
style=${backgroundStyle}
|
2020-10-18 05:21:43 +03:00
|
|
|
class=${messageClassString}
|
2020-10-17 03:50:00 +03:00
|
|
|
title=${formattedTimestamp}
|
|
|
|
>
|
|
|
|
<div class="message-content break-words w-full">
|
2021-07-20 05:23:06 +03:00
|
|
|
<div
|
|
|
|
style=${authorTextColor}
|
2021-12-10 01:06:48 +03:00
|
|
|
class="message-author font-bold"
|
2021-07-20 05:23:06 +03:00
|
|
|
title=${userMetadata}
|
|
|
|
>
|
2022-04-22 00:55:26 +03:00
|
|
|
${isBotFlair} ${authorAuthenticatedFlair} ${isModeratorFlair}
|
|
|
|
${displayName}
|
2020-10-17 03:50:00 +03:00
|
|
|
</div>
|
2021-11-03 05:28:19 +03:00
|
|
|
${isMessageModeratable &&
|
|
|
|
html`<${ModeratorActions}
|
|
|
|
message=${message}
|
|
|
|
accessToken=${accessToken}
|
|
|
|
/>`}
|
2020-10-17 03:50:00 +03:00
|
|
|
<div
|
|
|
|
class="message-text text-gray-300 font-normal overflow-y-hidden pt-2"
|
|
|
|
dangerouslySetInnerHTML=${{ __html: formattedMessage }}
|
|
|
|
></div>
|
|
|
|
</div>
|
|
|
|
</div>
|
|
|
|
`;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:23:06 +03:00
|
|
|
export async function formatMessageText(message, username) {
|
2020-11-18 04:29:03 +03:00
|
|
|
let formattedText = getMessageWithEmbeds(message);
|
|
|
|
formattedText = convertToMarkup(formattedText);
|
|
|
|
return await highlightUsername(formattedText, username);
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function highlightUsername(message, username) {
|
2020-11-18 04:29:03 +03:00
|
|
|
// https://github.com/julmot/mark.js/issues/115
|
|
|
|
const node = document.createElement('span');
|
|
|
|
node.innerHTML = message;
|
2021-03-14 22:08:32 +03:00
|
|
|
return new Promise((res) => {
|
2020-11-18 04:29:03 +03:00
|
|
|
new Mark(node).mark(username, {
|
|
|
|
element: 'span',
|
|
|
|
className: 'highlighted px-1 rounded font-bold bg-orange-500',
|
|
|
|
separateWordSearch: false,
|
|
|
|
accuracy: {
|
|
|
|
value: 'exactly',
|
2021-03-14 22:08:32 +03:00
|
|
|
limiters: [',', '.', "'", '?', '@'],
|
2020-11-18 04:29:03 +03:00
|
|
|
},
|
|
|
|
done() {
|
|
|
|
res(node.innerHTML);
|
2021-03-14 22:08:32 +03:00
|
|
|
},
|
2020-11-18 04:29:03 +03:00
|
|
|
});
|
|
|
|
});
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function getMessageWithEmbeds(message) {
|
|
|
|
var embedText = '';
|
|
|
|
// Make a temporary element so we can actually parse the html and pull anchor tags from it.
|
|
|
|
// This is a better approach than regex.
|
|
|
|
var container = document.createElement('p');
|
|
|
|
container.innerHTML = message;
|
|
|
|
|
|
|
|
var anchors = container.getElementsByTagName('a');
|
|
|
|
for (var i = 0; i < anchors.length; i++) {
|
|
|
|
const url = anchors[i].href;
|
2021-06-07 20:57:13 +03:00
|
|
|
if (url.indexOf('instagram.com/p/') > -1) {
|
2020-10-17 03:50:00 +03:00
|
|
|
embedText += getInstagramEmbedFromURL(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// If this message only consists of a single embeddable link
|
|
|
|
// then only return the embed and strip the link url from the text.
|
|
|
|
if (
|
|
|
|
embedText !== '' &&
|
|
|
|
anchors.length == 1 &&
|
|
|
|
isMessageJustAnchor(message, anchors[0])
|
|
|
|
) {
|
|
|
|
return embedText;
|
|
|
|
}
|
|
|
|
return message + embedText;
|
|
|
|
}
|
|
|
|
|
|
|
|
function getInstagramEmbedFromURL(url) {
|
|
|
|
const urlObject = new URL(url.replace(/\/$/, ''));
|
|
|
|
urlObject.pathname += '/embed';
|
|
|
|
return `<iframe class="chat-embed instagram-embed" src="${urlObject.href}" frameborder="0" allowfullscreen></iframe>`;
|
|
|
|
}
|
|
|
|
|
|
|
|
function isMessageJustAnchor(message, anchor) {
|
|
|
|
return stripTags(message) === stripTags(anchor.innerHTML);
|
|
|
|
}
|
|
|
|
|
|
|
|
function formatTimestamp(sentAt) {
|
|
|
|
sentAt = new Date(sentAt);
|
|
|
|
if (isNaN(sentAt)) {
|
|
|
|
return '';
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
let diffInDays = getDiffInDaysFromNow(sentAt);
|
2020-10-17 03:50:00 +03:00
|
|
|
if (diffInDays >= 1) {
|
|
|
|
return (
|
2021-08-06 07:40:32 +03:00
|
|
|
`at ${sentAt.toLocaleDateString('en-US', {
|
2020-10-17 03:50:00 +03:00
|
|
|
dateStyle: 'medium',
|
|
|
|
})} at ` + sentAt.toLocaleTimeString()
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2021-07-20 05:22:29 +03:00
|
|
|
return `${sentAt.toLocaleTimeString()}`;
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
You would call this when receiving a plain text
|
|
|
|
value back from an API, and before inserting the
|
|
|
|
text into the `contenteditable` area on a page.
|
|
|
|
*/
|
|
|
|
function convertToMarkup(str = '') {
|
2021-08-19 03:59:42 +03:00
|
|
|
return convertToText(str).replace(/\n/g, '<p></p>');
|
2020-10-17 03:50:00 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
function stripTags(str) {
|
|
|
|
return str.replace(/<\/?[^>]+(>|$)/g, '');
|
|
|
|
}
|