Skip to content

feat(Poll): add option to randomize poll choices #2082

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Apr 15, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion api/resolvers/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import performPaidAction from '../paidAction'
import { GqlAuthenticationError, GqlInputError } from '@/lib/error'
import { verifyHmac } from './wallet'
import { parse } from 'tldts'
import { shuffleArray } from '@/lib/rand'

function commentsOrderByClause (me, models, sort) {
const sharedSortsArray = []
Expand Down Expand Up @@ -1150,7 +1151,8 @@ export default {
poll.meVoted = false
}

poll.options = options
poll.randPollOptions = item?.randPollOptions
poll.options = poll.randPollOptions ? shuffleArray(options) : options
poll.count = options.reduce((t, o) => t + o.count, 0)

return poll
Expand Down
3 changes: 2 additions & 1 deletion api/typeDefs/item.js
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ export default gql`
text: String!, url: String!, boost: Int, status: String, logo: Int): ItemPaidAction!
upsertPoll(
id: ID, sub: String, title: String!, text: String, options: [String!]!, boost: Int, forward: [ItemForwardInput], pollExpiresAt: Date,
hash: String, hmac: String): ItemPaidAction!
randPollOptions: Boolean, hash: String, hmac: String): ItemPaidAction!
updateNoteId(id: ID!, noteId: String!): Item!
upsertComment(id: ID, text: String!, parentId: ID, boost: Int, hash: String, hmac: String): ItemPaidAction!
act(id: ID!, sats: Int, act: String, hasSendWallet: Boolean): ItemActPaidAction!
Expand All @@ -81,6 +81,7 @@ export default gql`
meInvoiceActionState: InvoiceActionState
count: Int!
options: [PollOption!]!
randPollOptions: Boolean
}

type Items {
Expand Down
8 changes: 7 additions & 1 deletion components/poll-form.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DateTimeInput, Form, Input, MarkdownInput, VariableInput } from '@/components/form'
import { Checkbox, DateTimeInput, Form, Input, MarkdownInput, VariableInput } from '@/components/form'
import { useApolloClient } from '@apollo/client'
import Countdown from './countdown'
import AdvPostForm, { AdvPostInitial } from './adv-post-form'
Expand Down Expand Up @@ -30,6 +30,7 @@ export function PollForm ({ item, sub, editThreshold, children }) {
text: item?.text || '',
options: initialOptions || ['', ''],
crosspost: item ? !!item.noteId : me?.privates?.nostrCrossposting,
randPollOptions: item?.poll?.randPollOptions || false,
pollExpiresAt: item ? item.pollExpiresAt : datePivot(new Date(), { hours: 25 }),
...AdvPostInitial({ forward: normalizeForwards(item?.forwards), boost: item?.boost }),
...SubSelectInitial({ sub: item?.subName || sub?.name })
Expand Down Expand Up @@ -68,6 +69,11 @@ export function PollForm ({ item, sub, editThreshold, children }) {
label='poll expiration'
name='pollExpiresAt'
className='pr-4'
groupClassName='mb-0'
/>
<Checkbox
label={<div className='d-flex align-items-center'>randomize order of poll choices</div>}
name='randPollOptions'
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

checkbox as children like poll expiration.

/>
</AdvPostForm>
<ItemButtonBar itemId={item?.id} />
Expand Down
1 change: 1 addition & 0 deletions fragments/items.js
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,7 @@ export const POLL_FIELDS = gql`
option
count
}
randPollOptions
}
}`

Expand Down
4 changes: 2 additions & 2 deletions fragments/paidAction.js
Original file line number Diff line number Diff line change
Expand Up @@ -192,10 +192,10 @@ export const UPSERT_POLL = gql`
${PAID_ACTION}
mutation upsertPoll($sub: String, $id: ID, $title: String!, $text: String,
$options: [String!]!, $boost: Int, $forward: [ItemForwardInput], $pollExpiresAt: Date,
${HASH_HMAC_INPUT_1}) {
$randPollOptions: Boolean, ${HASH_HMAC_INPUT_1}) {
upsertPoll(sub: $sub, id: $id, title: $title, text: $text,
options: $options, boost: $boost, forward: $forward, pollExpiresAt: $pollExpiresAt,
${HASH_HMAC_INPUT_2}) {
randPollOptions: $randPollOptions, ${HASH_HMAC_INPUT_2}) {
result {
id
deleteScheduledAt
Expand Down
4 changes: 4 additions & 0 deletions lib/rand.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
export function randInRange (min, max) {
return Math.random() * (max - min) + min
}

export function shuffleArray (array) {
return [...array].sort(() => Math.random() - 0.5)
}
1 change: 1 addition & 0 deletions lib/validate.js
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ export function pollSchema ({ numExistingChoices = 0, ...args }) {
test: arr => arr.length >= MIN_POLL_NUM_CHOICES - numExistingChoices
}),
pollExpiresAt: date().nullable().min(datePivot(new Date(), { days: 1 }), 'Expiration must be at least 1 day in the future'),
randPollOptions: boolean(),
...advPostSchemaMembers(args),
...subSelectSchemaMembers(args)
}).test({
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "Item" ADD COLUMN "randPollOptions" BOOLEAN NOT NULL DEFAULT false;
1 change: 1 addition & 0 deletions prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -603,6 +603,7 @@ model Item {
PollBlindVote PollBlindVote[]
ItemUserAgg ItemUserAgg[]
AutoSocialPost AutoSocialPost[]
randPollOptions Boolean @default(false)

@@index([uploadId])
@@index([lastZapAt])
Expand Down