aboutsummaryrefslogtreecommitdiff
path: root/crates/whirl_api/src/routes/worlds/vip/mod.rs
blob: 7b7aee012df98560c2c2c2a5cfeabb9a2a573f56 (plain) (blame)
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
// Copyright (C) 2021-2021 The Whirlsplash Collective
// SPDX-License-Identifier: GPL-3.0-only

mod structures;

use std::str::from_utf8;

use axum::response;

use crate::routes::worlds::vip::structures::Vip;

#[derive(Serialize, Deserialize)]
pub struct Parameters {
  username: Option<String>,
}

#[allow(clippy::needless_pass_by_value, clippy::unused_async)]
pub async fn vip(
  axum::extract::Query(req): axum::extract::Query<Parameters>,
) -> impl response::IntoResponse {
  let mut easy = curl::easy::Easy::new();
  let mut error = String::new();

  let username = req.username;
  if username.is_none()
    || username
      .as_ref()
      .map_or(false, std::string::String::is_empty)
  {
    error = "no username query parameter provided, defaulting to 'null'".to_string();
  }

  easy
    .url(&format!(
      "http://www-dynamic.us.worlds.net/cgi-bin/vip.pl?Username={}",
      username.unwrap_or_else(|| "null".to_string()),
    ))
    .unwrap();

  let mut response = Vec::new();

  // https://docs.rs/curl/0.4.6/curl/easy/struct.Easy.html
  {
    let mut transfer = easy.transfer();
    transfer
      .write_function(|data| {
        response.extend_from_slice(data);
        Ok(data.len())
      })
      .unwrap();
    transfer.perform().unwrap();
  }

  (
    hyper::StatusCode::OK,
    response::Json(Vip {
      vip:   from_utf8(&response)
        .unwrap()
        .to_string()
        .contains("You're already a VIP!"),
      error: if error.is_empty() { None } else { Some(error) },
    }),
  )
}