(
config: &DiscordConfig,
logged_in_user: Option<&CurrentUser>,
id: Id<UserMarker>,
)
| 405 | } |
| 406 | |
| 407 | async fn fetch_discord_user( |
| 408 | config: &DiscordConfig, |
| 409 | logged_in_user: Option<&CurrentUser>, |
| 410 | id: Id<UserMarker>, |
| 411 | ) -> ApiResult<DiscordUser> { |
| 412 | // shortcut if were trying to fetch the currently signed in user! |
| 413 | if let Some(current_user) = &logged_in_user { |
| 414 | if id == current_user.id { |
| 415 | return Ok(DiscordUser { |
| 416 | id, |
| 417 | avatar: current_user.avatar.map(|v| v.to_string()), |
| 418 | discriminator: current_user.discriminator.to_string(), |
| 419 | username: current_user.name.clone(), |
| 420 | }); |
| 421 | } |
| 422 | } |
| 423 | |
| 424 | match config.client.user(id).await { |
| 425 | Ok(v) => { |
| 426 | let user = v.model().await.map_err(|err| { |
| 427 | error!(?err, "failed fetching user"); |
| 428 | ApiErrorResponse::InternalError |
| 429 | })?; |
| 430 | Ok(DiscordUser { |
| 431 | id, |
| 432 | avatar: user.avatar.map(|v| v.to_string()), |
| 433 | discriminator: user.discriminator.to_string(), |
| 434 | username: user.name, |
| 435 | }) |
| 436 | } |
| 437 | Err(err) => match err.kind() { |
| 438 | twilight_http::error::ErrorType::Response { |
| 439 | error: |
| 440 | ApiError::General(GeneralApiError { |
| 441 | code: 10013, // Unknown user |
| 442 | .. |
| 443 | }), |
| 444 | .. |
| 445 | } => { |
| 446 | // Use mock values, user was most likely deleted |
| 447 | // |
| 448 | // Question: should we purge deleted user's plugins from the DB? |
| 449 | // is there potentially user info we might need to remove (think: gdpr?) |
| 450 | Ok(DiscordUser { |
| 451 | id, |
| 452 | username: "Deleted user".to_owned(), |
| 453 | discriminator: "0000".to_owned(), |
| 454 | avatar: None, |
| 455 | }) |
| 456 | } |
| 457 | _ => { |
| 458 | error!(?err, "failed fetching user"); |
| 459 | Err(ApiErrorResponse::InternalError) |
| 460 | } |
| 461 | }, |
| 462 | } |
| 463 | } |
| 464 |
no test coverage detected