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: {},
focus: '',
messages: {},
recent: {},
recent: [],
unread: {},
users: {},
...(hydration('chat') || {}),
@ -27,6 +27,9 @@ const slice = createSlice({
state.unread[channel] += 1;
}
},
addRecent: (state, {payload: {channel}}) => {
state.recent.push(channel);
},
editMessage: (state, {payload: {uuid, content}}) => {
state.messages[uuid] = content;
},
@ -54,7 +57,8 @@ const slice = createSlice({
messages.splice(messages.indexOf(uuid), 1);
},
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) => {
const buffer = await proxyRes.pipe(concat());
res.end(await hydration(req, buffer));
if ('text/html; charset=UTF-8' === proxyRes.headers['content-type']) {
res.end(await hydration(req, buffer));
}
else {
res.end(buffer);
}
});
proxy.on('error', (err, req, res) => {
if (res instanceof ServerResponse) {

View File

@ -22,10 +22,18 @@ export default (options = {}) => (
})
);
const channelName = ({name, type}) => `/${type}/${name}`;
registerHooks({
hydration: async (req) => {
const hydration = {};
const {user} = req;
let inferred;
const matches = req.url.match(/^\/chat\/([^/]+)\/([^/]+)/i);
if (matches) {
const [, type, name] = matches;
inferred = {type, name};
}
if (user) {
hydration.user = {
friends: await user.friends(),
@ -35,6 +43,31 @@ registerHooks({
else {
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;
},
}, module.id);