2023-08-05 05:36:21 +02:00
|
|
|
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
|
2023-08-05 04:57:06 +02:00
|
|
|
import { Subscription } from 'rxjs';
|
|
|
|
|
|
|
|
import { StatusWrapper } from '../../../../models/common.model';
|
|
|
|
import { ILanguage } from '../../../../states/settings.state';
|
|
|
|
import { LanguageService } from '../../../../services/language.service';
|
|
|
|
import { InstancesInfoService } from '../../../../services/instances-info.service';
|
2023-08-05 05:36:21 +02:00
|
|
|
import { MastodonWrapperService } from '../../../../services/mastodon-wrapper.service';
|
|
|
|
import { Translation } from '../../../../services/models/mastodon.interfaces';
|
2023-08-05 05:38:29 +02:00
|
|
|
import { NotificationService } from '../../../../services/notification.service';
|
|
|
|
import { HttpErrorResponse } from '@angular/common/http';
|
2023-08-05 04:57:06 +02:00
|
|
|
|
|
|
|
@Component({
|
|
|
|
selector: 'app-status-translate',
|
|
|
|
templateUrl: './status-translate.component.html',
|
|
|
|
styleUrls: ['./status-translate.component.scss']
|
|
|
|
})
|
|
|
|
export class StatusTranslateComponent implements OnInit, OnDestroy {
|
|
|
|
|
|
|
|
private languageSub: Subscription;
|
|
|
|
private languagesSub: Subscription;
|
2023-08-06 00:32:23 +02:00
|
|
|
private loadedTranslation: Translation;
|
2023-08-05 04:57:06 +02:00
|
|
|
|
|
|
|
selectedLanguage: ILanguage;
|
|
|
|
configuredLanguages: ILanguage[] = [];
|
|
|
|
|
|
|
|
isTranslationAvailable: boolean;
|
2023-08-06 00:32:23 +02:00
|
|
|
showTranslationButton: boolean = true;
|
2023-08-05 05:36:21 +02:00
|
|
|
translatedBy: string;
|
2023-08-05 04:57:06 +02:00
|
|
|
|
|
|
|
@Input() status: StatusWrapper;
|
2023-08-05 05:36:21 +02:00
|
|
|
@Output() translation = new EventEmitter<Translation>();
|
2023-08-05 04:57:06 +02:00
|
|
|
|
|
|
|
constructor(
|
2023-08-05 05:36:21 +02:00
|
|
|
private readonly mastodonWrapperService: MastodonWrapperService,
|
2023-08-05 04:57:06 +02:00
|
|
|
private readonly languageService: LanguageService,
|
|
|
|
private readonly instancesInfoService: InstancesInfoService,
|
2023-08-05 05:38:29 +02:00
|
|
|
private readonly notificationService: NotificationService
|
2023-08-05 04:57:06 +02:00
|
|
|
) { }
|
|
|
|
|
|
|
|
ngOnInit() {
|
|
|
|
this.languageSub = this.languageService.selectedLanguageChanged.subscribe(l => {
|
|
|
|
if (l) {
|
|
|
|
this.selectedLanguage = l;
|
|
|
|
this.analyseAvailability();
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
this.languagesSub = this.languageService.configuredLanguagesChanged.subscribe(l => {
|
|
|
|
if (l) {
|
|
|
|
this.configuredLanguages = l;
|
|
|
|
this.analyseAvailability();
|
|
|
|
}
|
|
|
|
});
|
2023-08-05 05:36:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
ngOnDestroy(): void {
|
|
|
|
if (this.languageSub) this.languageSub.unsubscribe();
|
|
|
|
if (this.languagesSub) this.languagesSub.unsubscribe();
|
2023-08-05 04:57:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
private analyseAvailability() {
|
|
|
|
this.instancesInfoService.getTranslationAvailability(this.status.provider)
|
|
|
|
.then(canTranslate => {
|
|
|
|
if (canTranslate
|
2023-08-05 05:36:21 +02:00
|
|
|
&& !this.status.isRemote
|
2023-08-12 22:50:35 +02:00
|
|
|
&& this.status.status.language
|
2023-08-05 04:57:06 +02:00
|
|
|
&& this.configuredLanguages.length > 0
|
|
|
|
&& this.configuredLanguages.findIndex(x => x.iso639 === this.status.status.language) === -1) {
|
|
|
|
|
|
|
|
this.isTranslationAvailable = true;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
this.isTranslationAvailable = false;
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(err => {
|
|
|
|
console.error(err);
|
|
|
|
this.isTranslationAvailable = false;
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2023-08-05 05:36:21 +02:00
|
|
|
translate(): boolean {
|
2023-08-06 00:32:23 +02:00
|
|
|
if(this.loadedTranslation){
|
|
|
|
this.translation.next(this.loadedTranslation);
|
|
|
|
this.showTranslationButton = false;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2023-08-05 05:36:21 +02:00
|
|
|
this.mastodonWrapperService.translate(this.status.provider, this.status.status.id, this.selectedLanguage.iso639)
|
2023-08-06 00:32:23 +02:00
|
|
|
.then(x => {
|
|
|
|
this.loadedTranslation = x;
|
2023-08-05 05:36:21 +02:00
|
|
|
this.translation.next(x);
|
|
|
|
this.translatedBy = x.provider;
|
2023-08-06 00:32:23 +02:00
|
|
|
this.showTranslationButton = false;
|
2023-08-05 05:36:21 +02:00
|
|
|
})
|
2023-08-05 05:38:29 +02:00
|
|
|
.catch((err: HttpErrorResponse) => {
|
2023-08-05 05:36:21 +02:00
|
|
|
console.error(err);
|
2023-08-05 05:38:29 +02:00
|
|
|
this.notificationService.notifyHttpError(err, this.status.provider);
|
2023-08-05 05:36:21 +02:00
|
|
|
});
|
|
|
|
return false;
|
2023-08-05 04:57:06 +02:00
|
|
|
}
|
2023-08-06 00:32:23 +02:00
|
|
|
|
|
|
|
revertTranslation(): boolean {
|
|
|
|
let revertTranslate: Translation;
|
|
|
|
revertTranslate = {
|
|
|
|
content: this.status.status.content,
|
|
|
|
language: this.loadedTranslation.detected_source_language,
|
|
|
|
detected_source_language: this.loadedTranslation.language,
|
|
|
|
provider: this.loadedTranslation.provider,
|
|
|
|
spoiler_text: this.status.status.spoiler_text
|
|
|
|
};
|
|
|
|
this.translation.next(revertTranslate);
|
|
|
|
|
|
|
|
this.showTranslationButton = true;
|
|
|
|
return false;
|
|
|
|
}
|
2023-08-05 04:57:06 +02:00
|
|
|
}
|