mirror of
https://github.com/HabitRPG/habitica.git
synced 2025-12-18 15:17:25 +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 moment from 'moment';
|
||||||
import filter from 'lodash/filter';
|
import filter from 'lodash/filter';
|
||||||
import sortBy from 'lodash/sortBy';
|
import sortBy from 'lodash/sortBy';
|
||||||
|
import groupBy from 'lodash/groupBy';
|
||||||
import { mapState } from 'client/libs/store';
|
import { mapState } from 'client/libs/store';
|
||||||
import styleHelper from 'client/mixins/styleHelper';
|
import styleHelper from 'client/mixins/styleHelper';
|
||||||
|
|
||||||
@@ -220,43 +221,51 @@ export default {
|
|||||||
computed: {
|
computed: {
|
||||||
...mapState({user: 'user.data'}),
|
...mapState({user: 'user.data'}),
|
||||||
conversations () {
|
conversations () {
|
||||||
let conversations = {};
|
const inboxGroup = groupBy(this.user.inbox.messages, 'uuid');
|
||||||
for (let messageId in this.user.inbox.messages) {
|
|
||||||
let message = this.user.inbox.messages[messageId];
|
|
||||||
let userId = message.uuid;
|
|
||||||
|
|
||||||
if (!conversations[userId]) {
|
// Create conversation objects
|
||||||
conversations[userId] = {
|
const convos = [];
|
||||||
name: message.user,
|
for (let key in inboxGroup) {
|
||||||
key: userId,
|
const convoSorted = sortBy(inboxGroup[key], [(o) => {
|
||||||
messages: [],
|
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 = {
|
const convoModel = {
|
||||||
text: message.text,
|
name: recentMessage.toUser ? recentMessage.toUser : recentMessage.user, // Handles case where from user sent the only message or the to user sent the only message
|
||||||
timestamp: message.timestamp,
|
key: recentMessage.toUUID ? recentMessage.toUUID : recentMessage.uuid,
|
||||||
user: message.user,
|
messages: newChatModels,
|
||||||
uuid: message.uuid,
|
lastMessageText: recentMessage.text,
|
||||||
id: message.id,
|
date: recentMessage.timestamp,
|
||||||
contributor: message.contributor,
|
|
||||||
};
|
};
|
||||||
|
|
||||||
if (message.sent) {
|
convos.push(convoModel);
|
||||||
newMessage.user = this.user.profile.name;
|
|
||||||
newMessage.uuid = this.user._id;
|
|
||||||
newMessage.contributor = this.user.contributor;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if (newMessage.text) conversations[userId].messages.push(newMessage);
|
// Sort models by most recent
|
||||||
conversations[userId].lastMessageText = message.text;
|
const conversations = sortBy(convos, [(o) => {
|
||||||
conversations[userId].date = message.timestamp;
|
|
||||||
}
|
|
||||||
|
|
||||||
conversations = sortBy(conversations, [(o) => {
|
|
||||||
return moment(o.date).toDate();
|
return moment(o.date).toDate();
|
||||||
}]);
|
}]);
|
||||||
conversations = conversations.reverse();
|
|
||||||
|
|
||||||
return conversations;
|
return conversations;
|
||||||
},
|
},
|
||||||
|
|||||||
Reference in New Issue
Block a user