diff --git a/src/app/components/create-status/create-status.component.ts b/src/app/components/create-status/create-status.component.ts index a386e956..9ef54b66 100644 --- a/src/app/components/create-status/create-status.component.ts +++ b/src/app/components/create-status/create-status.component.ts @@ -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; 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); } diff --git a/src/app/components/create-status/media/media.component.html b/src/app/components/create-status/media/media.component.html index 747b43f5..de6c6465 100644 --- a/src/app/components/create-status/media/media.component.html +++ b/src/app/components/create-status/media/media.component.html @@ -1,8 +1,8 @@
-
+
-
diff --git a/src/app/components/create-status/media/media.component.ts b/src/app/components/create-status/media/media.component.ts index a09d0a0e..456606ea 100644 --- a/src/app/components/create-status/media/media.component.ts +++ b/src/app/components/create-status/media/media.component.ts @@ -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; + } + } } diff --git a/src/app/components/stream/status/attachements/attachements.component.ts b/src/app/components/stream/status/attachements/attachements.component.ts index e3f2114a..35893b69 100644 --- a/src/app/components/stream/status/attachements/attachements.component.ts +++ b/src/app/components/stream/status/attachements/attachements.component.ts @@ -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); } diff --git a/src/app/services/mastodon-wrapper.service.ts b/src/app/services/mastodon-wrapper.service.ts index bb5c4c2f..e7e15f53 100644 --- a/src/app/services/mastodon-wrapper.service.ts +++ b/src/app/services/mastodon-wrapper.service.ts @@ -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 { + 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 { 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); }); } diff --git a/src/app/services/mastodon.service.ts b/src/app/services/mastodon.service.ts index 7bc5c0d1..c205c93d 100644 --- a/src/app/services/mastodon.service.ts +++ b/src/app/services/mastodon.service.ts @@ -132,12 +132,13 @@ export class MastodonService { return this.httpClient.post(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 { + 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 { 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; diff --git a/src/app/services/media.service.ts b/src/app/services/media.service.ts index 446ee543..b3c72470 100644 --- a/src/app/services/media.service.ts +++ b/src/app/services/media.service.ts @@ -51,26 +51,51 @@ 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 { if (media.attachment.description === media.description) return; - 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, account); - }); + 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; + let updatedMedia = medias.filter(x => x.id === media.id)[0]; + updatedMedia.attachment.description = att.description; + this.mediaSubject.next(medias); + }) + .catch((err) => { + console.warn('failing update'); + this.notificationService.notifyHttpError(err, account); + }); + } } async retrieveUpToDateMedia(account: AccountInfo): Promise { const allMedia = this.mediaSubject.value; let allPromises: Promise[] = []; - - for (const m of allMedia) { + + for (const m of allMedia) { let t = this.update(account, m); allPromises.push(t); } @@ -80,9 +105,9 @@ export class MediaService { return allMedia; } - addExistingMedia(media: MediaWrapper){ - if(!this.fileCache[media.attachment.url]) return; - + addExistingMedia(media: MediaWrapper) { + if (!this.fileCache[media.attachment.url]) return; + media.file = this.fileCache[media.attachment.url]; let medias = this.mediaSubject.value; medias.push(media); @@ -102,11 +127,15 @@ export class MediaService { migrateMedias(account: AccountInfo) { let medias = this.mediaSubject.value; medias.forEach(media => { - media.isMigrating = true; + 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; @@ -131,7 +160,7 @@ export class MediaWrapper { public id: string, public file: File, attachment: Attachment) { - this.attachment = attachment; + this.attachment = attachment; } private _attachment: Attachment; @@ -139,7 +168,7 @@ export class MediaWrapper { return this._attachment; } - public set attachment(value: Attachment){ + public set attachment(value: Attachment) { if (value && value.meta && value.meta.audio_encode) { this.audioType = `audio/${value.meta.audio_encode}`; } else if (value && value.pleroma && value.pleroma.mime_type) { @@ -152,4 +181,6 @@ export class MediaWrapper { public description: string; public isMigrating: boolean; public audioType: string; + + public isEdited: boolean; }