From 4cb1b16262e95f05d384e3e38d6f91c89314417f Mon Sep 17 00:00:00 2001 From: Storm Dragon Date: Fri, 1 Aug 2025 17:08:03 -0400 Subject: [PATCH] Vote command actually works now. --- ttyverse.pl | 132 ++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 122 insertions(+), 10 deletions(-) diff --git a/ttyverse.pl b/ttyverse.pl index a9c6ad4..3fbb0a4 100755 --- a/ttyverse.pl +++ b/ttyverse.pl @@ -4851,19 +4851,47 @@ sub vote_on_poll { my $choices = shift; # Find the post by its menu code - my $post_ref = &findtarget($post_code); + my $post_ref = &get_post($post_code); unless ($post_ref) { print $stdout "-- no such post: $post_code\n"; return 0; } - # Check if post has a poll - unless (exists($post_ref->{'poll'}) && $post_ref->{'poll'}) { - print $stdout "-- post $post_code does not have a poll\n"; - return 0; + # Check if post has a poll - handle boost-aware poll detection + my $poll = undef; + if (exists($post_ref->{'reblog'}) && exists($post_ref->{'reblog'}->{'poll'})) { + # Poll is in the boosted post + $poll = $post_ref->{'reblog'}->{'poll'}; + } elsif (exists($post_ref->{'poll'})) { + # Poll is in the main post + $poll = $post_ref->{'poll'}; } - my $poll = $post_ref->{'poll'}; + # If no poll found in cached data, this post may not have a poll + # or poll data was stripped by IPC. Try to proceed with voting anyway + # by making the API call directly with the post ID we have. + unless ($poll) { + print $stdout "-- no poll data in cache, attempting direct vote\n" if ($verbose); + + # We'll try to vote anyway using the post structure we have + # First, we need to get the correct post/poll ID for the API call + my $target_post_id; + if (exists($post_ref->{'reblog'}) && $post_ref->{'reblog'}->{'id_str'}) { + # This is a boost, get the reblogged post ID + $target_post_id = $post_ref->{'reblog'}->{'id_str'}; + } else { + # Regular post + $target_post_id = $post_ref->{'id_str'} || $post_ref->{'id'}; + } + + if (!$target_post_id) { + print $stdout "-- post $post_code does not have a poll\n"; + return 0; + } + + # Try to vote directly without poll validation + return &attempt_direct_poll_vote($target_post_id, $choices); + } # Check if poll is expired or already voted if ($poll->{'expired'}) { @@ -4901,8 +4929,15 @@ sub vote_on_poll { return 0; } - # Submit the vote - my $post_id = $post_ref->{'id_str'} || $post_ref->{'id'}; + # Submit the vote - get correct post ID for boosted posts + my $post_id; + if (exists($post_ref->{'reblog'}) && exists($post_ref->{'reblog'}->{'poll'})) { + # Use the reblog's ID for boosted posts with polls + $post_id = $post_ref->{'reblog'}->{'id_str'} || $post_ref->{'reblog'}->{'id'}; + } else { + # Use the main post ID + $post_id = $post_ref->{'id_str'} || $post_ref->{'id'}; + } my $vote_url = "$apibase/polls/$poll->{'id'}/votes"; # Build POST data with choices @@ -4916,7 +4951,7 @@ sub vote_on_poll { my $result = &backticks($useragent, '/dev/null', undef, $vote_url, $post_data, 1, @agentopts); - if ($result) { + if ($? == 0) { print $stdout "-- vote submitted successfully\n"; # Show which options were selected @@ -4934,13 +4969,90 @@ sub vote_on_poll { } } +# attempt to vote on a poll directly without full validation +# used when poll data is not available due to IPC limitations +sub attempt_direct_poll_vote { + my $post_id = shift; + my $choices = shift; + + print $stdout "-- attempting direct poll vote on post $post_id\n" if ($verbose); + + # First, try to get the poll ID by fetching the post + my $api_url = $idurl; + $api_url =~ s/%I/$post_id/g; + my $full_post = &grabjson($api_url, 0, 0, 0, undef, 1); + + unless ($full_post) { + print $stdout "-- could not fetch post data for voting\n"; + return 0; + } + + # Get poll data from the fetched post (boost-aware) + my $poll = undef; + if (exists($full_post->{'reblog'}) && exists($full_post->{'reblog'}->{'poll'})) { + $poll = $full_post->{'reblog'}->{'poll'}; + } elsif (exists($full_post->{'poll'})) { + $poll = $full_post->{'poll'}; + } + + unless ($poll && $poll->{'id'}) { + print $stdout "-- post does not have a poll\n"; + return 0; + } + + print $stdout "-- DEBUG: Found poll ID: " . $poll->{'id'} . "\n" if ($verbose); + + # Simple choice validation - just ensure choices are numbers + my @valid_choices = (); + foreach my $choice (split(/,/, $choices)) { + $choice =~ s/^\s+|\s+$//g; # trim whitespace + if ($choice =~ /^\d+$/ && $choice > 0) { + push @valid_choices, $choice - 1; # Convert to 0-based indexing + } else { + print $stdout "-- invalid choice: $choice\n"; + return 0; + } + } + + unless (@valid_choices) { + print $stdout "-- no valid choices specified\n"; + return 0; + } + + print $stdout "-- DEBUG: Valid choices: " . join(',', @valid_choices) . "\n" if ($verbose); + + # Submit the vote directly + my $vote_url = "$apibase/polls/$poll->{'id'}/votes"; + my $post_data = ''; + foreach my $choice_idx (@valid_choices) { + $post_data .= "&choices[]=$choice_idx"; + } + $post_data =~ s/^&//; # remove leading & + + print $stdout "-- DEBUG: Vote URL: $vote_url\n" if ($verbose); + print $stdout "-- DEBUG: Post data: $post_data\n" if ($verbose); + print $stdout "-- submitting direct vote on poll...\n"; + + my $result = &postjson($vote_url, $post_data); + + print $stdout "-- DEBUG: Vote result: " . (defined($result) ? "'$result'" : "undefined") . "\n" if ($verbose); + + if (defined($result)) { + print $stdout "-- vote submitted successfully\n"; + return 1; + } else { + print $stdout "-- failed to submit vote\n"; + return 0; + } +} + # reply to all users mentioned in a post sub reply_to_all { my $post_code = shift; my $reply_text = shift; # Find the post by its menu code - my $post_ref = &findtarget($post_code); + my $post_ref = &get_post($post_code); unless ($post_ref) { print $stdout "-- no such post: $post_code\n"; return 0;