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
use crate::api::discord::{global_discord_client, rcos_discord_verified_role_id};
use crate::api::rcos::users::discord_whois::DiscordWhoIs;
use crate::error::TelescopeError;
use crate::env::global_config;
use crate::web::services::auth::identity::AuthenticationCookie;
use actix_web::HttpResponse;
use reqwest::header::LOCATION;
use serenity::model::prelude::RoleId;
#[get("/join_discord")]
pub async fn handle(auth: AuthenticationCookie) -> Result<HttpResponse, TelescopeError> {
let user_id = auth.get_user_id_or_error().await?;
let discord = auth.get_discord();
if discord.is_none() {
return Err(TelescopeError::BadRequest {
header: "Could not join RCOS Discord".to_string(),
message: "Please log out and then login with Discord to continue".to_string(),
show_status_code: false,
});
}
let discord = discord.unwrap();
let discord_user_id: u64 = discord
.get_discord_id()
.await?
.as_str()
.parse::<u64>()
.expect("Malformed Discord user ID.");
let rcs_id: Option<String> = auth.get_rcs_id().await?;
if rcs_id.is_none() {
return Err(TelescopeError::BadRequest {
header: "Could not join RCOS Discord".to_string(),
message: "RPI CAS must be linked to join RCOS Discord.".to_string(),
show_status_code: false,
});
}
let rcs_id: String = rcs_id.unwrap();
let user_info = DiscordWhoIs::send(discord_user_id)
.await?
.get_user()
.expect("User must exist");
let fname: &str = user_info.first_name.as_str();
let lname: &str = user_info.last_name.as_str();
let cohort: Option<i64> = user_info.cohort.clone();
let cohort_format = cohort
.map(|cohort| format!("'{} ", (cohort + 4) % 100))
.unwrap_or("".to_string());
let nickname: String = format!(
"{} {} {}({})",
fname
.chars()
.zip(0..)
.filter(|(_, ind)| *ind < 20)
.map(|(c, _)| c)
.collect::<String>(),
lname.chars().next().unwrap(),
cohort_format,
rcs_id
);
let verified_role: RoleId = rcos_discord_verified_role_id()
.await?
.ok_or(TelescopeError::ise("Could not get Verified role ID."))?;
discord
.add_to_rcos_guild(Some(nickname), vec![verified_role])
.await?;
let rcos_discord_guild = global_config().discord_config.rcos_guild_id();
global_discord_client()
.add_member_role(rcos_discord_guild, discord_user_id, verified_role.0)
.await
.map_err(TelescopeError::serenity_error)?;
Ok(HttpResponse::Found()
.header(LOCATION, format!("/user/{}", user_id))
.finish())
}