mirror of
https://github.com/PretendoNetwork/miiverse-api.git
synced 2024-06-01 19:47:47 -04:00
pass document type to document methods
This commit is contained in:
parent
55ec9232a7
commit
c9200e21b2
|
@ -1,5 +1,5 @@
|
|||
import { Schema, model } from 'mongoose';
|
||||
import { ICommunity, ICommunityMethods, CommunityModel } from '@/types/mongoose/community';
|
||||
import { ICommunity, ICommunityMethods, CommunityModel, HydratedCommunityDocument } from '@/types/mongoose/community';
|
||||
|
||||
const CommunitySchema = new Schema<ICommunity, CommunityModel, ICommunityMethods>({
|
||||
platform_id: Number,
|
||||
|
@ -70,53 +70,47 @@ const CommunitySchema = new Schema<ICommunity, CommunityModel, ICommunityMethods
|
|||
}
|
||||
});
|
||||
|
||||
CommunitySchema.method('upEmpathy', async function upEmpathy(): Promise<void> {
|
||||
const empathy = this.get('empathy_count');
|
||||
this.set('empathy_count', empathy + 1);
|
||||
CommunitySchema.method<HydratedCommunityDocument>('upEmpathy', async function upEmpathy(): Promise<void> {
|
||||
this.empathy_count += 1;
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
CommunitySchema.method('downEmpathy', async function downEmpathy(): Promise<void> {
|
||||
const empathy = this.get('empathy_count');
|
||||
this.set('empathy_count', empathy - 1);
|
||||
CommunitySchema.method<HydratedCommunityDocument>('downEmpathy', async function downEmpathy(): Promise<void> {
|
||||
this.empathy_count -= 1;
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
CommunitySchema.method('upFollower', async function upFollower(): Promise<void> {
|
||||
const followers = this.get('followers');
|
||||
this.set('followers', followers + 1);
|
||||
CommunitySchema.method<HydratedCommunityDocument>('upFollower', async function upFollower(): Promise<void> {
|
||||
this.followers += 1;
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
CommunitySchema.method('downFollower', async function downFollower(): Promise<void> {
|
||||
const followers = this.get('followers');
|
||||
this.set('followers', followers - 1);
|
||||
CommunitySchema.method<HydratedCommunityDocument>('downFollower', async function downFollower(): Promise<void> {
|
||||
this.followers -= 1;
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
CommunitySchema.method('addUserFavorite', async function addUserFavorite(pid: number): Promise<void> {
|
||||
const userFavorites: number[] = this.get('user_favorites');
|
||||
if (!userFavorites.includes(pid)) {
|
||||
userFavorites.push(pid);
|
||||
CommunitySchema.method<HydratedCommunityDocument>('addUserFavorite', async function addUserFavorite(pid: number): Promise<void> {
|
||||
if (!this.user_favorites.includes(pid)) {
|
||||
this.user_favorites.push(pid);
|
||||
}
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
CommunitySchema.method('delUserFavorite', async function delUserFavorite(pid: number): Promise<void> {
|
||||
const userFavorites: number[] = this.get('user_favorites');
|
||||
if (userFavorites.includes(pid)) {
|
||||
userFavorites.splice(userFavorites.indexOf(pid), 1);
|
||||
CommunitySchema.method<HydratedCommunityDocument>('delUserFavorite', async function delUserFavorite(pid: number): Promise<void> {
|
||||
if (this.user_favorites.includes(pid)) {
|
||||
this.user_favorites.splice(this.user_favorites.indexOf(pid), 1);
|
||||
}
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
CommunitySchema.method('json', function json(): Record<string, any> {
|
||||
CommunitySchema.method<HydratedCommunityDocument>('json', function json(): Record<string, any> {
|
||||
return {
|
||||
community_id: this.community_id,
|
||||
name: this.name,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Schema, model } from 'mongoose';
|
||||
import { IContent, IContentMethods, ContentModel } from '@/types/mongoose/content';
|
||||
import { IContent, IContentMethods, ContentModel, HydratedContentDocument } from '@/types/mongoose/content';
|
||||
|
||||
const ContentSchema = new Schema<IContent, ContentModel, IContentMethods>({
|
||||
pid: Number,
|
||||
|
@ -17,39 +17,33 @@ const ContentSchema = new Schema<IContent, ContentModel, IContentMethods>({
|
|||
}
|
||||
});
|
||||
|
||||
ContentSchema.method('addToCommunities', async function addToCommunities(postID) {
|
||||
const communities = this.get('followed_communities');
|
||||
communities.addToSet(postID);
|
||||
ContentSchema.method<HydratedContentDocument>('addToCommunities', async function addToCommunities(postID) {
|
||||
this.followed_communities.addToSet(postID);
|
||||
await this.save();
|
||||
});
|
||||
|
||||
ContentSchema.method('removeFromCommunities', async function removeFromCommunities(postID) {
|
||||
const communities = this.get('followed_communities');
|
||||
communities.pull(postID);
|
||||
ContentSchema.method<HydratedContentDocument>('removeFromCommunities', async function removeFromCommunities(postID) {
|
||||
this.followed_communities.pull(postID);
|
||||
await this.save();
|
||||
});
|
||||
|
||||
ContentSchema.method('addToUsers', async function addToUsers(postID) {
|
||||
const users = this.get('followed_users');
|
||||
users.addToSet(postID);
|
||||
ContentSchema.method<HydratedContentDocument>('addToUsers', async function addToUsers(postID) {
|
||||
this.followed_users.addToSet(postID);
|
||||
await this.save();
|
||||
});
|
||||
|
||||
ContentSchema.method('removeFromUsers', async function removeFromUsers(postID) {
|
||||
const users = this.get('followed_users');
|
||||
users.pull(postID);
|
||||
ContentSchema.method<HydratedContentDocument>('removeFromUsers', async function removeFromUsers(postID) {
|
||||
this.followed_users.pull(postID);
|
||||
await this.save();
|
||||
});
|
||||
|
||||
ContentSchema.method('addToFollowers', async function addToFollowers(postID) {
|
||||
const users = this.get('following_users');
|
||||
users.addToSet(postID);
|
||||
ContentSchema.method<HydratedContentDocument>('addToFollowers', async function addToFollowers(postID) {
|
||||
this.following_users.addToSet(postID);
|
||||
await this.save();
|
||||
});
|
||||
|
||||
ContentSchema.method('removeFromFollowers', async function removeFromFollowers(postID) {
|
||||
const users = this.get('following_users');
|
||||
users.pull(postID);
|
||||
ContentSchema.method<HydratedContentDocument>('removeFromFollowers', async function removeFromFollowers(postID) {
|
||||
this.following_users.pull(postID);
|
||||
await this.save();
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
import { Schema, model } from 'mongoose';
|
||||
import moment from 'moment';
|
||||
import { Snowflake } from 'node-snowflake';
|
||||
import { IConversation, IConversationMethods, ConversationModel } from '@/types/mongoose/conversation';
|
||||
import { IConversation, IConversationMethods, ConversationModel, HydratedConversationDocument } from '@/types/mongoose/conversation';
|
||||
|
||||
const ConversationSchema = new Schema<IConversation, ConversationModel, IConversationMethods>({
|
||||
id: {
|
||||
|
@ -33,7 +32,7 @@ const ConversationSchema = new Schema<IConversation, ConversationModel, IConvers
|
|||
}]
|
||||
});
|
||||
|
||||
ConversationSchema.method('newMessage', async function newMessage(message, senderPID) {
|
||||
ConversationSchema.method<HydratedConversationDocument>('newMessage', async function newMessage(message, senderPID) {
|
||||
if (this.users[0].pid === senderPID) {
|
||||
this.users[1].read = false;
|
||||
this.markModified('users[1].read');
|
||||
|
@ -41,20 +40,24 @@ ConversationSchema.method('newMessage', async function newMessage(message, sende
|
|||
this.users[0].read = false;
|
||||
this.markModified('users[0].read');
|
||||
}
|
||||
this.set('last_updated', moment(new Date()));
|
||||
this.set('message_preview', message);
|
||||
|
||||
this.last_updated = new Date();
|
||||
this.message_preview = message;
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
ConversationSchema.method('markAsRead', async function markAsRead(pid) {
|
||||
ConversationSchema.method<HydratedConversationDocument>('markAsRead', async function markAsRead(pid) {
|
||||
const users = this.get('users');
|
||||
if (users[0].pid === pid) {
|
||||
users[0].read = true;
|
||||
} else if (users[1].pid === pid) {
|
||||
users[1].read = true;
|
||||
}
|
||||
this.set('users', users);
|
||||
|
||||
this.users = users;
|
||||
this.markModified('users');
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Schema, model } from 'mongoose';
|
||||
import { INotification, INotificationMethods, NotificationModel } from '@/types/mongoose/notification';
|
||||
import { HydratedNotificationDocument, INotification, INotificationMethods, NotificationModel } from '@/types/mongoose/notification';
|
||||
|
||||
const NotificationSchema = new Schema<INotification, NotificationModel, INotificationMethods>({
|
||||
pid: String,
|
||||
|
@ -14,8 +14,8 @@ const NotificationSchema = new Schema<INotification, NotificationModel, INotific
|
|||
lastUpdated: Date
|
||||
});
|
||||
|
||||
NotificationSchema.method('markRead', async function markRead() {
|
||||
this.set('read', true);
|
||||
NotificationSchema.method<HydratedNotificationDocument>('markRead', async function markRead() {
|
||||
this.read = true;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
import crypto from 'node:crypto';
|
||||
import moment from 'moment';
|
||||
import { Schema, model } from 'mongoose';
|
||||
import { IPost, IPostMethods, PostModel } from '@/types/mongoose/post';
|
||||
import { HydratedPostDocument, IPost, IPostMethods, PostModel } from '@/types/mongoose/post';
|
||||
import { HydratedCommunityDocument } from '@/types/mongoose/community';
|
||||
import { PostToJSONOptions } from '@/types/mongoose/post-to-json-options';
|
||||
import { PostPainting, PostScreenshot } from '@/types/common/post';
|
||||
|
@ -87,41 +87,41 @@ const PostSchema = new Schema<IPost, PostModel, IPostMethods>({
|
|||
id: false // * Disables the .id() getter used by Mongoose in TypeScript. Needed to have our own .id field
|
||||
});
|
||||
|
||||
PostSchema.method('upReply', async function upReply() {
|
||||
const replyCount = this.get('reply_count');
|
||||
PostSchema.method<HydratedPostDocument>('upReply', async function upReply() {
|
||||
const replyCount = this.reply_count || 0;
|
||||
if (replyCount + 1 < 0) {
|
||||
this.set('reply_count', 0);
|
||||
this.reply_count = 0;
|
||||
} else {
|
||||
this.set('reply_count', replyCount + 1);
|
||||
this.reply_count = replyCount + 1;
|
||||
}
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
PostSchema.method('downReply', async function downReply() {
|
||||
const replyCount = this.get('reply_count');
|
||||
PostSchema.method<HydratedPostDocument>('downReply', async function downReply() {
|
||||
const replyCount = this.reply_count || 0;
|
||||
if (replyCount - 1 < 0) {
|
||||
this.set('reply_count', 0);
|
||||
this.reply_count = 0;
|
||||
} else {
|
||||
this.set('reply_count', replyCount - 1);
|
||||
this.reply_count = replyCount - 1;
|
||||
}
|
||||
|
||||
await this.save();
|
||||
});
|
||||
|
||||
PostSchema.method('remove', async function remove(reason) {
|
||||
this.set('remove', true);
|
||||
this.set('removed_reason', reason);
|
||||
PostSchema.method<HydratedPostDocument>('remove', async function remove(reason) {
|
||||
this.removed = true;
|
||||
this.removed_reason = reason;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
PostSchema.method('unRemove', async function unRemove(reason) {
|
||||
this.set('remove', false);
|
||||
this.set('removed_reason', reason);
|
||||
PostSchema.method<HydratedPostDocument>('unRemove', async function unRemove(reason) {
|
||||
this.removed = false;
|
||||
this.removed_reason = reason;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
PostSchema.method('generatePostUID', async function generatePostUID(length: number) {
|
||||
PostSchema.method<HydratedPostDocument>('generatePostUID', async function generatePostUID(length: number) {
|
||||
const id = Buffer.from(String.fromCharCode(...crypto.getRandomValues(new Uint8Array(length * 2))), 'binary').toString('base64').replace(/[+/]/g, '').substring(0, length);
|
||||
|
||||
const inuse = await Post.findOne({ id });
|
||||
|
@ -133,23 +133,23 @@ PostSchema.method('generatePostUID', async function generatePostUID(length: numb
|
|||
}
|
||||
});
|
||||
|
||||
PostSchema.method('cleanedBody', function cleanedBody(): string {
|
||||
PostSchema.method<HydratedPostDocument>('cleanedBody', function cleanedBody(): string {
|
||||
return this.body ? this.body.replace(/[^A-Za-z\d\s-_!@#$%^&*(){}+=,.<>/?;:'"[\]]/g, '').replace(/[\n\r]+/gm, '') : '';
|
||||
});
|
||||
|
||||
PostSchema.method('cleanedMiiData', function cleanedMiiData(): string {
|
||||
PostSchema.method<HydratedPostDocument>('cleanedMiiData', function cleanedMiiData(): string {
|
||||
return this.mii.replace(/[^A-Za-z0-9+/=]/g, '').replace(/[\n\r]+/gm, '').trim();
|
||||
});
|
||||
|
||||
PostSchema.method('cleanedPainting', function cleanedPainting(): string {
|
||||
PostSchema.method<HydratedPostDocument>('cleanedPainting', function cleanedPainting(): string {
|
||||
return this.painting.replace(/[\n\r]+/gm, '').trim();
|
||||
});
|
||||
|
||||
PostSchema.method('cleanedAppData', function cleanedAppData(): string {
|
||||
PostSchema.method<HydratedPostDocument>('cleanedAppData', function cleanedAppData(): string {
|
||||
return this.app_data.replace(/[^A-Za-z0-9+/=]/g, '').replace(/[\n\r]+/gm, '').trim();
|
||||
});
|
||||
|
||||
PostSchema.method('formatPainting', function formatPainting(): PostPainting | undefined {
|
||||
PostSchema.method<HydratedPostDocument>('formatPainting', function formatPainting(): PostPainting | undefined {
|
||||
if (this.painting) {
|
||||
return {
|
||||
format: 'tga',
|
||||
|
@ -160,7 +160,7 @@ PostSchema.method('formatPainting', function formatPainting(): PostPainting | un
|
|||
}
|
||||
});
|
||||
|
||||
PostSchema.method('formatScreenshot', function formatScreenshot(): PostScreenshot | undefined {
|
||||
PostSchema.method<HydratedPostDocument>('formatScreenshot', function formatScreenshot(): PostScreenshot | undefined {
|
||||
if (this.screenshot && this.screenshot_length) {
|
||||
return {
|
||||
size: this.screenshot_length,
|
||||
|
@ -169,7 +169,7 @@ PostSchema.method('formatScreenshot', function formatScreenshot(): PostScreensho
|
|||
}
|
||||
});
|
||||
|
||||
PostSchema.method('json', function json(options: PostToJSONOptions, community?: HydratedCommunityDocument): Record<string, any> {
|
||||
PostSchema.method<HydratedPostDocument>('json', function json(options: PostToJSONOptions, community?: HydratedCommunityDocument): Record<string, any> {
|
||||
const json: Record<string, any> = {
|
||||
body: this.cleanedBody(),
|
||||
country_id: this.country_id ? this.country_id : 254,
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
import { Schema, model } from 'mongoose';
|
||||
import { ISettings, ISettingsMethods, SettingsModel } from '@/types/mongoose/settings';
|
||||
import { HydratedSettingsDocument, ISettings, ISettingsMethods, SettingsModel } from '@/types/mongoose/settings';
|
||||
|
||||
const SettingsSchema = new Schema<ISettings, SettingsModel, ISettingsMethods>({
|
||||
pid: Number,
|
||||
|
@ -48,47 +48,47 @@ const SettingsSchema = new Schema<ISettings, SettingsModel, ISettingsMethods>({
|
|||
}
|
||||
});
|
||||
|
||||
SettingsSchema.method('updateComment', async function updateComment(comment) {
|
||||
this.set('profile_comment', comment);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('updateComment', async function updateComment(comment) {
|
||||
this.profile_comment = comment;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('updateSkill', async function updateSkill(skill) {
|
||||
this.set('game_skill', skill);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('updateSkill', async function updateSkill(skill) {
|
||||
this.game_skill = skill;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('commentVisible', async function commentVisible(active) {
|
||||
this.set('profile_comment_visibility', active);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('commentVisible', async function commentVisible(active) {
|
||||
this.profile_comment_visibility = active;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('skillVisible', async function skillVisible(active) {
|
||||
this.set('game_skill_visibility', active);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('skillVisible', async function skillVisible(active) {
|
||||
this.game_skill_visibility = active;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('birthdayVisible', async function birthdayVisible(active) {
|
||||
this.set('birthday_visibility', active);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('birthdayVisible', async function birthdayVisible(active) {
|
||||
this.birthday_visibility = active;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('relationshipVisible', async function relationshipVisible(active) {
|
||||
this.set('relationship_visibility', active);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('relationshipVisible', async function relationshipVisible(active) {
|
||||
this.relationship_visibility = active;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('countryVisible', async function countryVisible(active) {
|
||||
this.set('country_visibility', active);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('countryVisible', async function countryVisible(active) {
|
||||
this.country_visibility = active;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('favCommunityVisible', async function favCommunityVisible(active) {
|
||||
this.set('profile_favorite_community_visibility', active);
|
||||
SettingsSchema.method<HydratedSettingsDocument>('favCommunityVisible', async function favCommunityVisible(active) {
|
||||
this.profile_favorite_community_visibility = active;
|
||||
await this.save();
|
||||
});
|
||||
|
||||
SettingsSchema.method('json', function json(): Record<string, any> {
|
||||
SettingsSchema.method<HydratedSettingsDocument>('json', function json(): Record<string, any> {
|
||||
return {
|
||||
pid: this.pid,
|
||||
screen_name: this.screen_name
|
||||
|
|
Loading…
Reference in a new issue