add reposts/quotes

fix #2
This commit is contained in:
trisua 2025-04-10 18:16:52 -04:00
parent 15e24b9a61
commit df32b9d65e
43 changed files with 708 additions and 234 deletions

View file

@ -34,11 +34,7 @@ impl DataManager {
settings: serde_json::from_str(&get!(x->5(String)).to_string()).unwrap(),
tokens: serde_json::from_str(&get!(x->6(String)).to_string()).unwrap(),
permissions: FinePermission::from_bits(get!(x->7(i32)) as u32).unwrap(),
is_verified: if get!(x->8(i32)) as i8 == 1 {
true
} else {
false
},
is_verified: get!(x->8(i32)) as i8 == 1,
notification_count: get!(x->9(i32)) as usize,
follower_count: get!(x->10(i32)) as usize,
following_count: get!(x->11(i32)) as usize,
@ -80,7 +76,7 @@ impl DataManager {
/// # Arguments
/// * `data` - a mock [`User`] object to insert
pub async fn create_user(&self, mut data: User) -> Result<()> {
if self.0.security.registration_enabled == false {
if !self.0.security.registration_enabled {
return Err(Error::RegistrationDisabled);
}
@ -124,10 +120,10 @@ impl DataManager {
&serde_json::to_string(&data.settings).unwrap(),
&serde_json::to_string(&data.tokens).unwrap(),
&(FinePermission::DEFAULT.bits() as i32),
&(if data.is_verified { 1 as i32 } else { 0 as i32 }),
&(0 as i32),
&(0 as i32),
&(0 as i32),
&(if data.is_verified { 1_i32 } else { 0_i32 }),
&0_i32,
&0_i32,
&0_i32,
&(data.last_seen as i64),
&String::new(),
&"[]"
@ -260,7 +256,7 @@ impl DataManager {
let res = execute!(
&conn,
"UPDATE users SET verified = $1 WHERE id = $2",
params![&(if x { 1 } else { 0 } as i32), &(id as i64)]
params![&{ if x { 1 } else { 0 } }, &(id as i64)]
);
if let Err(e) = res {
@ -327,7 +323,7 @@ impl DataManager {
let res = execute!(
&conn,
"UPDATE users SET username = $1 WHERE id = $3",
"UPDATE users SET username = $1 WHERE id = $2",
params![&to.as_str(), &(id as i64)]
);
@ -414,7 +410,7 @@ impl DataManager {
return Err(Error::DatabaseError(e.to_string()));
}
self.cache_clear_user(&user).await;
self.cache_clear_user(user).await;
Ok(())
}

View file

@ -174,7 +174,7 @@ macro_rules! auto_method {
if !user.permissions.check(FinePermission::$permission) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(crate::model::moderation::AuditLogEntry::new(
self.create_audit_log_entry($crate::model::moderation::AuditLogEntry::new(
user.id,
format!("invoked `{}` with x value `{id}`", stringify!($name)),
))
@ -204,7 +204,7 @@ macro_rules! auto_method {
if !user.permissions.check(FinePermission::$permission) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(crate::model::moderation::AuditLogEntry::new(
self.create_audit_log_entry($crate::model::moderation::AuditLogEntry::new(
user.id,
format!("invoked `{}` with x value `{id}`", stringify!($name)),
))
@ -236,7 +236,7 @@ macro_rules! auto_method {
if !user.permissions.check(FinePermission::$permission) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(crate::model::moderation::AuditLogEntry::new(
self.create_audit_log_entry($crate::model::moderation::AuditLogEntry::new(
user.id,
format!("invoked `{}` with x value `{id}`", stringify!($name)),
))

View file

@ -224,9 +224,9 @@ impl DataManager {
&serde_json::to_string(&data.read_access).unwrap().as_str(),
&serde_json::to_string(&data.write_access).unwrap().as_str(),
&serde_json::to_string(&data.join_access).unwrap().as_str(),
&(0 as i32),
&(0 as i32),
&(1 as i32)
&0_i32,
&0_i32,
&1_i32
]
);

View file

@ -21,7 +21,7 @@ pub struct DataManager(
impl DataManager {
/// Obtain a connection to the staging database.
pub(crate) async fn connect(&self) -> Result<Connection> {
Ok(Connection::open(&self.0.database.name)?)
Connection::open(&self.0.database.name)
}
/// Create a new [`DataManager`] (and init database).

View file

@ -50,7 +50,7 @@ impl DataManager {
) -> Result<Vec<(CommunityMembership, User)>> {
let mut users: Vec<(CommunityMembership, User)> = Vec::new();
for membership in list {
let owner = membership.owner.clone();
let owner = membership.owner;
users.push((membership, self.get_user_by_id(owner).await?));
}
Ok(users)

View file

@ -21,11 +21,7 @@ impl DataManager {
title: get!(x->2(String)),
content: get!(x->3(String)),
owner: get!(x->4(i64)) as usize,
read: if get!(x->5(i32)) as i8 == 1 {
true
} else {
false
},
read: get!(x->5(i32)) as i8 == 1,
}
}
@ -71,7 +67,7 @@ impl DataManager {
&data.title,
&data.content,
&(data.owner as i64),
&(if data.read { 1 } else { 0 } as i32)
&{ if data.read { 1 } else { 0 } }
]
);
@ -89,10 +85,8 @@ impl DataManager {
pub async fn delete_notification(&self, id: usize, user: &User) -> Result<()> {
let notification = self.get_notification_by_id(id).await?;
if user.id != notification.owner {
if !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
return Err(Error::NotAllowed);
}
if user.id != notification.owner && !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
return Err(Error::NotAllowed);
}
let conn = match self.connect().await {
@ -127,10 +121,8 @@ impl DataManager {
let notifications = self.get_notifications_by_owner(user.id).await?;
for notification in notifications {
if user.id != notification.owner {
if !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
return Err(Error::NotAllowed);
}
if user.id != notification.owner && !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
return Err(Error::NotAllowed);
}
self.delete_notification(notification.id, user).await?
@ -147,10 +139,8 @@ impl DataManager {
) -> Result<()> {
let y = self.get_notification_by_id(id).await?;
if y.owner != user.id {
if !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
return Err(Error::NotAllowed);
}
if y.owner != user.id && !user.permissions.check(FinePermission::MANAGE_NOTIFICATIONS) {
return Err(Error::NotAllowed);
}
// ...
@ -162,7 +152,7 @@ impl DataManager {
let res = execute!(
&conn,
"UPDATE notifications SET read = $1 WHERE id = $2",
params![&(if new_read { 1 } else { 0 } as i32), &(id as i64)]
params![&{ if new_read { 1 } else { 0 } }, &(id as i64)]
);
if let Err(e) = res {
@ -171,9 +161,9 @@ impl DataManager {
self.2.remove(format!("atto.notification:{}", id)).await;
if (y.read == true) && (new_read == false) {
if (y.read) && (!new_read) {
self.incr_user_notifications(user.id).await?;
} else if (y.read == false) && (new_read == true) {
} else if (!y.read) && (new_read) {
self.decr_user_notifications(user.id).await?;
}

View file

@ -33,11 +33,7 @@ impl DataManager {
owner: get!(x->3(i64)) as usize,
community: get!(x->4(i64)) 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)
} else {
None
},
replying_to: get!(x->6(Option<i64>)).map(|id| id as usize),
// likes
likes: get!(x->7(i32)) as isize,
dislikes: get!(x->8(i32)) as isize,
@ -79,20 +75,52 @@ impl DataManager {
Ok(res.unwrap())
}
/// Get the post the given post is reposting (if some).
pub async fn get_post_reposting(&self, post: &Post) -> Option<(User, Post)> {
if let Some(ref repost) = post.context.repost {
if let Some(reposting) = repost.reposting {
let mut x = match self.get_post_by_id(reposting).await {
Ok(p) => p,
Err(_) => return None,
};
x.mark_as_repost();
Some((
match self.get_user_by_id(x.owner).await {
Ok(ua) => ua,
Err(_) => return None,
},
x,
))
} else {
None
}
} else {
None
}
}
/// 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();
pub async fn fill_posts(
&self,
posts: Vec<Post>,
) -> Result<Vec<(Post, User, Option<(User, Post)>)>> {
let mut out: Vec<(Post, User, Option<(User, Post)>)> = Vec::new();
let mut users: HashMap<usize, User> = HashMap::new();
for post in posts {
let owner = post.owner.clone();
let owner = post.owner;
if let Some(user) = users.get(&owner) {
out.push((post, user.clone()));
out.push((
post.clone(),
user.clone(),
self.get_post_reposting(&post).await,
));
} else {
let user = self.get_user_by_id(owner).await?;
users.insert(owner, user.clone());
out.push((post, user));
out.push((post.clone(), user, self.get_post_reposting(&post).await));
}
}
@ -103,22 +131,62 @@ impl DataManager {
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();
user_id: usize,
) -> Result<Vec<(Post, User, Community, Option<(User, Post)>)>> {
let mut out: Vec<(Post, User, Community, Option<(User, Post)>)> = Vec::new();
let mut seen_before: HashMap<(usize, usize), (User, Community)> = HashMap::new();
let mut seen_user_follow_statuses: HashMap<(usize, usize), bool> = HashMap::new();
for post in posts {
let owner = post.owner.clone();
let community = post.community.clone();
let owner = post.owner;
let community = post.community;
if let Some((user, community)) = seen_before.get(&(owner, community)) {
out.push((post, user.clone(), community.to_owned()));
out.push((
post.clone(),
user.clone(),
community.to_owned(),
self.get_post_reposting(&post).await,
));
} else {
let user = self.get_user_by_id(owner).await?;
let community = self.get_community_by_id(community).await?;
// check relationship
if user.settings.private_profile {
if user_id == 0 {
continue;
}
if let Some(is_following) = seen_user_follow_statuses.get(&(user.id, user_id)) {
if !is_following {
// post owner is not following us
continue;
}
} else {
if self
.get_userfollow_by_initiator_receiver(user.id, user_id)
.await
.is_err()
{
// post owner is not following us
seen_user_follow_statuses.insert((user.id, user_id), false);
continue;
}
seen_user_follow_statuses.insert((user.id, user_id), true);
}
}
// ...
let community = self.get_community_by_id(community).await?;
seen_before.insert((owner, community.id), (user.clone(), community.clone()));
out.push((post, user, community));
out.push((
post.clone(),
user,
community,
self.get_post_reposting(&post).await,
));
}
}
@ -357,25 +425,13 @@ impl DataManager {
/// Check if the given `uid` can post in the given `community`.
pub async fn check_can_post(&self, community: &Community, uid: usize) -> bool {
match community.write_access {
CommunityWriteAccess::Owner => {
if uid != community.owner {
false
} else {
true
}
}
CommunityWriteAccess::Owner => uid == community.owner,
CommunityWriteAccess::Joined => {
match self
.get_membership_by_owner_community(uid, community.id)
.await
{
Ok(m) => {
if !m.role.check_member() {
false
} else {
true
}
}
Ok(m) => !(!m.role.check_member()),
Err(_) => false,
}
}
@ -388,11 +444,19 @@ impl DataManager {
/// # Arguments
/// * `data` - a mock [`JournalEntry`] object to insert
pub async fn create_post(&self, mut data: Post) -> Result<usize> {
// check values
if data.content.len() < 2 {
return Err(Error::DataTooShort("content".to_string()));
} else if data.content.len() > 4096 {
return Err(Error::DataTooLong("username".to_string()));
// check values (if this isn't reposting something else)
let is_reposting = if let Some(ref repost) = data.context.repost {
repost.reposting.is_some()
} else {
false
};
if !is_reposting {
if data.content.len() < 2 {
return Err(Error::DataTooShort("content".to_string()));
} else if data.content.len() > 4096 {
return Err(Error::DataTooLong("content".to_string()));
}
}
// check permission in community
@ -408,10 +472,37 @@ impl DataManager {
// mirror nsfw state
data.context.is_nsfw = community.context.is_nsfw;
// check if we're blocked
if let Some(replying_to) = data.replying_to {
// check if we're reposting a post
let reposting = if let Some(ref repost) = data.context.repost {
if let Some(id) = repost.reposting {
Some(self.get_post_by_id(id).await?)
} else {
None
}
} else {
None
};
if let Some(ref rt) = reposting {
data.context.reposts_enabled = false; // cannot repost reposts
// make sure we aren't trying to repost a repost
if if let Some(ref repost) = rt.context.repost {
!(!repost.is_repost)
} else {
false
} {
return Err(Error::MiscError("Cannot repost a repost".to_string()));
}
// ...
if !rt.context.reposts_enabled {
return Err(Error::MiscError("Post has reposts disabled".to_string()));
}
// check blocked status
if let Ok(_) = self
.get_userblock_by_initiator_receiver(replying_to, data.owner)
.get_userblock_by_initiator_receiver(rt.owner, data.owner)
.await
{
return Err(Error::NotAllowed);
@ -429,6 +520,14 @@ impl DataManager {
if !rt.context.comments_enabled {
return Err(Error::MiscError("Post has comments disabled".to_string()));
}
// check blocked status
if let Ok(_) = self
.get_userblock_by_initiator_receiver(rt.owner, data.owner)
.await
{
return Err(Error::NotAllowed);
}
}
// send mention notifications
@ -480,11 +579,11 @@ impl DataManager {
&if replying_to_id != "0" {
replying_to_id.parse::<i64>().unwrap()
} else {
0 as i64
0_i64
},
&(0 as i32),
&(0 as i32),
&(0 as i32)
&0_i32,
&0_i32,
&0_i32
]
);
@ -509,7 +608,7 @@ impl DataManager {
))
.await?;
if rt.context.comments_enabled == false {
if !rt.context.comments_enabled {
return Err(Error::NotAllowed);
}
}
@ -564,8 +663,14 @@ impl DataManager {
Ok(())
}
pub async fn update_post_context(&self, id: usize, user: User, x: PostContext) -> Result<()> {
pub async fn update_post_context(
&self,
id: usize,
user: User,
mut x: PostContext,
) -> Result<()> {
let y = self.get_post_by_id(id).await?;
x.repost = y.context.repost; // cannot change repost settings at all
let user_membership = self
.get_membership_by_owner_community(user.id, y.community)
@ -588,19 +693,19 @@ impl DataManager {
}
// check if we can manage pins
if x.is_pinned != y.context.is_pinned {
if !user_membership.role.check(CommunityPermission::MANAGE_PINS) {
// lacking this permission is overtaken by having the MANAGE_POSTS
// global permission
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `update_post_context(pinned)` with x value `{id}`"),
))
.await?
}
if x.is_pinned != y.context.is_pinned
&& !user_membership.role.check(CommunityPermission::MANAGE_PINS)
{
// lacking this permission is overtaken by having the MANAGE_POSTS
// global permission
if !user.permissions.check(FinePermission::MANAGE_POSTS) {
return Err(Error::NotAllowed);
} else {
self.create_audit_log_entry(AuditLogEntry::new(
user.id,
format!("invoked `update_post_context(pinned)` with x value `{id}`"),
))
.await?
}
}

View file

@ -26,11 +26,7 @@ impl DataManager {
owner: get!(x->2(i64)) as usize,
asset: get!(x->3(i64)) as usize,
asset_type: serde_json::from_str(&get!(x->4(String))).unwrap(),
is_like: if get!(x->5(i32)) as i8 == 1 {
true
} else {
false
},
is_like: get!(x->5(i32)) as i8 == 1,
}
}
@ -80,7 +76,7 @@ impl DataManager {
&(data.owner as i64),
&(data.asset as i64),
&serde_json::to_string(&data.asset_type).unwrap().as_str(),
&(if data.is_like { 1 } else { 0 } as i32)
&{ if data.is_like { 1 } else { 0 } }
]
);
@ -103,7 +99,7 @@ impl DataManager {
let community = self.get_community_by_id_no_void(data.asset).await.unwrap();
if community.owner != user.id {
if let Err(e) = self
self
.create_notification(Notification::new(
"Your community has received a like!".to_string(),
format!(
@ -112,10 +108,7 @@ impl DataManager {
),
community.owner,
))
.await
{
return Err(e);
}
.await?
}
}
}
@ -132,19 +125,16 @@ impl DataManager {
let post = self.get_post_by_id(data.asset).await.unwrap();
if post.owner != user.id {
if let Err(e) = self
self
.create_notification(Notification::new(
"Your post has received a like!".to_string(),
format!(
"[@{}](/api/v1/auth/user/find/{}) has liked your post!",
user.username, user.id
"[@{}](/api/v1/auth/user/find/{}) has liked your [post](/post/{})!",
user.username, user.id, data.asset
),
post.owner,
))
.await
{
return Err(e);
}
.await?
}
}
}
@ -160,10 +150,8 @@ impl DataManager {
pub async fn delete_reaction(&self, id: usize, user: &User) -> Result<()> {
let reaction = self.get_reaction_by_id(id).await?;
if user.id != reaction.owner {
if !user.permissions.check(FinePermission::MANAGE_REACTIONS) {
return Err(Error::NotAllowed);
}
if user.id != reaction.owner && !user.permissions.check(FinePermission::MANAGE_REACTIONS) {
return Err(Error::NotAllowed);
}
let conn = match self.connect().await {
@ -186,26 +174,22 @@ impl DataManager {
// decr corresponding
match reaction.asset_type {
AssetType::Community => {
if let Err(e) = {
{
if reaction.is_like {
self.decr_community_likes(reaction.asset).await
} else {
self.decr_community_dislikes(reaction.asset).await
}
} {
return Err(e);
}
}?
}
AssetType::Post => {
if let Err(e) = {
{
if reaction.is_like {
self.decr_post_likes(reaction.asset).await
} else {
self.decr_post_dislikes(reaction.asset).await
}
} {
return Err(e);
}
}?
}
AssetType::User => {
return Err(Error::NotAllowed);

View file

@ -193,7 +193,7 @@ impl DataManager {
let mut out: Vec<(UserFollow, User)> = Vec::new();
for userfollow in userfollows {
let receiver = userfollow.receiver.clone();
let receiver = userfollow.receiver;
out.push((userfollow, self.get_user_by_id(receiver).await?));
}
@ -208,7 +208,7 @@ impl DataManager {
let mut out: Vec<(UserFollow, User)> = Vec::new();
for userfollow in userfollows {
let initiator = userfollow.initiator.clone();
let initiator = userfollow.initiator;
out.push((userfollow, self.get_user_by_id(initiator).await?));
}
@ -254,10 +254,8 @@ impl DataManager {
pub async fn delete_userfollow(&self, id: usize, user: &User) -> Result<()> {
let follow = self.get_userfollow_by_id(id).await?;
if (user.id != follow.initiator) && (user.id != follow.receiver) {
if !user.permissions.check(FinePermission::MANAGE_FOLLOWS) {
return Err(Error::NotAllowed);
}
if (user.id != follow.initiator) && (user.id != follow.receiver) && !user.permissions.check(FinePermission::MANAGE_FOLLOWS) {
return Err(Error::NotAllowed);
}
let conn = match self.connect().await {