mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-17 14:47:53 +01:00
Fixed sorting and model creation for inboxes (#10039)
This commit is contained in:
@@ -160,6 +160,7 @@ import Vue from 'vue';
|
||||
import moment from 'moment';
|
||||
import filter from 'lodash/filter';
|
||||
import sortBy from 'lodash/sortBy';
|
||||
import groupBy from 'lodash/groupBy';
|
||||
import { mapState } from 'client/libs/store';
|
||||
import styleHelper from 'client/mixins/styleHelper';
|
||||
|
||||
@@ -220,43 +221,51 @@ export default {
|
||||
computed: {
|
||||
...mapState({user: 'user.data'}),
|
||||
conversations () {
|
||||
let conversations = {};
|
||||
for (let messageId in this.user.inbox.messages) {
|
||||
let message = this.user.inbox.messages[messageId];
|
||||
let userId = message.uuid;
|
||||
const inboxGroup = groupBy(this.user.inbox.messages, 'uuid');
|
||||
|
||||
if (!conversations[userId]) {
|
||||
conversations[userId] = {
|
||||
name: message.user,
|
||||
key: userId,
|
||||
messages: [],
|
||||
};
|
||||
// Create conversation objects
|
||||
const convos = [];
|
||||
for (let key in inboxGroup) {
|
||||
const convoSorted = sortBy(inboxGroup[key], [(o) => {
|
||||
return o.timestamp;
|
||||
}]);
|
||||
|
||||
// Fix poor inbox chat models
|
||||
const newChatModels = convoSorted.map(chat => {
|
||||
let newChat = Object.assign({}, chat);
|
||||
if (newChat.sent) {
|
||||
newChat.toUUID = newChat.uuid;
|
||||
newChat.toUser = newChat.user;
|
||||
newChat.uuid = this.user._id;
|
||||
newChat.user = this.user.profile.name;
|
||||
newChat.contributor = this.user.contributor;
|
||||
newChat.backer = this.user.backer;
|
||||
}
|
||||
return newChat;
|
||||
});
|
||||
|
||||
const recentMessage = newChatModels[newChatModels.length - 1];
|
||||
|
||||
// Special case where we have placeholder message because conversations are just grouped messages for now
|
||||
if (!recentMessage.text) {
|
||||
newChatModels.splice(newChatModels.length - 1, 1);
|
||||
}
|
||||
|
||||
let newMessage = {
|
||||
text: message.text,
|
||||
timestamp: message.timestamp,
|
||||
user: message.user,
|
||||
uuid: message.uuid,
|
||||
id: message.id,
|
||||
contributor: message.contributor,
|
||||
const convoModel = {
|
||||
name: recentMessage.toUser ? recentMessage.toUser : recentMessage.user, // Handles case where from user sent the only message or the to user sent the only message
|
||||
key: recentMessage.toUUID ? recentMessage.toUUID : recentMessage.uuid,
|
||||
messages: newChatModels,
|
||||
lastMessageText: recentMessage.text,
|
||||
date: recentMessage.timestamp,
|
||||
};
|
||||
|
||||
if (message.sent) {
|
||||
newMessage.user = this.user.profile.name;
|
||||
newMessage.uuid = this.user._id;
|
||||
newMessage.contributor = this.user.contributor;
|
||||
}
|
||||
|
||||
if (newMessage.text) conversations[userId].messages.push(newMessage);
|
||||
conversations[userId].lastMessageText = message.text;
|
||||
conversations[userId].date = message.timestamp;
|
||||
convos.push(convoModel);
|
||||
}
|
||||
|
||||
conversations = sortBy(conversations, [(o) => {
|
||||
// Sort models by most recent
|
||||
const conversations = sortBy(convos, [(o) => {
|
||||
return moment(o.date).toDate();
|
||||
}]);
|
||||
conversations = conversations.reverse();
|
||||
|
||||
return conversations;
|
||||
},
|
||||
|
||||
Reference in New Issue
Block a user