TODO: Associate uploads with posts
Currently, carried over from legacy SOGS, there is no association of files/images with posts and so, when a post is deleted, the attachment stays around until it expires. This also means we can't do things like automatically preventing expiry of attachments in pinned messages (because we have no way to get the attachments).
This also means that when some failure occurs and Session re-uploads it can end up re-trying to upload several times before it actually submits the post, and we get never-referenced files that hang around for 15 days.
Association for a client would look something like:
- Client uploads one or more files via new (non-legacy) upload endpoint, gets the image ids. These uploaded files, however, will be very short-lived, maybe 10 minutes.
- Client submits the post including ids of any attached images, at which point the expiries get extended to the default file lifetime (15 days, or whatever has been configured).
On the backend, I think we could use this column added to the files table:
message BIGINT REFERENCES messages(id) ON DELETE SET NULL,
along with a trigger to immediately expire messages if a post is deleted:
CREATE TRIGGER messages_delete_attachments AFTER UPDATE OF data ON messages
FOR EACH ROW WHEN NEW.data IS NULL and OLD.data IS NOT NULL
BEGIN
UPDATE files SET expiry = 0 WHERE message = OLD.id;
END;