better status analysis for CW

This commit is contained in:
Nicolas Constant 2020-03-28 18:58:40 -04:00
parent 687148bc3a
commit e6e75923b0
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
3 changed files with 26 additions and 8 deletions

View File

@ -90,6 +90,8 @@
<input type="text" class="form-control form-control-sm sub_section__text-input"
[(ngModel)]="setContentHidedCompletely" placeholder="example;other example" />
</div>
<span class="sub-section__title" *ngIf="contentWarningPolicyChanged"><br/>this settings needs a <a href (click)="reload()">reload</a> to be effective.</span>
</div>
<h4 class="panel__subtitle">About</h4>

View File

@ -108,6 +108,7 @@ export class SettingsComponent implements OnInit {
}
private setCwPolicy(id: ContentWarningPolicyEnum = null, addCw: string = null, removeCw: string = null, hide: string = null){
this.contentWarningPolicyChanged = true;
let settings = this.toolsService.getSettings();
let cwPolicySettings = new ContentWarningPolicy();
@ -118,19 +119,19 @@ export class SettingsComponent implements OnInit {
}
if(addCw){
cwPolicySettings.addCwOnContent = addCw.split(';').map(x => x.trim().toLowerCase());
cwPolicySettings.addCwOnContent = this.splitCwValues(addCw);
} else {
cwPolicySettings.addCwOnContent = settings.contentWarningPolicy.addCwOnContent;
}
if(removeCw){
cwPolicySettings.removeCwOnContent = removeCw.split(';').map(x => x.trim().toLowerCase());
cwPolicySettings.removeCwOnContent = this.splitCwValues(removeCw);
} else {
cwPolicySettings.removeCwOnContent = settings.contentWarningPolicy.removeCwOnContent;
}
if(hide){
cwPolicySettings.hideCompletlyContent = hide.split(';').map(x => x.trim().toLowerCase());
cwPolicySettings.hideCompletlyContent = this.splitCwValues(hide);
} else {
cwPolicySettings.hideCompletlyContent = settings.contentWarningPolicy.hideCompletlyContent;
}
@ -138,6 +139,10 @@ export class SettingsComponent implements OnInit {
this.toolsService.saveContentWarningPolicy(cwPolicySettings);
}
private splitCwValues(data: string): string[]{
return data.split(';').map(x => x.trim().toLowerCase()).filter((value, index, self) => self.indexOf(value) === index);
}
reload(): boolean {
window.location.reload();
return false;

View File

@ -98,22 +98,33 @@ export class StatusComponent implements OnInit {
private checkContentWarning(status: Status) {
let cwPolicy = this.toolsService.getSettings().contentWarningPolicy;
let splittedContent = (status.content + ' ' + status.spoiler_text).toLowerCase().split(' ');
let splittedContent = [];
if (cwPolicy.policy === ContentWarningPolicyEnum.HideAll || cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent) {
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;
console.warn(contentToParse);
splittedContent = contentToParse.toLowerCase().split(' ');
}
if (cwPolicy.policy === ContentWarningPolicyEnum.None && (status.sensitive || status.spoiler_text)) {
this.setContentWarning(status);
} else if (cwPolicy.policy === ContentWarningPolicyEnum.HideAll) {
let detected = cwPolicy.addCwOnContent.filter(x => splittedContent.find(y => y == x));
if(!detected || detected.length === 0) return;
let detected = cwPolicy.addCwOnContent.filter(x => splittedContent.find(y => y == x || y == `#${x}`));
console.warn(splittedContent);
console.warn(cwPolicy.addCwOnContent);
console.warn(detected);
if (!detected || detected.length === 0) return;
if(!status.spoiler_text){
if (!status.spoiler_text) {
status.spoiler_text = detected.join(' ');
}
this.setContentWarning(status);
} else if (cwPolicy.policy === ContentWarningPolicyEnum.AddOnAllContent) {
let detected = cwPolicy.removeCwOnContent.find(x => splittedContent.find(y => y == x) != null) != null;
if(!detected) {
if (!detected) {
this.setContentWarning(status);
}
}