fix: distinction

This commit is contained in:
cha0s 2020-12-13 15:38:15 -06:00
parent 8b0f1b109a
commit a97cbc2667
7 changed files with 52 additions and 22 deletions

View File

@ -36,8 +36,11 @@ export default function Messages() {
<Message
key={message.uuid}
isShort={
(0 === message.owner || messageOwner === message.owner)
&& messageDistinction === message.distinction
0 === message.owner
|| (
messageOwner === message.owner
&& messageDistinction === message.distinction
)
}
message={message}
/>

View File

@ -35,6 +35,14 @@ export default function Message(props) {
});
const username = useSelector((state) => usernameSelector(state, owner));
const $messageTime = <Time timestamp={timestamp} />;
const $distinction = (
<span className="chat--messageDistinction">
{/* eslint-disable-next-line no-bitwise */}
{!!(distinction & ADMIN) && <span className="admin" />}
{/* eslint-disable-next-line no-bitwise */}
{!!(distinction & MOD) && <span className="mod" />}
</span>
);
return (
<div
className={classnames(
@ -47,22 +55,13 @@ export default function Message(props) {
},
)}
>
{
!isShort && (
<header>
<div className="chat--messageOwner">{-1 === owner ? '! System !' : username}</div>
{!!distinction && (
<span className="chat--messageDistinction">
{/* eslint-disable-next-line no-bitwise */}
{!!(distinction & ADMIN) && <span className="admin" />}
{/* eslint-disable-next-line no-bitwise */}
{!!(distinction & MOD) && <span className="mod" />}
</span>
)}
{$messageTime}
</header>
)
}
<header>
{!isShort && (
<div className="chat--messageOwner">{-1 === owner ? '! System !' : username}</div>
)}
{!!distinction && $distinction}
{!isShort && $messageTime}
</header>
<div className="chat--messageText">
<Markdown message={message} />
{isShort && $messageTime}

View File

@ -5,6 +5,7 @@ import {
addMessage,
joined,
left,
toggleMessageDistinction,
} from '@reddichat/chat/client';
import {
addFriendship,
@ -44,6 +45,10 @@ export default function Dispatcher() {
dispatch(addMessage(packet.data));
break;
}
case 'MessageDistinction': {
dispatch(toggleMessageDistinction(packet.data));
break;
}
case 'Join': {
dispatch(joined(packet.data));
break;

View File

@ -123,6 +123,9 @@ const slice = createSlice({
submitMessageDistinction: () => {},
toggleMessageDistinction: ({messages}, {payload: {distinction, uuid}}) => {
const message = messages[uuid];
if (!message) {
return;
}
/* eslint-disable no-bitwise */
message.distinction = message.distinction & distinction
? message.distinction & ~distinction

View File

@ -2,7 +2,11 @@ import {createClient, keys} from '@latus/redis';
import {parseChannel} from '@reddichat/core';
const messageChannel = async (latus, uuid) => {
const key = (await keys(createClient(latus), `*:messages:${uuid}`)).pop();
const results = (await keys(createClient(latus), `*:messages:${uuid}`));
if (0 === results.length) {
return undefined;
}
const key = results.pop();
return parseChannel(key.split(':')[0]);
};

View File

@ -10,7 +10,7 @@ export default (latus) => class MessageDistinctionServer extends MessageDistinct
static async respond({data: {distinction, uuid}}, socket) {
const {req} = socket;
const message = await replaceMessage(
await replaceMessage(
req,
uuid,
(msg) => ({
@ -23,9 +23,12 @@ export default (latus) => class MessageDistinctionServer extends MessageDistinct
}),
);
const channel = await messageChannel(latus, uuid);
if (!channel) {
return;
}
socket
.to(renderChannel(channel))
.send(['MessageDistinction', {uuid, distinction: message.distinction}]);
.send(['MessageDistinction', {uuid, distinction}]);
}
static async validate(packet, socket) {

View File

@ -6,10 +6,23 @@ const replaceMessage = async (req, uuid, fn) => {
const {pubClient} = req.adapter;
const get = promisify(pubClient.get.bind(pubClient));
const key = (await keys(pubClient, `*:messages:${uuid}`)).pop();
if (!key) {
return Promise.resolve();
}
const message = fn(JSON.parse(await get(key)));
return new Promise((resolve, reject) => pubClient
.multi()
.set(key, JSON.stringify(message))
.eval(
[
"local ttl = redis.call('ttl', ARGV[1])",
'if ttl > 0 then',
"return redis.call('SETEX', ARGV[1], ttl, ARGV[2])",
'end',
].join(' '),
0,
key,
JSON.stringify(message),
)
.exec((error) => (error ? reject(error) : resolve(message))));
};