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(),
61 #![allow(clippy::unwrap_used)]
62 #![allow(clippy::indexing_slicing)]
65 use crate::utils::build_db_pool_for_tests;
67 use serial_test::serial;
72 async fn receive_activity_duplicate() {
73 let pool = &build_db_pool_for_tests().await;
74 let pool = &mut pool.into();
75 let ap_id: DbUrl = Url::parse("http://example.com/activity/531")
79 // inserting activity for first time
80 let res = ReceivedActivity::create(pool, &ap_id).await;
83 let res = ReceivedActivity::create(pool, &ap_id).await;
84 assert!(res.is_err());
89 async fn sent_activity_write_read() {
90 let pool = &build_db_pool_for_tests().await;
91 let pool = &mut pool.into();
92 let ap_id: DbUrl = Url::parse("http://example.com/activity/412")
96 "key1": "0xF9BA143B95FF6D82",
99 let sensitive = false;
101 let form = SentActivityForm {
102 ap_id: ap_id.clone(),
107 SentActivity::create(pool, form).await.unwrap();
109 let res = SentActivity::read_from_apub_id(pool, &ap_id).await.unwrap();
110 assert_eq!(res.ap_id, ap_id);
111 assert_eq!(res.data, data);
112 assert_eq!(res.sensitive, sensitive);