1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
|
//! The users API.
use std::backtrace::Backtrace;
use http::StatusCode;
use snafu::GenerateImplicitData;
use crate::api::users::user_blocks::BlockedUsersBuilder;
use crate::models::UserId;
use crate::{error, GitHubError, Octocrab};
pub use self::follow::{ListUserFollowerBuilder, ListUserFollowingBuilder};
use self::user_repos::ListUserReposBuilder;
mod follow;
mod user_blocks;
mod user_repos;
pub(crate) enum UserRef {
ByString(String),
ById(UserId),
}
impl std::fmt::Display for UserRef {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
UserRef::ByString(str) => write!(f, "users/{}", str),
UserRef::ById(id) => write!(f, "user/{}", id),
}
}
}
pub struct UserHandler<'octo> {
crab: &'octo Octocrab,
user: UserRef,
}
impl<'octo> UserHandler<'octo> {
pub(crate) fn new(crab: &'octo Octocrab, user: UserRef) -> Self {
Self { crab, user }
}
/// Get this users profile info
pub async fn profile(&self) -> crate::Result<crate::models::UserProfile> {
// build the route to get info on this user
let route = format!("/{}", self.user);
// get info on the specified user
self.crab.get(route, None::<&()>).await
}
/// List this users that follow this user
pub fn followers(&self) -> ListUserFollowerBuilder {
ListUserFollowerBuilder::new(self)
}
/// List this user is following
pub fn following(&self) -> ListUserFollowingBuilder {
ListUserFollowingBuilder::new(self)
}
pub fn repos(&self) -> ListUserReposBuilder<'_, '_> {
ListUserReposBuilder::new(self)
}
/// API for listing blocked users
/// you must pass authentication information with your requests
pub fn blocks(&self) -> BlockedUsersBuilder {
BlockedUsersBuilder::new(self)
}
///## Check if a user is blocked by the authenticated user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The token must have the following permission set: `blocking:read`
///
///```no_run
/// async fn run() -> octocrab::Result<bool> {
/// let is_blocked = octocrab::instance()
/// .users("current_user")
/// .is_blocked("some_user")
/// .await?;
/// Ok(is_blocked)
/// }
pub async fn is_blocked(&self, username: &str) -> crate::Result<bool> {
let route = format!("/user/blocks/{}", username);
let response = self.crab._get(route).await?;
Ok(response.status() == 204)
}
///## Blocks the given user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The token must have the following permission set: `blocking:read`
///
///```no_run
/// async fn run() -> octocrab::Result<()> {
/// octocrab::instance()
/// .users("current_user")
/// .block_user("some_user")
/// .await
/// }
pub async fn block_user(&self, username: &str) -> crate::Result<()> {
let route = format!("/user/blocks/{}", username);
/* '204 not found' is returned if user blocked */
let result: crate::Result<()> = self.crab.put(route, None::<&()>).await;
match result {
Ok(_) => Err(error::Error::GitHub {
source: GitHubError {
status_code: StatusCode::OK,
documentation_url: None,
errors: None,
message: "".to_string(),
},
backtrace: Backtrace::generate(),
}),
Err(_v) => Ok(()),
}
}
///## Unblocks the given user
///works with the following token types:
///[GitHub App user access tokens](https://docs.github.com/en/apps/creating-github-apps/authenticating-with-a-github-app/generating-a-user-access-token-for-a-github-app)
///[Fine-grained personal access tokens](https://docs.github.com/en/authentication/keeping-your-account-and-data-secure/managing-your-personal-access-tokens#creating-a-fine-grained-personal-access-token)
///
///The token must have the following permission set: `blocking:read`
///
///```no_run
/// async fn run() -> octocrab::Result<()> {
/// octocrab::instance()
/// .users("current_user")
/// .unblock_user("some_user")
/// .await
/// }
pub async fn unblock_user(&self, username: &str) -> crate::Result<()> {
let route = format!("/user/blocks/{}", username);
self.crab.delete(route, None::<&()>).await
}
}
|