add: profile replies tab

add: profile media tab
This commit is contained in:
trisua 2025-05-18 20:51:55 -04:00
parent 3e4ee8126a
commit c74f36b97c
10 changed files with 465 additions and 39 deletions

View file

@ -477,6 +477,116 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get all replies from the given user (from most recent).
///
/// # Arguments
/// * `id` - the ID of the user the requested posts belong to
/// * `batch` - the limit of posts in each page
/// * `page` - the page number
pub async fn get_replies_by_user(
&self,
id: usize,
batch: usize,
page: usize,
user: &Option<User>,
) -> Result<Vec<Post>> {
let other_user = self.get_user_by_id(id).await?;
let conn = match self.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 {
if ua.id == other_user.id {
hide_nsfw = false
}
}
if other_user.settings.private_profile {
hide_nsfw = false;
}
// ...
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE owner = $1 AND NOT replying_to = 0 AND NOT (context::json->>'is_profile_pinned')::boolean {} ORDER BY created DESC LIMIT $2 OFFSET $3",
if hide_nsfw {
"AND NOT (context::json->>'is_nsfw')::boolean"
} else {
""
}
),
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("post".to_string()));
}
Ok(res.unwrap())
}
/// Get all posts containing media from the given user (from most recent).
///
/// # Arguments
/// * `id` - the ID of the user the requested posts belong to
/// * `batch` - the limit of posts in each page
/// * `page` - the page number
pub async fn get_media_posts_by_user(
&self,
id: usize,
batch: usize,
page: usize,
user: &Option<User>,
) -> Result<Vec<Post>> {
let other_user = self.get_user_by_id(id).await?;
let conn = match self.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 {
if ua.id == other_user.id {
hide_nsfw = false
}
}
if other_user.settings.private_profile {
hide_nsfw = false;
}
// ...
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE owner = $1 AND NOT uploads = '[]' AND NOT (context::json->>'is_profile_pinned')::boolean {} ORDER BY created DESC LIMIT $2 OFFSET $3",
if hide_nsfw {
"AND NOT (context::json->>'is_nsfw')::boolean"
} else {
""
}
),
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
if res.is_err() {
return Err(Error::GeneralNotFound("post".to_string()));
}
Ok(res.unwrap())
}
/// Get all posts from the given user (searched).
///
/// # Arguments
@ -517,7 +627,7 @@ impl DataManager {
let res = query_rows!(
&conn,
&format!(
"SELECT * FROM posts WHERE owner = $1 AND tsvector_content @@ to_tsquery($2) AND replying_to = 0 AND NOT (context::json->>'is_profile_pinned')::boolean {} ORDER BY created DESC LIMIT $3 OFFSET $4",
"SELECT * FROM posts WHERE owner = $1 AND tsvector_content @@ to_tsquery($2) {} ORDER BY created DESC LIMIT $3 OFFSET $4",
if hide_nsfw {
"AND NOT (context::json->>'is_nsfw')::boolean"
} else {
@ -560,7 +670,7 @@ impl DataManager {
// ...
let res = query_rows!(
&conn,
"SELECT * FROM posts WHERE tsvector_content @@ to_tsquery($1) AND replying_to = 0 AND NOT (context::json->>'is_profile_pinned')::boolean ORDER BY created DESC LIMIT $2 OFFSET $3",
"SELECT * FROM posts WHERE tsvector_content @@ to_tsquery($1) ORDER BY created DESC LIMIT $2 OFFSET $3",
params![&text_query, &(batch as i64), &((page * batch) as i64)],
|x| { Self::get_post_from_row(x) }
);
@ -1493,11 +1603,7 @@ impl DataManager {
}
// incr user post count
let owner = self.get_user_by_id(y.owner).await?;
if owner.post_count > 0 {
self.incr_user_post_count(y.owner).await?;
}
self.incr_user_post_count(y.owner).await?;
// incr question answer count
if y.context.answering != 0 {