Skip to content

Commit

Permalink
client: remove redundant requests and set remote profile info
Browse files Browse the repository at this point in the history
  • Loading branch information
tulir committed Dec 19, 2024
1 parent 188bab1 commit 655acdd
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 17 deletions.
33 changes: 26 additions & 7 deletions pkg/connector/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ func (tc *TwitterClient) Connect(ctx context.Context) {
return
}

_, currentUser, err := tc.client.LoadMessagesPage()
inboxState, currentUser, err := tc.client.LoadMessagesPage()
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to load messages page")
if twittermeow.IsAuthError(err) {
Expand All @@ -132,20 +132,39 @@ func (tc *TwitterClient) Connect(ctx context.Context) {
return
}

tc.userLogin.RemoteName = currentUser.ScreenName
tc.userLogin.Save(ctx)
selfUser := inboxState.InboxInitialState.GetUserByID(tc.client.GetCurrentUserID())
if tc.userLogin.RemoteName != currentUser.ScreenName || tc.userLogin.RemoteProfile.Name != selfUser.Name {
tc.userLogin.RemoteName = currentUser.ScreenName
tc.userLogin.RemoteProfile = status.RemoteProfile{
// TODO fetch from /1.1/users/email_phone_info.json?
Phone: "",
Email: "",
Username: currentUser.ScreenName,
Name: selfUser.Name,
// TODO set on ghost and reuse same mxc
Avatar: "",
}
err = tc.userLogin.Save(ctx)
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to save user login after updating remote profile")
}
}

go tc.syncChannels(ctx)
err = tc.client.Connect()
go tc.syncChannels(ctx, inboxState)
tc.startPolling(ctx)
}

func (tc *TwitterClient) startPolling(ctx context.Context) {
err := tc.client.Connect()
if err != nil {
zerolog.Ctx(ctx).Err(err).Msg("Failed to start polling")
tc.userLogin.BridgeState.Send(status.BridgeState{
StateEvent: status.StateUnknownError,
Error: "twitter-connect-error",
})
return
} else {
tc.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}
tc.userLogin.BridgeState.Send(status.BridgeState{StateEvent: status.StateConnected})
}

func (tc *TwitterClient) Disconnect() {
Expand Down
16 changes: 10 additions & 6 deletions pkg/connector/client_sync.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,25 @@ import (
"maunium.net/go/mautrix/bridgev2/simplevent"

"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/payload"
"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/response"
"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/types"
"go.mau.fi/mautrix-twitter/pkg/twittermeow/methods"
)

func (tc *TwitterClient) syncChannels(ctx context.Context) {
func (tc *TwitterClient) syncChannels(ctx context.Context, initialInboxState *response.InboxInitialStateResponse) {
log := zerolog.Ctx(ctx)

reqQuery := ptr.Ptr(payload.DMRequestQuery{}.Default())
initalInboxState, err := tc.client.GetInitialInboxState(reqQuery)
if err != nil {
log.Error().Err(err).Msg("failed to fetch initial inbox state:")
return
if initialInboxState == nil {
var err error
initialInboxState, err = tc.client.GetInitialInboxState(reqQuery)
if err != nil {
log.Error().Err(err).Msg("failed to fetch initial inbox state:")
return
}
}

inboxData := initalInboxState.InboxInitialState
inboxData := initialInboxState.InboxInitialState
trustedInbox := inboxData.InboxTimelines.Trusted
cursor := trustedInbox.MinEntryID
paginationStatus := trustedInbox.Status
Expand Down
14 changes: 12 additions & 2 deletions pkg/connector/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,12 +20,14 @@ import (
"context"
"fmt"

"maunium.net/go/mautrix/bridge/status"
"maunium.net/go/mautrix/bridgev2"
"maunium.net/go/mautrix/bridgev2/database"
"maunium.net/go/mautrix/bridgev2/networkid"

"go.mau.fi/mautrix-twitter/pkg/twittermeow"
twitCookies "go.mau.fi/mautrix-twitter/pkg/twittermeow/cookies"
"go.mau.fi/mautrix-twitter/pkg/twittermeow/data/response"
)

type TwitterLogin struct {
Expand Down Expand Up @@ -100,10 +102,11 @@ func (t *TwitterLogin) SubmitCookies(ctx context.Context, cookies map[string]str
}
client := twittermeow.NewClient(clientOpts, t.User.Log)

_, settings, err := client.LoadMessagesPage()
inboxState, settings, err := client.LoadMessagesPage()
if err != nil {
return nil, fmt.Errorf("failed to load messages page after submitting cookies: %w", err)
}
selfUser := inboxState.InboxInitialState.GetUserByID(client.GetCurrentUserID())

id := networkid.UserLoginID(client.GetCurrentUserID())
ul, err := t.User.NewLogin(
Expand All @@ -112,6 +115,10 @@ func (t *TwitterLogin) SubmitCookies(ctx context.Context, cookies map[string]str
ID: id,
Metadata: meta,
RemoteName: settings.ScreenName,
RemoteProfile: status.RemoteProfile{
Username: settings.ScreenName,
Name: selfUser.Name,
},
},
&bridgev2.NewLoginParams{
DeleteOnConflict: true,
Expand All @@ -123,7 +130,10 @@ func (t *TwitterLogin) SubmitCookies(ctx context.Context, cookies map[string]str
return nil, err
}

ul.Client.Connect(ctx)
go func(ctx context.Context, client *TwitterClient, inboxState *response.InboxInitialStateResponse) {
client.syncChannels(ctx, inboxState)
client.startPolling(ctx)
}(context.WithoutCancel(ctx), ul.Client.(*TwitterClient), inboxState)

return &bridgev2.LoginStep{
Type: bridgev2.LoginStepTypeComplete,
Expand Down
4 changes: 2 additions & 2 deletions pkg/twittermeow/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ func (c *Client) Logout() (bool, error) {
return true, nil
}

func (c *Client) LoadMessagesPage() (*response.XInboxData, *response.AccountSettingsResponse, error) {
func (c *Client) LoadMessagesPage() (*response.InboxInitialStateResponse, *response.AccountSettingsResponse, error) {
err := c.session.LoadPage(endpoints.BASE_MESSAGES_URL)
if err != nil {
return nil, nil, fmt.Errorf("failed to load messages page: %w", err)
Expand Down Expand Up @@ -154,7 +154,7 @@ func (c *Client) LoadMessagesPage() (*response.XInboxData, *response.AccountSett
Str("initial_inbox_cursor", initialInboxState.InboxInitialState.Cursor).
Msg("Successfully loaded and authenticated as user")

return &initialInboxState.InboxInitialState, data, nil
return initialInboxState, data, nil
}

func (c *Client) GetCurrentUser() *response.AccountSettingsResponse {
Expand Down

0 comments on commit 655acdd

Please sign in to comment.