mirror of
https://github.com/PretendoNetwork/miiverse-api.git
synced 2026-09-08 16:45:13 -05:00
Added permissions for communities. Added additional checks for making new posts and new communities to support this change. Update regex to support more languages.
This commit is contained in:
@@ -1,6 +1,25 @@
|
||||
import { Schema, model } from 'mongoose';
|
||||
import { CommunityData } from '@/types/miiverse/community';
|
||||
import { ICommunity, ICommunityMethods, CommunityModel, HydratedCommunityDocument } from '@/types/mongoose/community';
|
||||
import { ICommunity, ICommunityMethods, CommunityModel, ICommunityPermissions, HydratedCommunityDocument } from '@/types/mongoose/community';
|
||||
|
||||
const PermissionsSchema = new Schema<ICommunityPermissions>({
|
||||
open: {
|
||||
type: Boolean,
|
||||
default: true
|
||||
},
|
||||
minimum_new_post_access_level: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
minimum_new_comment_access_level: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
minimum_new_community_access_level: {
|
||||
type: Number,
|
||||
default: 0
|
||||
},
|
||||
});
|
||||
|
||||
const CommunitySchema = new Schema<ICommunity, CommunityModel, ICommunityMethods>({
|
||||
platform_id: Number,
|
||||
@@ -68,7 +87,8 @@ const CommunitySchema = new Schema<ICommunity, CommunityModel, ICommunityMethods
|
||||
user_favorites: {
|
||||
type: [Number],
|
||||
default: []
|
||||
}
|
||||
},
|
||||
permissions: PermissionsSchema
|
||||
});
|
||||
|
||||
CommunitySchema.method<HydratedCommunityDocument>('addUserFavorite', async function addUserFavorite(pid: number): Promise<void> {
|
||||
|
||||
@@ -107,7 +107,7 @@ PostSchema.method<HydratedPostDocument>('generatePostUID', async function genera
|
||||
});
|
||||
|
||||
PostSchema.method<HydratedPostDocument>('cleanedBody', function cleanedBody(): string {
|
||||
return this.body ? this.body.replace(/[^A-Za-z\d\s-_!@#$%^&*(){}+=,.<>/?;:'"[\]]/g, '').replace(/[\n\r]+/gm, '') : '';
|
||||
return this.body ? this.body.replace(/[\p{L}\p{P}\d$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<>]/g, '').replace(/[\n\r]+/gm, '') : '';
|
||||
});
|
||||
|
||||
PostSchema.method<HydratedPostDocument>('cleanedMiiData', function cleanedMiiData(): string {
|
||||
|
||||
@@ -8,10 +8,11 @@ import {
|
||||
getCommunityByTitleID,
|
||||
getUserContent,
|
||||
} from '@/database';
|
||||
import { getValueFromQueryString } from '@/util';
|
||||
import { getValueFromQueryString, getUserAccountData } from '@/util';
|
||||
import { LOG_WARN } from '@/logger';
|
||||
import { Community } from '@/models/community';
|
||||
import { Post } from '@/models/post';
|
||||
import { GetUserDataResponse } from '@pretendonetwork/grpc/account/get_user_data_rpc';
|
||||
import { HydratedCommunityDocument } from '@/types/mongoose/community';
|
||||
import { SubCommunityQuery } from '@/types/mongoose/subcommunity-query';
|
||||
import { CommunityPostsQuery } from '@/types/mongoose/community-posts-query';
|
||||
@@ -268,6 +269,29 @@ router.post('/', multer().none(), async function (request: express.Request, resp
|
||||
return respondCommunityError(response, 400, 20);
|
||||
}
|
||||
|
||||
let pnid: GetUserDataResponse;
|
||||
|
||||
try {
|
||||
pnid = await getUserAccountData(request.pid);
|
||||
} catch (error) {
|
||||
// TODO - Log this error
|
||||
response.sendStatus(403);
|
||||
return;
|
||||
}
|
||||
|
||||
if (pnid.accessLevel < parentCommunity.permissions.minimum_new_community_access_level) {
|
||||
response.send(xmlbuilder.create({
|
||||
result: {
|
||||
has_error: '1',
|
||||
version: '1',
|
||||
code: '403',
|
||||
error_code: '911',
|
||||
message: 'NO_NEW_COMMUNITY'
|
||||
}
|
||||
}).end({ pretty: true, allowEmpty: true }));
|
||||
return;
|
||||
}
|
||||
|
||||
request.body.name = request.body.name.trim();
|
||||
request.body.icon = request.body.icon.trim();
|
||||
|
||||
|
||||
@@ -160,7 +160,7 @@ router.post('/', upload.none(), async function (request: express.Request, respon
|
||||
}
|
||||
|
||||
if (messageBody) {
|
||||
messageBody = messageBody.replace(/[^A-Za-z\d\s-_!@#$%^&*(){}‛¨ƒºª«»“”„¿¡←→↑↓√§¶†‡¦–—⇒⇔¤¢€£¥™©®+×÷=±∞ˇ˘˙¸˛˜′″µ°¹²³♭♪•…¬¯‰¼½¾♡♥●◆■▲▼☆★♀♂,./?;:'"\\<>]/g, '');
|
||||
messageBody = messageBody.replace(/[\p{L}\p{P}\d$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<>]/g, '');
|
||||
}
|
||||
|
||||
if (messageBody.length > 280) {
|
||||
|
||||
@@ -288,10 +288,10 @@ async function newPost(request: express.Request, response: express.Response): Pr
|
||||
}
|
||||
|
||||
// TODO - Clean this up
|
||||
// * Nesting this because of how manu checks there are, extremely unreadable otherwise
|
||||
// * Nesting this because of how many checks there are, extremely unreadable otherwise
|
||||
if (!(community.admins && community.admins.indexOf(request.pid) !== -1 && userSettings.account_status === 0)) {
|
||||
if (community.type >= 2) {
|
||||
if (!(parentPost && community.allows_comments && community.open)) {
|
||||
if (community.type >= 2 || user.accessLevel < community.permissions.minimum_new_post_access_level) {
|
||||
if (!(parentPost && user.accessLevel >= community.permissions.minimum_new_comment_access_level && community.permissions.open)) {
|
||||
response.sendStatus(403);
|
||||
return;
|
||||
}
|
||||
@@ -318,7 +318,7 @@ async function newPost(request: express.Request, response: express.Response): Pr
|
||||
}
|
||||
|
||||
if (messageBody) {
|
||||
messageBody = messageBody.replace(/[^A-Za-z\d\s-_!@#$%^&*(){}‛¨ƒºª«»“”„¿¡←→↑↓√§¶†‡¦–—⇒⇔¤¢€£¥™©®+×÷=±∞ˇ˘˙¸˛˜′″µ°¹²³♭♪•…¬¯‰¼½¾♡♥●◆■▲▼☆★♀♂,./?;:'"\\<>]/g, '');
|
||||
messageBody = messageBody.replace(/[\p{L}\p{P}\d$^¨←→↑↓√¦⇒⇔¤¢€£¥™©®+×÷=±∞˘˙¸˛˜°¹²³♭♪¬¯¼½¾♡♥●◆■▲▼☆★♀♂<>]/g, '');
|
||||
}
|
||||
|
||||
if (messageBody && messageBody.length > 280) {
|
||||
|
||||
@@ -8,6 +8,13 @@ enum COMMUNITY_TYPE {
|
||||
Private = 3
|
||||
}
|
||||
|
||||
export interface ICommunityPermissions {
|
||||
open: boolean;
|
||||
minimum_new_post_access_level: number;
|
||||
minimum_new_comment_access_level: number;
|
||||
minimum_new_community_access_level: number;
|
||||
}
|
||||
|
||||
export interface ICommunity {
|
||||
platform_id: number;
|
||||
name: string;
|
||||
@@ -30,6 +37,7 @@ export interface ICommunity {
|
||||
is_recommended: number;
|
||||
app_data: string;
|
||||
user_favorites: Types.Array<number>;
|
||||
permissions: ICommunityPermissions
|
||||
}
|
||||
|
||||
export interface ICommunityMethods {
|
||||
|
||||
Reference in New Issue
Block a user