Unverified Commit e1fdc068 authored by Renato Becker's avatar Renato Becker Committed by GitHub

Support thread messages as attachments (#220)

* Stat handling the new message payload form thread messages.

* Handle promise return.

* Increase widget height.

* Fix async methods.
parent a9e5309e
......@@ -44,7 +44,7 @@ const renderContent = ({ text, system, quoted, me, attachments, attachmentResolv
url={attachmentResolver(attachment.title_link)}
title={attachment.title}
/>) ||
(attachment.message_link && renderContent({
((attachment.message_link || attachment.tmid) && renderContent({
text: attachment.text,
quoted: true,
attachments: attachment.attachments,
......
......@@ -6,7 +6,8 @@ $message-text-font-size: 0.875rem;
.message-text {
font-size: $message-text-font-size;
word-break: break-all;
word-break: break-word;
word-wrap: break-word;
a {
text-decoration: underline dotted;
......
......@@ -6,6 +6,7 @@ import Commands from '../lib/commands';
import { loadConfig, processUnread } from '../lib/main';
import { parentCall } from './parentCall';
import { handleTranscript } from './transcript';
import { normalizeMessage, normalizeMessages } from './threads';
const commands = new Commands();
......@@ -51,7 +52,6 @@ export const initRoom = async() => {
if (!roomAgent) {
if (servedBy) {
roomAgent = await Livechat.agent({ rid });
store.setState({ roomAgent });
await store.setState({ agent: roomAgent });
}
}
......@@ -91,6 +91,11 @@ Livechat.onMessage(async(message) => {
message.ts = message.ts.toISOString();
}
message = await normalizeMessage(message);
if (!message) {
return;
}
await store.setState({
messages: upsert(store.state.messages, message, ({ _id }) => _id === message._id, ({ ts }) => ts),
});
......@@ -117,7 +122,7 @@ export const loadMessages = async() => {
}
await store.setState({ loading: true });
const messages = await Livechat.loadMessages(rid);
const messages = await Livechat.loadMessages(rid).then((data) => (normalizeMessages(data)));
await initRoom();
await store.setState({ messages: (messages || []).reverse(), noMoreMessages: false });
await store.setState({ loading: false });
......@@ -136,7 +141,7 @@ export const loadMoreMessages = async() => {
}
await store.setState({ loading: true });
const moreMessages = await Livechat.loadMessages(rid, { limit: messages.length + 10 });
const moreMessages = await Livechat.loadMessages(rid, { limit: messages.length + 10 }).then((data) => (normalizeMessages(data)));
await store.setState({
messages: (moreMessages || []).reverse(),
noMoreMessages: messages.length + 10 > moreMessages.length,
......
import { Livechat } from '../api';
import { store } from '../store';
import { upsert, createToken } from '../components/helpers';
const addParentMessage = async(parentMessage) => {
const { state } = store;
const { parentMessages = [] } = state;
const { tmid } = parentMessage;
if (!parentMessages.find((msg) => msg._id === tmid)) {
await store.setState({ parentMessages: upsert(parentMessages, parentMessage, ({ _id }) => _id === parentMessage._id, ({ ts }) => ts) });
}
};
const isThreadMessage = async(message) => {
if (!message || !message.replies) {
return false;
}
await addParentMessage(message);
return true;
};
const findParentMessage = async(tmid) => {
const { state } = store;
const { parentMessages = [], room, alerts } = state;
let parentMessage = parentMessages.find((msg) => msg._id === tmid);
if (!parentMessage) {
const { _id: rid } = room;
try {
parentMessage = await Livechat.message(tmid, { rid });
await addParentMessage(parentMessage);
} catch (error) {
const { data: { error: reason } } = error;
const alert = { id: createToken(), children: reason, error: true, timeout: 5000 };
await store.setState({ alerts: (alerts.push(alert), alerts) });
}
}
return parentMessage;
};
const normalizeThreadMessage = async(message) => {
const { state } = store;
const { messages = [] } = state;
let parentMessage = messages.find((msg) => msg._id === message.tmid);
if (!parentMessage) {
parentMessage = await findParentMessage(message.tmid);
}
const { msg, attachments = [] } = parentMessage;
return Object.assign(message, { threadMsg: parentMessage, attachments: [{ attachments, text: msg, tmid: message.tmid }] });
};
export const normalizeMessage = async(message) => {
const isThreadMsg = await isThreadMessage(message);
if (isThreadMsg) {
return false;
}
if (message && message.tmid && !message.threadMsg) {
return normalizeThreadMessage(message);
}
return message;
};
export const normalizeMessages = async(messages = []) => await Promise.all(messages.filter((message) => normalizeMessage(message)));
......@@ -7,7 +7,7 @@ const log = process.env.NODE_ENV === 'development' ?
const WIDGET_OPEN_WIDTH = 365;
const WIDGET_OPEN_HEIGHT = 510;
const WIDGET_OPEN_HEIGHT = 525;
const WIDGET_MINIMIZED_WIDTH = 54;
const WIDGET_MINIMIZED_HEIGHT = 54;
const WIDGET_MARGIN = 16;
......
......@@ -1353,10 +1353,10 @@
react-lifecycles-compat "^3.0.4"
warning "^3.0.0"
"@rocket.chat/sdk@^1.0.0-alpha.27":
version "1.0.0-alpha.27"
resolved "https://registry.yarnpkg.com/@rocket.chat/sdk/-/sdk-1.0.0-alpha.27.tgz#9bbcde27ce1fc180b23d45f9fa04132773a45d32"
integrity sha512-eC8lMDH4+BPjLxHGcB1DoUPf3HhO4atEfpxg/J7Wi2/+fuTVUv74A5K7H+sTitvVuM9R3xFRXIbjUGIRTx0/gA==
"@rocket.chat/sdk@^1.0.0-alpha.28":
version "1.0.0-alpha.28"
resolved "https://registry.yarnpkg.com/@rocket.chat/sdk/-/sdk-1.0.0-alpha.28.tgz#569f3c578c5c12ed54a9317d36fa8413208ce021"
integrity sha512-tQ+tIIX5R931cxIlTTn2ftCfiIo372vCG3omzDwzSfw6Kq24f7giUxVEpWfsI4GtuTU4caoren7wuGYwnST/+A==
dependencies:
"@types/event-emitter" "^0.3.2"
"@types/eventemitter3" "^2.0.2"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment