Skip to content
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

feat(channels): Add support for Threads #618

Merged
merged 14 commits into from
Nov 5, 2024
Merged

feat(channels): Add support for Threads #618

merged 14 commits into from
Nov 5, 2024

Conversation

elisa-a-v
Copy link
Contributor

@elisa-a-v elisa-a-v commented Oct 31, 2024

Addresses this issue

This PR introduces full integration with the Threads API, allowing the bots to post directly to Threads.

Considerations

  1. The official Threads API requires images to be hosted on a public server, so we need to upload them to S3 first. To achieve this:
    • Set up a bucket with the proper policy, which should at least have the statements shown below, replacing <BUCKET_NAME>, <AWS_ACCOUNT_ID>, and <USER> with the username for the IAM user whose credentials are specified in the AWS .env variables (AWS_ACCESS_KEY_ID and AWS_SECRET_ACCESS_KEY).
    • Add the bucket name and region to your environment variables as THREADS_BUCKET_NAME and THREADS_BUCKET_ZONE.
{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": {
                "AWS": "arn:aws:iam::<AWS_ACCOUNT_ID>:user/<USER>"
            },
            "Action": [
                "s3:PutObject",
                "s3:GetObject",
                "s3:PutObjectAcl"
            ],
            "Resource": "arn:aws:s3:::<BUCKET_NAME>/*"
        },
        {
            "Sid": "PublicReadGetObject",
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::<BUCKET_NAME>/*"
        }
    ]
}
  1. To generate the access tokens for the bots:
    • You need a meta developer account to create an app with the use case "Access to Threads API"
    • Enable the permissions threads_basic and threads_content_publish in your app.
    • Add a Threads Tester Role with the Threads user for the bot.
    • Add the THREADS_APP_ID and THREADS_APP_SECRET to the .env file
    • Run the script scripts/get-threads-keys.py and follow the instructions which will provide the credentials to create a Channel via Django admin.

TO DO:

  • Configure bucket to delete images after a set period of time. They don't need to be kept for long, we only need them until we have created the container in Threads.
  • Add instructions to wiki.
  • Tests?

@elisa-a-v elisa-a-v requested a review from ERosendo October 31, 2024 22:35
@elisa-a-v elisa-a-v requested a review from mlissner October 31, 2024 22:41
Comment on lines -50 to -51
TWITTER_ACCESS_TOKEN=""
TWITTER_ACCESS_TOKEN_SECRET=""
Copy link
Contributor Author

Choose a reason for hiding this comment

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

These were removed because they weren't used anymore, as discussed with @ERosendo

Copy link
Contributor

Choose a reason for hiding this comment

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

You're right. We should definitely clean up the settings module as well.

Copy link
Member

@mlissner mlissner left a comment

Choose a reason for hiding this comment

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

I gave it a quick skim. Looks about right, generally!

One thing to consider is the png->jpeg code we discussed today. My instinct on the call was that Doctor should be producing jpeg thumbnails for you instead of pngs, but jpeg is an inferior format for this and I wouldn't want to post jpegs on BlueSky or Twitter, where PNG makes more sense. So I think that means you could:

  • Call doctor to get png thumbnails for those, then...
  • Call doctor to get jpegs for threads

Or:

  • Call doctor for the pngs, then...
  • Convert them here.

Honestly, the latter seems easier and you've got the code. :shipit: !

I'm also a bit curious about your use of envs, but I'll let Eduardo chime in on that if we need changes there.

Finally, can you please open an issue to update our website to put the threads info on it in all the right places?

bc/channel/models.py Outdated Show resolved Hide resolved
Copy link
Contributor

@ERosendo ERosendo left a comment

Choose a reason for hiding this comment

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

@elisa-a-v The code looks great! Just wanted to point out a couple of things we might have missed when creating this to-do list:

  1. Threads logo: We need to add an SVG file for the Threads logo (the one we use in buttons). These files usually go in the inline folder inside the includes directory of the assets folder.

  2. Follow button template: The follow-button.html template needs to be updated to use the SVG file mentioned above. This fragment is used in the detail page for the little cases bot. For reference, I've included the red rendered by this template below:

    Screenshot 2024-11-04 at 1 30 24 PM

    We need the logo to make the button look complete:

    image

  3. AWS Credentials: I encountered some issues while setting up the AWS credentials. This might be a good opportunity to revisit our approach and potentially align it with the method used for courtlistener. Here's the relevant issue in courtlistener for reference: Error 403 in development environment when uploading files to S3 courtlistener#4018

  4. Token Refresh Mechanism: This PR introduces a script to generate our initial long-lived tokens. However, it currently lacks a mechanism for automatic token refresh before expiration.

    We can merge this PR as-is and create a separate issue to address the token refresh functionality. However, We should prioritize and implement this mechanism before our tokens expire.

Copy link
Contributor

@ERosendo ERosendo left a comment

Choose a reason for hiding this comment

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

LGTM :shipit:

@mlissner
Copy link
Member

mlissner commented Nov 4, 2024

Exciting. Shall I merge and what's the best strategy for launching?

@elisa-a-v
Copy link
Contributor Author

Shall I merge

Sure! If you both approved, go for it.

and what's the best strategy for launching?

Well, I'm not sure what you mean, I guess the first step would be to create a big cases Thread account if there isn't one, then run the script and create the new channel.

Oh and btw, you asked me to open an issue to add the Threads bot info to the website and everywhere else it should be displayed, should I open that issue in this same repo or a different one?

@mlissner mlissner merged commit 3f7e3bb into main Nov 5, 2024
10 checks passed
@mlissner mlissner deleted the threads-support branch November 5, 2024 20:18
@mlissner
Copy link
Member

mlissner commented Nov 5, 2024

Merged! Here we go, thank you!

For launching, I do mean how we create the accounts, set them up, etc, all that fun stuff. I'm happy to help if needed, but ideally we'd have this documented on the wiki, and it'd be something I wouldn't need to be too involved in? Maybe Gianfranco can help instead since he has server access?

Yep, the issue for adding threads info the website should go here. :)

Copy link

sentry-io bot commented Nov 5, 2024

Suspect Issues

This pull request was deployed and Sentry observed the following issues:

  • ‼️ ImportError: cannot import name 'AWS_SESSION_TOKEN' from 'bc.settings' (/opt/bigcases2/bc/settings/init.py) bc.core.utils.s3 in <module> View Issue

Did you find this useful? React with a 👍 or 👎

@ERosendo
Copy link
Contributor

ERosendo commented Nov 6, 2024

@mlissner Elisa created a new wiki page that outlines the steps needed to create a Threads app and get values for new environment variables.

https://github.com/freelawproject/bigcases2/wiki/One%E2%80%90Time-Setup-to-enable-Threads

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Development

Successfully merging this pull request may close these issues.

3 participants