diff --git a/pkg/store/container.go b/pkg/store/container.go new file mode 100644 index 0000000..8cae6e4 --- /dev/null +++ b/pkg/store/container.go @@ -0,0 +1,29 @@ +package store + +import ( + "context" + + "go.mau.fi/util/dbutil" + + "go.mau.fi/mautrix-meta/pkg/store/upgrades" +) + +type Container struct { + db *dbutil.Database +} + +func NewStore(db *dbutil.Database, log dbutil.DatabaseLogger) *Container { + return &Container{db: db.Child("meta_version", upgrades.Table, log)} +} + +func (c *Container) Upgrade(ctx context.Context) error { + return c.db.Upgrade(ctx) +} + +func newMetaSession(qh *dbutil.QueryHelper[*MetaSession]) *MetaSession { + return &MetaSession{} +} + +func (c *Container) GetSessionQuery() *MetaSessionQuery { + return &MetaSessionQuery{dbutil.MakeQueryHelper(c.db, newMetaSession)} +} \ No newline at end of file diff --git a/pkg/store/session_store.go b/pkg/store/session_store.go new file mode 100644 index 0000000..d95c105 --- /dev/null +++ b/pkg/store/session_store.go @@ -0,0 +1,73 @@ +package store + +import ( + "context" + "database/sql" + "errors" + + "go.mau.fi/mautrix-meta/messagix/cookies" + "go.mau.fi/mautrix-meta/messagix/types" + "go.mau.fi/util/dbutil" +) + +type MetaSession struct { + MetaID int64 + WADeviceID uint16 + Cookies *cookies.Cookies +} + +func (s *MetaSession) Scan(row dbutil.Scannable) (*MetaSession, error) { + var metaID sql.NullInt64 + var waDeviceID sql.NullInt32 + var platform sql.NullString + var newCookies cookies.Cookies + + err := row.Scan( + &metaID, + &platform, + &waDeviceID, + &dbutil.JSON{Data: &newCookies}, + ) + if err != nil { + return nil, err + } + + if platform.String == "instagram" { + newCookies.Platform = types.Instagram + } else if platform.String == "facebook" { + newCookies.Platform = types.Facebook + } else { + // TODO: Fix this err + return nil, errors.New("scanning db: unknown platform") + } + + if newCookies.IsLoggedIn() { + s.Cookies = &newCookies + } + + s.MetaID = metaID.Int64 + s.WADeviceID = uint16(waDeviceID.Int32) + + return s, nil +} + +const ( + getUserByMetaIDQuery = `SELECT meta_id, platform, wa_device_id, cookies FROM "meta_session" WHERE meta_id=$1` + insertUserQuery = `INSERT INTO "meta_session" (meta_id, platform, wa_device_id, cookies) VALUES ($1, $2, $3, $4)` +) + +type MetaSessionQuery struct { + *dbutil.QueryHelper[*MetaSession] +} + +func (q *MetaSessionQuery) GetByMetaID(ctx context.Context, metaID int64) (*MetaSession, error) { + return q.QueryOne(ctx, getUserByMetaIDQuery, metaID) +} + +func (q *MetaSessionQuery) Insert(ctx context.Context, session *MetaSession) error { + platform := "instagram" + if session.Cookies.Platform == types.Facebook { + platform = "facebook" + } + return q.Exec(ctx, insertUserQuery, session.MetaID, platform, session.WADeviceID, dbutil.JSON{Data: &session.Cookies}) +} diff --git a/pkg/store/upgrades/00-latest.sql b/pkg/store/upgrades/00-latest.sql new file mode 100644 index 0000000..30023af --- /dev/null +++ b/pkg/store/upgrades/00-latest.sql @@ -0,0 +1,12 @@ +-- v0 -> v1: Latest revision + +CREATE TABLE meta_session ( + meta_id BIGINT PRIMARY KEY, + platform TEXT NOT NULL, + wa_device_id INTEGER, + cookies jsonb, + + -- platform must be "instagram" or "facebook" + CONSTRAINT platform_check CHECK (platform = 'instagram' OR platform = 'facebook'), + CONSTRAINT user_meta_id_unique UNIQUE(meta_id) +); \ No newline at end of file diff --git a/pkg/store/upgrades/upgrades.go b/pkg/store/upgrades/upgrades.go new file mode 100644 index 0000000..a627201 --- /dev/null +++ b/pkg/store/upgrades/upgrades.go @@ -0,0 +1,16 @@ +package upgrades + +import ( + "embed" + + "go.mau.fi/util/dbutil" +) + +var Table dbutil.UpgradeTable + +//go:embed *.sql +var rawUpgrades embed.FS + +func init() { + Table.RegisterFS(rawUpgrades) +} \ No newline at end of file