-
-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0d4f18c
commit be724b8
Showing
4 changed files
with
130 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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) | ||
} |