2019-03-08 05:24:23 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
2019-03-11 00:23:33 +01:00
|
|
|
import { BehaviorSubject } from 'rxjs';
|
2019-03-08 05:24:23 +01:00
|
|
|
|
|
|
|
import { AccountInfo } from '../states/accounts.state';
|
|
|
|
import { Attachment } from './models/mastodon.interfaces';
|
2019-03-10 19:36:22 +01:00
|
|
|
import { MastodonService } from './mastodon.service';
|
|
|
|
import { NotificationService } from './notification.service';
|
2019-03-08 05:24:23 +01:00
|
|
|
|
|
|
|
|
|
|
|
@Injectable({
|
|
|
|
providedIn: 'root'
|
|
|
|
})
|
2019-03-11 00:23:33 +01:00
|
|
|
export class MediaService {
|
2019-03-08 05:24:23 +01:00
|
|
|
mediaSubject: BehaviorSubject<MediaWrapper[]> = new BehaviorSubject<MediaWrapper[]>([]);
|
|
|
|
|
2019-03-10 19:36:22 +01:00
|
|
|
constructor(
|
|
|
|
private readonly notificationService: NotificationService,
|
|
|
|
private readonly mastodonService: MastodonService) { }
|
2019-03-08 05:24:23 +01:00
|
|
|
|
2019-03-11 00:23:33 +01:00
|
|
|
uploadMedia(account: AccountInfo, files: File[]) {
|
2019-03-08 05:24:23 +01:00
|
|
|
for (let file of files) {
|
2019-03-10 19:36:22 +01:00
|
|
|
this.postMedia(account, file);
|
2019-03-11 00:23:33 +01:00
|
|
|
}
|
2019-03-08 05:24:23 +01:00
|
|
|
}
|
|
|
|
|
2019-03-11 00:23:33 +01:00
|
|
|
private postMedia(account: AccountInfo, file: File) {
|
2019-03-08 05:24:23 +01:00
|
|
|
const uniqueId = `${file.name}${file.size}${Math.random()}`;
|
2019-03-11 00:23:33 +01:00
|
|
|
const wrapper = new MediaWrapper(uniqueId, file, null);
|
2019-03-08 05:24:23 +01:00
|
|
|
|
|
|
|
let medias = this.mediaSubject.value;
|
|
|
|
medias.push(wrapper);
|
|
|
|
this.mediaSubject.next(medias);
|
|
|
|
|
2019-03-11 00:50:09 +01:00
|
|
|
this.mastodonService.uploadMediaAttachment(account, file, null)
|
2019-03-08 05:24:23 +01:00
|
|
|
.then((attachment: Attachment) => {
|
|
|
|
let currentMedias = this.mediaSubject.value;
|
|
|
|
let currentMedia = currentMedias.filter(x => x.id === uniqueId)[0];
|
2019-03-11 00:23:33 +01:00
|
|
|
if (currentMedia) {
|
2019-03-08 05:24:23 +01:00
|
|
|
currentMedia.attachment = attachment;
|
|
|
|
this.mediaSubject.next(currentMedias);
|
|
|
|
}
|
|
|
|
})
|
2019-03-11 00:23:33 +01:00
|
|
|
.catch((err) => {
|
2019-03-10 19:36:22 +01:00
|
|
|
this.remove(wrapper);
|
|
|
|
this.notificationService.notifyHttpError(err);
|
|
|
|
});
|
|
|
|
}
|
2019-03-08 05:24:23 +01:00
|
|
|
|
2019-03-11 00:47:15 +01:00
|
|
|
update(account: AccountInfo, media: MediaWrapper) {
|
2019-03-11 00:23:33 +01:00
|
|
|
if (media.attachment.description === media.description) return;
|
|
|
|
|
2019-03-10 19:36:22 +01:00
|
|
|
this.mastodonService.updateMediaAttachment(account, media.attachment.id, media.description)
|
|
|
|
.then((att: Attachment) => {
|
|
|
|
let medias = this.mediaSubject.value;
|
|
|
|
let updatedMedia = medias.filter(x => x.id === media.id)[0];
|
|
|
|
updatedMedia.attachment.description = att.description;
|
|
|
|
this.mediaSubject.next(medias);
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.notificationService.notifyHttpError(err);
|
2019-03-08 05:24:23 +01:00
|
|
|
});
|
|
|
|
}
|
2019-03-10 19:36:22 +01:00
|
|
|
|
2019-03-11 00:47:15 +01:00
|
|
|
remove(media: MediaWrapper) {
|
2019-03-10 19:36:22 +01:00
|
|
|
let medias = this.mediaSubject.value;
|
|
|
|
let filteredMedias = medias.filter(x => x.id !== media.id);
|
|
|
|
this.mediaSubject.next(filteredMedias);
|
|
|
|
}
|
2019-03-11 00:23:33 +01:00
|
|
|
|
|
|
|
clearMedia() {
|
|
|
|
this.mediaSubject.next([]);
|
|
|
|
}
|
|
|
|
|
2019-03-11 00:47:15 +01:00
|
|
|
migrateMedias(account: AccountInfo) {
|
|
|
|
let medias = this.mediaSubject.value;
|
|
|
|
medias.forEach(media => {
|
|
|
|
media.isMigrating = true;
|
|
|
|
});
|
|
|
|
this.mediaSubject.next(medias);
|
|
|
|
|
|
|
|
for (let media of medias) {
|
2019-03-11 00:50:09 +01:00
|
|
|
this.mastodonService.uploadMediaAttachment(account, media.file, media.description)
|
2019-03-11 00:47:15 +01:00
|
|
|
.then((attachment: Attachment) => {
|
|
|
|
let currentMedias = this.mediaSubject.value;
|
|
|
|
let currentMedia = currentMedias.filter(x => x.id === media.id)[0];
|
|
|
|
if (currentMedia) {
|
|
|
|
currentMedia.attachment = attachment;
|
|
|
|
currentMedia.isMigrating = false;
|
|
|
|
this.mediaSubject.next(currentMedias);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
this.remove(media);
|
|
|
|
this.notificationService.notifyHttpError(err);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
2019-03-08 05:24:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MediaWrapper {
|
|
|
|
constructor(
|
|
|
|
public id: string,
|
2019-03-11 00:23:33 +01:00
|
|
|
public file: File,
|
|
|
|
public attachment: Attachment) { }
|
|
|
|
|
|
|
|
public description: string;
|
|
|
|
public isMigrating: boolean;
|
2019-03-08 05:24:23 +01:00
|
|
|
}
|