add: profiles ui, communities ui, posts ui
This commit is contained in:
parent
00abbc8fa2
commit
eecf357325
36 changed files with 1460 additions and 147 deletions
|
@ -21,8 +21,8 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> User {
|
||||
User {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
username: get!(x->2(String)),
|
||||
password: get!(x->3(String)),
|
||||
salt: get!(x->4(String)),
|
||||
|
@ -31,9 +31,9 @@ impl DataManager {
|
|||
permissions: FinePermission::from_bits(get!(x->7(u32))).unwrap(),
|
||||
is_verified: if get!(x->8(i8)) == 1 { true } else { false },
|
||||
// counts
|
||||
notification_count: get!(x->9(i64)) as usize,
|
||||
follower_count: get!(x->10(i64)) as usize,
|
||||
following_count: get!(x->11(i64)) as usize,
|
||||
notification_count: get!(x->9(isize)) as usize,
|
||||
follower_count: get!(x->10(isize)) as usize,
|
||||
following_count: get!(x->11(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -116,7 +116,7 @@ impl DataManager {
|
|||
Ok(())
|
||||
}
|
||||
|
||||
/// Create a new user in the database.
|
||||
/// Delete an existing user in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the user
|
||||
|
|
|
@ -408,4 +408,131 @@ macro_rules! auto_method {
|
|||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident()@$select_fn:ident:$permission:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident) => {
|
||||
pub async fn $name(&self, id: usize, user: User) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
if user.id != y.owner {
|
||||
if !user.permissions.check(FinePermission::$permission) {
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, $query, &[&id.to_string()]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.$cache_key_tmpl(&y).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident) => {
|
||||
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
if user.id != y.owner {
|
||||
if !user.permissions.check(FinePermission::$permission) {
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, $query, &[&x, &id.to_string()]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.$cache_key_tmpl(&y).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident($x:ty)@$select_fn:ident:$permission:ident -> $query:literal --serde --cache-key-tmpl=$cache_key_tmpl:ident) => {
|
||||
pub async fn $name(&self, id: usize, user: User, x: $x) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
if user.id != y.owner {
|
||||
if !user.permissions.check(FinePermission::$permission) {
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
$query,
|
||||
&[&serde_json::to_string(&x).unwrap(), &id.to_string()]
|
||||
);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.$cache_key_tmpl(&y).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident()@$select_fn:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident --incr) => {
|
||||
pub async fn $name(&self, id: usize) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, $query, &[&id.to_string()]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.$cache_key_tmpl(&y).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
($name:ident()@$select_fn:ident -> $query:literal --cache-key-tmpl=$cache_key_tmpl:ident --decr) => {
|
||||
pub async fn $name(&self, id: usize) -> Result<()> {
|
||||
let y = self.$select_fn(id).await?;
|
||||
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, $query, &[&id.to_string()]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.$cache_key_tmpl(&y).await;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
@ -24,26 +24,29 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> Community {
|
||||
Community {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
title: get!(x->2(String)),
|
||||
context: serde_json::from_str(&get!(x->3(String))).unwrap(),
|
||||
owner: get!(x->4(i64)) as usize,
|
||||
owner: get!(x->4(isize)) as usize,
|
||||
read_access: serde_json::from_str(&get!(x->5(String))).unwrap(),
|
||||
write_access: serde_json::from_str(&get!(x->6(String))).unwrap(),
|
||||
// likes
|
||||
likes: get!(x->6(i64)) as isize,
|
||||
dislikes: get!(x->7(i64)) as isize,
|
||||
likes: get!(x->7(isize)) as isize,
|
||||
dislikes: get!(x->8(isize)) as isize,
|
||||
// counts
|
||||
member_count: get!(x->9(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_community_by_id()@get_community_from_row -> "SELECT * FROM communities WHERE id = $1" --name="community" --returns=Community --cache-key-tmpl="atto.community:{}");
|
||||
auto_method!(get_community_by_title(&str)@get_community_from_row -> "SELECT * FROM communities WHERE title = $1" --name="community" --returns=Community --cache-key-tmpl="atto.community:{}");
|
||||
|
||||
/// Create a new community in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`Community`] to insert
|
||||
pub async fn create_community(&self, data: Community) -> Result<()> {
|
||||
pub async fn create_community(&self, data: Community) -> Result<String> {
|
||||
// check values
|
||||
if data.title.len() < 2 {
|
||||
return Err(Error::DataTooShort("title".to_string()));
|
||||
|
@ -51,6 +54,17 @@ impl DataManager {
|
|||
return Err(Error::DataTooLong("title".to_string()));
|
||||
}
|
||||
|
||||
if !data.title.is_ascii() | data.title.contains(" ") {
|
||||
return Err(Error::MiscError(
|
||||
"Title contains characters which aren't allowed".to_string(),
|
||||
));
|
||||
}
|
||||
|
||||
// make sure community doesn't already exist with title
|
||||
if self.get_community_by_title(&data.title).await.is_ok() {
|
||||
return Err(Error::MiscError("Title already in use".to_string()));
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
|
@ -59,7 +73,7 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO communities VALUES ($1, $2, $3, $4, $5, $6, $7)",
|
||||
"INSERT INTO communities VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
&[
|
||||
&data.id.to_string().as_str(),
|
||||
&data.created.to_string().as_str(),
|
||||
|
@ -68,6 +82,9 @@ impl DataManager {
|
|||
&data.owner.to_string().as_str(),
|
||||
&serde_json::to_string(&data.read_access).unwrap().as_str(),
|
||||
&serde_json::to_string(&data.write_access).unwrap().as_str(),
|
||||
&0.to_string().as_str(),
|
||||
&0.to_string().as_str(),
|
||||
&0.to_string().as_str()
|
||||
]
|
||||
);
|
||||
|
||||
|
@ -85,17 +102,29 @@ impl DataManager {
|
|||
.unwrap();
|
||||
|
||||
// return
|
||||
Ok(())
|
||||
Ok(data.title)
|
||||
}
|
||||
|
||||
auto_method!(delete_community()@get_community_by_id:MANAGE_COMMUNITY_PAGES -> "DELETE communities pages WHERE id = $1" --cache-key-tmpl="atto.community:{}");
|
||||
auto_method!(update_community_title(String)@get_community_by_id:MANAGE_COMMUNITY_PAGES -> "UPDATE communities SET title = $1 WHERE id = $2" --cache-key-tmpl="atto.community:{}");
|
||||
auto_method!(update_community_context(CommunityContext)@get_community_by_id:MANAGE_COMMUNITY_PAGES -> "UPDATE communities SET prompt = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.community:{}");
|
||||
auto_method!(update_community_read_access(CommunityReadAccess)@get_community_by_id:MANAGE_COMMUNITY_PAGES -> "UPDATE communities SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.community:{}");
|
||||
auto_method!(update_community_write_access(CommunityWriteAccess)@get_community_by_id:MANAGE_COMMUNITY_PAGES -> "UPDATE communities SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.community:{}");
|
||||
pub async fn cache_clear_community(&self, community: &Community) {
|
||||
self.2
|
||||
.remove(format!("atto.community:{}", community.id))
|
||||
.await;
|
||||
self.2
|
||||
.remove(format!("atto.community:{}", community.title))
|
||||
.await;
|
||||
}
|
||||
|
||||
auto_method!(incr_community_likes() -> "UPDATE communities SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.community:{}" --incr);
|
||||
auto_method!(incr_community_dislikes() -> "UPDATE communities SET likes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.community:{}" --incr);
|
||||
auto_method!(decr_community_likes() -> "UPDATE communities SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl="atto.community:{}" --decr);
|
||||
auto_method!(decr_community_dislikes() -> "UPDATE communities SET likes = dislikes - 1 WHERE id = $1" --cache-key-tmpl="atto.community:{}" --decr);
|
||||
auto_method!(delete_community()@get_community_by_id:MANAGE_COMMUNITIES -> "DELETE communities pages WHERE id = $1" --cache-key-tmpl=cache_clear_community);
|
||||
auto_method!(update_community_title(String)@get_community_by_id:MANAGE_COMMUNITIES -> "UPDATE communities SET title = $1 WHERE id = $2" --cache-key-tmpl=cache_clear_community);
|
||||
auto_method!(update_community_context(CommunityContext)@get_community_by_id:MANAGE_COMMUNITIES -> "UPDATE communities SET prompt = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
|
||||
auto_method!(update_community_read_access(CommunityReadAccess)@get_community_by_id:MANAGE_COMMUNITIES -> "UPDATE communities SET read_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
|
||||
auto_method!(update_community_write_access(CommunityWriteAccess)@get_community_by_id:MANAGE_COMMUNITIES -> "UPDATE communities SET write_access = $1 WHERE id = $2" --serde --cache-key-tmpl=cache_clear_community);
|
||||
|
||||
auto_method!(incr_community_likes()@get_community_by_id -> "UPDATE communities SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --incr);
|
||||
auto_method!(incr_community_dislikes()@get_community_by_id -> "UPDATE communities SET likes = dislikes + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --incr);
|
||||
auto_method!(decr_community_likes()@get_community_by_id -> "UPDATE communities SET likes = likes - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr);
|
||||
auto_method!(decr_community_dislikes()@get_community_by_id -> "UPDATE communities SET likes = dislikes - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr);
|
||||
|
||||
auto_method!(incr_community_member_count()@get_community_by_id -> "UPDATE communities SET member_count = member_count + 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --incr);
|
||||
auto_method!(decr_community_member_count()@get_community_by_id -> "UPDATE communities SET member_count = member_count - 1 WHERE id = $1" --cache-key-tmpl=cache_clear_community --decr);
|
||||
}
|
||||
|
|
|
@ -8,5 +8,7 @@ CREATE TABLE IF NOT EXISTS communities (
|
|||
write_access TEXT NOT NULL,
|
||||
-- likes
|
||||
likes INTEGER NOT NULL,
|
||||
dislikes INTEGER NOT NULL
|
||||
dislikes INTEGER NOT NULL,
|
||||
-- counts
|
||||
member_count INTEGER NOT NULL
|
||||
)
|
||||
|
|
|
@ -17,9 +17,9 @@ impl DataManager {
|
|||
) -> IpBan {
|
||||
IpBan {
|
||||
ip: get!(x->0(String)),
|
||||
created: get!(x->1(i64)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
reason: get!(x->2(String)),
|
||||
moderator: get!(x->3(i64)) as usize,
|
||||
moderator: get!(x->3(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,5 +1,6 @@
|
|||
use super::*;
|
||||
use crate::cache::Cache;
|
||||
use crate::model::communities::Community;
|
||||
use crate::model::{
|
||||
Error, Result, auth::User, communities::CommunityMembership,
|
||||
communities_permissions::CommunityPermission, permissions::FinePermission,
|
||||
|
@ -19,16 +20,25 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> CommunityMembership {
|
||||
CommunityMembership {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
community: get!(x->3(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
owner: get!(x->2(isize)) as usize,
|
||||
community: get!(x->3(isize)) as usize,
|
||||
role: CommunityPermission::from_bits(get!(x->4(u32))).unwrap(),
|
||||
}
|
||||
}
|
||||
|
||||
auto_method!(get_membership_by_id()@get_membership_from_row -> "SELECT * FROM memberships WHERE id = $1" --name="journal membership" --returns=CommunityMembership --cache-key-tmpl="atto.membership:{}");
|
||||
|
||||
/// Replace a list of community memberships with the proper community.
|
||||
pub async fn fill_communities(&self, list: Vec<CommunityMembership>) -> Result<Vec<Community>> {
|
||||
let mut communities: Vec<Community> = Vec::new();
|
||||
for membership in &list {
|
||||
communities.push(self.get_community_by_id(membership.community).await?);
|
||||
}
|
||||
Ok(communities)
|
||||
}
|
||||
|
||||
/// Get a community membership by `owner` and `community`.
|
||||
pub async fn get_membership_by_owner_community(
|
||||
&self,
|
||||
|
@ -87,7 +97,7 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO memberships VALUES ($1, $2, $3, $4, $5",
|
||||
"INSERT INTO memberships VALUES ($1, $2, $3, $4, $5)",
|
||||
&[
|
||||
&data.id.to_string().as_str(),
|
||||
&data.created.to_string().as_str(),
|
||||
|
@ -101,6 +111,10 @@ impl DataManager {
|
|||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.incr_community_member_count(data.community)
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
@ -139,6 +153,8 @@ impl DataManager {
|
|||
|
||||
self.2.remove(format!("atto.membership:{}", id)).await;
|
||||
|
||||
self.decr_community_member_count(y.community).await.unwrap();
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
|
|
|
@ -16,11 +16,11 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> Notification {
|
||||
Notification {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
title: get!(x->2(String)),
|
||||
content: get!(x->3(String)),
|
||||
owner: get!(x->4(i64)) as usize,
|
||||
owner: get!(x->4(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -1,10 +1,9 @@
|
|||
use super::*;
|
||||
use crate::cache::Cache;
|
||||
use crate::model::communities::PostContext;
|
||||
use crate::model::{
|
||||
Error, Result,
|
||||
auth::User,
|
||||
communities::{CommunityWriteAccess, Post},
|
||||
communities::{Community, CommunityWriteAccess, Post, PostContext},
|
||||
permissions::FinePermission,
|
||||
};
|
||||
use crate::{auto_method, execute, get, query_row, query_rows};
|
||||
|
@ -22,11 +21,11 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> Post {
|
||||
Post {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
content: get!(x->2(String)),
|
||||
owner: get!(x->3(i64)) as usize,
|
||||
community: get!(x->4(i64)) as usize,
|
||||
owner: get!(x->3(isize)) as usize,
|
||||
community: get!(x->4(isize)) as usize,
|
||||
context: serde_json::from_str(&get!(x->5(String))).unwrap(),
|
||||
replying_to: if let Some(id) = get!(x->6(Option<i64>)) {
|
||||
Some(id as usize)
|
||||
|
@ -34,10 +33,10 @@ impl DataManager {
|
|||
None
|
||||
},
|
||||
// likes
|
||||
likes: get!(x->7(i64)) as isize,
|
||||
dislikes: get!(x->8(i64)) as isize,
|
||||
likes: get!(x->7(isize)) as isize,
|
||||
dislikes: get!(x->8(isize)) as isize,
|
||||
// other counts
|
||||
comment_count: get!(x->9(i64)) as usize,
|
||||
comment_count: get!(x->9(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -49,17 +48,22 @@ impl DataManager {
|
|||
/// * `id` - the ID of the post the requested posts are commenting on
|
||||
/// * `batch` - the limit of posts in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_post_comments(&self, id: usize, batch: usize, page: usize) -> Result<Post> {
|
||||
pub async fn get_post_comments(
|
||||
&self,
|
||||
id: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_row!(
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM posts WHERE replying_to = $1 ORDER BY created DESC LIMIT $2 OFFSET $3",
|
||||
&[&(id as i64), &(batch as i64), &((page * batch) as i64)],
|
||||
|x| { Ok(Self::get_post_from_row(x)) }
|
||||
|x| { Self::get_post_from_row(x) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
|
@ -69,6 +73,38 @@ impl DataManager {
|
|||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Complete a vector of just posts with their owner as well.
|
||||
pub async fn fill_posts(&self, posts: Vec<Post>) -> Result<Vec<(Post, User)>> {
|
||||
let mut out: Vec<(Post, User)> = Vec::new();
|
||||
|
||||
for post in posts {
|
||||
let owner = post.owner.clone();
|
||||
out.push((post, self.get_user_by_id(owner).await?));
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Complete a vector of just posts with their owner and community as well.
|
||||
pub async fn fill_posts_with_community(
|
||||
&self,
|
||||
posts: Vec<Post>,
|
||||
) -> Result<Vec<(Post, User, Community)>> {
|
||||
let mut out: Vec<(Post, User, Community)> = Vec::new();
|
||||
|
||||
for post in posts {
|
||||
let owner = post.owner.clone();
|
||||
let community = post.community.clone();
|
||||
out.push((
|
||||
post,
|
||||
self.get_user_by_id(owner).await?,
|
||||
self.get_community_by_id(community).await?,
|
||||
));
|
||||
}
|
||||
|
||||
Ok(out)
|
||||
}
|
||||
|
||||
/// Get all posts from the given user (from most recent).
|
||||
///
|
||||
/// # Arguments
|
||||
|
@ -100,11 +136,42 @@ impl DataManager {
|
|||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Get all posts from the given community (from most recent).
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `id` - the ID of the community the requested posts belong to
|
||||
/// * `batch` - the limit of posts in each page
|
||||
/// * `page` - the page number
|
||||
pub async fn get_posts_by_community(
|
||||
&self,
|
||||
id: usize,
|
||||
batch: usize,
|
||||
page: usize,
|
||||
) -> Result<Vec<Post>> {
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = query_rows!(
|
||||
&conn,
|
||||
"SELECT * FROM posts WHERE community = $1 AND replying_to IS NULL 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) }
|
||||
);
|
||||
|
||||
if res.is_err() {
|
||||
return Err(Error::GeneralNotFound("post".to_string()));
|
||||
}
|
||||
|
||||
Ok(res.unwrap())
|
||||
}
|
||||
|
||||
/// Create a new journal entry in the database.
|
||||
///
|
||||
/// # Arguments
|
||||
/// * `data` - a mock [`JournalEntry`] object to insert
|
||||
pub async fn create_post(&self, data: Post) -> Result<()> {
|
||||
pub async fn create_post(&self, data: Post) -> Result<usize> {
|
||||
// check values
|
||||
if data.content.len() < 2 {
|
||||
return Err(Error::DataTooShort("content".to_string()));
|
||||
|
@ -112,21 +179,21 @@ impl DataManager {
|
|||
return Err(Error::DataTooLong("username".to_string()));
|
||||
}
|
||||
|
||||
// check permission in page
|
||||
let page = match self.get_community_by_id(data.community).await {
|
||||
// check permission in community
|
||||
let community = match self.get_community_by_id(data.community).await {
|
||||
Ok(p) => p,
|
||||
Err(e) => return Err(e),
|
||||
};
|
||||
|
||||
match page.write_access {
|
||||
match community.write_access {
|
||||
CommunityWriteAccess::Owner => {
|
||||
if data.owner != page.owner {
|
||||
if data.owner != community.owner {
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
}
|
||||
CommunityWriteAccess::Joined => {
|
||||
if let Err(_) = self
|
||||
.get_membership_by_owner_community(data.owner, page.id)
|
||||
.get_membership_by_owner_community(data.owner, community.id)
|
||||
.await
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
|
@ -135,6 +202,16 @@ impl DataManager {
|
|||
_ => (),
|
||||
};
|
||||
|
||||
// check if we're blocked
|
||||
if let Some(replying_to) = data.replying_to {
|
||||
if let Ok(_) = self
|
||||
.get_userblock_by_initiator_receiver(replying_to, data.owner)
|
||||
.await
|
||||
{
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
}
|
||||
|
||||
// ...
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
|
@ -143,7 +220,7 @@ impl DataManager {
|
|||
|
||||
let res = execute!(
|
||||
&conn,
|
||||
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9)",
|
||||
"INSERT INTO posts VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)",
|
||||
&[
|
||||
&Some(data.id.to_string()),
|
||||
&Some(data.created.to_string()),
|
||||
|
@ -171,13 +248,43 @@ impl DataManager {
|
|||
self.incr_post_comments(id).await.unwrap();
|
||||
}
|
||||
|
||||
// return
|
||||
Ok(data.id)
|
||||
}
|
||||
|
||||
pub async fn delete_post(&self, id: usize, user: User) -> Result<()> {
|
||||
let y = self.get_post_by_id(id).await?;
|
||||
|
||||
if user.id != y.owner {
|
||||
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
|
||||
return Err(Error::NotAllowed);
|
||||
}
|
||||
}
|
||||
|
||||
let conn = match self.connect().await {
|
||||
Ok(c) => c,
|
||||
Err(e) => return Err(Error::DatabaseConnection(e.to_string())),
|
||||
};
|
||||
|
||||
let res = execute!(&conn, "DELETE FROM posts WHERE id = $1", &[&id.to_string()]);
|
||||
|
||||
if let Err(e) = res {
|
||||
return Err(Error::DatabaseError(e.to_string()));
|
||||
}
|
||||
|
||||
self.2.remove(format!("atto.post:{}", id)).await;
|
||||
|
||||
// decr parent comment count
|
||||
if let Some(replying_to) = y.replying_to {
|
||||
self.decr_post_comments(replying_to).await.unwrap();
|
||||
}
|
||||
|
||||
// return
|
||||
Ok(())
|
||||
}
|
||||
|
||||
auto_method!(delete_post()@get_post_by_id:MANAGE_COMMUNITY_ENTRIES -> "DELETE FROM posts WHERE id = $1" --cache-key-tmpl="atto.post:{}");
|
||||
auto_method!(update_post_content(String)@get_post_by_id:MANAGE_COMMUNITY_ENTRIES -> "UPDATE posts SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.post:{}");
|
||||
auto_method!(update_post_context(PostContext)@get_post_by_id:MANAGE_COMMUNITY_ENTRIES -> "UPDATE posts SET context = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.post:{}");
|
||||
auto_method!(update_post_content(String)@get_post_by_id:MANAGE_POSTS -> "UPDATE posts SET content = $1 WHERE id = $2" --cache-key-tmpl="atto.post:{}");
|
||||
auto_method!(update_post_context(PostContext)@get_post_by_id:MANAGE_POSTS -> "UPDATE posts SET context = $1 WHERE id = $2" --serde --cache-key-tmpl="atto.post:{}");
|
||||
|
||||
auto_method!(incr_post_likes() -> "UPDATE posts SET likes = likes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
|
||||
auto_method!(incr_post_dislikes() -> "UPDATE posts SET likes = dislikes + 1 WHERE id = $1" --cache-key-tmpl="atto.post:{}" --incr);
|
||||
|
|
|
@ -21,10 +21,10 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> Reaction {
|
||||
Reaction {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
owner: get!(x->2(i64)) as usize,
|
||||
asset: get!(x->3(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
owner: get!(x->2(isize)) as usize,
|
||||
asset: get!(x->3(isize)) as usize,
|
||||
asset_type: serde_json::from_str(&get!(x->4(String))).unwrap(),
|
||||
is_like: if get!(x->5(i8)) == 1 { true } else { false },
|
||||
}
|
||||
|
|
|
@ -16,10 +16,10 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> UserBlock {
|
||||
UserBlock {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
initiator: get!(x->2(i64)) as usize,
|
||||
receiver: get!(x->3(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
initiator: get!(x->2(isize)) as usize,
|
||||
receiver: get!(x->3(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,10 @@ impl DataManager {
|
|||
#[cfg(feature = "postgres")] x: &Row,
|
||||
) -> UserFollow {
|
||||
UserFollow {
|
||||
id: get!(x->0(i64)) as usize,
|
||||
created: get!(x->1(i64)) as usize,
|
||||
initiator: get!(x->2(i64)) as usize,
|
||||
receiver: get!(x->3(i64)) as usize,
|
||||
id: get!(x->0(isize)) as usize,
|
||||
created: get!(x->1(isize)) as usize,
|
||||
initiator: get!(x->2(isize)) as usize,
|
||||
receiver: get!(x->3(isize)) as usize,
|
||||
}
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue