From 4bd1531056d108ed2b04453e299cf9c6132e8572 Mon Sep 17 00:00:00 2001 From: Alex Gleason Date: Thu, 10 Mar 2022 16:25:11 -0600 Subject: [PATCH] More poll normalization fixes --- app/soapbox/components/poll.js | Bin 6614 -> 6528 bytes .../normalizers/__tests__/status-test.js | Bin 6001 -> 5994 bytes app/soapbox/normalizers/status.ts | 49 ++++++++++++++---- app/soapbox/reducers/__tests__/polls-test.js | Bin 900 -> 902 bytes 4 files changed, 39 insertions(+), 10 deletions(-) diff --git a/app/soapbox/components/poll.js b/app/soapbox/components/poll.js index d3ed229427261a428000f36f0f52eac578c851db..57d693003154ebcd62813e6d7c18dfc834ba3827 100644 GIT binary patch delta 70 zcmca+++e)n9*;^veol^FetBMeS$;`svAteqUUE)pN@}r2enClQex7GuN@~SsD_(#0 V&AlS48M(m9K}u6L$B0!i0|4F48pHqq delta 156 zcmZoLzGl4P9#2euK}lwQo?d!tiH3Sveo1PIx~4)+je;Eh&2cR diff --git a/app/soapbox/normalizers/__tests__/status-test.js b/app/soapbox/normalizers/__tests__/status-test.js index 54117e1671b2a2a091bf94f851b6ad2d74f66d96..f3c836f2313d396904b3dd3ccbd210584f2fbc5f 100644 GIT binary patch delta 38 ncmeyU_eyUAvnWemX->{&R#AT@5vNoQ5J%IROF^L)NP;i`_X-OC delta 28 hcmaE*_fc;HvnW$^>}GaRf2PSHjG`R1n$}z(3;>082ekkI diff --git a/app/soapbox/normalizers/status.ts b/app/soapbox/normalizers/status.ts index 71210c5027..7272062652 100644 --- a/app/soapbox/normalizers/status.ts +++ b/app/soapbox/normalizers/status.ts @@ -57,11 +57,12 @@ const PollRecord = ImmutableRecord({ emojis: ImmutableList(), expired: false, expires_at: new Date(), + id: '', multiple: false, options: ImmutableList(), voters_count: 0, votes_count: 0, - own_votes: ImmutableList(), + own_votes: null, voted: false, }); @@ -106,14 +107,42 @@ const normalizeMentions = (status: ImmutableMap) => { }); }; -// Normalize poll -const normalizePoll = (status: ImmutableMap) => { +// Normalize poll options +const normalizePollOptions = (poll: ImmutableMap) => { + return poll.update('options', (options: ImmutableList>) => { + return options.map(PollOptionRecord); + }); +}; + +// Normalize own_votes to `null` if empty (like Mastodon) +const normalizePollOwnVotes = (poll: ImmutableMap) => { + return poll.update('own_votes', ownVotes => { + return ownVotes?.size > 0 ? ownVotes : null; + }); +}; + +// Whether the user voted in the poll +const normalizePollVoted = (poll: ImmutableMap) => { + return poll.update('voted', voted => { + return typeof voted === 'boolean' ? voted : poll.get('own_votes')?.size > 0; + }); +}; + +// Normalize the actual poll +const normalizePoll = (poll: ImmutableMap) => { + return PollRecord( + poll.withMutations((poll: ImmutableMap) => { + normalizePollOptions(poll); + normalizePollOwnVotes(poll); + normalizePollVoted(poll); + }), + ); +}; + +// Normalize the poll in the status, if applicable +const normalizeStatusPoll = (status: ImmutableMap) => { if (status.hasIn(['poll', 'options'])) { - return status.update('poll', ImmutableMap(), poll => { - return PollRecord(poll).update('options', (options: ImmutableList>) => { - return options.map(PollOptionRecord); - }); - }); + return status.update('poll', ImmutableMap(), normalizePoll); } else { return status.set('poll', null); } @@ -161,12 +190,12 @@ const fixQuote = (status: ImmutableMap) => { }); }; -export const normalizeStatus = (status: ImmutableMap): IStatus => { +export const normalizeStatus = (status: ImmutableMap): IStatus => { return StatusRecord( status.withMutations(status => { normalizeAttachments(status); normalizeMentions(status); - normalizePoll(status); + normalizeStatusPoll(status); fixMentionsOrder(status); addSelfMention(status); fixQuote(status); diff --git a/app/soapbox/reducers/__tests__/polls-test.js b/app/soapbox/reducers/__tests__/polls-test.js index c3ecd5152b1dfc926e2b6aa1ac012261d9ffb8c1..b19ad858c5d2c98f745fc6c024aa6b53ce8fb2fe 100644 GIT binary patch delta 16 XcmZo+Z)4xU!_1Obnv*k`kGTv0C#M9l delta 14 VcmZo;Z(-lS!^{*NJ6V9a3;-OL1Fir7