Normalize poll and poll options

This commit is contained in:
Alex Gleason 2022-02-24 00:07:18 -05:00
parent 706bc08c88
commit f0dc233973
No known key found for this signature in database
GPG Key ID: 7211D1F99744FBB7
3 changed files with 48 additions and 1 deletions

View File

@ -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;

View File

@ -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);
});
});

View File

@ -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);