mirror of
https://github.com/modrinth/code.git
synced 2026-08-26 17:44:50 +00:00
feat(labrinth): user blocking (#6837)
* feat(labrinth): user blocking still wip, nothing is hooked up * style(labrinth): cargo fmt * refactor(labrinth): move block/unblock routes to public api * feat(labrinth): block friend requests if blocked * feat(labrinth): remove friend/request on block * feat(labrinth): get blocked users endpoint
This commit is contained in:
@@ -0,0 +1,94 @@
|
||||
use crate::database::models::DBUserId;
|
||||
|
||||
pub struct DBBlockedUser {
|
||||
pub user_id: DBUserId,
|
||||
pub blocked_id: DBUserId,
|
||||
}
|
||||
|
||||
impl DBBlockedUser {
|
||||
pub async fn insert<'a, E>(&self, exec: E) -> Result<(), sqlx::Error>
|
||||
where
|
||||
E: crate::database::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query!(
|
||||
"
|
||||
INSERT INTO blocked_users (user_id, blocked_id)
|
||||
VALUES ($1, $2)
|
||||
ON CONFLICT (user_id, blocked_id) DO NOTHING
|
||||
",
|
||||
self.user_id.0,
|
||||
self.blocked_id.0,
|
||||
)
|
||||
.execute(exec)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub async fn is_blocked<'a, E>(
|
||||
user_id: DBUserId,
|
||||
blocked_id: DBUserId,
|
||||
exec: E,
|
||||
) -> Result<bool, sqlx::Error>
|
||||
where
|
||||
E: crate::database::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
let blocked = sqlx::query_scalar!(
|
||||
"
|
||||
SELECT 1 FROM blocked_users
|
||||
WHERE user_id = $1 AND blocked_id = $2
|
||||
",
|
||||
user_id.0,
|
||||
blocked_id.0,
|
||||
)
|
||||
.fetch_optional(exec)
|
||||
.await?;
|
||||
|
||||
Ok(blocked.is_some())
|
||||
}
|
||||
|
||||
pub async fn get_blocked_for_user<'a, E>(
|
||||
user_id: DBUserId,
|
||||
exec: E,
|
||||
) -> Result<Vec<DBUserId>, sqlx::Error>
|
||||
where
|
||||
E: crate::database::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
let blocked = sqlx::query_scalar!(
|
||||
"
|
||||
SELECT blocked_id FROM blocked_users
|
||||
WHERE user_id = $1
|
||||
",
|
||||
user_id.0,
|
||||
)
|
||||
.fetch_all(exec)
|
||||
.await?
|
||||
.into_iter()
|
||||
.map(DBUserId)
|
||||
.collect();
|
||||
|
||||
Ok(blocked)
|
||||
}
|
||||
|
||||
pub async fn remove<'a, E>(
|
||||
user_id: DBUserId,
|
||||
blocked_id: DBUserId,
|
||||
exec: E,
|
||||
) -> Result<(), sqlx::Error>
|
||||
where
|
||||
E: crate::database::Executor<'a, Database = sqlx::Postgres>,
|
||||
{
|
||||
sqlx::query!(
|
||||
"
|
||||
DELETE FROM blocked_users
|
||||
WHERE user_id = $1 AND blocked_id = $2
|
||||
",
|
||||
user_id.0,
|
||||
blocked_id.0,
|
||||
)
|
||||
.execute(exec)
|
||||
.await?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
@@ -2,6 +2,7 @@ use thiserror::Error;
|
||||
|
||||
pub mod affiliate_code_item;
|
||||
pub mod analytics_event_item;
|
||||
pub mod blocked_user_item;
|
||||
pub mod categories;
|
||||
pub mod charge_item;
|
||||
pub mod collection_item;
|
||||
|
||||
Reference in New Issue
Block a user