Skip to content

Commit

Permalink
remove store in favor of serializing into login metadata
Browse files Browse the repository at this point in the history
  • Loading branch information
JJTech0130 committed Jul 2, 2024
1 parent fb6a9cb commit fbd56ea
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 213 deletions.
35 changes: 21 additions & 14 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,43 @@ package connector

import (
"context"
"strconv"

"go.mau.fi/mautrix-meta/messagix"
//"go.mau.fi/mautrix-meta/pkg/store"
"go.mau.fi/mautrix-meta/messagix/cookies"
"go.mau.fi/mautrix-meta/messagix/types"

"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/networkid"
)

type MetaClient struct {
Main *MetaConnector
//session *store.MetaSession
Main *MetaConnector
client *messagix.Client
}

func NewMetaClient(ctx context.Context, main *MetaConnector, login *bridgev2.UserLogin) (*MetaClient, error) {
FBID, err := strconv.ParseInt(string(login.ID), 10, 64)
if err != nil {
return nil, err
func cookiesFromMetadata(metadata map[string]interface{}) *cookies.Cookies {
platform := types.Platform(metadata["platform"].(float64))

m := make(map[string]string)
for k, v := range metadata["cookies"].(map[string]interface{}) {
m[k] = v.(string)
}
session, err := main.store.GetSessionQuery().GetByMetaID(ctx, FBID)
if err != nil {
return nil, err

c := &cookies.Cookies{
Platform: platform,
}
c.UpdateValues(m)
return c
}

func NewMetaClient(ctx context.Context, main *MetaConnector, login *bridgev2.UserLogin) (*MetaClient, error) {
cookies := cookiesFromMetadata(login.Metadata.Extra)

log := login.User.Log.With().Str("component", "messagix").Logger()
client := messagix.NewClient(session.Cookies, log)
client := messagix.NewClient(cookies, log)

return &MetaClient{
Main: main,
//session: session,
Main: main,
client: client,
}, nil
}
Expand Down
8 changes: 1 addition & 7 deletions pkg/connector/connector.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,12 @@ package connector
import (
"context"

"go.mau.fi/util/dbutil"
"maunium.net/go/mautrix/bridgev2"

"go.mau.fi/mautrix-meta/pkg/store"
)

type MetaConnector struct {
Bridge *bridgev2.Bridge
Config *MetaConfig

store *store.Container
}

func NewConnector() *MetaConnector {
Expand Down Expand Up @@ -74,12 +69,11 @@ func (s *MetaConnector) GetName() bridgev2.BridgeName {
}

func (m *MetaConnector) Init(bridge *bridgev2.Bridge) {
m.store = store.NewStore(bridge.DB.Database, dbutil.ZeroLogger(bridge.Log.With().Str("db_section", "meta").Logger()))
m.Bridge = bridge
}

func (m *MetaConnector) Start(ctx context.Context) error {
return m.store.Upgrade(ctx)
return nil
}

func (m *MetaConnector) LoadUserLogin(ctx context.Context, login *bridgev2.UserLogin) error {
Expand Down
72 changes: 15 additions & 57 deletions pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ import (

"go.mau.fi/mautrix-meta/messagix"
"go.mau.fi/mautrix-meta/messagix/cookies"
"go.mau.fi/mautrix-meta/messagix/table"

//"go.mau.fi/mautrix-meta/messagix/table"
"go.mau.fi/mautrix-meta/messagix/types"
"go.mau.fi/mautrix-meta/pkg/store"
//"go.mau.fi/mautrix-meta/pkg/store"
)

const FlowIDFacebookCookies = "cookies-facebook"
Expand Down Expand Up @@ -95,79 +96,33 @@ func (m *MetaCookieLogin) Start(ctx context.Context) (*bridgev2.LoginStep, error

func (m *MetaCookieLogin) Cancel() {}

func (m *MetaCookieLogin) getFBID(currentUser types.UserInfo, tbl *table.LSTable) int64 {
var newFBID int64
// TODO figure out why the contact IDs for self is different than the fbid in the ready event
for _, row := range tbl.LSVerifyContactRowExists {
if row.IsSelf && row.ContactId != newFBID {
if newFBID != 0 {
// Hopefully this won't happen
m.User.Log.Warn().Int64("prev_fbid", newFBID).Int64("new_fbid", row.ContactId).Msg("Got multiple fbids for self")
} else {
m.User.Log.Debug().Int64("fbid", row.ContactId).Msg("Found own fbid")
}
newFBID = row.ContactId
}
}
if newFBID == 0 {
newFBID = currentUser.GetFBID()
m.User.Log.Warn().Int64("fbid", newFBID).Msg("Own contact entry not found, falling back to fbid in current user object")
}
return newFBID
}

func (m *MetaCookieLogin) SubmitCookies(ctx context.Context, strCookies map[string]string) (*bridgev2.LoginStep, error) {
c := &cookies.Cookies{
Platform: types.Instagram,
}
if m.Flow == FlowIDFacebookCookies {
c.Platform = types.Facebook
}

c.UpdateValues(strCookies)

// Check if the cookies are valid
if !c.IsLoggedIn() {
return nil, fmt.Errorf("invalid cookies")
}

log := m.User.Log.With().Str("component", "messagix").Logger()
client := messagix.NewClient(c, log)

currentUser, initialTable, err := client.LoadMessagesPage()
if err != nil {
return nil, err
}

FBID := m.getFBID(currentUser, initialTable)

// Store the cookies in the database

q := m.Main.store.GetSessionQuery()
session, err := q.GetByMetaID(ctx, FBID)
user, _, err := client.LoadMessagesPage()
if err != nil {
return nil, fmt.Errorf("failed to get existing session: %w", err)
}
if session != nil {
log.Debug().Int64("fbid", FBID).Msg("Deleting existing session")
err := q.Delete(ctx, FBID)
if err != nil {
return nil, fmt.Errorf("failed to delete existing session: %w", err)
}
return nil, fmt.Errorf("failed to load messages page: %w", err)
}

newSession := store.MetaSession{
MetaID: FBID,
Cookies: c,
}
err = q.Insert(ctx, &newSession)
id, err := client.FetchFBID()
if err != nil {
return nil, fmt.Errorf("failed to save new session: %w", err)
return nil, fmt.Errorf("failed to fetch FBID: %w", err)
}

// Store the association between the Matrix user and the cookies

login_id := networkid.UserLoginID(fmt.Sprint(FBID))
login_id := networkid.UserLoginID(fmt.Sprint(id))

ul, err := m.Main.Bridge.GetExistingUserLoginByID(ctx, login_id)
if err != nil {
Expand All @@ -183,16 +138,19 @@ func (m *MetaCookieLogin) SubmitCookies(ctx context.Context, strCookies map[stri
ID: login_id,
Metadata: database.UserLoginMetadata{
StandardUserLoginMetadata: database.StandardUserLoginMetadata{
// TODO: Is this the correct metadata? Any extra?
RemoteName: currentUser.GetName(),
RemoteName: user.GetName(),
},
Extra: map[string]interface{}{
"platform": c.Platform,
"cookies": c,
},
},
}, nil)
if err != nil {
return nil, fmt.Errorf("failed to save new login: %w", err)
}
} else {
ul.Metadata.RemoteName = currentUser.GetName()
ul.Metadata.RemoteName = user.GetName()
err := ul.Save(ctx)
if err != nil {
return nil, fmt.Errorf("failed to update existing login: %w", err)
Expand All @@ -208,6 +166,6 @@ func (m *MetaCookieLogin) SubmitCookies(ctx context.Context, strCookies map[stri
return &bridgev2.LoginStep{
Type: bridgev2.LoginStepTypeComplete,
StepID: "fi.mau.meta.complete",
Instructions: fmt.Sprintf("Logged in as %s (%d)", currentUser.GetName(), FBID),
Instructions: fmt.Sprintf("Logged in as %s (%d)", user.GetName(), id),
}, nil
}
29 changes: 0 additions & 29 deletions pkg/store/container.go

This file was deleted.

78 changes: 0 additions & 78 deletions pkg/store/session_store.go

This file was deleted.

12 changes: 0 additions & 12 deletions pkg/store/upgrades/00-latest.sql

This file was deleted.

16 changes: 0 additions & 16 deletions pkg/store/upgrades/upgrades.go

This file was deleted.

0 comments on commit fbd56ea

Please sign in to comment.