egg-mode-rs / egg-mode

a twitter api crate for rust
https://crates.io/crates/egg-mode
Mozilla Public License 2.0
371 stars 65 forks source link

Checking whether the target account follows the source account #107

Closed bdashore3 closed 4 years ago

bdashore3 commented 4 years ago

Hi, I was looking at the docs and wanted to know how to check if the account that mentioned me actually follows me.

I'm currently using the mentions timeline and I understand that gives me a TwitterUser struct. How would I tell if that user is following me?

QuietMisdreavus commented 4 years ago

The way you find whether a user follows another user is by using the user::relation function. You'd pull that info like this:

let other_user = tweet.user.unwrap();
let relation = egg_mode::user::relation(me.id, other_user.id).await?;

if relation.source.followed_by {
    // add marker saying "Follows you" or the like
}
bdashore3 commented 4 years ago

Thanks for the response! However, what if I wanted to use relation_lookup() since me.id is the authenticated user?

QuietMisdreavus commented 4 years ago

If you wanted to use relation_lookup instead, it would look something like this:

use egg_mode::user::Connection::FollowedBy;

let other_user = tweet.user.unwrap();
let lookup = egg_mode::user::relation_lookup(&[other_user.id]).await?;

if lookup[0].connections.contains(&FollowedBy) {
    // add marker saying "Follows you" or the like
}
bdashore3 commented 4 years ago

So, cargo says the function takes 2 arguments and I added the user token in there, but I'm getting this error and I'm not sure why. I tried providing a non-refed u64 and binded the array separate to the function call.

image

let other_user = tweet.user.unwrap();
let lookup = relation_lookup(&[other_user.id], &config.token).await?;
QuietMisdreavus commented 4 years ago

Oh dang, it looks like the impl From<&u64> for UserID got removed at some point. Replace the array with something like [other_user.id].iter().cloned(), that should make it work. I'll have to add some of the From impls back in.

bdashore3 commented 4 years ago

Yes! That worked! Now for some weird reason, there's this error and I don't know what to make of it. They're both the same type, so why is Rust giving that error?

image

Here's the code

let other_user = status.user.as_ref().unwrap();
let lookup = relation_lookup([other_user.id].iter().cloned(), &config.token).await?;

if lookup[0].connections.contains(&Connection::FollowedBy) { <- Error occurring on the contains check
      let _ = retweet(status.id, &config.token).await;
      let _ = like(status.id, &config.token).await;

      TWEET_MAP.insert(status.id, since_epoch + 3600);
}
QuietMisdreavus commented 4 years ago

Oh goodness, why did i not throw PartialEq on that enum? Since there's no == operator for that type, you'll have to use the matches! macro instead:

let other_user = status.user.as_ref().unwrap();
let lookup = relation_lookup([other_user.id].iter().cloned(), &config.token).await?;

if lookup[0].connections.iter().any(|c| matches!(c, &Connection::FollowedBy)) {
      let _ = retweet(status.id, &config.token).await;
      let _ = like(status.id, &config.token).await;

      TWEET_MAP.insert(status.id, since_epoch + 3600);
}