From 9ed46c335d6020b10f720d9738b8252d424dd24c Mon Sep 17 00:00:00 2001
From: Freya Murphy <freya@freyacat.org>
Date: Mon, 1 Apr 2024 15:02:51 -0400
Subject: start custom banner and avatar loading

---
 src/db/rest/media/api_profile_avatar.sql | 36 ++++++++++++++++++++++++++++++++
 1 file changed, 36 insertions(+)
 create mode 100644 src/db/rest/media/api_profile_avatar.sql

(limited to 'src/db/rest/media/api_profile_avatar.sql')

diff --git a/src/db/rest/media/api_profile_avatar.sql b/src/db/rest/media/api_profile_avatar.sql
new file mode 100644
index 0000000..8607999
--- /dev/null
+++ b/src/db/rest/media/api_profile_avatar.sql
@@ -0,0 +1,36 @@
+CREATE FUNCTION api.profile_avatar(
+	user_id INTEGER DEFAULT 0
+)
+RETURNS sys."*/*"
+LANGUAGE plpgsql VOLATILE
+AS $BODY$
+DECLARE
+	_id INTEGER;
+	_mod INTEGER;
+	_name TEXT;
+BEGIN
+	SELECT media_id INTO _id
+	FROM admin.user_media m
+	WHERE m.user_id = profile_avatar.user_id
+	AND type = 'avatar'::admin.user_media_type;
+
+	-- get default if not exists
+	IF NOT FOUND THEN
+		_mod = MOD(user_id, 24);
+		_name = 'default_avatar_' || _mod || '.png';
+
+		SELECT id INTO _id
+		FROM admin.media
+		WHERE name = _name;
+	END IF;
+
+	RETURN _api.serve_media(_id);
+END
+$BODY$;
+
+GRANT EXECUTE ON FUNCTION api.profile_avatar(INTEGER)
+	TO rest_anon, rest_user;
+GRANT SELECT ON TABLE admin.user_media
+	TO rest_anon, rest_user;
+GRANT SELECT ON TABLE admin.media
+	TO rest_anon, rest_user;
-- 
cgit v1.2.3-freya

