1
0
mirror of https://github.com/NicolasConstant/sengi synced 2025-02-09 00:18:44 +01:00

fix cw analysis issues in thread

This commit is contained in:
Nicolas Constant 2020-04-01 02:29:51 -04:00
parent 4ea7181590
commit 71139eb4c0
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
17 changed files with 187 additions and 91 deletions

View File

@ -81,7 +81,8 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
this.isSending = true; this.isSending = true;
this.mastodonService.getStatus(value.provider, value.status.in_reply_to_id) this.mastodonService.getStatus(value.provider, value.status.in_reply_to_id)
.then((status: Status) => { .then((status: Status) => {
this.statusReplyingToWrapper = new StatusWrapper(status, value.provider); let cwResult = this.toolsService.checkContentWarning(status);
this.statusReplyingToWrapper = new StatusWrapper(cwResult.status, value.provider, cwResult.applyCw, cwResult.hide);
const mentions = this.getMentions(this.statusReplyingToWrapper.status, this.statusReplyingToWrapper.provider); const mentions = this.getMentions(this.statusReplyingToWrapper.status, this.statusReplyingToWrapper.provider);
for (const mention of mentions) { for (const mention of mentions) {
@ -564,7 +565,8 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
}) })
.then((status: Status) => { .then((status: Status) => {
if (this.statusReplyingToWrapper) { if (this.statusReplyingToWrapper) {
this.notificationService.newStatusPosted(this.statusReplyingToWrapper.status.id, new StatusWrapper(status, account)); let cwPolicy = this.toolsService.checkContentWarning(status);
this.notificationService.newStatusPosted(this.statusReplyingToWrapper.status.id, new StatusWrapper(cwPolicy.status, account, cwPolicy.applyCw, cwPolicy.hide));
} }
return status; return status;

View File

@ -1,7 +1,7 @@
import { Component, OnInit, Output, EventEmitter, Input, ViewChild, ElementRef } from '@angular/core'; import { Component, OnInit, Output, EventEmitter, Input, ViewChild, ElementRef } from '@angular/core';
import { StatusWrapper } from '../../../../models/common.model'; import { StatusWrapper } from '../../../../models/common.model';
import { OpenThreadEvent } from '../../../../services/tools.service'; import { OpenThreadEvent, ToolsService } from '../../../../services/tools.service';
import { AccountWrapper } from '../../../../models/account.models'; import { AccountWrapper } from '../../../../models/account.models';
import { FavoriteResult, BookmarkResult } from '../../../../services/mastodon.service'; import { FavoriteResult, BookmarkResult } from '../../../../services/mastodon.service';
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service'; import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
@ -41,8 +41,8 @@ export class BookmarksComponent implements OnInit {
@ViewChild('statusstream') public statustream: ElementRef; @ViewChild('statusstream') public statustream: ElementRef;
constructor( constructor(
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonWrapperService) { } private readonly mastodonService: MastodonWrapperService) { }
@ -63,7 +63,8 @@ export class BookmarksComponent implements OnInit {
.then((result: BookmarkResult) => { .then((result: BookmarkResult) => {
this.maxId = result.max_id; this.maxId = result.max_id;
for (const s of result.bookmarked) { for (const s of result.bookmarked) {
const wrapper = new StatusWrapper(s, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
}) })
@ -99,7 +100,8 @@ export class BookmarksComponent implements OnInit {
this.maxId = result.max_id; this.maxId = result.max_id;
for (const s of statuses) { for (const s of statuses) {
const wrapper = new StatusWrapper(s, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
}) })

View File

@ -2,7 +2,7 @@ import { Component, OnInit, Input, Output, EventEmitter, ViewChild, ElementRef }
import { faUserFriends } from "@fortawesome/free-solid-svg-icons"; import { faUserFriends } from "@fortawesome/free-solid-svg-icons";
import { AccountWrapper } from '../../../../models/account.models'; import { AccountWrapper } from '../../../../models/account.models';
import { OpenThreadEvent } from '../../../../services/tools.service'; import { OpenThreadEvent, ToolsService } from '../../../../services/tools.service';
import { StatusWrapper } from '../../../../models/common.model'; import { StatusWrapper } from '../../../../models/common.model';
import { NotificationService } from '../../../../services/notification.service'; import { NotificationService } from '../../../../services/notification.service';
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service'; import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
@ -42,6 +42,7 @@ export class DirectMessagesComponent implements OnInit {
@ViewChild('statusstream') public statustream: ElementRef; @ViewChild('statusstream') public statustream: ElementRef;
constructor( constructor(
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonWrapperService) { } private readonly mastodonService: MastodonWrapperService) { }
@ -60,7 +61,8 @@ export class DirectMessagesComponent implements OnInit {
this.mastodonService.getConversations(this.account.info) this.mastodonService.getConversations(this.account.info)
.then((conversations: Conversation[]) => { .then((conversations: Conversation[]) => {
for (const c of conversations) { for (const c of conversations) {
const wrapper = new ConversationWrapper(c, this.account.info, this.account.avatar); let cwPolicy = this.toolsService.checkContentWarning(c.last_status);
const wrapper = new ConversationWrapper(c, this.account.info, this.account.avatar, cwPolicy.applyCw, cwPolicy.hide);
this.conversations.push(wrapper); this.conversations.push(wrapper);
} }
}) })
@ -95,7 +97,8 @@ export class DirectMessagesComponent implements OnInit {
} }
for (const c of conversations) { for (const c of conversations) {
const wrapper = new ConversationWrapper(c, this.account.info, this.account.avatar); let cwPolicy = this.toolsService.checkContentWarning(c.last_status);
const wrapper = new ConversationWrapper(c, this.account.info, this.account.avatar, cwPolicy.applyCw, cwPolicy.hide);
this.conversations.push(wrapper); this.conversations.push(wrapper);
} }
}) })
@ -121,13 +124,14 @@ export class DirectMessagesComponent implements OnInit {
} }
class ConversationWrapper { class ConversationWrapper {
constructor( constructor(
public conversation: Conversation, public conversation: Conversation,
public provider: AccountInfo, public provider: AccountInfo,
public userAvatar: string public userAvatar: string,
applyCw: boolean,
hideStatus: boolean
) { ) {
this.lastStatus = new StatusWrapper(conversation.last_status, provider); this.lastStatus = new StatusWrapper(conversation.last_status, provider, applyCw, hideStatus);
} }
lastStatus: StatusWrapper; lastStatus: StatusWrapper;

View File

@ -1,7 +1,7 @@
import { Component, OnInit, Output, EventEmitter, Input, ViewChild, ElementRef } from '@angular/core'; import { Component, OnInit, Output, EventEmitter, Input, ViewChild, ElementRef } from '@angular/core';
import { StatusWrapper } from '../../../../models/common.model'; import { StatusWrapper } from '../../../../models/common.model';
import { OpenThreadEvent } from '../../../../services/tools.service'; import { OpenThreadEvent, ToolsService } from '../../../../services/tools.service';
import { AccountWrapper } from '../../../../models/account.models'; import { AccountWrapper } from '../../../../models/account.models';
import { FavoriteResult } from '../../../../services/mastodon.service'; import { FavoriteResult } from '../../../../services/mastodon.service';
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service'; import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
@ -42,6 +42,7 @@ export class FavoritesComponent implements OnInit {
@ViewChild('statusstream') public statustream: ElementRef; @ViewChild('statusstream') public statustream: ElementRef;
constructor( constructor(
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly mastodonService: MastodonWrapperService) { } private readonly mastodonService: MastodonWrapperService) { }
@ -62,7 +63,8 @@ export class FavoritesComponent implements OnInit {
.then((result: FavoriteResult) => { .then((result: FavoriteResult) => {
this.maxId = result.max_id; this.maxId = result.max_id;
for (const s of result.favorites) { for (const s of result.favorites) {
const wrapper = new StatusWrapper(s, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
}) })
@ -99,7 +101,8 @@ export class FavoritesComponent implements OnInit {
this.maxId = result.max_id; this.maxId = result.max_id;
for (const s of statuses) { for (const s of statuses) {
const wrapper = new StatusWrapper(s, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
}) })

View File

@ -7,7 +7,7 @@ import { StatusWrapper } from '../../../../models/common.model';
import { Status, Notification } from '../../../../services/models/mastodon.interfaces'; import { Status, Notification } from '../../../../services/models/mastodon.interfaces';
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service'; import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
import { NotificationService } from '../../../../services/notification.service'; import { NotificationService } from '../../../../services/notification.service';
import { OpenThreadEvent } from '../../../../services/tools.service'; import { OpenThreadEvent, ToolsService } from '../../../../services/tools.service';
@Component({ @Component({
@ -45,6 +45,7 @@ export class MentionsComponent implements OnInit, OnDestroy {
private lastId: string; private lastId: string;
constructor( constructor(
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly userNotificationService: UserNotificationService, private readonly userNotificationService: UserNotificationService,
private readonly mastodonService: MastodonWrapperService) { private readonly mastodonService: MastodonWrapperService) {
@ -80,7 +81,8 @@ export class MentionsComponent implements OnInit, OnDestroy {
let orderedMentions = [...userNotification.mentions.map(x => x.status)].reverse(); let orderedMentions = [...userNotification.mentions.map(x => x.status)].reverse();
for (let m of orderedMentions) { for (let m of orderedMentions) {
if (!this.statuses.find(x => x.status.id === m.id)) { if (!this.statuses.find(x => x.status.id === m.id)) {
const statusWrapper = new StatusWrapper(m, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(m);
const statusWrapper = new StatusWrapper(cwPolicy.status, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.unshift(statusWrapper); this.statuses.unshift(statusWrapper);
} }
} }
@ -113,7 +115,8 @@ export class MentionsComponent implements OnInit, OnDestroy {
} }
for (const s of statuses) { for (const s of statuses) {
const wrapper = new StatusWrapper(s, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }

View File

@ -40,6 +40,7 @@ export class NotificationsComponent implements OnInit, OnDestroy {
private lastId: string; private lastId: string;
constructor( constructor(
private readonly toolsService: ToolsService,
private readonly notificationService: NotificationService, private readonly notificationService: NotificationService,
private readonly userNotificationService: UserNotificationService, private readonly userNotificationService: UserNotificationService,
private readonly mastodonService: MastodonWrapperService) { } private readonly mastodonService: MastodonWrapperService) { }
@ -72,7 +73,8 @@ export class NotificationsComponent implements OnInit, OnDestroy {
if (userNotification && userNotification.notifications) { if (userNotification && userNotification.notifications) {
let orderedNotifications = [...userNotification.notifications].reverse(); let orderedNotifications = [...userNotification.notifications].reverse();
for (let n of orderedNotifications) { for (let n of orderedNotifications) {
const notificationWrapper = new NotificationWrapper(n, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(n.status);
const notificationWrapper = new NotificationWrapper(n, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
if (!this.notifications.find(x => x.wrapperId === notificationWrapper.wrapperId)) { if (!this.notifications.find(x => x.wrapperId === notificationWrapper.wrapperId)) {
this.notifications.unshift(notificationWrapper); this.notifications.unshift(notificationWrapper);
} }
@ -105,7 +107,8 @@ export class NotificationsComponent implements OnInit, OnDestroy {
} }
for (const s of notifications) { for (const s of notifications) {
const wrapper = new NotificationWrapper(s, this.account.info); let cwPolicy = this.toolsService.checkContentWarning(s.status);
const wrapper = new NotificationWrapper(s, this.account.info, cwPolicy.applyCw, cwPolicy.hide);
this.notifications.push(wrapper); this.notifications.push(wrapper);
} }
@ -133,14 +136,14 @@ export class NotificationsComponent implements OnInit, OnDestroy {
} }
export class NotificationWrapper { export class NotificationWrapper {
constructor(notification: Notification, provider: AccountInfo) { constructor(notification: Notification, provider: AccountInfo, applyCw: boolean, hideStatus: boolean) {
this.type = notification.type; this.type = notification.type;
switch(this.type){ switch(this.type){
case 'mention': case 'mention':
case 'reblog': case 'reblog':
case 'favourite': case 'favourite':
case 'poll': case 'poll':
this.status= new StatusWrapper(notification.status, provider); this.status= new StatusWrapper(notification.status, provider, applyCw, hideStatus);
break; break;
} }
this.account = notification.account; this.account = notification.account;

View File

@ -92,7 +92,8 @@ export class SearchComponent implements OnInit {
this.hashtags = results.hashtags; this.hashtags = results.hashtags;
for (let status of results.statuses) { for (let status of results.statuses) {
const statusWrapper = new StatusWrapper(status, this.lastAccountUsed); let cwPolicy = this.toolsService.checkContentWarning(status);
const statusWrapper = new StatusWrapper(cwPolicy.status, this.lastAccountUsed, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(statusWrapper); this.statuses.push(statusWrapper);
} }
} }

View File

@ -149,9 +149,7 @@ export class ActionBarComponent implements OnInit, OnDestroy {
this.isLocked = false; this.isLocked = false;
} }
if (status.sensitive || status.spoiler_text) { this.isContentWarningActive = this.statusWrapper.applyCw;
this.isContentWarningActive = true;
}
this.checkIfBookmarksAreAvailable(this.selectedAccounts[0]); this.checkIfBookmarksAreAvailable(this.selectedAccounts[0]);
this.checkIfFavorited(); this.checkIfFavorited();

View File

@ -244,7 +244,8 @@ export class StatusUserContextMenuComponent implements OnInit, OnDestroy {
this.navigationService.redraft(this.statusWrapper) this.navigationService.redraft(this.statusWrapper)
} }
const deletedStatus = new StatusWrapper(this.displayedStatus, selectedAccount); let cwPolicy = this.toolsService.checkContentWarning(this.displayedStatus);
const deletedStatus = new StatusWrapper(cwPolicy.status, selectedAccount, cwPolicy.applyCw, cwPolicy.hide);
this.notificationService.deleteStatus(deletedStatus); this.notificationService.deleteStatus(deletedStatus);
}) })
.catch(err => { .catch(err => {

View File

@ -89,7 +89,8 @@ export class PollComponent implements OnInit {
if (accountChanged && !this.pollPerAccountId[newSelectedAccount.id] && (this.statusWrapper.status.visibility === 'public' || this.statusWrapper.status.visibility === 'unlisted')) { if (accountChanged && !this.pollPerAccountId[newSelectedAccount.id] && (this.statusWrapper.status.visibility === 'public' || this.statusWrapper.status.visibility === 'unlisted')) {
this.setStatsAtZero(); this.setStatsAtZero();
this.pollPerAccountId[newSelectedAccount.id] = this.toolsService.getStatusUsableByAccount(newSelectedAccount, new StatusWrapper(this.statusWrapper.status, this.statusWrapper.provider)) let statusWrapper = new StatusWrapper(this.statusWrapper.status, this.statusWrapper.provider, this.statusWrapper.applyCw, this.statusWrapper.hide);
this.pollPerAccountId[newSelectedAccount.id] = this.toolsService.getStatusUsableByAccount(newSelectedAccount, statusWrapper)
.then((status: Status) => { .then((status: Status) => {
if(!status || !(status.poll)) return null; if(!status || !(status.poll)) return null;
return this.mastodonService.getPoll(newSelectedAccount, status.poll.id); return this.mastodonService.getPoll(newSelectedAccount, status.poll.id);

View File

@ -70,10 +70,11 @@ export class StatusComponent implements OnInit {
} }
this.isDirectMessage = this.displayedStatus.visibility === 'direct'; this.isDirectMessage = this.displayedStatus.visibility === 'direct';
this.displayedStatusWrapper = new StatusWrapper(this.displayedStatus, value.provider); let cwPolicy = this.toolsService.checkContentWarning(this.displayedStatus);
this.displayedStatusWrapper = new StatusWrapper(cwPolicy.status, value.provider, cwPolicy.applyCw, cwPolicy.hide);
this.checkLabels(this.displayedStatus); this.checkLabels(this.displayedStatus);
this.checkContentWarning(this.displayedStatus); this.setContentWarning(this.displayedStatusWrapper);
if (!this.displayedStatus.account.display_name) { if (!this.displayedStatus.account.display_name) {
this.displayedStatus.account.display_name = this.displayedStatus.account.username; this.displayedStatus.account.display_name = this.displayedStatus.account.username;
@ -98,53 +99,53 @@ export class StatusComponent implements OnInit {
ngOnInit() { ngOnInit() {
} }
private checkContentWarning(status: Status) { // private checkContentWarning(status: Status) {
let cwPolicy = this.toolsService.getSettings().contentWarningPolicy; // let cwPolicy = this.toolsService.getSettings().contentWarningPolicy;
let splittedContent = []; // let splittedContent = [];
if ((cwPolicy.policy === ContentWarningPolicyEnum.HideAll && cwPolicy.addCwOnContent.length > 0) // if ((cwPolicy.policy === ContentWarningPolicyEnum.HideAll && cwPolicy.addCwOnContent.length > 0)
|| (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent && cwPolicy.removeCwOnContent.length > 0) // || (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent && cwPolicy.removeCwOnContent.length > 0)
|| (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0)) { // || (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0)) {
let parser = new DOMParser(); // let parser = new DOMParser();
let dom = parser.parseFromString((status.content + ' ' + status.spoiler_text).replace("<br/>", " ").replace("<br>", " ").replace(/\n/g, ' '), 'text/html') // let dom = parser.parseFromString((status.content + ' ' + status.spoiler_text).replace("<br/>", " ").replace("<br>", " ").replace(/\n/g, ' '), 'text/html')
let contentToParse = dom.body.textContent; // let contentToParse = dom.body.textContent;
splittedContent = contentToParse.toLowerCase().split(' '); // splittedContent = contentToParse.toLowerCase().split(' ');
} // }
if (cwPolicy.policy === ContentWarningPolicyEnum.None && (status.sensitive || status.spoiler_text)) { // if (cwPolicy.policy === ContentWarningPolicyEnum.None && (status.sensitive || status.spoiler_text)) {
this.setContentWarning(status); // this.setContentWarning(status);
} else if (cwPolicy.policy === ContentWarningPolicyEnum.HideAll) { // } else if (cwPolicy.policy === ContentWarningPolicyEnum.HideAll) {
let detected = cwPolicy.addCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`)); // let detected = cwPolicy.addCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
if (!detected || detected.length === 0) { // if (!detected || detected.length === 0) {
this.status.sensitive = false; // this.status.sensitive = false;
} else { // } else {
if (!status.spoiler_text) { // if (!status.spoiler_text) {
status.spoiler_text = detected.join(' '); // status.spoiler_text = detected.join(' ');
} // }
this.setContentWarning(status); // this.setContentWarning(status);
} // }
} else if (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent) { // } else if (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent) {
let detected = cwPolicy.removeCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`)); // let detected = cwPolicy.removeCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
if (detected && detected.length > 0) { // if (detected && detected.length > 0) {
this.status.sensitive = false; // this.status.sensitive = false;
} else { // } else {
this.setContentWarning(status); // this.setContentWarning(status);
} // }
} // }
if (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0) { // if (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0) {
let detected = cwPolicy.hideCompletlyContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`)); // let detected = cwPolicy.hideCompletlyContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
if (detected && detected.length > 0) { // if (detected && detected.length > 0) {
this.hideStatus = true; // this.hideStatus = true;
} // }
} // }
} // }
private setContentWarning(status: Status) { private setContentWarning(status: StatusWrapper) {
this.status.sensitive = true; this.hideStatus = status.hide;
this.isContentWarned = true; this.isContentWarned = status.applyCw;
this.contentWarningText = this.emojiConverter.applyEmojis(this.displayedStatus.emojis, status.spoiler_text, EmojiTypeEnum.medium); this.contentWarningText = this.emojiConverter.applyEmojis(this.displayedStatus.emojis, status.status.spoiler_text, EmojiTypeEnum.medium);
} }
removeContentWarning(): boolean { removeContentWarning(): boolean {

View File

@ -103,7 +103,12 @@ export class StreamNotificationsComponent implements OnInit, OnDestroy {
if (!userNotification) return; if (!userNotification) return;
let mentions = userNotification.mentions.map(x => new NotificationWrapper(x, this.account)).reverse(); let mentions = userNotification.mentions
.map(x => {
let cwPolicy = this.toolsService.checkContentWarning(x.status);
return new NotificationWrapper(x, this.account, cwPolicy.applyCw, cwPolicy.hide);
})
.reverse();
this.lastMentionId = userNotification.lastMentionsId; this.lastMentionId = userNotification.lastMentionsId;
if (!mentions) return; if (!mentions) return;
@ -126,7 +131,10 @@ export class StreamNotificationsComponent implements OnInit, OnDestroy {
.then((notifications: Notification[]) => { .then((notifications: Notification[]) => {
this.isNotificationsLoading = false; this.isNotificationsLoading = false;
this.notifications = notifications.map(x => new NotificationWrapper(x, this.account)); this.notifications = notifications.map(x => {
let cwPolicy = this.toolsService.checkContentWarning(x.status);
return new NotificationWrapper(x, this.account, cwPolicy.applyCw, cwPolicy.hide);
});
this.lastNotificationId = this.notifications[this.notifications.length - 1].notification.id; this.lastNotificationId = this.notifications[this.notifications.length - 1].notification.id;
}) })
.catch(err => { .catch(err => {
@ -138,7 +146,8 @@ export class StreamNotificationsComponent implements OnInit, OnDestroy {
let streaming = this.streamingService.getStreaming(this.account, streamElement); let streaming = this.streamingService.getStreaming(this.account, streamElement);
this.notificationSubscription = streaming.statusUpdateSubjet.subscribe((notification: StatusUpdate) => { this.notificationSubscription = streaming.statusUpdateSubjet.subscribe((notification: StatusUpdate) => {
if (notification && notification.type === EventEnum.notification) { if (notification && notification.type === EventEnum.notification) {
const n = new NotificationWrapper(notification.notification, this.account); let cwPolicy = this.toolsService.checkContentWarning(notification.status);
const n = new NotificationWrapper(notification.notification, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.notifications.unshift(n); this.notifications.unshift(n);
} }
}); });
@ -200,7 +209,8 @@ export class StreamNotificationsComponent implements OnInit, OnDestroy {
} }
for (const s of result) { for (const s of result) {
const wrapper = new NotificationWrapper(s, this.account); let cwPolicy = this.toolsService.checkContentWarning(s.status);
const wrapper = new NotificationWrapper(s, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.notifications.push(wrapper); this.notifications.push(wrapper);
} }
@ -228,7 +238,8 @@ export class StreamNotificationsComponent implements OnInit, OnDestroy {
} }
for (const s of result) { for (const s of result) {
const wrapper = new NotificationWrapper(s, this.account); let cwPolicy = this.toolsService.checkContentWarning(s.status);
const wrapper = new NotificationWrapper(s, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.mentions.push(wrapper); this.mentions.push(wrapper);
} }

View File

@ -152,7 +152,8 @@ export class StreamStatusesComponent implements OnInit, OnDestroy {
return; return;
} }
const wrapper = new StatusWrapper(update.status, this.account); let cwPolicy = this.toolsService.checkContentWarning(update.status);
const wrapper = new StatusWrapper(cwPolicy.status, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.unshift(wrapper); this.statuses.unshift(wrapper);
} else { } else {
this.bufferStream.push(update.status); this.bufferStream.push(update.status);
@ -235,7 +236,8 @@ export class StreamStatusesComponent implements OnInit, OnDestroy {
continue; continue;
} }
const wrapper = new StatusWrapper(status, this.account); let cwPolicy = this.toolsService.checkContentWarning(status);
const wrapper = new StatusWrapper(cwPolicy.status, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.unshift(wrapper); this.statuses.unshift(wrapper);
} }
@ -256,7 +258,8 @@ export class StreamStatusesComponent implements OnInit, OnDestroy {
continue; continue;
} }
const wrapper = new StatusWrapper(s, this.account); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
}) })
@ -292,7 +295,8 @@ export class StreamStatusesComponent implements OnInit, OnDestroy {
continue; continue;
} }
const wrapper = new StatusWrapper(s, this.account); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, this.account, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
}) })

View File

@ -167,14 +167,15 @@ export class ThreadComponent implements OnInit, OnDestroy {
for (let i = 0; i < contextStatuses.length; i++) { for (let i = 0; i < contextStatuses.length; i++) {
let s = contextStatuses[i]; let s = contextStatuses[i];
const wrapper = new StatusWrapper(s, currentAccount); let cwPolicy = this.toolsService.checkContentWarning(s);
const wrapper = new StatusWrapper(cwPolicy.status, currentAccount, cwPolicy.applyCw, cwPolicy.hide);
if(i == position) wrapper.isSelected = true; if(i == position) wrapper.isSelected = true;
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }
this.hasContentWarnings = this.statuses.filter(x => x.status.sensitive || x.status.spoiler_text).length > 1; this.hasContentWarnings = this.statuses.filter(x => x.applyCw).length > 1;
return position; return position;
}); });

View File

@ -203,7 +203,8 @@ export class UserProfileComponent implements OnInit {
.then((statuses: Status[]) => { .then((statuses: Status[]) => {
for (const status of statuses) { for (const status of statuses) {
status.pinned = true; status.pinned = true;
const wrapper = new StatusWrapper(status, userAccount); let cwPolicy = this.toolsService.checkContentWarning(status);
const wrapper = new StatusWrapper(cwPolicy.status, userAccount, cwPolicy.applyCw, cwPolicy.hide);
this.pinnedStatuses.push(wrapper); this.pinnedStatuses.push(wrapper);
} }
}) })
@ -355,7 +356,8 @@ export class UserProfileComponent implements OnInit {
} }
for (const status of statuses) { for (const status of statuses) {
const wrapper = new StatusWrapper(status, userAccount); let cwPolicy = this.toolsService.checkContentWarning(status);
const wrapper = new StatusWrapper(cwPolicy.status, userAccount, cwPolicy.applyCw, cwPolicy.hide);
this.statuses.push(wrapper); this.statuses.push(wrapper);
} }

View File

@ -15,7 +15,9 @@ export class OpenMediaEvent {
export class StatusWrapper { export class StatusWrapper {
constructor( constructor(
public status: Status, public status: Status,
public provider: AccountInfo public provider: AccountInfo,
public applyCw: boolean,
public hide: boolean
) { } ) { }
public isSelected: boolean; public isSelected: boolean;

View File

@ -5,13 +5,13 @@ import { AccountInfo } from '../states/accounts.state';
import { MastodonWrapperService } from './mastodon-wrapper.service'; import { MastodonWrapperService } from './mastodon-wrapper.service';
import { Account, Results, Status, Emoji } from "./models/mastodon.interfaces"; import { Account, Results, Status, Emoji } from "./models/mastodon.interfaces";
import { StatusWrapper } from '../models/common.model'; import { StatusWrapper } from '../models/common.model';
import { AccountSettings, SaveAccountSettings, GlobalSettings, SaveSettings, ContentWarningPolicy, SaveContentWarningPolicy } from '../states/settings.state'; import { AccountSettings, SaveAccountSettings, GlobalSettings, SaveSettings, ContentWarningPolicy, SaveContentWarningPolicy, ContentWarningPolicyEnum } from '../states/settings.state';
import { AppInfo, RegisteredAppsStateModel } from '../states/registered-apps.state'; import { AppInfo, RegisteredAppsStateModel } from '../states/registered-apps.state';
@Injectable({ @Injectable({
providedIn: 'root' providedIn: 'root'
}) })
export class ToolsService { export class ToolsService {
private accountAvatar: { [id: string]: string; } = {}; private accountAvatar: { [id: string]: string; } = {};
private instanceInfos: { [id: string]: InstanceInfo } = {}; private instanceInfos: { [id: string]: InstanceInfo } = {};
@ -19,6 +19,58 @@ export class ToolsService {
private readonly mastodonService: MastodonWrapperService, private readonly mastodonService: MastodonWrapperService,
private readonly store: Store) { } private readonly store: Store) { }
checkContentWarning(status: Status): StatusWithCwPolicyResult {
if(!status) {
return new StatusWithCwPolicyResult(status, false, false);
}
let applyCw = false;
let hideStatus = false;
let cwPolicy = this.getSettings().contentWarningPolicy;
let splittedContent = [];
if ((cwPolicy.policy === ContentWarningPolicyEnum.HideAll && cwPolicy.addCwOnContent.length > 0)
|| (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent && cwPolicy.removeCwOnContent.length > 0)
|| (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0)) {
let parser = new DOMParser();
let dom = parser.parseFromString((status.content + ' ' + status.spoiler_text).replace("<br/>", " ").replace("<br>", " ").replace(/\n/g, ' '), 'text/html')
let contentToParse = dom.body.textContent;
splittedContent = contentToParse.toLowerCase().split(' ');
}
if (cwPolicy.policy === ContentWarningPolicyEnum.None && (status.sensitive || status.spoiler_text)) {
applyCw = true;
} else if (cwPolicy.policy === ContentWarningPolicyEnum.HideAll) {
let detected = cwPolicy.addCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
if (!detected || detected.length === 0) {
applyCw = false;
} else {
if (!status.spoiler_text) {
status.spoiler_text = detected.join(' ');
}
applyCw = true;
}
} else if (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent) {
let detected = cwPolicy.removeCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
if (detected && detected.length > 0) {
applyCw = false;
} else {
applyCw = true;
}
}
if (cwPolicy.hideCompletlyContent && cwPolicy.hideCompletlyContent.length > 0) {
let detected = cwPolicy.hideCompletlyContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
if (detected && detected.length > 0) {
hideStatus = true;
}
}
return new StatusWithCwPolicyResult(status, applyCw, hideStatus);
}
getInstanceInfo(acc: AccountInfo): Promise<InstanceInfo> { getInstanceInfo(acc: AccountInfo): Promise<InstanceInfo> {
if (this.instanceInfos[acc.instance]) { if (this.instanceInfos[acc.instance]) {
return Promise.resolve(this.instanceInfos[acc.instance]); return Promise.resolve(this.instanceInfos[acc.instance]);
@ -97,7 +149,7 @@ export class ToolsService {
getSettings(): GlobalSettings { getSettings(): GlobalSettings {
let settings = <GlobalSettings>this.store.snapshot().globalsettings.settings; let settings = <GlobalSettings>this.store.snapshot().globalsettings.settings;
if(!settings.contentWarningPolicy){ if (!settings.contentWarningPolicy) {
var newCwPolicy = new ContentWarningPolicy(); var newCwPolicy = new ContentWarningPolicy();
this.saveContentWarningPolicy(newCwPolicy); this.saveContentWarningPolicy(newCwPolicy);
return <GlobalSettings>this.store.snapshot().globalsettings.settings; return <GlobalSettings>this.store.snapshot().globalsettings.settings;
@ -112,7 +164,7 @@ export class ToolsService {
]); ]);
} }
saveContentWarningPolicy(cwSettings: ContentWarningPolicy){ saveContentWarningPolicy(cwSettings: ContentWarningPolicy) {
this.store.dispatch([ this.store.dispatch([
new SaveContentWarningPolicy(cwSettings) new SaveContentWarningPolicy(cwSettings)
]); ]);
@ -220,4 +272,9 @@ export enum InstanceType {
GlitchSoc = 3, GlitchSoc = 3,
Florence = 4, Florence = 4,
Pixelfed = 5 Pixelfed = 5
}
export class StatusWithCwPolicyResult {
constructor(public status: Status, public applyCw: boolean, public hide: boolean) {
}
} }