From c6d893a71dede65dd88a0dfac81178c8e81e27d2 Mon Sep 17 00:00:00 2001 From: Eugen Rochko Date: Thu, 22 Sep 2016 21:39:53 +0200 Subject: [PATCH] Uploading/undoing media modifies status text. Also: status text trimmed before validation --- .../components/reducers/compose.jsx | 40 ++++++++++++++----- app/controllers/media_controller.rb | 2 +- app/models/status.rb | 4 ++ 3 files changed, 34 insertions(+), 12 deletions(-) diff --git a/app/assets/javascripts/components/reducers/compose.jsx b/app/assets/javascripts/components/reducers/compose.jsx index 3974e40c64..caddb864c9 100644 --- a/app/assets/javascripts/components/reducers/compose.jsx +++ b/app/assets/javascripts/components/reducers/compose.jsx @@ -27,6 +27,32 @@ function statusToTextMentions(status) { return Immutable.OrderedSet([`@${status.getIn(['account', 'acct'])} `]).union(status.get('mentions').map(mention => `@${mention.get('acct')} `)).join(''); }; +function clearAll(state) { + return state.withMutations(map => { + map.set('text', ''); + map.set('is_submitting', false); + map.set('in_reply_to', null); + map.update('media_attachments', list => list.clear()); + }); +}; + +function appendMedia(state, media) { + return state.withMutations(map => { + map.update('media_attachments', list => list.push(media)); + map.set('is_uploading', false); + map.update('text', oldText => `${oldText} ${media.get('text_url')}`.trim()); + }); +}; + +function removeMedia(state, mediaId) { + const media = state.get('media_attachments').find(item => item.get('id') === mediaId); + + return state.withMutations(map => { + map.update('media_attachments', list => list.filterNot(item => item.get('id') === mediaId)); + map.update('text', text => text.replace(media.get('text_url'), '').trim()); + }); +}; + export default function compose(state = initialState, action) { switch(action.type) { case COMPOSE_CHANGE: @@ -44,25 +70,17 @@ export default function compose(state = initialState, action) { case COMPOSE_SUBMIT_REQUEST: return state.set('is_submitting', true); case COMPOSE_SUBMIT_SUCCESS: - return state.withMutations(map => { - map.set('text', ''); - map.set('is_submitting', false); - map.set('in_reply_to', null); - map.update('media_attachments', list => list.clear()); - }); + return clearAll(state); case COMPOSE_SUBMIT_FAIL: return state.set('is_submitting', false); case COMPOSE_UPLOAD_REQUEST: return state.set('is_uploading', true); case COMPOSE_UPLOAD_SUCCESS: - return state.withMutations(map => { - map.update('media_attachments', list => list.push(Immutable.fromJS(action.media))); - map.set('is_uploading', false); - }); + return appendMedia(state, Immutable.fromJS(action.media)); case COMPOSE_UPLOAD_FAIL: return state.set('is_uploading', false); case COMPOSE_UPLOAD_UNDO: - return state.update('media_attachments', list => list.filterNot(item => item.get('id') === action.media_id)); + return removeMedia(state, action.media_id); case COMPOSE_UPLOAD_PROGRESS: return state.set('progress', Math.round((action.loaded / action.total) * 100)); case TIMELINE_DELETE: diff --git a/app/controllers/media_controller.rb b/app/controllers/media_controller.rb index 5a7b0bcbd6..edfcdae530 100644 --- a/app/controllers/media_controller.rb +++ b/app/controllers/media_controller.rb @@ -2,7 +2,7 @@ class MediaController < ApplicationController before_action :set_media_attachment def show - redirect TagManager.instance.url_for(@media_attachment.status) + redirect_to TagManager.instance.url_for(@media_attachment.status) end private diff --git a/app/models/status.rb b/app/models/status.rb index e336cfe620..ae5c4b4962 100644 --- a/app/models/status.rb +++ b/app/models/status.rb @@ -89,4 +89,8 @@ class Status < ApplicationRecord def self.reblogs_map(status_ids, account_id) self.where(reblog_of_id: status_ids).where(account_id: account_id).map { |s| [s.reblog_of_id, true] }.to_h end + + before_validation do + self.text.strip! + end end