keep attachments on edition, fix #563

This commit is contained in:
Nicolas Constant 2023-04-24 00:17:13 -04:00
parent 314c736cf4
commit 982a670352
No known key found for this signature in database
GPG Key ID: 1E9F677FB01A5688
7 changed files with 80 additions and 25 deletions

View File

@ -89,6 +89,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
this.isEditing = true; this.isEditing = true;
this.editingStatusId = value.status.id; this.editingStatusId = value.status.id;
this.redraftedStatus = value; this.redraftedStatus = value;
this.mediaService.loadMedia(value.status.media_attachments);
} }
} }
@ -629,7 +630,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
let postPromise: Promise<Status>; let postPromise: Promise<Status>;
if (this.isEditing) { if (this.isEditing) {
postPromise = this.mastodonService.editStatus(account, editingStatusId, s, visibility, title, inReplyToId, attachments.map(x => x.id), poll, scheduledAt); postPromise = this.mastodonService.editStatus(account, editingStatusId, s, visibility, title, inReplyToId, attachments, poll, scheduledAt);
} else { } else {
postPromise = this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, attachments.map(x => x.id), poll, scheduledAt); postPromise = this.mastodonService.postNewStatus(account, s, visibility, title, inReplyToId, attachments.map(x => x.id), poll, scheduledAt);
} }

View File

@ -1,8 +1,8 @@
<div *ngFor="let m of media" class="media"> <div *ngFor="let m of media" class="media">
<div *ngIf="m.attachment === null" class="media__loading" title="{{m.file.name}}"> <div *ngIf="m.attachment === null" class="media__loading" title="{{getName(m)}}">
<app-waiting-animation class="waiting-icon"></app-waiting-animation> <app-waiting-animation class="waiting-icon"></app-waiting-animation>
</div> </div>
<div *ngIf="m.attachment !== null && m.attachment.type !== 'audio'" class="media__loaded" title="{{m.file.name}}" <div *ngIf="m.attachment !== null && m.attachment.type !== 'audio'" class="media__loaded" title="{{getName(m)}}"
(mouseleave)="updateMedia(m)"> (mouseleave)="updateMedia(m)">
<div class="media__loaded--migrating" *ngIf="m.isMigrating"> <div class="media__loaded--migrating" *ngIf="m.isMigrating">
<app-waiting-animation class="waiting-icon"></app-waiting-animation> <app-waiting-animation class="waiting-icon"></app-waiting-animation>

View File

@ -56,4 +56,13 @@ export class MediaComponent implements OnInit, OnDestroy {
this.mediaService.update(account, media); this.mediaService.update(account, media);
return false; return false;
} }
getName(media: MediaWrapper): string {
if(media && media.file && media.file.name){
return media.file.name;
}
if(media.attachment && media.attachment.description){
return media.attachment.description;
}
}
} }

View File

@ -30,6 +30,10 @@ export class AttachementsComponent implements OnInit {
@Input('attachments') @Input('attachments')
set attachments(value: Attachment[]) { set attachments(value: Attachment[]) {
this.imageAttachments = [];
this.videoAttachments = [];
this.audioAttachments = [];
this._attachments = value; this._attachments = value;
this.setAttachments(value); this.setAttachments(value);
} }

View File

@ -124,10 +124,10 @@ export class MastodonWrapperService {
}); });
} }
editStatus(account: AccountInfo, statusId: string, status: string, visibility: VisibilityEnum, spoiler: string = null, in_reply_to_id: string = null, mediaIds: string[], poll: PollParameters = null, scheduled_at: string = null): Promise<Status> { editStatus(account: AccountInfo, statusId: string, status: string, visibility: VisibilityEnum, spoiler: string = null, in_reply_to_id: string = null, attachements: Attachment[], poll: PollParameters = null, scheduled_at: string = null): Promise<Status> {
return this.refreshAccountIfNeeded(account) return this.refreshAccountIfNeeded(account)
.then((refreshedAccount: AccountInfo) => { .then((refreshedAccount: AccountInfo) => {
return this.mastodonService.editStatus(refreshedAccount, statusId, status, visibility, spoiler, in_reply_to_id, mediaIds, poll, scheduled_at); return this.mastodonService.editStatus(refreshedAccount, statusId, status, visibility, spoiler, in_reply_to_id, attachements, poll, scheduled_at);
}); });
} }

View File

@ -132,12 +132,13 @@ export class MastodonService {
return this.httpClient.post<Status>(url, statusData, { headers: headers }).toPromise(); return this.httpClient.post<Status>(url, statusData, { headers: headers }).toPromise();
} }
editStatus(account: AccountInfo, statusId: string, status: string, visibility: VisibilityEnum, spoiler: string = null, in_reply_to_id: string = null, mediaIds: string[], poll: PollParameters = null, scheduled_at: string = null): Promise<Status> { editStatus(account: AccountInfo, statusId: string, status: string, visibility: VisibilityEnum, spoiler: string = null, in_reply_to_id: string = null, attachements: Attachment[], poll: PollParameters = null, scheduled_at: string = null): Promise<Status> {
const url = `https://${account.instance}${this.apiRoutes.editStatus.replace('{0}', statusId)}`; const url = `https://${account.instance}${this.apiRoutes.editStatus.replace('{0}', statusId)}`;
const statusData = new StatusData(); const statusData = new StatusData();
statusData.status = status; statusData.status = status;
statusData.media_ids = mediaIds; statusData.media_ids = attachements.map(x => x.id);
statusData.media_attributes = attachements.map(x => new MediaAttributes(x.id, x.description));
if (poll) { if (poll) {
statusData['poll'] = poll; statusData['poll'] = poll;
@ -643,6 +644,8 @@ class StatusData {
status: string; status: string;
in_reply_to_id: string; in_reply_to_id: string;
media_ids: string[]; media_ids: string[];
media_attributes: MediaAttributes[];
// poll: PollParameters; // poll: PollParameters;
sensitive: boolean; sensitive: boolean;
spoiler_text: string; spoiler_text: string;
@ -650,6 +653,13 @@ class StatusData {
// scheduled_at: string; // scheduled_at: string;
} }
class MediaAttributes {
constructor(
public id: string,
public description: string){
}
}
export class PollParameters { export class PollParameters {
options: string[] = []; options: string[] = [];
expires_in: number; expires_in: number;

View File

@ -51,9 +51,32 @@ export class MediaService {
}); });
} }
loadMedia(attachments: Attachment[]) {
const wrappers: MediaWrapper[] = [];
for (const att of attachments) {
const uniqueId = `${att.id}${Math.random()}`;
const wrapper = new MediaWrapper(uniqueId, null, att);
wrapper.description = att.description;
wrapper.isEdited = true;
wrappers.push(wrapper);
}
this.mediaSubject.next(wrappers);
}
update(account: AccountInfo, media: MediaWrapper): Promise<void> { update(account: AccountInfo, media: MediaWrapper): Promise<void> {
if (media.attachment.description === media.description) return; if (media.attachment.description === media.description) return;
if (media.isEdited) {
media.attachment.description = media.description;
let medias = this.mediaSubject.value;
let updatedMedia = medias.filter(x => x.id === media.id)[0];
updatedMedia.attachment.description = media.attachment.description;
this.mediaSubject.next(medias);
} else {
return this.mastodonService.updateMediaAttachment(account, media.attachment.id, media.description) return this.mastodonService.updateMediaAttachment(account, media.attachment.id, media.description)
.then((att: Attachment) => { .then((att: Attachment) => {
let medias = this.mediaSubject.value; let medias = this.mediaSubject.value;
@ -62,9 +85,11 @@ export class MediaService {
this.mediaSubject.next(medias); this.mediaSubject.next(medias);
}) })
.catch((err) => { .catch((err) => {
console.warn('failing update');
this.notificationService.notifyHttpError(err, account); this.notificationService.notifyHttpError(err, account);
}); });
} }
}
async retrieveUpToDateMedia(account: AccountInfo): Promise<MediaWrapper[]> { async retrieveUpToDateMedia(account: AccountInfo): Promise<MediaWrapper[]> {
const allMedia = this.mediaSubject.value; const allMedia = this.mediaSubject.value;
@ -80,8 +105,8 @@ export class MediaService {
return allMedia; return allMedia;
} }
addExistingMedia(media: MediaWrapper){ addExistingMedia(media: MediaWrapper) {
if(!this.fileCache[media.attachment.url]) return; if (!this.fileCache[media.attachment.url]) return;
media.file = this.fileCache[media.attachment.url]; media.file = this.fileCache[media.attachment.url];
let medias = this.mediaSubject.value; let medias = this.mediaSubject.value;
@ -102,11 +127,15 @@ export class MediaService {
migrateMedias(account: AccountInfo) { migrateMedias(account: AccountInfo) {
let medias = this.mediaSubject.value; let medias = this.mediaSubject.value;
medias.forEach(media => { medias.forEach(media => {
if (!media.isEdited) {
media.isMigrating = true; media.isMigrating = true;
}
}); });
this.mediaSubject.next(medias); this.mediaSubject.next(medias);
for (let media of medias) { for (let media of medias) {
if (media.isEdited) continue;
this.mastodonService.uploadMediaAttachment(account, media.file, media.description) this.mastodonService.uploadMediaAttachment(account, media.file, media.description)
.then((attachment: Attachment) => { .then((attachment: Attachment) => {
this.fileCache[attachment.url] = media.file; this.fileCache[attachment.url] = media.file;
@ -139,7 +168,7 @@ export class MediaWrapper {
return this._attachment; return this._attachment;
} }
public set attachment(value: Attachment){ public set attachment(value: Attachment) {
if (value && value.meta && value.meta.audio_encode) { if (value && value.meta && value.meta.audio_encode) {
this.audioType = `audio/${value.meta.audio_encode}`; this.audioType = `audio/${value.meta.audio_encode}`;
} else if (value && value.pleroma && value.pleroma.mime_type) { } else if (value && value.pleroma && value.pleroma.mime_type) {
@ -152,4 +181,6 @@ export class MediaWrapper {
public description: string; public description: string;
public isMigrating: boolean; public isMigrating: boolean;
public audioType: string; public audioType: string;
public isEdited: boolean;
} }