mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +01:00
* fix: first batch of layout issues for private messages + auto sizing textarea * username second line - open profile on face-avatar/conversation name - fix textarea height * refresh on sync * new "you dont have any messages" style + changed min textarea height * new conversationItem style / layout * reset message unread on reload * fix styles / textarea height * list optOut / chatRevoked informations for each conversation + show why its disabled * Block / Unblock - correct disabled states - $gray-200 instead of 300/400 * canReceive not checking chatRevoked * fix: faceAvatar / userLink open the selected conversation user * check if the target user is blocking the logged-in user * check if blocks is undefined * max-height instead of height * fix "no messages" state + canReceive on a new conversation * fixed conversations width (280px on max 768 width page) * call autosize after message is sent * only color the placeholder * only load the current user avatar/settings/flags * show only the current avatar on private messages
98 lines
2.2 KiB
JavaScript
98 lines
2.2 KiB
JavaScript
import { inboxModel as Inbox, setUserStyles } from '../../models/message';
|
|
import { model as User } from '../../models/user';
|
|
|
|
/**
|
|
* Get the current user (avatar/setting etc) for conversations
|
|
* @param users
|
|
* @returns {Promise<void>}
|
|
*/
|
|
async function usersMapByConversations (users) {
|
|
const usersMap = {};
|
|
|
|
const usersQuery = {
|
|
_id: { $in: users },
|
|
};
|
|
|
|
const loadedUsers = await User.find(usersQuery, {
|
|
_id: 1,
|
|
contributor: 1,
|
|
backer: 1,
|
|
items: 1,
|
|
preferences: 1,
|
|
stats: 1,
|
|
flags: 1,
|
|
inbox: 1,
|
|
}).exec();
|
|
|
|
for (const usr of loadedUsers) {
|
|
const loadedUserConversation = {
|
|
_id: usr._id,
|
|
backer: usr.backer,
|
|
contributor: usr.contributor,
|
|
optOut: usr.inbox.optOut,
|
|
blocks: usr.inbox.blocks || [],
|
|
};
|
|
// map user values to conversation properties
|
|
setUserStyles(loadedUserConversation, usr);
|
|
|
|
usersMap[usr._id] = loadedUserConversation;
|
|
}
|
|
|
|
return usersMap;
|
|
}
|
|
|
|
export async function listConversations (owner) {
|
|
// group messages by user owned by logged-in user
|
|
const query = Inbox
|
|
.aggregate([
|
|
{
|
|
$match: {
|
|
ownerId: owner._id,
|
|
},
|
|
},
|
|
{
|
|
$group: {
|
|
_id: '$uuid',
|
|
user: { $last: '$user' },
|
|
username: { $last: '$username' },
|
|
timestamp: { $last: '$timestamp' },
|
|
text: { $last: '$text' },
|
|
count: { $sum: 1 },
|
|
},
|
|
},
|
|
{ $sort: { timestamp: -1 } }, // sort by latest message
|
|
]);
|
|
|
|
const conversationsList = await query.exec();
|
|
|
|
const userIdList = conversationsList.map(c => c._id);
|
|
|
|
// get user-info based on conversations
|
|
const usersMap = await usersMapByConversations(userIdList);
|
|
|
|
const conversations = conversationsList.map(res => {
|
|
const uuid = res._id;
|
|
|
|
const conversation = {
|
|
uuid,
|
|
...res,
|
|
};
|
|
|
|
if (usersMap[uuid]) {
|
|
const user = usersMap[uuid];
|
|
|
|
conversation.userStyles = user.userStyles;
|
|
conversation.contributor = user.contributor;
|
|
conversation.backer = user.backer;
|
|
|
|
const isOwnerBlocked = user.blocks.includes(owner._id);
|
|
|
|
conversation.canReceive = !(user.optOut || isOwnerBlocked) || owner.isAdmin();
|
|
}
|
|
|
|
return conversation;
|
|
});
|
|
|
|
return conversations;
|
|
}
|