Skip to content

Commit b2dde91

Browse files
committed
unify user/userid paths into singular handlers
1 parent b32674c commit b2dde91

4 files changed

Lines changed: 89 additions & 257 deletions

File tree

src/web/handlers.rs

Lines changed: 38 additions & 185 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use super::{
33
schema::{
44
AvailableLogs, AvailableLogsParams, Channel, ChannelIdType, ChannelLogsByDatePath,
55
ChannelLogsStats, ChannelParam, ChannelsList, LogsParams, LogsPathChannel, SearchParams,
6-
UserLogPathParams, UserLogsPath, UserLogsStats, UserNameHistoryParam, UserParam,
6+
UserIdType, UserLogPathParams, UserLogsDatePath, UserLogsStats, UserNameHistoryParam,
7+
UserParam,
78
},
89
};
910
use crate::{
@@ -95,41 +96,12 @@ pub async fn get_channel_stats(
9596
Ok(Json(stats))
9697
}
9798

98-
pub async fn get_user_stats_by_name(
99-
path: Path<UserLogPathParams>,
100-
range_params: Query<LogRangeParams>,
101-
app: State<App>,
102-
) -> Result<Json<UserLogsStats>> {
103-
get_user_stats(path, false, range_params, app).await
104-
}
105-
106-
pub async fn get_user_stats_by_id(
107-
path: Path<UserLogPathParams>,
108-
range_params: Query<LogRangeParams>,
109-
app: State<App>,
110-
) -> Result<Json<UserLogsStats>> {
111-
get_user_stats(path, true, range_params, app).await
112-
}
113-
114-
async fn get_user_stats(
115-
Path(UserLogPathParams {
116-
channel_id_type,
117-
channel,
118-
user,
119-
}): Path<UserLogPathParams>,
120-
user_is_id: bool,
99+
pub async fn get_user_stats(
100+
Path(user_params): Path<UserLogPathParams>,
121101
Query(range_params): Query<LogRangeParams>,
122102
app: State<App>,
123103
) -> Result<Json<UserLogsStats>> {
124-
let channel_id = match channel_id_type {
125-
ChannelIdType::Name => app.get_user_id_by_name(&channel).await?,
126-
ChannelIdType::Id => channel.clone(),
127-
};
128-
let user_id = if user_is_id {
129-
user.clone()
130-
} else {
131-
app.get_user_id_by_name(&user).await?
132-
};
104+
let (channel_id, user_id) = resolve_user_params(&user_params, &app).await?;
133105

134106
app.check_opted_out(&channel_id, Some(&user_id))?;
135107

@@ -190,47 +162,14 @@ async fn get_channel_logs_inner(
190162
Ok((cache, logs))
191163
}
192164

193-
pub async fn get_user_logs_by_name(
194-
path: Path<UserLogPathParams>,
195-
Query(range_params): Query<LogRangeParams>,
196-
Query(logs_params): Query<LogsParams>,
197-
query: RawQuery,
198-
app: State<App>,
199-
) -> Result<impl IntoApiResponse> {
200-
get_user_logs(path, range_params, logs_params, query, false, app).await
201-
}
202-
203-
pub async fn get_user_logs_id(
204-
path: Path<UserLogPathParams>,
165+
pub async fn get_user_logs(
166+
Path(user_params): Path<UserLogPathParams>,
205167
Query(range_params): Query<LogRangeParams>,
206168
Query(logs_params): Query<LogsParams>,
207-
query: RawQuery,
208-
app: State<App>,
209-
) -> Result<impl IntoApiResponse> {
210-
get_user_logs(path, range_params, logs_params, query, true, app).await
211-
}
212-
213-
async fn get_user_logs(
214-
Path(UserLogPathParams {
215-
channel_id_type,
216-
channel,
217-
user,
218-
}): Path<UserLogPathParams>,
219-
range_params: LogRangeParams,
220-
logs_params: LogsParams,
221169
RawQuery(query): RawQuery,
222-
user_is_id: bool,
223170
app: State<App>,
224171
) -> Result<impl IntoApiResponse> {
225-
let channel_id = match channel_id_type {
226-
ChannelIdType::Name => app.get_user_id_by_name(&channel).await?,
227-
ChannelIdType::Id => channel.clone(),
228-
};
229-
let user_id = if user_is_id {
230-
user.clone()
231-
} else {
232-
app.get_user_id_by_name(&user).await?
233-
};
172+
let (channel_id, user_id) = resolve_user_params(&user_params, &app).await?;
234173

235174
app.check_opted_out(&channel_id, Some(&user_id))?;
236175

@@ -241,7 +180,12 @@ async fn get_user_logs(
241180
let available_logs = read_available_user_logs(&app.db, &channel_id, &user_id).await?;
242181
let latest_log = available_logs.first().ok_or(Error::NotFound)?;
243182

244-
let user_id_type = if user_is_id { "userid" } else { "user" };
183+
let UserLogPathParams {
184+
channel_id_type,
185+
channel,
186+
user_id_type,
187+
user,
188+
} = user_params;
245189

246190
let mut new_uri =
247191
format!("/{channel_id_type}/{channel}/{user_id_type}/{user}/{latest_log}");
@@ -253,43 +197,18 @@ async fn get_user_logs(
253197
}
254198
}
255199

256-
pub async fn get_user_logs_by_date_name(
257-
app: State<App>,
258-
path: Path<UserLogsPath>,
259-
params: Query<LogsParams>,
260-
) -> Result<impl IntoApiResponse> {
261-
let user_id = app.get_user_id_by_name(&path.user).await?;
262-
263-
get_user_logs_by_date(app, path, params, user_id).await
264-
}
265-
266-
pub async fn get_user_logs_by_date_id(
267-
app: State<App>,
268-
path: Path<UserLogsPath>,
269-
params: Query<LogsParams>,
270-
) -> Result<impl IntoApiResponse> {
271-
let user_id = path.user.clone();
272-
get_user_logs_by_date(app, path, params, user_id).await
273-
}
274-
275-
async fn get_user_logs_by_date(
200+
pub async fn get_user_logs_by_date(
276201
app: State<App>,
277-
Path(user_logs_path): Path<UserLogsPath>,
202+
Path(user_params): Path<UserLogPathParams>,
203+
Path(user_logs_date): Path<UserLogsDatePath>,
278204
Query(logs_params): Query<LogsParams>,
279-
user_id: String,
280205
) -> Result<impl IntoApiResponse> {
281-
let channel_id = match user_logs_path.channel_info.channel_id_type {
282-
ChannelIdType::Name => {
283-
app.get_user_id_by_name(&user_logs_path.channel_info.channel)
284-
.await?
285-
}
286-
ChannelIdType::Id => user_logs_path.channel_info.channel.clone(),
287-
};
206+
let (channel_id, user_id) = resolve_user_params(&user_params, &app).await?;
288207

289208
app.check_opted_out(&channel_id, Some(&user_id))?;
290209

291-
let year = user_logs_path.year.parse()?;
292-
let month = user_logs_path.month.parse()?;
210+
let year = user_logs_date.year.parse()?;
211+
let month = user_logs_date.month.parse()?;
293212

294213
let from = NaiveDate::from_ymd_opt(year, month, 1)
295214
.ok_or_else(|| Error::InvalidParam("Invalid date".to_owned()))?
@@ -384,42 +303,12 @@ pub async fn random_channel_line(
384303
Ok((no_cache_header(), logs))
385304
}
386305

387-
pub async fn random_user_line_by_name(
306+
pub async fn random_user_line(
388307
app: State<App>,
389-
Path(UserLogPathParams {
390-
channel_id_type,
391-
channel,
392-
user,
393-
}): Path<UserLogPathParams>,
394-
query: Query<LogsParams>,
395-
) -> Result<impl IntoApiResponse> {
396-
let user_id = app.get_user_id_by_name(&user).await?;
397-
random_user_line(app, channel_id_type, channel, user_id, query).await
398-
}
399-
400-
pub async fn random_user_line_by_id(
401-
app: State<App>,
402-
Path(UserLogPathParams {
403-
channel_id_type,
404-
channel,
405-
user,
406-
}): Path<UserLogPathParams>,
407-
query: Query<LogsParams>,
408-
) -> Result<impl IntoApiResponse> {
409-
random_user_line(app, channel_id_type, channel, user, query).await
410-
}
411-
412-
async fn random_user_line(
413-
app: State<App>,
414-
channel_id_type: ChannelIdType,
415-
channel: String,
416-
user_id: String,
308+
Path(user_params): Path<UserLogPathParams>,
417309
Query(logs_params): Query<LogsParams>,
418310
) -> Result<impl IntoApiResponse> {
419-
let channel_id = match channel_id_type {
420-
ChannelIdType::Name => app.get_user_id_by_name(&channel).await?,
421-
ChannelIdType::Id => channel,
422-
};
311+
let (channel_id, user_id) = resolve_user_params(&user_params, &app).await?;
423312

424313
app.check_opted_out(&channel_id, Some(&user_id))?;
425314

@@ -433,61 +322,13 @@ async fn random_user_line(
433322
Ok((no_cache_header(), logs))
434323
}
435324

436-
pub async fn search_user_logs_by_name(
437-
app: State<App>,
438-
Path(UserLogPathParams {
439-
channel_id_type,
440-
channel,
441-
user,
442-
}): Path<UserLogPathParams>,
443-
Query(search_params): Query<SearchParams>,
444-
Query(logs_params): Query<LogsParams>,
445-
) -> Result<impl IntoApiResponse> {
446-
let user_id = app.get_user_id_by_name(&user).await?;
447-
search_user_logs(
448-
app,
449-
channel_id_type,
450-
channel,
451-
user_id,
452-
search_params,
453-
logs_params,
454-
)
455-
.await
456-
}
457-
458-
pub async fn search_user_logs_by_id(
325+
pub async fn search_user_logs(
459326
app: State<App>,
460-
Path(UserLogPathParams {
461-
channel_id_type,
462-
channel,
463-
user,
464-
}): Path<UserLogPathParams>,
327+
Path(user_params): Path<UserLogPathParams>,
465328
Query(search_params): Query<SearchParams>,
466329
Query(logs_params): Query<LogsParams>,
467330
) -> Result<impl IntoApiResponse> {
468-
search_user_logs(
469-
app,
470-
channel_id_type,
471-
channel,
472-
user,
473-
search_params,
474-
logs_params,
475-
)
476-
.await
477-
}
478-
479-
async fn search_user_logs(
480-
app: State<App>,
481-
channel_id_type: ChannelIdType,
482-
channel: String,
483-
user_id: String,
484-
search_params: SearchParams,
485-
logs_params: LogsParams,
486-
) -> Result<impl IntoApiResponse> {
487-
let channel_id = match channel_id_type {
488-
ChannelIdType::Name => app.get_user_id_by_name(&channel).await?,
489-
ChannelIdType::Id => channel,
490-
};
331+
let (channel_id, user_id) = resolve_user_params(&user_params, &app).await?;
491332

492333
app.check_opted_out(&channel_id, Some(&user_id))?;
493334

@@ -549,3 +390,15 @@ fn cache_header(secs: u64) -> TypedHeader<CacheControl> {
549390
pub fn no_cache_header() -> TypedHeader<CacheControl> {
550391
TypedHeader(CacheControl::new().with_no_cache())
551392
}
393+
394+
async fn resolve_user_params(params: &UserLogPathParams, app: &App) -> Result<(String, String)> {
395+
let channel_id = match params.channel_id_type {
396+
ChannelIdType::Name => app.get_user_id_by_name(&params.channel).await?,
397+
ChannelIdType::Id => params.channel.clone(),
398+
};
399+
let user_id = match params.user_id_type {
400+
UserIdType::Name => app.get_user_id_by_name(&params.user).await?,
401+
UserIdType::Id => params.user.clone(),
402+
};
403+
Ok((channel_id, user_id))
404+
}

0 commit comments

Comments
 (0)