summaryrefslogtreecommitdiff
path: root/packages/backend/src/server/api/endpoints/notes
diff options
context:
space:
mode:
authorJulia <julia@insertdomain.name>2024-10-29 03:04:25 +0000
committerJulia <julia@insertdomain.name>2024-10-29 03:04:25 +0000
commit1520bc1715cd974faa9c20ae5caeceb16a4c0b8e (patch)
treea72cd1371e1c662026b92fa8e631859afed8dbbb /packages/backend/src/server/api/endpoints/notes
parentmerge: Collapse user activity, files, and listenbrainz on mobile (resolves #7... (diff)
parentfix poll option limit in masto API (diff)
downloadsharkey-1520bc1715cd974faa9c20ae5caeceb16a4c0b8e.tar.gz
sharkey-1520bc1715cd974faa9c20ae5caeceb16a4c0b8e.tar.bz2
sharkey-1520bc1715cd974faa9c20ae5caeceb16a4c0b8e.zip
merge: Split character limits between local and remote notes (resolves #723) (!669)
View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/669 Closes #723 Approved-by: dakkar <dakkar@thenautilus.net> Approved-by: Julia <julia@insertdomain.name>
Diffstat (limited to 'packages/backend/src/server/api/endpoints/notes')
-rw-r--r--packages/backend/src/server/api/endpoints/notes/create.test.ts20
-rw-r--r--packages/backend/src/server/api/endpoints/notes/create.ts14
-rw-r--r--packages/backend/src/server/api/endpoints/notes/edit.ts14
3 files changed, 33 insertions, 15 deletions
diff --git a/packages/backend/src/server/api/endpoints/notes/create.test.ts b/packages/backend/src/server/api/endpoints/notes/create.test.ts
index f3d887bb20..18d80e867b 100644
--- a/packages/backend/src/server/api/endpoints/notes/create.test.ts
+++ b/packages/backend/src/server/api/endpoints/notes/create.test.ts
@@ -5,15 +5,12 @@
process.env.NODE_ENV = 'test';
-import { readFile } from 'node:fs/promises';
-import { fileURLToPath } from 'node:url';
-import { dirname } from 'node:path';
import { describe, test, expect } from '@jest/globals';
+import { loadConfig } from '@/config.js';
import { getValidator } from '../../../../../test/prelude/get-api-validator.js';
import { paramDef } from './create.js';
-const _filename = fileURLToPath(import.meta.url);
-const _dirname = dirname(_filename);
+const config = loadConfig();
const VALID = true;
const INVALID = false;
@@ -21,7 +18,12 @@ const INVALID = false;
describe('api:notes/create', () => {
describe('validation', () => {
const v = getValidator(paramDef);
- const tooLong = readFile(_dirname + '/../../../../../test/resources/misskey.svg', 'utf-8');
+ const tooLong = (limit: number) => {
+ const arr: string[] = [''];
+ arr.length = limit + 1;
+ arr.fill('a');
+ return arr.join('');
+ };
test('reject empty', () => {
const valid = v({ });
@@ -71,8 +73,8 @@ describe('api:notes/create', () => {
.toBe(INVALID);
});
- test('over 500 characters cw', async () => {
- expect(v({ text: 'Body', cw: await tooLong }))
+ test('over max characters cw', async () => {
+ expect(v({ text: '', cw: tooLong(config.maxNoteLength) }))
.toBe(INVALID);
});
});
@@ -220,7 +222,7 @@ describe('api:notes/create', () => {
});
test('reject poll with too long choice', async () => {
- expect(v({ poll: { choices: [await tooLong, '2'] } }))
+ expect(v({ poll: { choices: [tooLong(config.maxNoteLength), '2'] } }))
.toBe(INVALID);
});
diff --git a/packages/backend/src/server/api/endpoints/notes/create.ts b/packages/backend/src/server/api/endpoints/notes/create.ts
index 412491afaa..d1cf0123dc 100644
--- a/packages/backend/src/server/api/endpoints/notes/create.ts
+++ b/packages/backend/src/server/api/endpoints/notes/create.ts
@@ -90,6 +90,12 @@ export const meta = {
id: '3ac74a84-8fd5-4bb0-870f-01804f82ce16',
},
+ maxCwLength: {
+ message: 'You tried posting a content warning which is too long.',
+ code: 'MAX_CW_LENGTH',
+ id: '7004c478-bda3-4b4f-acb2-4316398c9d52',
+ },
+
cannotReplyToSpecifiedVisibilityNoteWithExtendedVisibility: {
message: 'You cannot reply to a specified visibility note with extended visibility.',
code: 'CANNOT_REPLY_TO_SPECIFIED_VISIBILITY_NOTE_WITH_EXTENDED_VISIBILITY',
@@ -147,7 +153,7 @@ export const paramDef = {
visibleUserIds: { type: 'array', uniqueItems: true, items: {
type: 'string', format: 'misskey:id',
} },
- cw: { type: 'string', nullable: true, minLength: 1, maxLength: 500 },
+ cw: { type: 'string', nullable: true, minLength: 1 },
localOnly: { type: 'boolean', default: false },
reactionAcceptance: { type: 'string', nullable: true, enum: [null, 'likeOnly', 'likeOnlyForRemote', 'nonSensitiveOnly', 'nonSensitiveOnlyForLocalLikeOnlyForRemote'], default: null },
noExtractMentions: { type: 'boolean', default: false },
@@ -250,10 +256,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private noteCreateService: NoteCreateService,
) {
super(meta, paramDef, async (ps, me) => {
- const contentLength = (ps.text?.length ?? 0) + (ps.cw?.length ?? 0);
- if (contentLength > this.config.maxNoteLength) {
+ if (ps.text && ps.text.length > this.config.maxNoteLength) {
throw new ApiError(meta.errors.maxLength);
}
+ if (ps.cw && ps.cw.length > this.config.maxCwLength) {
+ throw new ApiError(meta.errors.maxCwLength);
+ }
let visibleUsers: MiUser[] = [];
if (ps.visibleUserIds) {
diff --git a/packages/backend/src/server/api/endpoints/notes/edit.ts b/packages/backend/src/server/api/endpoints/notes/edit.ts
index b9be145caf..dc94c78e75 100644
--- a/packages/backend/src/server/api/endpoints/notes/edit.ts
+++ b/packages/backend/src/server/api/endpoints/notes/edit.ts
@@ -86,6 +86,12 @@ export const meta = {
id: '3ac74a84-8fd5-4bb0-870f-01804f82ce16',
},
+ maxCwLength: {
+ message: 'You tried posting a content warning which is too long.',
+ code: 'MAX_CW_LENGTH',
+ id: '7004c478-bda3-4b4f-acb2-4316398c9d52',
+ },
+
cannotReplyToSpecifiedVisibilityNoteWithExtendedVisibility: {
message: 'You cannot reply to a specified visibility note with extended visibility.',
code: 'CANNOT_REPLY_TO_SPECIFIED_VISIBILITY_NOTE_WITH_EXTENDED_VISIBILITY',
@@ -197,7 +203,7 @@ export const paramDef = {
format: 'misskey:id',
},
},
- cw: { type: 'string', nullable: true, minLength: 1, maxLength: 500 },
+ cw: { type: 'string', nullable: true, minLength: 1 },
localOnly: { type: 'boolean', default: false },
reactionAcceptance: { type: 'string', nullable: true, enum: [null, 'likeOnly', 'likeOnlyForRemote', 'nonSensitiveOnly', 'nonSensitiveOnlyForLocalLikeOnlyForRemote'], default: null },
noExtractMentions: { type: 'boolean', default: false },
@@ -297,10 +303,12 @@ export default class extends Endpoint<typeof meta, typeof paramDef> { // eslint-
private noteEditService: NoteEditService,
) {
super(meta, paramDef, async (ps, me) => {
- const contentLength = (ps.text?.length ?? 0) + (ps.cw?.length ?? 0);
- if (contentLength > this.config.maxNoteLength) {
+ if (ps.text && ps.text.length > this.config.maxNoteLength) {
throw new ApiError(meta.errors.maxLength);
}
+ if (ps.cw && ps.cw.length > this.config.maxCwLength) {
+ throw new ApiError(meta.errors.maxCwLength);
+ }
let visibleUsers: MiUser[] = [];
if (ps.visibleUserIds) {