From efc10086cbb6726c3728c8edda93b8fd00436717 Mon Sep 17 00:00:00 2001 From: Vika Date: Mon, 4 Mar 2024 04:40:50 +0300 Subject: Support ?q=category queries Warning, untested. But hopefully works! --- src/database/postgres/mod.rs | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'src/database/postgres/mod.rs') diff --git a/src/database/postgres/mod.rs b/src/database/postgres/mod.rs index ee7dd1c..71c4d58 100644 --- a/src/database/postgres/mod.rs +++ b/src/database/postgres/mod.rs @@ -74,6 +74,21 @@ impl PostgresStorage { #[async_trait::async_trait] impl Storage for PostgresStorage { + #[tracing::instrument(skip(self))] + async fn categories(&self, url: &str) -> Result> { + sqlx::query_scalar::<_, String>(" +SELECT jsonb_array_elements(mf2['properties']['category']) AS category +FROM kittybox.mf2_json +WHERE + jsonb_typeof(mf2['properties']['category']) = 'array' + AND uid LIKE ($1 + '%') + GROUP BY category ORDER BY count(*) DESC +") + .bind(url) + .fetch_all(&self.db) + .await + .map_err(|err| err.into()) + } #[tracing::instrument(skip(self))] async fn post_exists(&self, url: &str) -> Result { sqlx::query_as::<_, (bool,)>("SELECT exists(SELECT 1 FROM kittybox.mf2_json WHERE uid = $1 OR mf2['properties']['url'] ? $1)") -- cgit 1.4.1