Add API method for unmarking favourite posts

This commit is contained in:
silverpill 2021-10-18 23:42:56 +00:00
parent b6be5c8489
commit 486c819bc3
2 changed files with 45 additions and 1 deletions

View file

@ -27,7 +27,10 @@ use crate::models::posts::queries::{
update_post,
};
use crate::models::posts::types::PostCreateData;
use crate::models::reactions::queries::create_reaction;
use crate::models::reactions::queries::{
create_reaction,
delete_reaction,
};
use super::types::{Status, StatusData};
#[post("")]
@ -144,6 +147,25 @@ async fn favourite(
Ok(HttpResponse::Ok().json(status))
}
#[post("/{status_id}/unfavourite")]
async fn unfavourite(
auth: BearerAuth,
config: web::Data<Config>,
db_pool: web::Data<Pool>,
web::Path(status_id): web::Path<Uuid>,
) -> Result<HttpResponse, HttpError> {
let db_client = &mut **get_database_client(&db_pool).await?;
let current_user = get_current_user(db_client, auth.token()).await?;
match delete_reaction(db_client, &current_user.id, &status_id).await {
Err(DatabaseError::NotFound(_)) => (), // post not favourited
other_result => other_result?,
}
let mut post = get_post_by_id(db_client, &status_id).await?;
get_actions_for_post(db_client, &current_user.id, &mut post).await?;
let status = Status::from_post(post, &config.instance_url());
Ok(HttpResponse::Ok().json(status))
}
// https://docs.opensea.io/docs/metadata-standards
#[derive(Serialize)]
struct PostMetadata {
@ -236,6 +258,7 @@ pub fn status_api_scope() -> Scope {
.service(get_status)
.service(get_context)
.service(favourite)
.service(unfavourite)
.service(make_permanent)
.service(get_signature)
}

View file

@ -24,6 +24,27 @@ pub async fn create_reaction(
Ok(())
}
pub async fn delete_reaction(
db_client: &mut impl GenericClient,
author_id: &Uuid,
post_id: &Uuid,
) -> Result<(), DatabaseError> {
let transaction = db_client.transaction().await?;
let deleted_count = transaction.execute(
"
DELETE FROM post_reaction
WHERE author_id = $1 AND post_id = $2
",
&[&author_id, &post_id],
).await?;
if deleted_count == 0 {
return Err(DatabaseError::NotFound("reaction"));
}
update_reaction_count(&transaction, post_id, -1).await?;
transaction.commit().await?;
Ok(())
}
pub async fn get_favourited(
db_client: &impl GenericClient,
user_id: &Uuid,