make post encoding more generic and structure accurate

This commit is contained in:
Jonathan Barrow 2023-10-02 15:44:09 -04:00
parent c9200e21b2
commit faa12036bb
No known key found for this signature in database
GPG Key ID: E86E9FE9049C741F
8 changed files with 125 additions and 146 deletions

View File

@ -4,7 +4,7 @@ import { Schema, model } from 'mongoose';
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';
import { PostData, PostPainting, PostScreenshot, PostTopicTag } from '@/types/miiverse/post';
const PostSchema = new Schema<IPost, PostModel, IPostMethods>({
id: String,
@ -169,59 +169,60 @@ PostSchema.method<HydratedPostDocument>('formatScreenshot', function formatScree
}
});
PostSchema.method<HydratedPostDocument>('json', function json(options: PostToJSONOptions, community?: HydratedCommunityDocument): Record<string, any> {
const json: Record<string, any> = {
PostSchema.method<HydratedPostDocument>('formatTopicTag', function formatTopicTag(): PostTopicTag {
return {
name: this.topic_tag,
title_id: this.title_id
};
});
PostSchema.method<HydratedPostDocument>('json', function json(options: PostToJSONOptions, community?: HydratedCommunityDocument): PostData {
const post: PostData = {
app_data: undefined, // TODO - I try to keep these fields in the real order they show up in, but idk where this one goes
body: this.cleanedBody(),
country_id: this.country_id ? this.country_id : 254,
community_id: this.community_id, // TODO - This sucks
country_id: this.country_id,
created_at: moment(this.created_at).format('YYYY-MM-DD HH:MM:SS'),
feeling_id: this.feeling_id,
id: this.id,
is_autopost: this.is_autopost,
is_community_private_autopost: this.is_community_private_autopost,
is_spoiler: this.is_spoiler,
is_app_jumpable: this.is_app_jumpable,
empathy_count: this.empathy_count,
is_autopost: this.is_autopost ? 1 : 0,
is_community_private_autopost: this.is_community_private_autopost ? 1 : 0,
is_spoiler: this.is_spoiler ? 1 : 0,
is_app_jumpable: this.is_app_jumpable ? 1 : 0,
empathy_count: this.empathy_count || 0,
language_id: this.language_id,
number: '0',
mii: undefined, // * Conditionally set later
mii_face_url: undefined, // * Conditionally set later
number: 0,
painting: this.formatPainting(),
pid: this.pid,
platform_id: this.platform_id,
region_id: this.region_id,
reply_count: this.reply_count,
reply_count: this.reply_count || 0,
screen_name: this.screen_name,
title_id: this.title_id
screenshot: this.formatScreenshot(),
topic_tag: undefined, // * Conditionally set later
title_id: this.title_id,
};
if (this.app_data && options.app_data) {
json.app_data = this.cleanedAppData();
}
if (options.topics && community) {
json.community_id = community.community_id;
} else {
json.community_id = this.community_id;
if (options.app_data) {
post.app_data = this.cleanedAppData();
}
if (options.with_mii) {
json.mii = this.cleanedMiiData();
json.mii_face_url = this.mii_face_url;
post.mii = this.cleanedMiiData();
post.mii_face_url = this.mii_face_url;
}
if (this.painting) {
json.painting = this.formatPainting();
if (options.topic_tag) {
post.topic_tag = this.formatTopicTag();
}
if (this.screenshot && this.screenshot_length) {
json.screenshot = this.formatScreenshot();
if (community) {
post.community_id = community.community_id;
}
if (this.topic_tag && options.topic_tag) {
json.topic_tag = {
name: this.topic_tag,
title_id: this.title_id
};
}
return json;
return post;
});
PostSchema.pre('save', async function(next) {

View File

@ -10,7 +10,7 @@ import { Community } from '@/models/community';
import { IPost } from '@/types/mongoose/post';
import { HydratedEndpointDocument } from '@/types/mongoose/endpoint';
import { HydratedCommunityDocument } from '@/types/mongoose/community';
import { WWPData, WWPPost, WWPTopic } from '@/types/common/wara-wara-plaza';
import { WWPData, WWPTopic } from '@/types/miiverse/wara-wara-plaza';
const router = express.Router();
const ONE_HOUR = 60 * 60 * 1000;
@ -115,33 +115,12 @@ async function generateTopicsData(communities: HydratedCommunityDocument[]): Pro
const people = await getCommunityPeople(community);
for (const person of people) {
const hydratedPost = Post.hydrate(person.post);
const post = Post.hydrate(person.post).json({
with_mii: true,
topic_tag: true
});
const post: WWPPost = {
body: hydratedPost.cleanedBody(),
community_id: 0xFFFFFFFF, // * This is how it was in the real WWP. Unsure why, but it works
country_id: hydratedPost.country_id,
created_at: moment(hydratedPost.created_at).format('YYYY-MM-DD HH:MM:SS'),
feeling_id: hydratedPost.feeling_id,
id: hydratedPost.id,
is_autopost: hydratedPost.is_autopost ? 1 : 0,
is_community_private_autopost: hydratedPost.is_community_private_autopost ? 1 : 0,
is_spoiler: hydratedPost.is_spoiler ? 1 : 0,
is_app_jumpable: hydratedPost.is_app_jumpable ? 1 : 0,
empathy_count: hydratedPost.empathy_count || 0,
language_id: hydratedPost.language_id,
mii: hydratedPost.cleanedMiiData(),
mii_face_url: hydratedPost.mii_face_url,
number: 0,
painting: hydratedPost.formatPainting(),
pid: hydratedPost.pid,
platform_id: hydratedPost.platform_id,
region_id: hydratedPost.region_id,
reply_count: hydratedPost.reply_count || 0,
screen_name: hydratedPost.screen_name,
screenshot: hydratedPost.formatScreenshot(),
title_id: hydratedPost.title_id,
};
post.community_id = 0xFFFFFFFF; // * Make this match above. This is how it was in the real WWP. Unsure why, but it works
topic.people.push({
person: {

View File

@ -1,11 +0,0 @@
export type PostPainting = {
format: string;
content: string;
size: number;
url: string;
};
export type PostScreenshot = {
size: number;
url: string;
};

View File

@ -1,70 +0,0 @@
// TODO - Maybe this can become more generalized, and not specific to WWP?
export type WWPPost = {
body?: string;
community_id: number;
country_id: number;
created_at: string;
feeling_id: number;
id: string;
is_autopost: 0 | 1;
is_community_private_autopost: 0 | 1;
is_spoiler: 0 | 1;
is_app_jumpable: 0 | 1;
empathy_count: number;
language_id: number;
mii: string;
mii_face_url: string;
number: number;
painting?: {
format: string;
content: string;
size: number;
url: string;
};
pid: number;
platform_id: number;
region_id: number;
reply_count: number;
screen_name: string;
screenshot?: {
size: number;
url: string;
};
title_id: string;
};
export type WWPPerson = {
person: {
posts: {
post: WWPPost;
}[];
}
};
export type WWPTopic = {
empathy_count: number;
has_shop_page: 0 | 1;
icon: string;
title_ids: {
title_id: string;
}[];
title_id: string;
community_id: number;
is_recommended: 0 | 1;
name: string;
people: WWPPerson[];
position: number;
};
export type WWPData = {
result: {
has_error: 0 | 1;
version: 1;
expire: string;
request_name: 'topics';
topics: {
topic: WWPTopic;
}[];
}
};

View File

@ -0,0 +1,44 @@
export type PostData = {
app_data?: string; // TODO - I try to keep these fields in the real order they show up in, but idk where this one goes
body?: string;
community_id: number | string; // TODO - Remove this union. Only done to bypass some errors which don't break anything
country_id: number;
created_at: string;
feeling_id: number;
id: string;
is_autopost: 0 | 1;
is_community_private_autopost: 0 | 1;
is_spoiler: 0 | 1;
is_app_jumpable: 0 | 1;
empathy_count: number;
language_id: number;
mii?: string;
mii_face_url?: string;
number: number;
painting?: PostPainting;
pid: number;
platform_id: number;
region_id: number;
reply_count: number;
screen_name: string;
screenshot?: PostScreenshot;
topic_tag?: PostTopicTag;
title_id: string;
};
export type PostPainting = {
format: string;
content: string;
size: number;
url: string;
};
export type PostScreenshot = {
size: number;
url: string;
};
export type PostTopicTag = {
name: string;
title_id: string;
};

View File

@ -0,0 +1,37 @@
import { PostData } from '@/types/miiverse/post';
export type WWPPerson = {
person: {
posts: {
post: PostData;
}[];
}
};
export type WWPTopic = {
empathy_count: number;
has_shop_page: 0 | 1;
icon: string;
title_ids: {
title_id: string;
}[];
title_id: string;
community_id: number;
is_recommended: 0 | 1;
name: string;
people: WWPPerson[];
position: number;
};
export type WWPData = {
result: {
has_error: 0 | 1;
version: 1;
expire: string;
request_name: 'topics';
topics: {
topic: WWPTopic;
}[];
}
};

View File

@ -1,7 +1,5 @@
export interface PostToJSONOptions {
name?: string;
with_mii: boolean;
app_data?: boolean;
topic_tag?: boolean;
topics?: boolean;
}

View File

@ -1,7 +1,7 @@
import { Model, Types, HydratedDocument } from 'mongoose';
import { HydratedCommunityDocument } from '@/types/mongoose/community';
import { PostToJSONOptions } from '@/types/mongoose/post-to-json-options';
import { PostPainting, PostScreenshot } from '@/types/common/post';
import { PostData, PostPainting, PostScreenshot, PostTopicTag } from '@/types/miiverse/post';
export interface IPost {
id: string;
@ -51,7 +51,8 @@ export interface IPostMethods {
cleanedAppData(): string;
formatPainting(): PostPainting | undefined;
formatScreenshot(): PostScreenshot | undefined;
json(options: PostToJSONOptions, community?: HydratedCommunityDocument): Record<string, any>;
formatTopicTag(): PostTopicTag;
json(options: PostToJSONOptions, community?: HydratedCommunityDocument): PostData;
}
interface IPostQueryHelpers {}