Skip to content

Commit

Permalink
Merge pull request #305 from askmeaboutlo0m/default-content-warnings
Browse files Browse the repository at this point in the history
Make content warnings part of the default section
  • Loading branch information
mvdicarlo authored Jul 24, 2024
2 parents 7d8fd79 + c096b06 commit baee24d
Show file tree
Hide file tree
Showing 35 changed files with 254 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ export interface DefaultOptions {
tags: TagData;
description: DescriptionData;
rating?: SubmissionRating | string;
spoilerText?: string;
sources: string[];
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,5 @@ export interface ItakuFileOptions extends DefaultFileOptions {
visibility: string;
shareOnFeed: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,4 +4,5 @@ export interface ItakuNotificationOptions extends DefaultOptions {
folders: string[];
visibility: string;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultFileOptions } from '../../submission/default-options.interface';
export interface MastodonFileOptions extends DefaultFileOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
altText?: string;
replyToUrl?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultOptions } from '../../submission/default-options.interface';
export interface MastodonNotificationOptions extends DefaultOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
replyToUrl?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultFileOptions } from '../../submission/default-options.interface';
export interface MissKeyFileOptions extends DefaultFileOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
altText?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,6 @@ import { DefaultOptions } from '../../submission/default-options.interface';
export interface MissKeyNotificationOptions extends DefaultOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultFileOptions } from '../../submission/default-options.interface';
export interface PixelfedFileOptions extends DefaultFileOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
altText?: string;
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultFileOptions } from '../../submission/default-options.interface';
export interface PleromaFileOptions extends DefaultFileOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
altText?: string;
replyToUrl?: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { DefaultOptions } from '../../submission/default-options.interface';
export interface PleromaNotificationOptions extends DefaultOptions {
useTitle: boolean;
spoilerText?: string;
spoilerTextOverwrite?: boolean;
visibility: string;
replyToUrl?: string;
}
4 changes: 4 additions & 0 deletions commons/src/models/default-options.entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,10 @@ export class DefaultOptionsEntity implements DefaultOptions {
@IsOptional()
rating?: SubmissionRating | string;

@Expose()
@IsOptional()
spoilerText?: string;

@IsArray()
sources: string[];

Expand Down
5 changes: 5 additions & 0 deletions commons/src/websites/itaku/itaku.file.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@ export class ItakuFileOptionsEntity extends DefaultFileOptionsEntity implements
@IsOptional()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

constructor(entity?: Partial<ItakuFileOptions>) {
super(entity as DefaultFileOptions);
}
Expand Down
7 changes: 6 additions & 1 deletion commons/src/websites/itaku/itaku.notification.options.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Expose } from 'class-transformer';
import { IsArray, IsString, IsOptional } from 'class-validator';
import { IsArray, IsString, IsOptional, IsBoolean } from 'class-validator';
import { DefaultOptions } from '../../interfaces/submission/default-options.interface';
import { ItakuNotificationOptions } from '../../interfaces/websites/itaku/itaku.notification.options.interface';
import { DefaultValue } from '../../models/decorators/default-value.decorator';
Expand All @@ -24,6 +24,11 @@ export class ItakuNotificationOptionsEntity
@IsOptional()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

constructor(entity?: Partial<ItakuNotificationOptions>) {
super(entity as DefaultOptions);
}
Expand Down
5 changes: 5 additions & 0 deletions commons/src/websites/mastodon/mastodon.file.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class MastodonFileOptionsEntity
@IsString()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class MastodonNotificationOptionsEntity
@IsString()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
5 changes: 5 additions & 0 deletions commons/src/websites/misskey/misskey.file.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class MissKeyFileOptionsEntity
@IsString()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
5 changes: 5 additions & 0 deletions commons/src/websites/misskey/misskey.notification.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class MissKeyNotificationOptionsEntity
@IsString()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
5 changes: 5 additions & 0 deletions commons/src/websites/pixelfed/pixelfed.file.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ export class PixelfedFileOptionsEntity
@IsString()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
7 changes: 6 additions & 1 deletion commons/src/websites/pleroma/pleroma.file.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,12 @@ export class PleromaFileOptionsEntity extends DefaultFileOptionsEntity
@IsOptional()
@IsString()
spoilerText?: string;


@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
5 changes: 5 additions & 0 deletions commons/src/websites/pleroma/pleroma.notification.options.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ export class PleromaNotificationOptionsEntity extends DefaultOptionsEntity
@IsString()
spoilerText?: string;

@Expose()
@IsBoolean()
@IsOptional()
spoilerTextOverwrite?: boolean;

@Expose()
@IsString()
@DefaultValue('public')
Expand Down
17 changes: 17 additions & 0 deletions electron-app/src/server/submission/parser/parser.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export class ParserService {
submission,
tags,
title: this.getTitle(submission, defaultPart, websitePart),
spoilerText: this.getSpoilerText(defaultPart, websitePart),
};

if (this.isFileSubmission(submission)) {
Expand Down Expand Up @@ -127,6 +128,22 @@ export class ParserService {
return (websitePart.data.title || defaultPart.data.title || submission.title).substring(0, 160);
}

private getSpoilerText(
defaultPart: SubmissionPartEntity<DefaultOptions>,
websitePart: SubmissionPartEntity<any>,
): string {
const overwrite = websitePart.data.spoilerTextOverwrite;
const defaultSpoilerText = defaultPart.data.spoilerText || '';
const websiteSpoilerText = `${websitePart.data.spoilerText || ''}`;
if (overwrite === undefined) {
return websiteSpoilerText.trim() === '' ? defaultSpoilerText : websiteSpoilerText;
} else if (overwrite) {
return websiteSpoilerText;
} else {
return defaultSpoilerText;
}
}

private isFileSubmission(submission: Submission): submission is FileSubmission {
return submission instanceof FileSubmissionEntity;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ export class DescriptionParser {
).trim();

if (description.length) {
// Insert {default}, {title}, {tags} shortcuts
// Insert {default}, {title}, {tags}, {cw} shortcuts
let tags = await this.parserService.parseTags(website, defaultPart, websitePart);
description = this.insertDefaultShortcuts(description, [
{
Expand All @@ -68,6 +68,10 @@ export class DescriptionParser {
name: 'tags',
content: website.generateTagsString(tags, description, websitePart),
},
{
name: 'cw',
content: FormContent.getSpoilerText(defaultPart.data, websitePart.data),
},
]);

// Parse all potential shortcut data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ export interface PostData<T extends Submission, K extends DefaultOptions> {
submission: T;
tags: string[];
title: string;
spoilerText: string;
}
16 changes: 16 additions & 0 deletions electron-app/src/server/utils/form-content.util.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,20 @@ export default class FormContent {
? _.get(websiteDescription, 'value', '')
: _.get(defaultDescription, 'value', '');
}

static getSpoilerText(
defaultData: { spoilerText?: string },
partData: { spoilerText?: string; spoilerTextOverwrite?: boolean },
): string {
const partSpoilerText = partData.spoilerText || '';
const overwrite =
partData.spoilerTextOverwrite === undefined
? partSpoilerText.trim() !== ''
: partData.spoilerTextOverwrite;
if (overwrite) {
return partSpoilerText;
} else {
return defaultData.spoilerText || '';
}
}
}
11 changes: 8 additions & 3 deletions electron-app/src/server/websites/itaku/itaku.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -174,8 +174,8 @@ export class Itaku extends Website {
postData.add_to_feed = 'true';
}

if (data.options.spoilerText) {
postData.content_warning = data.options.spoilerText;
if (data.spoilerText) {
postData.content_warning = data.spoilerText;
}

if (fileRecord.type === FileSubmissionType.IMAGE) {
Expand Down Expand Up @@ -256,7 +256,7 @@ export class Itaku extends Website {

validateFileSubmission(
submission: FileSubmission,
submissionPart: SubmissionPart<any>,
submissionPart: SubmissionPart<ItakuFileOptions>,
defaultPart: SubmissionPart<DefaultOptions>,
): ValidationParts {
const problems: string[] = [];
Expand Down Expand Up @@ -290,6 +290,11 @@ export class Itaku extends Website {
problems.push(`Posting multiple images requires share on feed to be enabled`);
}

const spoilerText = FormContent.getSpoilerText(defaultPart.data, submissionPart.data);
if (spoilerText.length > 30) {
problems.push(`Max content warning length allowed is 30 characters`);
}

return { problems, warnings };
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -142,8 +142,8 @@ export abstract class Megalodon extends Website {
statusOptions.in_reply_to_id = replyToId;
}

if (data.options.spoilerText) {
statusOptions.spoiler_text = data.options.spoilerText;
if (data.spoilerText) {
statusOptions.spoiler_text = data.spoilerText;
}

// Mastodon may return a 422 error if the media is still processing,
Expand Down Expand Up @@ -200,8 +200,8 @@ export abstract class Megalodon extends Website {
let status = `${data.options.useTitle && data.title ? `${data.title}\n` : ''}${
data.description
}`;
if (data.options.spoilerText) {
statusOptions.spoiler_text = data.options.spoilerText;
if (data.spoilerText) {
statusOptions.spoiler_text = data.spoilerText;
}

const replyToId = this.getPostIdFromUrl(data.options.replyToUrl);
Expand Down
8 changes: 4 additions & 4 deletions electron-app/src/server/websites/misskey/misskey.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,8 +155,8 @@ export class MissKey extends Website {
if (i !== 0) {
statusOptions.in_reply_to_id = lastId;
}
if (data.options.spoilerText) {
statusOptions.spoiler_text = data.options.spoilerText;
if (data.spoilerText) {
statusOptions.spoiler_text = data.spoilerText;
}

this.checkCancelled(cancellationToken);
Expand Down Expand Up @@ -196,8 +196,8 @@ export class MissKey extends Website {
let status = `${data.options.useTitle && data.title ? `${data.title}\n` : ''}${
data.description
}`.substring(0, maxChars);
if (data.options.spoilerText) {
statusOptions.spoiler_text = data.options.spoilerText;
if (data.spoilerText) {
statusOptions.spoiler_text = data.spoilerText;
}

this.checkCancelled(cancellationToken);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,13 @@ export default class DescriptionInput extends React.Component<Props, State> {
Inserts the website tags or the default tags separated by ' #'
</span>
</li>
<li>
<code>{'{cw}'}</code>
<span className="mx-1">-</span>
<span>
Inserts the content warning
</span>
</li>
</ul>
</div>
}
Expand Down
Loading

0 comments on commit baee24d

Please sign in to comment.