Files
habitica/website/server/models/message.js
negue 2535fd7095 Combined Message Pages/Redesign (#15310)
* split component prepare new views / states

* extract empty and disabled state as components

* fix empty state mail icon

* first logic switching between modes, move page to /private-messages/index.vue

* extract autoCompleteHelper.js

* style header + start new message input

* style plus button + focus input

* state logic, types for sanity

* WIP PM new Message started

* add /members/username test

* first design changes to messageCard

* delete private message or chat - based on the mode

* copy as todo

* mention links to modal

* report chat or private message

* WIP likeButton

* likeButton styling

* hide like on private message cards

* fix unit test

* replace copy as todo - to just a copy to clipboard

* style changes

* menu position + like button width

* dropdown items background + like font

* fix like button padding

* move api endpoints and tests around to group inbox methods  + like for inbox private messages

* restyle system messages

* Dropdown Radius and Padding

* WIP system messages

* fix lint

* copy delta commit of allowing liking own private messages

* enable liking private messages

* fix menu non hovered item icon color

* fix import path

* ignore background on system messages

* requested changes + migration

* update migration to update the unique id to some messages and delete the duplicates

* migration based on users pagination

* fix(migration): use Promise.all

* change to bulkWrites per User, and all messages in one run (of a user)

* check for array

* use rest operator ...

* skip sorting to get the users

* remove migration, disable like for private messages without uniqueMessageId

* lean+bulkWrite for likes, add time checks for like and auth for further debugging

* add a limit 2 get the messages by uniqueId

* Adding a simple server start script

* remove pinned nodemon dep

* fix inbox controller/tests

* fix / requested style changes

* fix empty state padding /

* hide avatar weapons on messages - fix avatar spacing on messages

* Hourglass Simplification (#15323)

* begin removing obsolete tests

* begin refactoring

* update cron tests

* cleanup

* finish basic implementation of new logic

* add more subscription tests

* subscription test improvements

* return nextHourglassDate again

* fix gem limit

* fix(test): short circuit this.

* fix(admin): correct logic and style for shrimple subs

* WIP(frontend): draft of main subs page view

* fix hourglass count

* Fix hourglass logic for upgrades

* fix admin panel display

* WIP(subs): extant Stripe state

* fix admin panel strings

* fix missing transaction type

* add new field for cumulative subscription count

* show date for hourglass bonus if it was received

* fix test

* feat(subscription): max Gems progress readout

* fix(css): correct and refactor heights and selection states

* fix(subs): correct border-radius and redirect

* fix(stripe): correct redirect after success

* Admin panel display fixes

* don’t give additional HG for new sub if they already got one this month

* fix issue with promo hourglasses

* fix(subscription): update layout when gifting

* fix(subscriptions): more gift layout revisions

* fix(subscriptions): minor visual updates

* fix(subs): pass autoRenews through Stripe

* fix(subs): gifts DON't renew

* fix(lint): unnecessary ternary

* fix(lint): do negate object ig

* fix(subs): try again on gifts

* fix(subs): unhovery and un-12-monthy

* fix bug with incorrectly giving HG bonus

* remove only

* fix test

* fix test

* fix(subs): also redirect to subs after gift sub

* fix(subs): fix typeError

* fix(g1g1): don't try to find Gems promo during bogo

---------

Co-authored-by: Phillip Thelen <phillip@habitica.com>
Co-authored-by: Kalista Payne <sabe@habitica.com>

* chore(sprites): update subproject

* fix(layout): tighten cancellation note

* fix(subs): Google wording and HG escape

* chore(testing): fake g1g1 dates

* fix(subs): don't hide HG preview entirely

* fix(subs): center next hourglass message

* working validatedTextInput.vue within start-new-conversation-input-header.vue 🎉

* fix(git): remove changes from old develop

* Revert "fix(git): remove changes from old develop"

This reverts commit 0e30f7df00.

* fix(git): no actually just this file i guesss

* adding an empty loading state, hiding

* fought the avatar arch nemesis again

* fix chatMessages (party chat) message spacing

* move disabled text back to above the input area - re-enable input area

* show disabled private messages top panel

* fix font color

* fixing uiStates - removing disabled - moving the own user check to the last

* fix(lint): add missing prop defaults

* fix(lint): object default should be fn

* fix(chat): correct grammar in error

---------

Co-authored-by: SabreCat <sabe@habitica.com>
Co-authored-by: Kalista Payne <sabrecat@gmail.com>
Co-authored-by: Phillip Thelen <phillip@habitica.com>
2025-01-16 16:52:24 -06:00

174 lines
5.1 KiB
JavaScript

import mongoose from 'mongoose';
import { v4 as uuid } from 'uuid';
import { defaults } from 'lodash';
import removeMd from 'remove-markdown';
import baseModel from '../libs/baseModel';
import shared from '../../common';
const defaultSchema = () => ({
id: String,
timestamp: Date,
text: String,
unformattedText: String,
info: { $type: mongoose.Schema.Types.Mixed },
// sender properties
user: String, // profile name (unfortunately)
username: String,
contributor: { $type: mongoose.Schema.Types.Mixed },
backer: { $type: mongoose.Schema.Types.Mixed },
uuid: String, // sender uuid
userStyles: { $type: mongoose.Schema.Types.Mixed },
flags: { $type: mongoose.Schema.Types.Mixed, default: {} },
flagCount: { $type: Number, default: 0 },
likes: { $type: mongoose.Schema.Types.Mixed },
client: String,
_meta: { $type: mongoose.Schema.Types.Mixed },
});
const chatSchema = new mongoose.Schema({
...defaultSchema(),
groupId: { $type: String, ref: 'Group' },
}, {
minimize: false, // Allow for empty flags to be saved
typeKey: '$type', // So that we can use fields named `type`
});
chatSchema.plugin(baseModel, {
noSet: ['_id'],
});
const inboxSchema = new mongoose.Schema({
sent: { $type: Boolean, default: false }, // if the owner sent this message
// the uuid of the user where the message is stored,
// we store two copies of each inbox messages:
// one for the sender and one for the receiver
ownerId: { $type: String, ref: 'User' },
uniqueMessageId: String,
...defaultSchema(),
}, {
minimize: false, // Allow for empty flags to be saved
typeKey: '$type', // So that we can use fields named `type`
});
inboxSchema.plugin(baseModel, {
noSet: ['_id'],
});
export const chatModel = mongoose.model('Chat', chatSchema);
export const inboxModel = mongoose.model('Inbox', inboxSchema);
export function setUserStyles (newMessage, user) {
const userStyles = {};
userStyles.items = { gear: {} };
let userCopy = user;
if (user.toObject) userCopy = user.toObject();
if (userCopy.items) {
userStyles.items.gear = {};
if (userCopy.preferences && userCopy.preferences.costume) {
userStyles.items.gear.costume = { ...userCopy.items.gear.costume };
} else {
userStyles.items.gear.equipped = { ...userCopy.items.gear.equipped };
}
userStyles.items.currentMount = userCopy.items.currentMount;
userStyles.items.currentPet = userCopy.items.currentPet;
}
if (userCopy.preferences) {
userStyles.preferences = {};
if (userCopy.preferences.style) userStyles.preferences.style = userCopy.preferences.style;
userStyles.preferences.hair = userCopy.preferences.hair;
userStyles.preferences.skin = userCopy.preferences.skin;
userStyles.preferences.shirt = userCopy.preferences.shirt;
userStyles.preferences.chair = userCopy.preferences.chair;
userStyles.preferences.size = userCopy.preferences.size;
userStyles.preferences.chair = userCopy.preferences.chair;
userStyles.preferences.background = userCopy.preferences.background;
userStyles.preferences.costume = userCopy.preferences.costume;
}
if (userCopy.stats) {
userStyles.stats = {};
userStyles.stats.class = userCopy.stats.class;
if (userCopy.stats.buffs) {
userStyles.stats.buffs = {
seafoam: userCopy.stats.buffs.seafoam,
shinySeed: userCopy.stats.buffs.shinySeed,
spookySparkles: userCopy.stats.buffs.spookySparkles,
snowball: userCopy.stats.buffs.snowball,
};
}
}
let contributorCopy = user.contributor;
if (contributorCopy && contributorCopy.toObject) {
contributorCopy = contributorCopy.toObject();
}
newMessage.contributor = contributorCopy;
newMessage.userStyles = userStyles;
if (newMessage.markModified) {
newMessage.markModified('userStyles contributor');
}
}
// Sanitize an input message, separate from messageDefaults because
// it must run before mentions are highlighted
export function sanitizeText (msg) {
// Trim messages longer than the MAX_MESSAGE_LENGTH
return msg.substring(0, shared.constants.MAX_MESSAGE_LENGTH);
}
export function messageDefaults (msg, user, client, flagCount = 0, info = {}) {
const id = uuid();
const message = {
id,
_id: id,
text: msg,
unformattedText: removeMd(msg),
info,
timestamp: Number(new Date()),
likes: {},
flags: {},
flagCount,
client,
};
if (user) {
defaults(message, {
uuid: user._id,
contributor: user.contributor && user.contributor.toObject(),
backer: user.backer && user.backer.toObject(),
user: user.profile.name,
username: user.flags && user.flags.verifiedUsername
&& user.auth && user.auth.local && user.auth.local.username,
});
} else {
message.uuid = 'system';
}
return message;
}
export function mapInboxMessage (msg, user) {
if (msg.sent) {
msg.toUUID = msg.uuid;
msg.toUser = msg.user;
msg.toUserName = msg.username;
msg.toUserContributor = msg.contributor;
msg.toUserBacker = msg.backer;
msg.uuid = user._id;
msg.user = user.profile.name;
msg.username = user.auth.local.username;
msg.contributor = user.contributor;
msg.backer = user.backer;
}
return msg;
}