WIP: actually adding attachments

- attachments table was already defined
    - change the hash type to sha256
    - define sql function to upsert attachment rows
This commit is contained in:
towards-a-new-leftypol 2024-01-16 17:13:55 -05:00
parent f03d11aafb
commit 103bf86017
4 changed files with 52 additions and 1 deletions

View File

@ -2,6 +2,7 @@
"postgrest_url": "http://localhost:3000", "postgrest_url": "http://localhost:3000",
"jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2hhbl9hcmNoaXZlciJ9.rGIKZokTDKTuQLIv8138bUby5PELfDipYYIDpJzH02c", "jwt": "eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJyb2xlIjoiY2hhbl9hcmNoaXZlciJ9.rGIKZokTDKTuQLIv8138bUby5PELfDipYYIDpJzH02c",
"backup_read_root": "/home/phil/linixy/tmp/leftypol_back/lainchan.leftypol.org", "backup_read_root": "/home/phil/linixy/tmp/leftypol_back/lainchan.leftypol.org",
"media_root_path": "/home/phil/linixy/tmp/chan_archive_media",
"site_name": "leftychan", "site_name": "leftychan",
"site_url": "https://leftychan.net" "site_url": "https://leftychan.net"
} }

View File

@ -103,7 +103,7 @@ CREATE TABLE IF NOT EXISTS attachments
( attachment_id bigserial primary key ( attachment_id bigserial primary key
, mimetype text NOT NULL , mimetype text NOT NULL
, creation_time timestamp with time zone NOT NULL , creation_time timestamp with time zone NOT NULL
, sha256_hash text NOT NULL , sha256_hash text NOT NULL UNIQUE
, phash bigint , phash bigint
, illegal boolean NOT NULL DEFAULT false , illegal boolean NOT NULL DEFAULT false
, post_id bigint NOT NULL , post_id bigint NOT NULL
@ -181,6 +181,36 @@ $$ LANGUAGE sql;
-- 1:21 for full db (nothing inserted) -- 1:21 for full db (nothing inserted)
CREATE OR REPLACE FUNCTION insert_attachments_and_return_ids(
attachments_payload attachments[])
RETURNS TABLE (attachment_id bigint, post_id bigint, sha256_hash text) AS $$
WITH
selected AS (
SELECT attachment_id, post_id, sha256_hash
FROM attachments
WHERE sha256_hash IN (
SELECT sha256_hash FROM unnest(attachments_payload)
)
),
to_insert AS (
SELECT new_a.*
FROM unnest(attachments_payload) AS new_a
LEFT OUTER JOIN selected s
ON new_a.sha256_hash = s.sha256_hash
WHERE s.attachment_id IS NULL
),
inserted AS (
INSERT INTO attachments (mimetype, creation_time, sha256_hash, phash, illegal, post_id)
SELECT mimetype, creation_time, sha256_hash, phash, illegal, post_id
FROM to_insert
RETURNING attachment_id, post_id, sha256_hash
)
SELECT * FROM inserted
UNION ALL
SELECT * FROM selected;
$$ LANGUAGE sql;
CREATE OR REPLACE FUNCTION fetch_top_threads( CREATE OR REPLACE FUNCTION fetch_top_threads(
p_start_time TIMESTAMPTZ, p_start_time TIMESTAMPTZ,
lookback INT DEFAULT 10000 lookback INT DEFAULT 10000

19
src/AttachmentType.hs Normal file
View File

@ -0,0 +1,19 @@
{-# LANGUAGE DeriveAnyClass #-}
module AttachmentType
( Attachment (..)
) where
import GHC.Generics
import Data.Int (Int64)
import Data.Aeson (FromJSON)
import Data.Text (Text)
data Attachment = Attachment
{ attachment_id :: Maybe Int64
, mimetype :: Text
, creation_time :: UTCTime
, sha256_hash :: Int
, phash :: Int64
, phash :: Bool
, post_id :: Int64
} deriving (Show, Generic, FromJSON)

View File

@ -9,6 +9,7 @@ data JSONSettings = JSONSettings
{ postgrest_url :: String { postgrest_url :: String
, jwt :: String , jwt :: String
, backup_read_root :: FilePath , backup_read_root :: FilePath
, media_root_path :: FilePath
, site_name :: String , site_name :: String
, site_url :: String , site_url :: String
} deriving (Show, Generic) } deriving (Show, Generic)