habitica/website/server/libs/chat/group-chat.js

134 lines
4.5 KiB
JavaScript
Raw Permalink Normal View History

2019-10-08 14:57:10 +00:00
import _ from 'lodash';
Move inbox to its own model (#10428) * shared model for chat and inbox * disable inbox schema * inbox: use separate model * remove old code that used group.chat * add back chat field (not used) and remove old tests * remove inbox exclusions when loading user * add GET /api/v3/inbox/messages * add comment * implement DELETE /inbox/messages/:messageid in v4 * implement GET /inbox/messages in v4 and update tests * implement DELETE /api/v4/inbox/clear * fix url * fix doc * update /export/inbox.html * update other data exports * add back messages in user schema * add user.toJSONWithInbox * add compativility until migration is done * more compatibility * fix tojson called twice * add compatibility methods * fix common tests * fix v4 integration tests * v3 get user -> with inbox * start to fix tests * fix v3 integration tests * wip * wip, client use new route * update tests for members/send-private-message * tests for get user in v4 * add tests for DELETE /inbox/messages/:messageId * add tests for DELETE /inbox/clear in v4 * update docs * fix tests * initial migration * fix migration * fix migration * migration fixes * migrate api.enterCouponCode * migrate api.castSpell * migrate reset, reroll, rebirth * add routes to v4 version * fix tests * fixes * api.updateUser * remove .only * get user -> userLib * refactor inbox.vue to work with new data model * fix return message when messaging yourself * wip fix bug with new conversation * wip * fix remaining ui issues * move api.registerLocal, fixes * keep only v3 version of GET /inbox/messages
2018-09-21 13:12:20 +00:00
import { chatModel as Chat } from '../../models/message';
2019-05-15 10:07:17 +00:00
import shared from '../../../common';
import { // eslint-disable-line import/no-cycle
MAX_CHAT_COUNT,
MAX_SUBBED_GROUP_CHAT_COUNT,
} from '../../models/group';
2019-05-15 10:07:17 +00:00
const questScrolls = shared.content.quests;
// @TODO: Don't use this method when the group can be saved.
export async function getGroupChat (group) {
let maxChatCount = MAX_CHAT_COUNT;
if (group.chatLimitCount && group.chatLimitCount >= MAX_CHAT_COUNT) {
maxChatCount = group.chatLimitCount;
} else if (group.hasActiveGroupPlan()) {
maxChatCount = MAX_SUBBED_GROUP_CHAT_COUNT;
}
2019-10-08 14:57:10 +00:00
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) => {
2019-10-08 14:57:10 +00:00
const foundMessage = previous.find(message => message.id === current.id);
if (!foundMessage) previous.push(current);
return previous;
}, []);
}
2019-05-15 10:07:17 +00:00
export function translateMessage (lang, info) {
let msg;
let foundText = '';
2019-10-08 14:57:10 +00:00
const { spells } = shared.content;
const { quests } = shared.content;
2019-05-15 10:07:17 +00:00
switch (info.type) { // eslint-disable-line default-case
2019-05-15 10:07:17 +00:00
case 'quest_start':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatQuestStarted', { questName: questScrolls[info.quest].text(lang) }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'boss_damage':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatBossDamage', {
username: info.user,
bossName: questScrolls[info.quest].boss.name(lang),
userDamage: info.userDamage,
bossDamage: info.bossDamage,
2019-10-08 14:57:10 +00:00
}, lang);
2019-05-15 10:07:17 +00:00
break;
case 'boss_dont_attack':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatBossDontAttack', { username: info.user, bossName: questScrolls[info.quest].boss.name(lang), userDamage: info.userDamage }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'boss_rage':
msg = questScrolls[info.quest].boss.rage.effect(lang);
2019-05-15 10:07:17 +00:00
break;
case 'boss_defeated':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatBossDefeated', { bossName: questScrolls[info.quest].boss.name(lang) }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'user_found_items':
foundText = _.reduce(info.items, (m, v, k) => {
m.push(`${v} ${questScrolls[info.quest].collect[k].text(lang)}`);
return m;
}, []).join(', ');
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatFindItems', { username: info.user, items: foundText }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'all_items_found':
msg = shared.i18n.t('chatItemQuestFinish', lang);
2019-05-15 10:07:17 +00:00
break;
case 'spell_cast_party':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatCastSpellParty', { username: info.user, spell: spells[info.class][info.spell].text(lang) }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'spell_cast_user':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatCastSpellUser', { username: info.user, spell: spells[info.class][info.spell].text(lang), target: info.target }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'spell_cast_party_multi':
msg = shared.i18n.t('chatCastSpellPartyTimes', { username: info.user, spell: spells[info.class][info.spell].text(lang), times: info.times }, lang);
break;
case 'spell_cast_user_multi':
2023-07-18 15:20:58 +00:00
msg = shared.i18n.t('chatCastSpellUserTimes', {
username: info.user,
spell: spells[info.class][info.spell].text(lang),
target: info.target,
times: info.times,
}, lang);
break;
2019-05-15 10:07:17 +00:00
case 'quest_cancel':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatQuestCancelled', { username: info.user, questName: questScrolls[info.quest].text(lang) }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'quest_abort':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('chatQuestAborted', { username: info.user, questName: questScrolls[info.quest].text(lang) }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'tavern_quest_completed':
msg = quests[info.quest].completionChat(lang);
2019-05-15 10:07:17 +00:00
break;
case 'tavern_boss_rage_tired':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('tavernBossTired', { rageName: quests[info.quest].boss.rage.title(lang), bossName: quests[info.quest].boss.name(lang) }, lang);
2019-05-15 10:07:17 +00:00
break;
case 'tavern_boss_rage':
msg = quests[info.quest].boss.rage[info.scene](lang);
2019-05-15 10:07:17 +00:00
break;
case 'tavern_boss_desperation':
msg = quests[info.quest].boss.desperation.text(lang);
2019-05-15 10:07:17 +00:00
break;
case 'claim_task':
2019-10-08 14:57:10 +00:00
msg = shared.i18n.t('userIsClamingTask', { username: info.user, task: info.task }, lang);
2019-05-15 10:07:17 +00:00
break;
2023-07-11 23:25:59 +00:00
default:
2023-07-18 15:20:58 +00:00
msg = 'Error translating party chat. Unknown message type.';
2019-05-15 10:07:17 +00:00
}
return msg;
}