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

mod structures;

use std::str::from_utf8;

use actix_web::{HttpRequest, HttpResponse};

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

// error: this argument is passed by value, but not consumed in the function
// body
#[allow(clippy::needless_pass_by_value)]
pub fn vip(req: HttpRequest) -> HttpResponse {
  let queries = qstring::QString::from(req.query_string());
  let mut easy = curl::easy::Easy::new();
  let mut error = String::new();

  let username = queries.get("username");
  if username.is_none() || username.map_or(false, str::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("null"),
    ))
    .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();
  }

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