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.editingStatusId = value.status.id;
this.redraftedStatus = value;
this.mediaService.loadMedia(value.status.media_attachments);
}
}
@ -629,7 +630,7 @@ export class CreateStatusComponent implements OnInit, OnDestroy {
let postPromise: Promise<Status>;
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 {
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 *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>
</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)">
<div class="media__loaded--migrating" *ngIf="m.isMigrating">
<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);
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')
set attachments(value: Attachment[]) {
this.imageAttachments = [];
this.videoAttachments = [];
this.audioAttachments = [];
this._attachments = 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)
.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();
}
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 statusData = new StatusData();
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) {
statusData['poll'] = poll;
@ -643,6 +644,8 @@ class StatusData {
status: string;
in_reply_to_id: string;
media_ids: string[];
media_attributes: MediaAttributes[];
// poll: PollParameters;
sensitive: boolean;
spoiler_text: string;
@ -650,6 +653,13 @@ class StatusData {
// scheduled_at: string;
}
class MediaAttributes {
constructor(
public id: string,
public description: string){
}
}
export class PollParameters {
options: string[] = [];
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> {
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)
.then((att: Attachment) => {
let medias = this.mediaSubject.value;
@ -62,9 +85,11 @@ export class MediaService {
this.mediaSubject.next(medias);
})
.catch((err) => {
console.warn('failing update');
this.notificationService.notifyHttpError(err, account);
});
}
}
async retrieveUpToDateMedia(account: AccountInfo): Promise<MediaWrapper[]> {
const allMedia = this.mediaSubject.value;
@ -102,11 +127,15 @@ export class MediaService {
migrateMedias(account: AccountInfo) {
let medias = this.mediaSubject.value;
medias.forEach(media => {
if (!media.isEdited) {
media.isMigrating = true;
}
});
this.mediaSubject.next(medias);
for (let media of medias) {
if (media.isEdited) continue;
this.mastodonService.uploadMediaAttachment(account, media.file, media.description)
.then((attachment: Attachment) => {
this.fileCache[attachment.url] = media.file;
@ -152,4 +181,6 @@ export class MediaWrapper {
public description: string;
public isMigrating: boolean;
public audioType: string;
public isEdited: boolean;
}