feat: add direct message link (#12138)

* feat: add direct message link

/message/:mid
add /:index? to chat routes
add copy link to chat messages
add messageCount to each room object
add infinitescroll in both directions to chat

* fix more tests

* test: more text fixes

* test: fix tests

* remove async

* dont crash if element not in dom

clamp scrollToIndex values to 0, msgCount
This commit is contained in:
Barış Soner Uşaklı
2023-10-31 10:15:06 -04:00
committed by GitHub
parent 7a8c27bf90
commit 4c4f3ac983
18 changed files with 236 additions and 44 deletions

View File

@@ -48,7 +48,19 @@ chatsController.get = async function (req, res, next) {
return res.render('chats', payload);
}
const room = await messaging.loadRoom(req.uid, { uid: uid, roomId: req.params.roomid });
const { index } = req.params;
let start = 0;
payload.scrollToIndex = null;
if (index) {
const msgCount = await db.getObjectField(`chat:room:${req.params.roomid}`, 'messageCount');
start = Math.max(0, parseInt(msgCount, 10) - index - 49);
payload.scrollToIndex = Math.min(msgCount, Math.max(0, parseInt(index, 10) || 1));
}
const room = await messaging.loadRoom(req.uid, {
uid: uid,
roomId: req.params.roomid,
start: start,
});
if (!room) {
return next();
}
@@ -72,5 +84,26 @@ chatsController.redirectToChat = async function (req, res, next) {
return next();
}
const roomid = parseInt(req.params.roomid, 10);
helpers.redirect(res, `/user/${userslug}/chats${roomid ? `/${roomid}` : ''}`);
const index = parseInt(req.params.index, 10);
helpers.redirect(res, `/user/${userslug}/chats${roomid ? `/${roomid}` : ''}${index ? `/${index}` : ''}`);
};
chatsController.redirectToMessage = async function (req, res, next) {
const mid = parseInt(req.params.mid, 10);
if (!mid) {
return next();
}
const [userslug, roomId] = await Promise.all([
user.getUserField(req.uid, 'userslug'),
messaging.getMessageField(mid, 'roomId'),
]);
if (!userslug || !roomId) {
return next();
}
const index = await db.sortedSetRank(`chat:room:${roomId}:mids`, mid);
if (!(parseInt(index, 10) >= 0)) {
return next();
}
helpers.redirect(res, `/user/${userslug}/chats/${roomId}${index ? `/${index + 1}` : ''}`, true);
};