merge: Add "force content warning" setting for user moderation (resolves #905) (!876)

View MR for information: https://activitypub.software/TransFem-org/Sharkey/-/merge_requests/876

Closes #905

Approved-by: dakkar <dakkar@thenautilus.net>
Approved-by: Marie <github@yuugi.dev>
This commit is contained in:
dakkar 2025-02-20 10:20:49 +00:00
commit 534c35cca2
46 changed files with 843 additions and 108 deletions

View file

@ -3,6 +3,8 @@
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { IdService } from '@/core/IdService.js';
process.env.NODE_ENV = 'test';
import * as assert from 'assert';
@ -20,7 +22,7 @@ import { CoreModule } from '@/core/CoreModule.js';
import { FederatedInstanceService } from '@/core/FederatedInstanceService.js';
import { LoggerService } from '@/core/LoggerService.js';
import type { IActor, IApDocument, ICollection, IObject, IPost } from '@/core/activitypub/type.js';
import { MiMeta, MiNote, UserProfilesRepository } from '@/models/_.js';
import { MiMeta, MiNote, MiUser, UserProfilesRepository } from '@/models/_.js';
import { DI } from '@/di-symbols.js';
import { secureRndstr } from '@/misc/secure-rndstr.js';
import { DownloadService } from '@/core/DownloadService.js';
@ -93,6 +95,7 @@ describe('ActivityPub', () => {
let rendererService: ApRendererService;
let jsonLdService: JsonLdService;
let resolver: MockResolver;
let idService: IdService;
const metaInitial = {
cacheRemoteFiles: true,
@ -140,6 +143,7 @@ describe('ActivityPub', () => {
imageService = app.get<ApImageService>(ApImageService);
jsonLdService = app.get<JsonLdService>(JsonLdService);
resolver = new MockResolver(await app.resolve<LoggerService>(LoggerService));
idService = app.get<IdService>(IdService);
// Prevent ApPersonService from fetching instance, as it causes Jest import-after-test error
const federatedInstanceService = app.get<FederatedInstanceService>(FederatedInstanceService);
@ -477,4 +481,143 @@ describe('ActivityPub', () => {
});
});
});
describe(ApRendererService, () => {
let note: MiNote;
let author: MiUser;
beforeEach(() => {
author = new MiUser({
id: idService.gen(),
});
note = new MiNote({
id: idService.gen(),
userId: author.id,
visibility: 'public',
localOnly: false,
text: 'Note text',
cw: null,
renoteCount: 0,
repliesCount: 0,
clippedCount: 0,
reactions: {},
fileIds: [],
attachedFileTypes: [],
visibleUserIds: [],
mentions: [],
// This is fucked tbh - it's JSON stored in a TEXT column that gets parsed/serialized all over the place
mentionedRemoteUsers: '[]',
reactionAndUserPairCache: [],
emojis: [],
tags: [],
hasPoll: false,
});
});
describe('renderNote', () => {
describe('summary', () => {
// I actually don't know why it does this, but the logic was already there so I've preserved it.
it('should be zero-width space when CW is empty string', async () => {
note.cw = '';
const result = await rendererService.renderNote(note, author, false);
expect(result.summary).toBe(String.fromCharCode(0x200B));
});
it('should be undefined when CW is null', async () => {
const result = await rendererService.renderNote(note, author, false);
expect(result.summary).toBeUndefined();
});
it('should be CW when present without mandatoryCW', async () => {
note.cw = 'original';
const result = await rendererService.renderNote(note, author, false);
expect(result.summary).toBe('original');
});
it('should be mandatoryCW when present without CW', async () => {
author.mandatoryCW = 'mandatory';
const result = await rendererService.renderNote(note, author, false);
expect(result.summary).toBe('mandatory');
});
it('should be merged when CW and mandatoryCW are both present', async () => {
note.cw = 'original';
author.mandatoryCW = 'mandatory';
const result = await rendererService.renderNote(note, author, false);
expect(result.summary).toBe('original, mandatory');
});
it('should be CW when CW includes mandatoryCW', async () => {
note.cw = 'original and mandatory';
author.mandatoryCW = 'mandatory';
const result = await rendererService.renderNote(note, author, false);
expect(result.summary).toBe('original and mandatory');
});
});
});
describe('renderUpnote', () => {
describe('summary', () => {
// I actually don't know why it does this, but the logic was already there so I've preserved it.
it('should be zero-width space when CW is empty string', async () => {
note.cw = '';
const result = await rendererService.renderUpNote(note, author, false);
expect(result.summary).toBe(String.fromCharCode(0x200B));
});
it('should be undefined when CW is null', async () => {
const result = await rendererService.renderUpNote(note, author, false);
expect(result.summary).toBeUndefined();
});
it('should be CW when present without mandatoryCW', async () => {
note.cw = 'original';
const result = await rendererService.renderUpNote(note, author, false);
expect(result.summary).toBe('original');
});
it('should be mandatoryCW when present without CW', async () => {
author.mandatoryCW = 'mandatory';
const result = await rendererService.renderUpNote(note, author, false);
expect(result.summary).toBe('mandatory');
});
it('should be merged when CW and mandatoryCW are both present', async () => {
note.cw = 'original';
author.mandatoryCW = 'mandatory';
const result = await rendererService.renderUpNote(note, author, false);
expect(result.summary).toBe('original, mandatory');
});
it('should be CW when CW includes mandatoryCW', async () => {
note.cw = 'original and mandatory';
author.mandatoryCW = 'mandatory';
const result = await rendererService.renderUpNote(note, author, false);
expect(result.summary).toBe('original and mandatory');
});
});
});
});
});

View file

@ -0,0 +1,92 @@
/*
* SPDX-FileCopyrightText: hazelnoot and other Sharkey contributors
* SPDX-License-Identifier: AGPL-3.0-only
*/
import { appendContentWarning } from '@/misc/append-content-warning.js';
describe(appendContentWarning, () => {
it('should return additional when original is null', () => {
const result = appendContentWarning(null, 'additional');
expect(result).toBe('additional');
});
it('should return additional when original is undefined', () => {
const result = appendContentWarning(undefined, 'additional');
expect(result).toBe('additional');
});
it('should return additional when original is empty', () => {
const result = appendContentWarning('', 'additional');
expect(result).toBe('additional');
});
it('should return original when additional is empty', () => {
const result = appendContentWarning('original', '');
expect(result).toBe('original');
});
it('should append additional when it does not exist in original', () => {
const result = appendContentWarning('original', 'additional');
expect(result).toBe('original, additional');
});
it('should append additional when it exists in original but has preceeding word', () => {
const result = appendContentWarning('notadditional', 'additional');
expect(result).toBe('notadditional, additional');
});
it('should append additional when it exists in original but has following word', () => {
const result = appendContentWarning('additionalnot', 'additional');
expect(result).toBe('additionalnot, additional');
});
it('should append additional when it exists in original multiple times but has preceeding or following word', () => {
const result = appendContentWarning('notadditional additionalnot', 'additional');
expect(result).toBe('notadditional additionalnot, additional');
});
it('should not append additional when it exists in original', () => {
const result = appendContentWarning('an additional word', 'additional');
expect(result).toBe('an additional word');
});
it('should not append additional when original starts with it', () => {
const result = appendContentWarning('additional word', 'additional');
expect(result).toBe('additional word');
});
it('should not append additional when original ends with it', () => {
const result = appendContentWarning('an additional', 'additional');
expect(result).toBe('an additional');
});
it('should not append additional when it appears multiple times', () => {
const result = appendContentWarning('an additional additional word', 'additional');
expect(result).toBe('an additional additional word');
});
it('should not append additional when it appears multiple times but some have preceeding or following', () => {
const result = appendContentWarning('a notadditional additional additionalnot word', 'additional');
expect(result).toBe('a notadditional additional additionalnot word');
});
it('should prepend additional when reverse is true', () => {
const result = appendContentWarning('original', 'additional', true);
expect(result).toBe('additional, original');
});
});