mirror of
https://github.com/sudoxnym/habitica.git
synced 2026-07-27 05:29:59 +00:00
25 lines
887 B
JavaScript
25 lines
887 B
JavaScript
|
|
import { model as Chat } from '../../models/chat';
|
||
|
|
import { MAX_CHAT_COUNT, MAX_SUBBED_GROUP_CHAT_COUNT } from '../../models/group';
|
||
|
|
|
||
|
|
// @TODO: Don't use this method when the group can be saved.
|
||
|
|
export async function getGroupChat (group) {
|
||
|
|
const maxChatCount = group.isSubscribed() ? MAX_SUBBED_GROUP_CHAT_COUNT : MAX_CHAT_COUNT;
|
||
|
|
|
||
|
|
const groupChat = await Chat.find({groupId: group._id})
|
||
|
|
.limit(maxChatCount)
|
||
|
|
.sort('-timestamp')
|
||
|
|
.exec();
|
||
|
|
|
||
|
|
// @TODO: Concat old chat to keep continuity of chat stored on group object
|
||
|
|
const currentGroupChat = group.chat || [];
|
||
|
|
const concatedGroupChat = groupChat.concat(currentGroupChat);
|
||
|
|
|
||
|
|
group.chat = concatedGroupChat.reduce((previous, current) => {
|
||
|
|
const foundMessage = previous.find(message => {
|
||
|
|
return message.id === current.id;
|
||
|
|
});
|
||
|
|
if (!foundMessage) previous.push(current);
|
||
|
|
return previous;
|
||
|
|
}, []);
|
||
|
|
}
|