2019-03-08 05:24:23 +01:00
|
|
|
import { Injectable } from '@angular/core';
|
|
|
|
import { HttpClient, HttpHeaders } from '@angular/common/http';
|
|
|
|
import { BehaviorSubject, Subject } from 'rxjs';
|
|
|
|
|
|
|
|
import { AccountInfo } from '../states/accounts.state';
|
|
|
|
import { ApiRoutes } from './models/api.settings';
|
|
|
|
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-10 19:36:22 +01:00
|
|
|
export class MediaService {
|
2019-03-08 05:24:23 +01:00
|
|
|
private apiRoutes = new ApiRoutes();
|
|
|
|
|
|
|
|
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-10 19:36:22 +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-08 05:24:23 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-03-10 19:36:22 +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-10 19:36:22 +01:00
|
|
|
const wrapper = new MediaWrapper(uniqueId, file, null, null);
|
2019-03-08 05:24:23 +01:00
|
|
|
|
|
|
|
let medias = this.mediaSubject.value;
|
|
|
|
medias.push(wrapper);
|
|
|
|
this.mediaSubject.next(medias);
|
|
|
|
|
2019-03-10 19:36:22 +01:00
|
|
|
this.mastodonService.uploadMediaAttachment(account, file)
|
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];
|
|
|
|
if(currentMedia){
|
|
|
|
currentMedia.attachment = attachment;
|
|
|
|
this.mediaSubject.next(currentMedias);
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.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-10 19:36:22 +01:00
|
|
|
update( account: AccountInfo, media: MediaWrapper): any {
|
|
|
|
if(media.attachment.description === media.description) return;
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
remove(media: MediaWrapper): any {
|
|
|
|
let medias = this.mediaSubject.value;
|
|
|
|
let filteredMedias = medias.filter(x => x.id !== media.id);
|
|
|
|
this.mediaSubject.next(filteredMedias);
|
|
|
|
}
|
2019-03-08 05:24:23 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class MediaWrapper {
|
|
|
|
constructor(
|
|
|
|
public id: string,
|
|
|
|
public file: File,
|
2019-03-10 19:36:22 +01:00
|
|
|
public attachment: Attachment,
|
|
|
|
public description: string) {}
|
2019-03-08 05:24:23 +01:00
|
|
|
}
|