forked from rust-lang/crates.io
-
Notifications
You must be signed in to change notification settings - Fork 0
/
me.rs
191 lines (166 loc) · 6.47 KB
/
me.rs
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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
use crate::auth::AuthCheck;
use crate::util::diesel::prelude::*;
use axum::extract::Path;
use axum::response::Response;
use axum::Json;
use axum_extra::json;
use axum_extra::response::ErasedJson;
use futures_util::FutureExt;
use http::request::Parts;
use std::collections::HashMap;
use crate::app::AppState;
use crate::controllers::helpers::pagination::{Paginated, PaginationOptions};
use crate::controllers::helpers::{ok_true, Paginate};
use crate::models::krate::CrateName;
use crate::models::{CrateOwner, Follow, OwnerKind, User, Version, VersionOwnerAction};
use crate::schema::{crate_owners, crates, emails, follows, users, versions};
use crate::util::errors::{bad_request, AppResult};
use crate::util::BytesRequest;
use crate::views::{EncodableMe, EncodablePrivateUser, EncodableVersion, OwnedCrate};
/// Handles the `GET /me` route.
pub async fn me(app: AppState, req: Parts) -> AppResult<Json<EncodableMe>> {
use diesel_async::RunQueryDsl;
let mut conn = app.db_read_prefer_primary().await?;
let user_id = AuthCheck::only_cookie()
.check(&req, &mut conn)
.await?
.user_id();
let (user, verified, email, verification_sent): (User, Option<bool>, Option<String>, bool) =
users::table
.find(user_id)
.left_join(emails::table)
.select((
User::as_select(),
emails::verified.nullable(),
emails::email.nullable(),
emails::token_generated_at.nullable().is_not_null(),
))
.first(&mut conn)
.await?;
let owned_crates = CrateOwner::by_owner_kind(OwnerKind::User)
.inner_join(crates::table)
.filter(crate_owners::owner_id.eq(user_id))
.select((crates::id, crates::name, crate_owners::email_notifications))
.order(crates::name.asc())
.load(&mut conn)
.await?
.into_iter()
.map(|(id, name, email_notifications)| OwnedCrate {
id,
name,
email_notifications,
})
.collect();
let verified = verified.unwrap_or(false);
let verification_sent = verified || verification_sent;
Ok(Json(EncodableMe {
user: EncodablePrivateUser::from(user, email, verified, verification_sent),
owned_crates,
}))
}
/// Handles the `GET /me/updates` route.
pub async fn updates(app: AppState, req: Parts) -> AppResult<ErasedJson> {
let mut conn = app.db_read_prefer_primary().await?;
let auth = AuthCheck::only_cookie().check(&req, &mut conn).await?;
let user = auth.user();
let followed_crates = Follow::belonging_to(user).select(follows::crate_id);
let query = versions::table
.inner_join(crates::table)
.left_outer_join(users::table)
.filter(crates::id.eq_any(followed_crates))
.order(versions::created_at.desc())
.select(<(Version, CrateName, Option<User>)>::as_select())
.pages_pagination(PaginationOptions::builder().gather(&req)?);
let data: Paginated<(Version, CrateName, Option<User>)> =
query.async_load(&mut conn).boxed().await?;
let more = data.next_page_params().is_some();
let versions = data.iter().map(|(v, ..)| v).collect::<Vec<_>>();
let actions = VersionOwnerAction::async_for_versions(&mut conn, &versions).await?;
let data = data
.into_iter()
.zip(actions)
.map(|((v, cn, pb), voas)| (v, cn, pb, voas));
let versions = data
.into_iter()
.map(|(version, crate_name, published_by, actions)| {
EncodableVersion::from(version, &crate_name.name, published_by, actions)
})
.collect::<Vec<_>>();
Ok(json!({
"versions": versions,
"meta": { "more": more },
}))
}
/// Handles the `PUT /confirm/:email_token` route
pub async fn confirm_user_email(state: AppState, Path(token): Path<String>) -> AppResult<Response> {
use diesel::update;
use diesel_async::RunQueryDsl;
let mut conn = state.db_write().await?;
let updated_rows = update(emails::table.filter(emails::token.eq(&token)))
.set(emails::verified.eq(true))
.execute(&mut conn)
.await?;
if updated_rows == 0 {
return Err(bad_request("Email belonging to token not found."));
}
ok_true()
}
/// Handles `PUT /me/email_notifications` route
pub async fn update_email_notifications(app: AppState, req: BytesRequest) -> AppResult<Response> {
use diesel::pg::upsert::excluded;
use diesel_async::RunQueryDsl;
let (parts, body) = req.0.into_parts();
#[derive(Deserialize)]
struct CrateEmailNotifications {
id: i32,
email_notifications: bool,
}
let updates: HashMap<i32, bool> = serde_json::from_slice::<Vec<CrateEmailNotifications>>(&body)
.map_err(|_| bad_request("invalid json request"))?
.iter()
.map(|c| (c.id, c.email_notifications))
.collect();
let mut conn = app.db_write().await?;
let user_id = AuthCheck::default()
.check(&parts, &mut conn)
.await?
.user_id();
// Build inserts from existing crates belonging to the current user
let to_insert = CrateOwner::by_owner_kind(OwnerKind::User)
.filter(crate_owners::owner_id.eq(user_id))
.select((
crate_owners::crate_id,
crate_owners::owner_id,
crate_owners::owner_kind,
crate_owners::email_notifications,
))
.load(&mut conn)
.await?
.into_iter()
// Remove records whose `email_notifications` will not change from their current value
.map(
|(c_id, o_id, o_kind, e_notifications): (i32, i32, i32, bool)| {
let current_e_notifications = *updates.get(&c_id).unwrap_or(&e_notifications);
(
crate_owners::crate_id.eq(c_id),
crate_owners::owner_id.eq(o_id),
crate_owners::owner_kind.eq(o_kind),
crate_owners::email_notifications.eq(current_e_notifications),
)
},
)
.collect::<Vec<_>>();
// Upsert crate owners; this should only actually execute updates
diesel::insert_into(crate_owners::table)
.values(&to_insert)
.on_conflict((
crate_owners::crate_id,
crate_owners::owner_id,
crate_owners::owner_kind,
))
.do_update()
.set(crate_owners::email_notifications.eq(excluded(crate_owners::email_notifications)))
.execute(&mut conn)
.await?;
ok_true()
}