]> cat aescling's git repositories - mastodon.git/commitdiff
When deleting & redrafting a poll, fill in closest expires_in (#11203)
authorThibG <thib@sitedethib.com>
Mon, 1 Jul 2019 22:36:16 +0000 (00:36 +0200)
committerEugen Rochko <eugen@zeonfederated.com>
Mon, 1 Jul 2019 22:36:16 +0000 (00:36 +0200)
Use the smallest preset expires_in such that the new poll would
not expire before the old one.

In the typical case of a quick delete & redraft, this results in
using the same poll duration.

Fixes #10567

app/javascript/mastodon/reducers/compose.js

index 8cdd29bfeb3b5a7689a8187e0fa53507e136cfd6..fae7522b2632e0923a39cedf47800c62edd53496 100644 (file)
@@ -195,6 +195,12 @@ const expandMentions = status => {
   return fragment.innerHTML;
 };
 
+const expiresInFromExpiresAt = expires_at => {
+  if (!expires_at) return 24 * 3600;
+  const delta = (new Date(expires_at).getTime() - Date.now()) / 1000;
+  return [300, 1800, 3600, 21600, 86400, 259200, 604800].find(expires_in => expires_in >= delta) || 24 * 3600;
+};
+
 export default function compose(state = initialState, action) {
   switch(action.type) {
   case STORE_HYDRATE:
@@ -353,7 +359,7 @@ export default function compose(state = initialState, action) {
         map.set('poll', ImmutableMap({
           options: action.status.getIn(['poll', 'options']).map(x => x.get('title')),
           multiple: action.status.getIn(['poll', 'multiple']),
-          expires_in: 24 * 3600,
+          expires_in: expiresInFromExpiresAt(action.status.getIn(['poll', 'expires_at'])),
         }));
       }
     });