about summary refs log tree commit diff
path: root/src/database
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-12-31 01:03:54 +0300
committerVika <vika@fireburn.ru>2024-12-31 01:03:54 +0300
commit2e70534967447e1c34c68d21444a81b851bd4d13 (patch)
treee528acba8684fcb2ea67cc87a40836b2d5767203 /src/database
parentfecd5b75396f02be26d75aa5db48aaef723298c5 (diff)
downloadkittybox-2e70534967447e1c34c68d21444a81b851bd4d13.tar.zst
Storage::all_posts to return all posts for ?q=source w/o filters
Change-Id: I5d1220b6a2abbcb460bdb13c088c3dbd7e4d9856
Diffstat (limited to 'src/database')
-rw-r--r--src/database/file/mod.rs5
-rw-r--r--src/database/memory.rs5
-rw-r--r--src/database/mod.rs4
-rw-r--r--src/database/postgres/mod.rs11
4 files changed, 25 insertions, 0 deletions
diff --git a/src/database/file/mod.rs b/src/database/file/mod.rs
index 6343f1f..5890f95 100644
--- a/src/database/file/mod.rs
+++ b/src/database/file/mod.rs
@@ -754,4 +754,9 @@ impl Storage for FileStorage {
 
         Ok(())
     }
+
+    async fn all_posts<'this>(&'this self, user: &url::Url) -> Result<impl futures::Stream<Item = serde_json::Value> + Send + 'this> {
+        todo!();
+        Ok(futures::stream::empty()) // for type inference
+    }
 }
diff --git a/src/database/memory.rs b/src/database/memory.rs
index f799f2c..412deef 100644
--- a/src/database/memory.rs
+++ b/src/database/memory.rs
@@ -239,4 +239,9 @@ impl Storage for MemoryStorage {
         todo!()
     }
 
+    async fn all_posts<'this>(&'this self, user: &url::Url) -> Result<impl futures::Stream<Item = serde_json::Value> + Send + 'this> {
+        todo!();
+        Ok(futures::stream::pending())
+    }
+
 }
diff --git a/src/database/mod.rs b/src/database/mod.rs
index 0993715..4390ae7 100644
--- a/src/database/mod.rs
+++ b/src/database/mod.rs
@@ -356,6 +356,10 @@ pub trait Storage: std::fmt::Debug + Clone + Send + Sync {
     /// Besides, it may even allow for nice tricks like storing the
     /// webmentions separately and rehydrating them on feed reads.
     fn add_or_update_webmention(&self, target: &str, mention_type: MentionType, mention: serde_json::Value) -> impl Future<Output = Result<()>> + Send;
+
+    /// Return a stream of all posts ever made by a certain user, in
+    /// reverse-chronological order.
+    fn all_posts<'this>(&'this self, user: &url::Url) -> impl Future<Output = Result<impl futures::Stream<Item = serde_json::Value> + Send + 'this>> + Send;
 }
 
 #[cfg(test)]
diff --git a/src/database/postgres/mod.rs b/src/database/postgres/mod.rs
index 8d67bb4..1a1b98d 100644
--- a/src/database/postgres/mod.rs
+++ b/src/database/postgres/mod.rs
@@ -1,5 +1,6 @@
 use std::borrow::Cow;
 
+use futures::{Stream, StreamExt};
 use kittybox_util::{micropub::Channel as MicropubChannel, MentionType};
 use sqlx::{ConnectOptions, Executor, PgPool};
 use crate::micropub::{MicropubUpdate, MicropubPropertyDeletion};
@@ -72,6 +73,16 @@ impl Storage for PostgresStorage {
 
     }
 
+    async fn all_posts<'this>(&'this self, user: &url::Url) -> Result<impl Stream<Item = serde_json::Value> + Send + 'this> {
+        let authority = user.authority().to_owned();
+        Ok(
+            sqlx::query_scalar::<_, serde_json::Value>("SELECT mf2 FROM kittybox.mf2_json WHERE owner = $1 ORDER BY (mf2_json.mf2 #>> '{properties,published,0}') DESC")
+                .bind(authority)
+                .fetch(&self.db)
+                .filter_map(|f| std::future::ready(f.ok()))
+        )
+    }
+
     #[tracing::instrument(skip(self))]
     async fn categories(&self, url: &str) -> Result<Vec<String>> {
         sqlx::query_scalar::<_, String>("