db: add telegram_file table
Signed-off-by: Sumner Evans <sumner.evans@automattic.com>
This commit is contained in:
+10
-4
@@ -25,17 +25,23 @@ import (
|
||||
)
|
||||
|
||||
type Container struct {
|
||||
db *dbutil.Database
|
||||
*dbutil.Database
|
||||
|
||||
TelegramFile *TelegramFileQuery
|
||||
}
|
||||
|
||||
func NewStore(db *dbutil.Database, log dbutil.DatabaseLogger) *Container {
|
||||
return &Container{db: db.Child("telegram_version", upgrades.Table, log)}
|
||||
return &Container{
|
||||
Database: db.Child("telegram_version", upgrades.Table, log),
|
||||
|
||||
TelegramFile: &TelegramFileQuery{dbutil.MakeQueryHelper(db, newTelegramFile)},
|
||||
}
|
||||
}
|
||||
|
||||
func (c *Container) Upgrade(ctx context.Context) error {
|
||||
return c.db.Upgrade(ctx)
|
||||
return c.Database.Upgrade(ctx)
|
||||
}
|
||||
|
||||
func (c *Container) GetScopedStore(telegramUserID int64) *scopedStore {
|
||||
return &scopedStore{c.db, telegramUserID}
|
||||
return &scopedStore{c.Database, telegramUserID}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,96 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"time"
|
||||
|
||||
"go.mau.fi/util/dbutil"
|
||||
)
|
||||
|
||||
const (
|
||||
insertTelegramFileQuery = `
|
||||
INSERT INTO telegram_file (
|
||||
id, mxc, mime_type, was_converted, timestamp, size, width, height, thumbnail, decryption_info)
|
||||
VALUES ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10)
|
||||
`
|
||||
getTelegramFileSelect = `
|
||||
SELECT id, mxc, mime_type, was_converted, timestamp, size, width, height, thumbnail, decryption_info
|
||||
FROM telegram_file
|
||||
`
|
||||
getTelegramFileByLocationIDQuery = getTelegramFileSelect + "WHERE id=$1"
|
||||
getTelegramFileByMXCQuery = getTelegramFileSelect + "WHERE mxc=$1"
|
||||
)
|
||||
|
||||
type TelegramFileQuery struct {
|
||||
*dbutil.QueryHelper[*TelegramFile]
|
||||
}
|
||||
|
||||
type TelegramFile struct {
|
||||
qh *dbutil.QueryHelper[*TelegramFile]
|
||||
|
||||
LocationID string
|
||||
MXC string
|
||||
MimeType string
|
||||
WasConverted bool
|
||||
Timestamp time.Time
|
||||
Size int64
|
||||
Width int
|
||||
Height int
|
||||
ThumbnailID string
|
||||
DecryptionInfo json.RawMessage
|
||||
}
|
||||
|
||||
var _ dbutil.DataStruct[*TelegramFile] = (*TelegramFile)(nil)
|
||||
|
||||
func newTelegramFile(qh *dbutil.QueryHelper[*TelegramFile]) *TelegramFile {
|
||||
return &TelegramFile{qh: qh}
|
||||
}
|
||||
|
||||
func (fq *TelegramFileQuery) GetByLocationID(ctx context.Context, locationID string) (*TelegramFile, error) {
|
||||
return fq.QueryOne(ctx, getTelegramFileByLocationIDQuery, locationID)
|
||||
}
|
||||
|
||||
func (fq *TelegramFileQuery) GetByMXC(ctx context.Context, mxc string) (*TelegramFile, error) {
|
||||
return fq.QueryOne(ctx, getTelegramFileByMXCQuery, mxc)
|
||||
}
|
||||
|
||||
func (f *TelegramFile) sqlVariables() []any {
|
||||
return []any{
|
||||
f.LocationID,
|
||||
f.MXC,
|
||||
f.MimeType,
|
||||
f.WasConverted,
|
||||
f.Timestamp.UnixMilli(),
|
||||
f.Size,
|
||||
f.Width,
|
||||
f.Height,
|
||||
f.ThumbnailID,
|
||||
f.DecryptionInfo,
|
||||
}
|
||||
}
|
||||
|
||||
func (f *TelegramFile) Insert(ctx context.Context) error {
|
||||
return f.qh.Exec(ctx, insertTelegramFileQuery, f.sqlVariables()...)
|
||||
}
|
||||
|
||||
func (f *TelegramFile) Scan(row dbutil.Scannable) (*TelegramFile, error) {
|
||||
var thumbnailID sql.NullString
|
||||
var timestamp int64
|
||||
err := row.Scan(
|
||||
&f.LocationID,
|
||||
&f.MXC,
|
||||
&f.MimeType,
|
||||
&f.WasConverted,
|
||||
×tamp,
|
||||
&f.Size,
|
||||
&f.Width,
|
||||
&f.Height,
|
||||
&thumbnailID,
|
||||
&f.DecryptionInfo,
|
||||
)
|
||||
f.Timestamp = time.UnixMilli(timestamp)
|
||||
f.ThumbnailID = thumbnailID.String
|
||||
return f, err
|
||||
}
|
||||
@@ -30,3 +30,18 @@ CREATE TABLE telegram_channel_access_hashes (
|
||||
|
||||
PRIMARY KEY (user_id, channel_id)
|
||||
);
|
||||
|
||||
CREATE TABLE telegram_file (
|
||||
id TEXT PRIMARY KEY,
|
||||
mxc TEXT NOT NULL,
|
||||
mime_type TEXT,
|
||||
was_converted BOOLEAN NOT NULL DEFAULT false,
|
||||
timestamp BIGINT NOT NULL DEFAULT 0,
|
||||
size BIGINT,
|
||||
width INTEGER,
|
||||
height INTEGER,
|
||||
thumbnail TEXT,
|
||||
decryption_info jsonb,
|
||||
FOREIGN KEY (thumbnail) REFERENCES telegram_file(id)
|
||||
ON UPDATE CASCADE ON DELETE SET NULL
|
||||
);
|
||||
|
||||
Reference in New Issue
Block a user