From 0fd88fedea69ae9edf762b6eb206b7ae37003bcb Mon Sep 17 00:00:00 2001 From: Tulir Asokan Date: Sat, 27 May 2023 13:49:57 +0300 Subject: [PATCH] Make mxc column non-unique for files. Fixes #71 --- database/file.go | 4 +-- database/upgrades/00-latest-revision.sql | 6 +++-- .../upgrades/22-file-cache-duplicate-mxc.sql | 26 +++++++++++++++++++ portal.go | 2 +- 4 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 database/upgrades/22-file-cache-duplicate-mxc.sql diff --git a/database/file.go b/database/file.go index 3afe13d..ea9cf9e 100644 --- a/database/file.go +++ b/database/file.go @@ -39,8 +39,8 @@ func (fq *FileQuery) Get(url string, encrypted bool) *File { return fq.New().Scan(fq.db.QueryRow(query, url, encrypted)) } -func (fq *FileQuery) GetByMXC(mxc id.ContentURI) *File { - query := fileSelect + " WHERE mxc=$1" +func (fq *FileQuery) GetEmojiByMXC(mxc id.ContentURI) *File { + query := fileSelect + " WHERE mxc=$1 AND emoji_name<>'' LIMIT 1" return fq.New().Scan(fq.db.QueryRow(query, mxc.String())) } diff --git a/database/upgrades/00-latest-revision.sql b/database/upgrades/00-latest-revision.sql index 95b32e8..f7697b8 100644 --- a/database/upgrades/00-latest-revision.sql +++ b/database/upgrades/00-latest-revision.sql @@ -1,4 +1,4 @@ --- v0 -> v21 (compatible with v19+): Latest revision +-- v0 -> v22 (compatible with v19+): Latest revision CREATE TABLE guild ( dcid TEXT PRIMARY KEY, @@ -160,7 +160,7 @@ CREATE TABLE role ( CREATE TABLE discord_file ( url TEXT, encrypted BOOLEAN, - mxc TEXT NOT NULL UNIQUE, + mxc TEXT NOT NULL, id TEXT, emoji_name TEXT, @@ -174,3 +174,5 @@ CREATE TABLE discord_file ( PRIMARY KEY (url, encrypted) ); + +CREATE INDEX discord_file_mxc_idx ON discord_file (mxc); diff --git a/database/upgrades/22-file-cache-duplicate-mxc.sql b/database/upgrades/22-file-cache-duplicate-mxc.sql new file mode 100644 index 0000000..b0bac3b --- /dev/null +++ b/database/upgrades/22-file-cache-duplicate-mxc.sql @@ -0,0 +1,26 @@ +-- v22 (compatible with v19+): Allow non-unique mxc URIs in file cache +CREATE TABLE new_discord_file ( + url TEXT, + encrypted BOOLEAN, + mxc TEXT NOT NULL, + + id TEXT, + emoji_name TEXT, + + size BIGINT NOT NULL, + width INTEGER, + height INTEGER, + mime_type TEXT NOT NULL, + decryption_info jsonb, + timestamp BIGINT NOT NULL, + + PRIMARY KEY (url, encrypted) +); + +INSERT INTO new_discord_file (url, encrypted, mxc, id, emoji_name, size, width, height, mime_type, decryption_info, timestamp) +SELECT url, encrypted, mxc, id, emoji_name, size, width, height, mime_type, decryption_info, timestamp FROM discord_file; + +DROP TABLE discord_file; +ALTER TABLE new_discord_file RENAME TO discord_file; + +CREATE INDEX discord_file_mxc_idx ON discord_file (mxc); diff --git a/portal.go b/portal.go index 62fe02c..9c7ae3a 100644 --- a/portal.go +++ b/portal.go @@ -1790,7 +1790,7 @@ func (portal *Portal) handleMatrixReaction(sender *User, evt *event.Event) { emojiID := reaction.RelatesTo.Key if strings.HasPrefix(emojiID, "mxc://") { uri, _ := id.ParseContentURI(emojiID) - emojiFile := portal.bridge.DB.File.GetByMXC(uri) + emojiFile := portal.bridge.DB.File.GetEmojiByMXC(uri) if emojiFile == nil || emojiFile.ID == "" || emojiFile.EmojiName == "" { go portal.sendMessageMetrics(evt, fmt.Errorf("%w %s", errUnknownEmoji, emojiID), "Ignoring") return