2 diesel::OptionalExtension,
4 source::activity::{ReceivedActivity, SentActivity, SentActivityForm},
5 utils::{get_conn, DbPool},
9 result::{DatabaseErrorKind, Error, Error::DatabaseError},
13 use diesel_async::RunQueryDsl;
16 pub async fn create(pool: &mut DbPool<'_>, form: SentActivityForm) -> Result<Self, Error> {
17 use crate::schema::sent_activity::dsl::sent_activity;
18 let conn = &mut get_conn(pool).await?;
19 insert_into(sent_activity)
21 .get_result::<Self>(conn)
25 pub async fn read_from_apub_id(pool: &mut DbPool<'_>, object_id: &DbUrl) -> Result<Self, Error> {
26 use crate::schema::sent_activity::dsl::{ap_id, sent_activity};
27 let conn = &mut get_conn(pool).await?;
29 .filter(ap_id.eq(object_id))
35 impl ReceivedActivity {
36 pub async fn create(pool: &mut DbPool<'_>, ap_id_: &DbUrl) -> Result<(), Error> {
37 use crate::schema::received_activity::dsl::{ap_id, id, received_activity};
38 let conn = &mut get_conn(pool).await?;
39 let res = insert_into(received_activity)
40 .values(ap_id.eq(ap_id_))
41 .on_conflict_do_nothing()
43 .get_result::<i64>(conn)
47 // new activity inserted successfully
52 DatabaseErrorKind::UniqueViolation,
53 Box::<String>::default(),
62 use crate::utils::build_db_pool_for_tests;
64 use serial_test::serial;
69 async fn receive_activity_duplicate() {
70 let pool = &build_db_pool_for_tests().await;
71 let pool = &mut pool.into();
72 let ap_id: DbUrl = Url::parse("http://example.com/activity/531")
76 // inserting activity for first time
77 let res = ReceivedActivity::create(pool, &ap_id).await;
80 let res = ReceivedActivity::create(pool, &ap_id).await;
81 assert!(res.is_err());
86 async fn sent_activity_write_read() {
87 let pool = &build_db_pool_for_tests().await;
88 let pool = &mut pool.into();
89 let ap_id: DbUrl = Url::parse("http://example.com/activity/412")
93 "key1": "0xF9BA143B95FF6D82",
96 let sensitive = false;
98 let form = SentActivityForm {
104 SentActivity::create(pool, form).await.unwrap();
106 let res = SentActivity::read_from_apub_id(pool, &ap_id).await.unwrap();
107 assert_eq!(res.ap_id, ap_id);
108 assert_eq!(res.data, data);
109 assert_eq!(res.sensitive, sensitive);