From 98b720ee868c4cec566a6d5c8a9a93263b1523a9 Mon Sep 17 00:00:00 2001 From: Ping Chen Date: Fri, 3 Jan 2025 10:30:15 +0900 Subject: [PATCH] set LoginCookiesParams.Fields --- pkg/connector/client.go | 12 +++++++++++- pkg/connector/handlegchat.go | 15 +++++++++------ pkg/connector/login.go | 13 ++++++++++++- pkg/gchatmeow/cookies.go | 4 ++-- pkg/msgconv/from-gchat.go | 2 +- 5 files changed, 35 insertions(+), 11 deletions(-) diff --git a/pkg/connector/client.go b/pkg/connector/client.go index 4eeccfc..3f9e52f 100644 --- a/pkg/connector/client.go +++ b/pkg/connector/client.go @@ -6,6 +6,7 @@ import ( "strings" "time" + "maunium.net/go/mautrix/bridge/status" "maunium.net/go/mautrix/bridgev2" "maunium.net/go/mautrix/bridgev2/database" "maunium.net/go/mautrix/bridgev2/networkid" @@ -41,7 +42,16 @@ func NewClient(userLogin *bridgev2.UserLogin, client *gchatmeow.Client) *GChatCl func (c *GChatClient) Connect(ctx context.Context) error { c.client.OnConnect.AddObserver(func(interface{}) { c.onConnect(ctx) }) c.client.OnStreamEvent.AddObserver(func(evt interface{}) { c.onStreamEvent(ctx, evt) }) - return c.client.Connect(ctx, time.Duration(90)*time.Minute) + + err := c.client.Connect(ctx, time.Duration(90)*time.Minute) + if err != nil { + c.userLogin.BridgeState.Send(status.BridgeState{ + StateEvent: status.StateBadCredentials, + Error: "googlechat-invalid-credentials", + Message: err.Error(), + }) + } + return err } func (c *GChatClient) Disconnect() { diff --git a/pkg/connector/handlegchat.go b/pkg/connector/handlegchat.go index cf25c17..7be2da2 100644 --- a/pkg/connector/handlegchat.go +++ b/pkg/connector/handlegchat.go @@ -70,12 +70,15 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) { EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventTyping, state.UserId.Id, state.StartTimestampUsec), }) case proto.Event_READ_RECEIPT_CHANGED: - receipts := evt.Body.GetReadReceiptChanged().ReadReceiptSet.ReadReceipts - for _, receipt := range receipts { - c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Receipt{ - EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventReadReceipt, receipt.User.UserId.Id, receipt.ReadTimeMicros), - ReadUpTo: time.UnixMicro(receipt.ReadTimeMicros), - }) + changed := evt.Body.GetReadReceiptChanged() + if changed != nil { + receipts := changed.ReadReceiptSet.ReadReceipts + for _, receipt := range receipts { + c.userLogin.Bridge.QueueRemoteEvent(c.userLogin, &simplevent.Receipt{ + EventMeta: c.makeEventMeta(evt, bridgev2.RemoteEventReadReceipt, receipt.User.UserId.Id, receipt.ReadTimeMicros), + ReadUpTo: time.UnixMicro(receipt.ReadTimeMicros), + }) + } } case proto.Event_GROUP_UPDATED: c.handleGroupUpdated(ctx, evt) diff --git a/pkg/connector/login.go b/pkg/connector/login.go index c12f304..941dd23 100644 --- a/pkg/connector/login.go +++ b/pkg/connector/login.go @@ -51,12 +51,23 @@ type UserLoginMetadata struct { var _ bridgev2.LoginProcessCookies = (*GChatCookieLogin)(nil) func (gl *GChatCookieLogin) Start(ctx context.Context) (*bridgev2.LoginStep, error) { + fields := make([]bridgev2.LoginCookieField, 5) + for i, key := range gchatmeow.CookieNames { + fields[i] = bridgev2.LoginCookieField{ + ID: key, + Required: true, + Sources: []bridgev2.LoginCookieFieldSource{ + {Type: bridgev2.LoginCookieTypeCookie, Name: key}, + }, + } + } step := &bridgev2.LoginStep{ Type: bridgev2.LoginStepTypeCookies, StepID: LoginStepIDCookies, Instructions: "Enter a JSON object with your cookies, or a cURL command copied from browser devtools.", CookiesParams: &bridgev2.LoginCookiesParams{ - URL: "https://chat.google.com/", + URL: "https://chat.google.com/", + Fields: fields, }, } return step, nil diff --git a/pkg/gchatmeow/cookies.go b/pkg/gchatmeow/cookies.go index b414a9e..3220ac2 100644 --- a/pkg/gchatmeow/cookies.go +++ b/pkg/gchatmeow/cookies.go @@ -13,12 +13,12 @@ type Cookies struct { } var ( - cookies = []string{"COMPASS", "SSID", "SID", "OSID", "HSID"} + CookieNames = []string{"COMPASS", "SSID", "SID", "OSID", "HSID"} ) func (c *Cookies) UpdateValues(values map[string]string) { r := reflect.ValueOf(c) - for _, key := range cookies { + for _, key := range CookieNames { field := reflect.Indirect(r).FieldByName(key) field.SetString(values[key]) } diff --git a/pkg/msgconv/from-gchat.go b/pkg/msgconv/from-gchat.go index 67b7f1c..2a49f76 100644 --- a/pkg/msgconv/from-gchat.go +++ b/pkg/msgconv/from-gchat.go @@ -57,7 +57,7 @@ func (mc *MessageConverter) ToMatrix(ctx context.Context, portal *bridgev2.Porta } } - // cm.MergeCaption() + cm.MergeCaption() return cm, nil }