Store IDs of incoming Like() activities

This commit is contained in:
silverpill 2021-12-14 22:59:15 +00:00
parent 42624ab9a5
commit 99f32e8202
7 changed files with 34 additions and 9 deletions

View file

@ -0,0 +1 @@
ALTER TABLE post_reaction ADD COLUMN activity_id VARCHAR(250) UNIQUE;

View file

@ -51,6 +51,7 @@ CREATE TABLE post_reaction (
id UUID PRIMARY KEY,
author_id UUID NOT NULL REFERENCES actor_profile (id) ON DELETE CASCADE,
post_id UUID NOT NULL REFERENCES post (id) ON DELETE CASCADE,
activity_id VARCHAR(250) UNIQUE,
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT now(),
UNIQUE (author_id, post_id)
);

View file

@ -389,7 +389,12 @@ pub async fn receive_activity(
post.id
},
};
create_reaction(db_client, &author.id, &post_id).await?;
create_reaction(
db_client,
&author.id,
&post_id,
Some(&activity.id),
).await?;
},
(FOLLOW, _) => {
let source_profile = get_or_fetch_profile_by_actor_id(

View file

@ -233,7 +233,7 @@ async fn favourite(
return Err(HttpError::NotFoundError("post"));
};
let reaction_created = match create_reaction(
db_client, &current_user.id, &status_id,
db_client, &current_user.id, &status_id, None,
).await {
Ok(_) => {
post.reaction_count += 1;

View file

@ -1 +1,2 @@
pub mod queries;
mod types;

View file

@ -9,21 +9,25 @@ use crate::models::posts::queries::{
get_post_author,
};
use crate::utils::id::new_uuid;
use super::types::DbReaction;
pub async fn create_reaction(
db_client: &mut impl GenericClient,
author_id: &Uuid,
post_id: &Uuid,
) -> Result<(), DatabaseError> {
activity_id: Option<&String>,
) -> Result<DbReaction, DatabaseError> {
let transaction = db_client.transaction().await?;
let reaction_id = new_uuid();
transaction.execute(
let row = transaction.query_one(
"
INSERT INTO post_reaction (id, author_id, post_id)
VALUES ($1, $2, $3)
INSERT INTO post_reaction (id, author_id, post_id, activity_id)
VALUES ($1, $2, $3, $4)
RETURNING post_reaction
",
&[&reaction_id, &author_id, &post_id],
&[&reaction_id, &author_id, &post_id, &activity_id],
).await.map_err(catch_unique_violation("reaction"))?;
let reaction: DbReaction = row.try_get("post_reaction")?;
update_reaction_count(&transaction, post_id, 1).await?;
let post_author = get_post_author(&transaction, post_id).await?;
if post_author.is_local() && post_author.id != *author_id {
@ -33,9 +37,9 @@ pub async fn create_reaction(
&post_author.id,
post_id,
).await?;
}
};
transaction.commit().await?;
Ok(())
Ok(reaction)
}
pub async fn delete_reaction(

View file

@ -0,0 +1,13 @@
use chrono::{DateTime, Utc};
use postgres_types::FromSql;
use uuid::Uuid;
#[derive(FromSql)]
#[postgres(name = "post_reaction")]
pub struct DbReaction {
pub id: Uuid,
pub author_id: Uuid,
pub post_id: Uuid,
pub activity_id: Option<String>,
pub created_at: DateTime<Utc>,
}