add: block list stacks
This commit is contained in:
parent
9bb5f38f76
commit
b71ae1f5a4
28 changed files with 700 additions and 219 deletions
|
@ -537,8 +537,11 @@ pub fn routes() -> Router {
|
|||
.route("/stacks/{id}/privacy", post(stacks::update_privacy_request))
|
||||
.route("/stacks/{id}/mode", post(stacks::update_mode_request))
|
||||
.route("/stacks/{id}/sort", post(stacks::update_sort_request))
|
||||
.route("/stacks/{id}/users", get(stacks::get_users_request))
|
||||
.route("/stacks/{id}/users", post(stacks::add_user_request))
|
||||
.route("/stacks/{id}/users", delete(stacks::remove_user_request))
|
||||
.route("/stacks/{id}/block", post(stacks::block_request))
|
||||
.route("/stacks/{id}/block", delete(stacks::unblock_request))
|
||||
.route("/stacks/{id}", delete(stacks::delete_request))
|
||||
// uploads
|
||||
.route("/uploads/{id}", get(uploads::get_request))
|
||||
|
|
|
@ -1,7 +1,16 @@
|
|||
use crate::{State, get_user_from_token};
|
||||
use axum::{Extension, Json, extract::Path, response::IntoResponse};
|
||||
use crate::{get_user_from_token, routes::pages::PaginatedQuery, State};
|
||||
use axum::{
|
||||
extract::{Path, Query},
|
||||
response::IntoResponse,
|
||||
Extension, Json,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{oauth, stacks::UserStack, ApiReturn, Error};
|
||||
use tetratto_core::model::{
|
||||
oauth,
|
||||
permissions::FinePermission,
|
||||
stacks::{StackBlock, StackPrivacy, UserStack},
|
||||
ApiReturn, Error,
|
||||
};
|
||||
use super::{
|
||||
AddOrRemoveStackUser, CreateStack, UpdateStackMode, UpdateStackName, UpdateStackPrivacy,
|
||||
UpdateStackSort,
|
||||
|
@ -221,3 +230,93 @@ pub async fn delete_request(
|
|||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn get_users_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
Query(props): Query<PaginatedQuery>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadProfiles) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let stack = match data.get_stack_by_id(id).await {
|
||||
Ok(s) => s,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
if stack.privacy == StackPrivacy::Private
|
||||
&& user.id != stack.owner
|
||||
&& !user.permissions.check(FinePermission::MANAGE_STACKS)
|
||||
{
|
||||
return Json(Error::NotAllowed.into());
|
||||
}
|
||||
|
||||
match data.get_stack_users(id, 12, props.page).await {
|
||||
Ok(users) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: Some({
|
||||
let mut out = Vec::new();
|
||||
|
||||
for mut u in users.clone() {
|
||||
u.clean();
|
||||
out.push(u)
|
||||
}
|
||||
|
||||
out
|
||||
}),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn block_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadProfiles) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
match data.create_stackblock(StackBlock::new(user.id, id)).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn unblock_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
) -> impl IntoResponse {
|
||||
let data = &(data.read().await).0;
|
||||
let user = match get_user_from_token!(jar, data, oauth::AppScope::UserReadProfiles) {
|
||||
Some(ua) => ua,
|
||||
None => return Json(Error::NotAllowed.into()),
|
||||
};
|
||||
|
||||
let block = match data.get_stackblock_by_initiator_stack(user.id, id).await {
|
||||
Ok(b) => b,
|
||||
Err(e) => return Json(e.into()),
|
||||
};
|
||||
|
||||
match data.delete_stackblock(block.id, user).await {
|
||||
Ok(_) => Json(ApiReturn {
|
||||
ok: true,
|
||||
message: "Success".to_string(),
|
||||
payload: (),
|
||||
}),
|
||||
Err(e) => Json(e.into()),
|
||||
}
|
||||
}
|
||||
|
|
|
@ -126,7 +126,7 @@ pub fn routes() -> Router {
|
|||
.route("/developer/app/{id}", get(developer::app_request))
|
||||
// stacks
|
||||
.route("/stacks", get(stacks::list_request))
|
||||
.route("/stacks/{id}", get(stacks::posts_request))
|
||||
.route("/stacks/{id}", get(stacks::feed_request))
|
||||
.route("/stacks/{id}/manage", get(stacks::manage_request))
|
||||
}
|
||||
|
||||
|
|
|
@ -80,6 +80,19 @@ pub async fn settings_request(
|
|||
Err(e) => return Err(Html(render_error(e, &jar, &data, &None).await)),
|
||||
};
|
||||
|
||||
let stackblocks = {
|
||||
let mut out = Vec::new();
|
||||
|
||||
for block in data.0.get_stackblocks_by_initiator(profile.id).await {
|
||||
out.push(match data.0.get_stack_by_id(block.stack).await {
|
||||
Ok(s) => s,
|
||||
Err(_) => continue,
|
||||
});
|
||||
}
|
||||
|
||||
out
|
||||
};
|
||||
|
||||
let uploads = match data.0.get_uploads_by_owner(profile.id, 12, req.page).await {
|
||||
Ok(ua) => ua,
|
||||
Err(e) => {
|
||||
|
@ -98,6 +111,7 @@ pub async fn settings_request(
|
|||
context.insert("stacks", &stacks);
|
||||
context.insert("following", &following);
|
||||
context.insert("blocks", &blocks);
|
||||
context.insert("stackblocks", &stackblocks);
|
||||
context.insert(
|
||||
"user_tokens_serde",
|
||||
&serde_json::to_string(&tokens)
|
||||
|
@ -128,59 +142,37 @@ pub async fn settings_request(
|
|||
)
|
||||
.unwrap();
|
||||
|
||||
let light = serde_json::Value::from("Light");
|
||||
let mut profile_theme = settings_map
|
||||
.get("profile_theme")
|
||||
.unwrap_or(&light)
|
||||
.as_str()
|
||||
.unwrap();
|
||||
if let Some(color_surface) = settings_map.get("theme_color_surface") {
|
||||
let color_surface = color_surface.as_str().unwrap();
|
||||
for setting in &settings_map {
|
||||
if !setting.0.starts_with("theme_color_text")
|
||||
| (setting.0 == "theme_color_text_primary")
|
||||
| (setting.0 == "theme_color_text_secondary")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
if profile_theme.is_empty() | (profile_theme == "Auto") {
|
||||
profile_theme = "Light";
|
||||
}
|
||||
let value = setting.1.as_str().unwrap();
|
||||
|
||||
let default_surface = serde_json::Value::from(if profile_theme == "Light" {
|
||||
"#f3f2f1"
|
||||
if !value.starts_with("#") {
|
||||
// we can only parse hex right now
|
||||
continue;
|
||||
}
|
||||
|
||||
let c1 = Color::from(color_surface);
|
||||
let c2 = Color::from(value);
|
||||
let contrast = c1.contrast(&c2);
|
||||
|
||||
if contrast < MINIMUM_CONTRAST_THRESHOLD {
|
||||
failing_color_keys.push((setting.0, contrast));
|
||||
}
|
||||
}
|
||||
|
||||
context.insert("failing_color_keys", &failing_color_keys);
|
||||
} else {
|
||||
"#19171c"
|
||||
});
|
||||
|
||||
let mut color_surface = settings_map
|
||||
.get("theme_color_surface")
|
||||
.unwrap_or(&default_surface)
|
||||
.as_str()
|
||||
.unwrap();
|
||||
|
||||
if color_surface.is_empty() {
|
||||
color_surface = default_surface.as_str().unwrap();
|
||||
context.insert("failing_color_keys", &Vec::<&str>::new());
|
||||
}
|
||||
|
||||
for setting in &settings_map {
|
||||
if !setting.0.starts_with("theme_color_text")
|
||||
| (setting.0 == "theme_color_text_primary")
|
||||
| (setting.0 == "theme_color_text_secondary")
|
||||
{
|
||||
continue;
|
||||
}
|
||||
|
||||
let value = setting.1.as_str().unwrap();
|
||||
|
||||
if !value.starts_with("#") {
|
||||
// we can only parse hex right now
|
||||
continue;
|
||||
}
|
||||
|
||||
let c1 = Color::from(color_surface);
|
||||
let c2 = Color::from(value);
|
||||
let contrast = c1.contrast(&c2);
|
||||
|
||||
if contrast < MINIMUM_CONTRAST_THRESHOLD {
|
||||
failing_color_keys.push((setting.0, contrast));
|
||||
}
|
||||
}
|
||||
|
||||
context.insert("failing_color_keys", &failing_color_keys);
|
||||
|
||||
// return
|
||||
Ok(Html(
|
||||
data.1.render("profile/settings.html", &context).unwrap(),
|
||||
|
|
|
@ -4,7 +4,12 @@ use axum::{
|
|||
Extension,
|
||||
};
|
||||
use axum_extra::extract::CookieJar;
|
||||
use tetratto_core::model::{permissions::FinePermission, stacks::StackPrivacy, Error, auth::User};
|
||||
use tetratto_core::model::{
|
||||
auth::User,
|
||||
permissions::FinePermission,
|
||||
stacks::{StackMode, StackPrivacy},
|
||||
Error,
|
||||
};
|
||||
use crate::{assets::initial_context, get_lang, get_user_from_token, State};
|
||||
use super::{render_error, PaginatedQuery};
|
||||
|
||||
|
@ -35,7 +40,7 @@ pub async fn list_request(jar: CookieJar, Extension(data): Extension<State>) ->
|
|||
}
|
||||
|
||||
/// `/stacks/{id}`
|
||||
pub async fn posts_request(
|
||||
pub async fn feed_request(
|
||||
jar: CookieJar,
|
||||
Extension(data): Extension<State>,
|
||||
Path(id): Path<usize>,
|
||||
|
@ -65,32 +70,50 @@ pub async fn posts_request(
|
|||
));
|
||||
}
|
||||
|
||||
let ignore_users = crate::ignore_users_gen!(user!, data);
|
||||
let list = match data
|
||||
.0
|
||||
.get_stack_posts(
|
||||
user.id,
|
||||
stack.id,
|
||||
12,
|
||||
req.page,
|
||||
&ignore_users,
|
||||
&Some(user.clone()),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(l) => l,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
let lang = get_lang!(jar, data.0);
|
||||
let mut context = initial_context(&data.0.0.0, lang, &Some(user)).await;
|
||||
let mut context = initial_context(&data.0.0.0, lang, &Some(user.clone())).await;
|
||||
|
||||
context.insert("page", &req.page);
|
||||
context.insert("stack", &stack);
|
||||
context.insert("list", &list);
|
||||
|
||||
if stack.mode == StackMode::BlockList {
|
||||
let list = match data.0.get_stack_users(stack.id, 12, req.page).await {
|
||||
Ok(l) => l,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
context.insert("list", &list);
|
||||
context.insert(
|
||||
"is_blocked",
|
||||
&data
|
||||
.0
|
||||
.get_stackblock_by_initiator_stack(user.id, stack.id)
|
||||
.await
|
||||
.is_ok(),
|
||||
);
|
||||
} else {
|
||||
let ignore_users = crate::ignore_users_gen!(user!, data);
|
||||
let list = match data
|
||||
.0
|
||||
.get_stack_posts(
|
||||
user.id,
|
||||
stack.id,
|
||||
12,
|
||||
req.page,
|
||||
&ignore_users,
|
||||
&Some(user.clone()),
|
||||
)
|
||||
.await
|
||||
{
|
||||
Ok(l) => l,
|
||||
Err(e) => return Err(Html(render_error(e, &jar, &data, &Some(user)).await)),
|
||||
};
|
||||
|
||||
context.insert("list", &list);
|
||||
}
|
||||
|
||||
// return
|
||||
Ok(Html(data.1.render("stacks/posts.html", &context).unwrap()))
|
||||
Ok(Html(data.1.render("stacks/feed.html", &context).unwrap()))
|
||||
}
|
||||
|
||||
/// `/stacks/{id}/manage`
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue