add: ability to generate invite codes in bulk add: better mark as nsfw
ui
This commit is contained in:
parent
2a77c61bf2
commit
4843688fcf
13 changed files with 126 additions and 90 deletions
|
@ -1,4 +1,4 @@
|
|||
use oiseau::{cache::Cache, query_rows};
|
||||
use oiseau::{cache::Cache, query_row, query_rows};
|
||||
use tetratto_shared::unix_epoch_timestamp;
|
||||
use crate::model::{
|
||||
Error, Result,
|
||||
|
@ -24,7 +24,12 @@ impl DataManager {
|
|||
auto_method!(get_invite_code_by_code(&str)@get_invite_code_from_row -> "SELECT * FROM invite_codes WHERE code = $1" --name="invite_code" --returns=InviteCode);
|
||||
|
||||
/// Get invite_codes by `owner`.
|
||||
pub async fn get_invite_codes_by_owner(&self, owner: usize) -> Result<Vec<InviteCode>> {
|
||||
pub async fn get_invite_codes_by_owner(
|
||||
&self,
|
||||
owner: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<InviteCode>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
|
@ -32,8 +37,8 @@ impl DataManager {
|
|||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM invite_codes WHERE owner = $1",
|
||||
&[&(owner as i64)],
|
||||
"SELECT * FROM invite_codes WHERE owner = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||
&[&(owner as i64), &(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Self::get_invite_code_from_row(x) }
|
||||
);
|
||||
|
||||
|
@ -44,6 +49,27 @@ impl DataManager {
|
|||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get invite_codes by `owner`.
|
||||
pub async fn get_invite_codes_by_owner_count(&self, owner: usize) -> Result<i32> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(
|
||||
&conn,
|
||||
"SELECT COUNT(*)::int FROM invite_codes WHERE owner = $1",
|
||||
&[&(owner as i64)],
|
||||
|x| Ok(x.get::<usize, i32>(0))
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("invite_code".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Fill a vector of invite codes with the user that used them.
|
||||
pub async fn fill_invite_codes(
|
||||
&self,
|
||||
|
@ -89,7 +115,7 @@ impl DataManager {
|
|||
|
||||
// our account is old enough, but we need to make sure we don't already have
|
||||
// 2 invite codes
|
||||
if self.get_invite_codes_by_owner(user.id).await?.len()
|
||||
if (self.get_invite_codes_by_owner_count(user.id).await? as usize)
|
||||
>= Self::MAXIMUM_FREE_INVITE_CODES
|
||||
{
|
||||
return Err(Error::MiscError(
|
||||
|
@ -99,7 +125,7 @@ impl DataManager {
|
|||
}
|
||||
} else if !user.permissions.check(FinePermission::MANAGE_USERS) {
|
||||
// check count since we're also not a moderator with MANAGE_USERS
|
||||
if self.get_invite_codes_by_owner(user.id).await?.len()
|
||||
if (self.get_invite_codes_by_owner_count(user.id).await? as usize)
|
||||
>= Self::MAXIMUM_SUPPORTER_INVITE_CODES
|
||||
{
|
||||
return Err(Error::MiscError(
|
||||
|
|
|
@ -689,31 +689,15 @@ impl DataManager {
|
|||
id: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
user: &Option<User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
// check if we should hide nsfw posts
|
||||
let mut hide_nsfw: bool = true;
|
||||
|
||||
if let Some(ua) = user {
|
||||
hide_nsfw = !ua.settings.show_nsfw;
|
||||
}
|
||||
|
||||
// ...
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
&format!(
|
||||
"SELECT * FROM posts WHERE owner = $1 AND replying_to = 0 AND NOT (context::json->>'is_profile_pinned')::boolean AND is_deleted = 0 {} ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||
if hide_nsfw {
|
||||
"AND NOT (context::json->>'is_nsfw')::boolean"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
),
|
||||
"SELECT * FROM posts WHERE owner = $1 AND replying_to = 0 AND NOT (context::json->>'is_profile_pinned')::boolean AND is_deleted = 0 ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Self::get_post_from_row(x) }
|
||||
);
|
||||
|
@ -1008,31 +992,15 @@ impl DataManager {
|
|||
tag: &str,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
user: &Option<User>,
|
||||
) -> Result<Vec<Post>> {
|
||||
let conn = match self.0.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
// check if we should hide nsfw posts
|
||||
let mut hide_nsfw: bool = true;
|
||||
|
||||
if let Some(ua) = user {
|
||||
hide_nsfw = !ua.settings.show_nsfw;
|
||||
}
|
||||
|
||||
// ...
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
&format!(
|
||||
"SELECT * FROM posts WHERE owner = $1 AND context::json->>'tags' LIKE $2 AND is_deleted = 0 {} ORDER BY created DESC LIMIT $3 OFFSET $4",
|
||||
if hide_nsfw {
|
||||
"AND NOT (context::json->>'is_nsfw')::boolean"
|
||||
} else {
|
||||
""
|
||||
}
|
||||
),
|
||||
"SELECT * FROM posts WHERE owner = $1 AND context::json->>'tags' LIKE $2 AND is_deleted = 0 ORDER BY created DESC LIMIT $3 OFFSET $4",
|
||||
params![
|
||||
&(id as i64),
|
||||
&format!("%\"{tag}\"%"),
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue