flow: stay hydrated

This commit is contained in:
cha0s 2020-07-16 13:55:46 -05:00
parent 2c22db517b
commit 2000f99fb3
4 changed files with 47 additions and 3 deletions

View File

@ -14,7 +14,7 @@ const slice = createSlice({
channels: {}, channels: {},
focus: '', focus: '',
messages: {}, messages: {},
recent: {}, recent: [],
unread: {}, unread: {},
users: {}, users: {},
...(hydration('chat') || {}), ...(hydration('chat') || {}),
@ -27,6 +27,9 @@ const slice = createSlice({
state.unread[channel] += 1; state.unread[channel] += 1;
} }
}, },
addRecent: (state, {payload: {channel}}) => {
state.recent.push(channel);
},
editMessage: (state, {payload: {uuid, content}}) => { editMessage: (state, {payload: {uuid, content}}) => {
state.messages[uuid] = content; state.messages[uuid] = content;
}, },
@ -54,7 +57,8 @@ const slice = createSlice({
messages.splice(messages.indexOf(uuid), 1); messages.splice(messages.indexOf(uuid), 1);
}, },
removeRecent: (state, {payload: {channel}}) => { removeRecent: (state, {payload: {channel}}) => {
delete state.recent[channel]; const {recent} = state;
recent.splice(recent.indexOf(channel), 1);
}, },
}, },
}); });

2
src/common/state/util.js Normal file
View File

@ -0,0 +1,2 @@
// eslint-disable-next-line import/prefer-default-export
export const channelName = ({name, type}) => `/${type}/${name}`;

View File

@ -41,7 +41,12 @@ export async function createHttpServer() {
}); });
proxy.on('proxyRes', async (proxyRes, req, res) => { proxy.on('proxyRes', async (proxyRes, req, res) => {
const buffer = await proxyRes.pipe(concat()); const buffer = await proxyRes.pipe(concat());
if ('text/html; charset=UTF-8' === proxyRes.headers['content-type']) {
res.end(await hydration(req, buffer)); res.end(await hydration(req, buffer));
}
else {
res.end(buffer);
}
}); });
proxy.on('error', (err, req, res) => { proxy.on('error', (err, req, res) => {
if (res instanceof ServerResponse) { if (res instanceof ServerResponse) {

View File

@ -22,10 +22,18 @@ export default (options = {}) => (
}) })
); );
const channelName = ({name, type}) => `/${type}/${name}`;
registerHooks({ registerHooks({
hydration: async (req) => { hydration: async (req) => {
const hydration = {}; const hydration = {};
const {user} = req; const {user} = req;
let inferred;
const matches = req.url.match(/^\/chat\/([^/]+)\/([^/]+)/i);
if (matches) {
const [, type, name] = matches;
inferred = {type, name};
}
if (user) { if (user) {
hydration.user = { hydration.user = {
friends: await user.friends(), friends: await user.friends(),
@ -35,6 +43,31 @@ registerHooks({
else { else {
hydration.user = null; hydration.user = null;
} }
const favorites = [];
if (inferred) {
favorites.push(inferred);
}
if (favorites.length > 0) {
const chat = {
channels: {},
focus: inferred ? channelName(inferred) : '',
messages: {},
recent: [],
};
for (let i = 0; i < favorites.length; i++) {
const channel = channelName(favorites[i]);
const messages = [];
chat.channels[channel] = {
messages,
users: [],
};
messages.forEach((message) => {
chat.messages[message.uuid] = message;
});
chat.recent.push(channel);
}
hydration.chat = chat;
}
return hydration; return hydration;
}, },
}, module.id); }, module.id);