|
import pool from './db';
|
|
|
|
let schemaPromise;
|
|
|
|
export function ensureUserBlocksSchema() {
|
|
if (!schemaPromise) {
|
|
schemaPromise = pool.query(`
|
|
CREATE TABLE IF NOT EXISTS user_blocks (
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
|
blocker_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
blocked_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
|
created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
|
|
UNIQUE (blocker_id, blocked_id),
|
|
CHECK (blocker_id <> blocked_id)
|
|
)
|
|
`).then(async () => {
|
|
await pool.query('CREATE INDEX IF NOT EXISTS idx_user_blocks_blocker ON user_blocks(blocker_id)');
|
|
await pool.query('CREATE INDEX IF NOT EXISTS idx_user_blocks_blocked ON user_blocks(blocked_id)');
|
|
}).catch((error) => {
|
|
schemaPromise = null;
|
|
throw error;
|
|
});
|
|
}
|
|
return schemaPromise;
|
|
}
|
|
|
|
export const UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
|
|
|
|
export async function hasBlocked(blockerId, blockedId, client = pool) {
|
|
await ensureUserBlocksSchema();
|
|
const result = await client.query(
|
|
'SELECT EXISTS(SELECT 1 FROM user_blocks WHERE blocker_id = $1 AND blocked_id = $2) AS blocked',
|
|
[blockerId, blockedId]
|
|
);
|
|
return result.rows[0].blocked;
|
|
}
|