add: followers/following ui
This commit is contained in:
parent
17564ede49
commit
e183a01887
13 changed files with 469 additions and 86 deletions
|
@ -18,14 +18,15 @@ pub async fn redirect_from_id(
|
|||
Extension(data): Extension<State>,
|
||||
Path(id): Path<String>,
|
||||
) -> impl IntoResponse {
|
||||
match (data.read().await).0
|
||||
match (data.read().await)
|
||||
.0
|
||||
.get_user_by_id(match id.parse::<usize>() {
|
||||
Ok(id) => id,
|
||||
Err(_) => return Redirect::to("/"),
|
||||
})
|
||||
.await
|
||||
{
|
||||
Ok(u) => Redirect::to(&format!("/user/{}", u.username)),
|
||||
Ok(u) => Redirect::to(&format!("/@{}", u.username)),
|
||||
Err(_) => Redirect::to("/"),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -25,7 +25,9 @@ pub fn routes() -> Router {
|
|||
.route("/auth/login", get(auth::login_request))
|
||||
// profile
|
||||
.route("/settings", get(profile::settings_request))
|
||||
.route("/user/{username}", get(profile::posts_request))
|
||||
.route("/@{username}", get(profile::posts_request))
|
||||
.route("/@{username}/following", get(profile::following_request))
|
||||
.route("/@{username}/followers", get(profile::followers_request))
|
||||
// communities
|
||||
.route("/communities", get(communities::list_request))
|
||||
.route("/community/{title}", get(communities::feed_request))
|
||||
|
|
|
@ -66,7 +66,7 @@ pub fn profile_context(
|
|||
context.insert("is_blocking", &is_blocking);
|
||||
}
|
||||
|
||||
/// `/user/{username}`
|
||||
/// `/@{username}`
|
||||
pub async fn posts_request(
|
||||
jar: CookieJar,
|
||||
Path(username): Path<String>,
|
||||
|
@ -189,3 +189,255 @@ pub async fn posts_request(
|
|||
// return
|
||||
Ok(Html(data.1.render("profile/posts.html", &context).unwrap()))
|
||||
}
|
||||
|
||||
/// `/@{username}/following`
|
||||
pub async fn following_request(
|
||||
jar: CookieJar,
|
||||
Path(username): Path<String>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = get_user_from_token!(jar, data.0);
|
||||
|
||||
let other_user = match data.0.get_user_by_username(&username).await {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// check if we're blocked
|
||||
if let Some(ref ua) = user {
|
||||
if data
|
||||
.0
|
||||
.get_userblock_by_initiator_receiver(other_user.id, ua.id)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// check for private profile
|
||||
if other_user.settings.private_profile {
|
||||
if let Some(ref ua) = user {
|
||||
if ua.id != other_user.id {
|
||||
if data
|
||||
.0
|
||||
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// fetch data
|
||||
let list = match data
|
||||
.0
|
||||
.get_userfollows_by_initiator(other_user.id, 12, props.page)
|
||||
.await
|
||||
{
|
||||
Ok(l) => match data.0.fill_userfollows_with_receiver(l).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
let communities = match data.0.get_memberships_by_owner(other_user.id).await {
|
||||
Ok(m) => match data.0.fill_communities(m).await {
|
||||
Ok(m) => m,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// init context
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &user).await;
|
||||
|
||||
let is_self = if let Some(ref ua) = user {
|
||||
ua.id == other_user.id
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_following = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_userfollow_by_initiator_receiver(ua.id, other_user.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_following_you = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_userfollow_by_receiver_initiator(ua.id, other_user.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_blocking = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_userblock_by_initiator_receiver(ua.id, other_user.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
context.insert("list", &list);
|
||||
profile_context(
|
||||
&mut context,
|
||||
&other_user,
|
||||
&communities,
|
||||
is_self,
|
||||
is_following,
|
||||
is_following_you,
|
||||
is_blocking,
|
||||
);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
data.1.render("profile/following.html", &context).unwrap(),
|
||||
))
|
||||
}
|
||||
|
||||
/// `/@{username}/followers`
|
||||
pub async fn followers_request(
|
||||
jar: CookieJar,
|
||||
Path(username): Path<String>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
Extension(data): Extension<State>,
|
||||
) -> impl IntoResponse {
|
||||
let data = data.read().await;
|
||||
let user = get_user_from_token!(jar, data.0);
|
||||
|
||||
let other_user = match data.0.get_user_by_username(&username).await {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// check if we're blocked
|
||||
if let Some(ref ua) = user {
|
||||
if data
|
||||
.0
|
||||
.get_userblock_by_initiator_receiver(other_user.id, ua.id)
|
||||
.await
|
||||
.is_ok()
|
||||
{
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// check for private profile
|
||||
if other_user.settings.private_profile {
|
||||
if let Some(ref ua) = user {
|
||||
if ua.id != other_user.id {
|
||||
if data
|
||||
.0
|
||||
.get_userfollow_by_initiator_receiver(other_user.id, ua.id)
|
||||
.await
|
||||
.is_err()
|
||||
{
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Err(Html(
|
||||
render_error(Error::NotAllowed, &jar, &data, &user).await,
|
||||
));
|
||||
}
|
||||
}
|
||||
|
||||
// fetch data
|
||||
let list = match data
|
||||
.0
|
||||
.get_userfollows_by_receiver(other_user.id, 12, props.page)
|
||||
.await
|
||||
{
|
||||
Ok(l) => match data.0.fill_userfollows_with_initiator(l).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
let communities = match data.0.get_memberships_by_owner(other_user.id).await {
|
||||
Ok(m) => match data.0.fill_communities(m).await {
|
||||
Ok(m) => m,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
},
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &user).await)),
|
||||
};
|
||||
|
||||
// init context
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0, lang, &user).await;
|
||||
|
||||
let is_self = if let Some(ref ua) = user {
|
||||
ua.id == other_user.id
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_following = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_userfollow_by_initiator_receiver(ua.id, other_user.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_following_you = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_userfollow_by_receiver_initiator(ua.id, other_user.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
let is_blocking = if let Some(ref ua) = user {
|
||||
data.0
|
||||
.get_userblock_by_initiator_receiver(ua.id, other_user.id)
|
||||
.await
|
||||
.is_ok()
|
||||
} else {
|
||||
false
|
||||
};
|
||||
|
||||
context.insert("list", &list);
|
||||
profile_context(
|
||||
&mut context,
|
||||
&other_user,
|
||||
&communities,
|
||||
is_self,
|
||||
is_following,
|
||||
is_following_you,
|
||||
is_blocking,
|
||||
);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
data.1.render("profile/followers.html", &context).unwrap(),
|
||||
))
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue