From f0dc2339731cf5b897778cda1467a2118dc6ff68 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 24 Feb 2022 00:07:18 -0500 Subject: [PATCH] Normalize poll and poll options --- .../ui/util/pending_status_builder.js | 2 +- .../normalizers/__tests__/status-test.js | 17 +++++++++++ app/soapbox/normalizers/status.js | 30 +++++++++++++++++++ 3 files changed, 48 insertions(+), 1 deletion(-) diff --git a/app/soapbox/features/ui/util/pending_status_builder.js b/app/soapbox/features/ui/util/pending_status_builder.js index 5bcc095c6..8ea186f56 100644 --- a/app/soapbox/features/ui/util/pending_status_builder.js +++ b/app/soapbox/features/ui/util/pending_status_builder.js @@ -17,7 +17,7 @@ const buildMentions = pendingStatus => { const buildPoll = pendingStatus => { if (pendingStatus.hasIn(['poll', 'options'])) { return pendingStatus.get('poll').update('options', options => { - return options.map(option => ImmutableMap({ title: option, votes_count: 0 })); + return options.map(title => ImmutableMap({ title })); }); } else { return null; diff --git a/app/soapbox/normalizers/__tests__/status-test.js b/app/soapbox/normalizers/__tests__/status-test.js index c582d230d..26428e3ba 100644 --- a/app/soapbox/normalizers/__tests__/status-test.js +++ b/app/soapbox/normalizers/__tests__/status-test.js @@ -134,4 +134,21 @@ describe('normalizeStatus', () => { expect(result.toJS()).toMatchObject(missing); }); + + it('normalizes poll and poll options', () => { + const status = fromJS({ poll: { options: [{ title: 'Apples' }] } }); + const result = normalizeStatus(status); + + const expected = { + options: [{ title: 'Apples', votes_count: 0 }], + emojis: [], + expired: false, + multiple: false, + voters_count: 0, + votes_count: 0, + }; + + expect(result.get('poll').toJS()).toMatchObject(expected); + expect(result.getIn(['poll', 'expires_at']) instanceof Date).toBe(true); + }); }); diff --git a/app/soapbox/normalizers/status.js b/app/soapbox/normalizers/status.js index 38819fb2d..8927546e8 100644 --- a/app/soapbox/normalizers/status.js +++ b/app/soapbox/normalizers/status.js @@ -28,6 +28,18 @@ const baseStatus = ImmutableMap({ visibility: 'public', }); +const basePollOption = ImmutableMap({ title: '', votes_count: 0 }); + +const basePoll = ImmutableMap({ + emojis: ImmutableList(), + expired: false, + expires_at: new Date(Date.now() + 1000 * (60 * 5)), // 5 minutes + multiple: false, + options: ImmutableList(), + voters_count: 0, + votes_count: 0, +}); + // Merger function for only overriding undefined values const mergeDefined = (oldVal, newVal) => oldVal === undefined ? newVal : oldVal; @@ -77,6 +89,23 @@ const normalizeMentions = status => { }); }; +// Normalize poll option +const normalizePollOption = option => { + return option.mergeWith(mergeDefined, basePollOption); +}; + +// Normalize poll +const normalizePoll = status => { + if (status.hasIn(['poll', 'options'])) { + return status.update('poll', ImmutableMap(), poll => { + return poll.mergeWith(mergeDefined, basePoll).update('options', options => { + return options.map(normalizePollOption); + }); + }); + } else { + return status.set('poll', null); + } +}; // Fix order of mentions const fixMentionsOrder = status => { const mentions = status.get('mentions', ImmutableList()); @@ -124,6 +153,7 @@ export const normalizeStatus = status => { mergeBase(status); normalizeAttachments(status); normalizeMentions(status); + normalizePoll(status); fixMentionsOrder(status); addSelfMention(status); fixQuote(status);