Skip to content

Commit 0128413

Browse files
committed
clean up and lint
1 parent 7457b9d commit 0128413

File tree

11 files changed

+39
-107
lines changed

11 files changed

+39
-107
lines changed

go.mod

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,7 @@ go 1.23.0
55
toolchain go1.23.3
66

77
require (
8-
github.com/google/go-querystring v1.1.0
9-
github.com/mattn/go-colorable v0.1.13
10-
github.com/rs/zerolog v1.33.0
118
go.mau.fi/util v0.8.2
12-
golang.org/x/net v0.31.0
139
google.golang.org/protobuf v1.35.2
1410
maunium.net/go/mautrix v0.22.1-0.20241126202918-4b970e0ea7e6
1511
)
@@ -20,10 +16,12 @@ require (
2016
github.com/gorilla/mux v1.8.0 // indirect
2117
github.com/gorilla/websocket v1.5.0 // indirect
2218
github.com/lib/pq v1.10.9 // indirect
19+
github.com/mattn/go-colorable v0.1.13 // indirect
2320
github.com/mattn/go-isatty v0.0.19 // indirect
2421
github.com/mattn/go-sqlite3 v1.14.24 // indirect
2522
github.com/petermattis/goid v0.0.0-20241025130422-66cb2e6d7274 // indirect
2623
github.com/rs/xid v1.6.0 // indirect
24+
github.com/rs/zerolog v1.33.0 // indirect
2725
github.com/skip2/go-qrcode v0.0.0-20200617195104-da1b6568686e // indirect
2826
github.com/tidwall/gjson v1.18.0 // indirect
2927
github.com/tidwall/match v1.1.1 // indirect
@@ -33,6 +31,7 @@ require (
3331
go.mau.fi/zeroconfig v0.1.3 // indirect
3432
golang.org/x/crypto v0.29.0 // indirect
3533
golang.org/x/exp v0.0.0-20241108190413-2d47ceb2692f // indirect
34+
golang.org/x/net v0.31.0 // indirect
3635
golang.org/x/sync v0.9.0 // indirect
3736
golang.org/x/sys v0.27.0 // indirect
3837
golang.org/x/text v0.20.0 // indirect

go.sum

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,8 @@ github.com/coreos/go-systemd/v22 v22.5.0/go.mod h1:Y58oyj3AT4RCenI/lSvhwexgC+NSV
77
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
88
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
99
github.com/godbus/dbus/v5 v5.0.4/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA=
10-
github.com/google/go-cmp v0.5.2/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
1110
github.com/google/go-cmp v0.6.0 h1:ofyhxvXcZhMsU5ulbFiLKl/XBFqE1GSq7atu8tAmTRI=
1211
github.com/google/go-cmp v0.6.0/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
13-
github.com/google/go-querystring v1.1.0 h1:AnCroh3fv4ZBgVIf1Iwtovgjaw/GiKJo8M8yD/fhyJ8=
14-
github.com/google/go-querystring v1.1.0/go.mod h1:Kcdr2DB4koayq7X8pmAG4sNG59So17icRSOU623lUBU=
1512
github.com/gorilla/mux v1.8.0 h1:i40aqfkR1h2SlN9hojwV5ZA91wcXFOvkdNIeFDP5koI=
1613
github.com/gorilla/mux v1.8.0/go.mod h1:DVbg23sWSpFRCP0SfiEN6jmj59UnW/n46BH5rLB71So=
1714
github.com/gorilla/websocket v1.5.0 h1:PPwGk2jz7EePpoHN/+ClbZu8SPxiqlu12wZP/3sWmnc=
@@ -69,7 +66,6 @@ golang.org/x/sys v0.27.0 h1:wBqf8DvsY9Y/2P8gAfPDEYNuS30J4lPHJxXSb/nJZ+s=
6966
golang.org/x/sys v0.27.0/go.mod h1:/VUhepiaJMQUp4+oa/7Zr1D23ma6VTLIYjOOTFZPUcA=
7067
golang.org/x/text v0.20.0 h1:gK/Kv2otX8gz+wn7Rmb3vT96ZwuoxnQlY+HlJVj7Qug=
7168
golang.org/x/text v0.20.0/go.mod h1:D4IsuqiFMhST5bX19pQ9ikHC2GsaKyk/oF+pn3ducp4=
72-
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
7369
google.golang.org/protobuf v1.35.2 h1:8Ar7bF+apOIoThw1EdZl0p1oWvMqTHmpA2fRTyZO8io=
7470
google.golang.org/protobuf v1.35.2/go.mod h1:9fA7Ob0pmnwhb644+1+CVWFRbNajQ6iRojtC/QF5bRE=
7571
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=

pkg/connector/client.go

Lines changed: 2 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ func (c *GChatClient) onConnect(ctx context.Context) {
6363
return
6464
}
6565
for _, item := range res.WorldItems {
66+
// TODO room name for DM, and full members list
6667
name := item.RoomName
6768
if name == nil {
6869
name = ptr.Ptr("dm")
@@ -100,21 +101,13 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
100101
fmt.Println("Invalid event", raw)
101102
return
102103
}
103-
fmt.Println("- onEvent", evt)
104104
switch *evt.Type {
105105
case proto.Event_MESSAGE_POSTED:
106106
msg := evt.Body.GetMessagePosted().Message
107107
senderId := *msg.Creator.UserId.Id
108108
c.UserLogin.Bridge.QueueRemoteEvent(c.UserLogin, &simplevent.Message[*proto.Message]{
109109
EventMeta: simplevent.EventMeta{
110110
Type: bridgev2.RemoteEventMessage,
111-
// LogContext: func(c zerolog.Context) zerolog.Context {
112-
// return c.
113-
// Str("message_id", evtData.MessageID).
114-
// Str("sender", sender.IDStr).
115-
// Str("sender_login", sender.ScreenName).
116-
// Bool("is_from_me", isFromMe)
117-
// },
118111
PortalKey: networkid.PortalKey{
119112
ID: networkid.PortalID(evt.GroupId.String()),
120113
Receiver: c.UserLogin.ID,
@@ -127,9 +120,7 @@ func (c *GChatClient) onStreamEvent(ctx context.Context, raw any) {
127120
},
128121
// Timestamp: evtData.CreatedAt,
129122
},
130-
ID: networkid.MessageID(*msg.LocalId),
131-
// TargetMessage: networkid.MessageID(evtData.MessageID),
132-
// Data: XMDFromEventMessage(&evtData),
123+
ID: networkid.MessageID(*msg.LocalId),
133124
Data: msg,
134125
ConvertMessageFunc: func(ctx context.Context, portal *bridgev2.Portal, intent bridgev2.MatrixAPI, data *proto.Message) (*bridgev2.ConvertedMessage, error) {
135126
return c.convertToMatrix(ctx, portal, intent, data), nil

pkg/gchatmeow/api.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,13 @@ import (
55
"fmt"
66
"net/http"
77
"net/url"
8+
"strconv"
89

910
pb "google.golang.org/protobuf/proto"
1011
"google.golang.org/protobuf/reflect/protoreflect"
1112

13+
"go.mau.fi/util/ptr"
14+
1215
"go.mau.fi/mautrix-googlechat/pkg/gchatmeow/proto"
1316
)
1417

@@ -27,7 +30,7 @@ func (c *Client) gcRequest(ctx context.Context, endpoint string, requestPB proto
2730
}
2831

2932
params := url.Values{}
30-
params.Set("c", string(c.apiReqID))
33+
params.Set("c", strconv.FormatInt(c.apiReqID, 10))
3134
params.Set("rt", "b")
3235
res, err := c.baseRequest(
3336
ctx,
@@ -37,7 +40,7 @@ func (c *Client) gcRequest(ctx context.Context, endpoint string, requestPB proto
3740
requestData,
3841
headers,
3942
params,
40-
"POST",
43+
http.MethodPost,
4144
)
4245
if err != nil {
4346
return err
@@ -114,7 +117,7 @@ func (c *Client) getMembers(ctx context.Context, gcid *string) (*proto.GetMember
114117
func (c *Client) paginatedWorld(ctx context.Context) (*proto.PaginatedWorldResponse, error) {
115118
request := &proto.PaginatedWorldRequest{
116119
RequestHeader: c.gcRequestHeader,
117-
FetchFromUserSpaces: GetPointer(true),
120+
FetchFromUserSpaces: ptr.Ptr(true),
118121
FetchOptions: []proto.PaginatedWorldRequest_FetchOptions{
119122
proto.PaginatedWorldRequest_EXCLUDE_GROUP_LITE,
120123
},

pkg/gchatmeow/channel.go

Lines changed: 13 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package gchatmeow
22

33
import (
4-
_ "bytes"
54
"context"
65
"encoding/base64"
76
"encoding/binary"
@@ -18,7 +17,6 @@ import (
1817
"strconv"
1918
"strings"
2019
"time"
21-
_ "unicode/utf16"
2220
"unicode/utf8"
2321

2422
"go.mau.fi/util/pblite"
@@ -45,7 +43,6 @@ var (
4543
lenRegex = regexp.MustCompile(lenRegexPattern)
4644
)
4745

48-
// Channel represents a BrowserChannel client
4946
type Channel struct {
5047
session *Session
5148
maxRetries int
@@ -65,12 +62,10 @@ type Channel struct {
6562
OnReceiveArray *Event
6663
}
6764

68-
// ChunkParser parses data from the backward channel into chunks
6965
type ChunkParser struct {
7066
buf []byte
7167
}
7268

73-
// NewChunkParser creates a new ChunkParser instance
7469
func NewChunkParser() *ChunkParser {
7570
return &ChunkParser{
7671
buf: make([]byte, 0),
@@ -181,12 +176,10 @@ func NewChannel(session *Session, maxRetries, retryBackoffBase int) (*Channel, e
181176
}, nil
182177
}
183178

184-
// IsConnected returns whether the channel is currently connected
185179
func (c *Channel) IsConnected() bool {
186180
return c.isConnected
187181
}
188182

189-
// Listen listens for messages on the backwards channel
190183
func (c *Channel) Listen(ctx context.Context, maxAge time.Duration) error {
191184
retries := 0
192185
skipBackoff := false
@@ -205,12 +198,12 @@ func (c *Channel) Listen(ctx context.Context, maxAge time.Duration) error {
205198
}
206199

207200
if retries > 0 && !skipBackoff {
208-
backoffSeconds := time.Duration(pow(c.retryBackoffBase, retries)) * time.Second
209-
log.Printf("Backing off for %v seconds", backoffSeconds)
201+
backoffTime := time.Duration(pow(c.retryBackoffBase, retries)) * time.Second
202+
log.Printf("Backing off for %v seconds", backoffTime)
210203
select {
211204
case <-ctx.Done():
212205
return ctx.Err()
213-
case <-time.After(backoffSeconds):
206+
case <-time.After(backoffTime):
214207
}
215208
}
216209
skipBackoff = false
@@ -250,13 +243,12 @@ func (c *Channel) Listen(ctx context.Context, maxAge time.Duration) error {
250243
return fmt.Errorf("ran out of retries for long-polling request")
251244
}
252245

253-
// register registers a new channel and returns the csessionid
254246
func (c *Channel) register(ctx context.Context) (string, error) {
255247
c.sidParam = ""
256248
c.aid = 0
257249
c.ofs = 0
258250

259-
resp, err := c.session.FetchRaw(ctx, "GET", channelURLBase+"register?ignore_compass_cookie=1", nil, http.Header{
251+
resp, err := c.session.FetchRaw(ctx, http.MethodGet, channelURLBase+"register?ignore_compass_cookie=1", nil, http.Header{
260252
"Content-Type": {"application/x-protobuf"},
261253
}, true, nil)
262254
if err != nil {
@@ -281,13 +273,13 @@ func (c *Channel) register(ctx context.Context) (string, error) {
281273
return "", nil
282274
}
283275

284-
func (c *Channel) sendStreamEvent(ctx context.Context, request proto.StreamEventsRequest) error {
276+
func (c *Channel) sendStreamEvent(ctx context.Context, request *proto.StreamEventsRequest) error {
285277
params := url.Values{
286278
"VER": []string{"8"}, // channel protocol version
287279
"RID": []string{fmt.Sprintf("%d", c.rid)}, // request identifier
288280
"t": []string{"1"}, // trial
289281
"SID": []string{c.sidParam}, // session ID
290-
"AID": []string{string(c.aid)}, // last acknowledged id
282+
"AID": []string{strconv.Itoa(c.aid)}, // last acknowledged id
291283
}
292284
c.rid++
293285

@@ -296,7 +288,7 @@ func (c *Channel) sendStreamEvent(ctx context.Context, request proto.StreamEvent
296288
"Content-Type": []string{"application/x-www-form-urlencoded"},
297289
}
298290

299-
protoBytes, err := pblite.Marshal(&request)
291+
protoBytes, err := pblite.Marshal(request)
300292
if err != nil {
301293
return fmt.Errorf("failed to marshal events request: %w", err)
302294
}
@@ -314,7 +306,7 @@ func (c *Channel) sendStreamEvent(ctx context.Context, request proto.StreamEvent
314306
}
315307
c.ofs++
316308

317-
res, err := c.session.FetchRaw(ctx, "POST", channelURLBase+"events", params, headers, true, []byte(formData.Encode()))
309+
res, err := c.session.FetchRaw(ctx, http.MethodPost, channelURLBase+"events", params, headers, true, []byte(formData.Encode()))
318310
if err != nil {
319311
return err
320312
}
@@ -328,10 +320,9 @@ func (c *Channel) sendInitialPing(ctx context.Context) error {
328320
interactiveState := proto.PingEvent_INTERACTIVE
329321
notificationsEnabled := true
330322
event := proto.PingEvent{State: &state, ApplicationFocusState: &focusState, ClientInteractiveState: &interactiveState, ClientNotificationsEnabled: &notificationsEnabled}
331-
return c.sendStreamEvent(ctx, proto.StreamEventsRequest{PingEvent: &event})
323+
return c.sendStreamEvent(ctx, &proto.StreamEventsRequest{PingEvent: &event})
332324
}
333325

334-
// longPollRequest opens a long-polling request and receives arrays
335326
func (c *Channel) longPollRequest(ctx context.Context) error {
336327
params := url.Values{
337328
"VER": {strconv.Itoa(protocolVersion)},
@@ -353,7 +344,7 @@ func (c *Channel) longPollRequest(ctx context.Context) error {
353344
params.Set("SID", c.sidParam)
354345
}
355346

356-
resp, err := c.session.FetchRaw(ctx, "GET", channelURLBase+"events", params, http.Header{
347+
resp, err := c.session.FetchRaw(ctx, http.MethodGet, channelURLBase+"events", params, http.Header{
357348
"referer": {"https://chat.google.com/"},
358349
}, true, nil)
359350
if err != nil {
@@ -389,14 +380,14 @@ func (c *Channel) longPollRequest(ctx context.Context) error {
389380
"VER": []string{"8"},
390381
"RID": []string{"rpc"},
391382
"SID": []string{c.sidParam},
392-
"AID": []string{string(c.aid)},
383+
"AID": []string{strconv.Itoa(c.aid)},
393384
"CI": []string{"0"},
394385
"TYPE": []string{"xmlhttp"},
395386
"zx": []string{uniqueID()},
396387
"t": []string{"1"},
397388
}
398389

399-
if _, err := c.session.FetchRaw(ctx, "GET", channelURLBase+"events", params, nil, true, nil); err != nil {
390+
if _, err := c.session.FetchRaw(ctx, http.MethodGet, channelURLBase+"events", params, nil, true, nil); err != nil {
400391
return fmt.Errorf("failed to acknowledge sid")
401392
}
402393

@@ -432,14 +423,10 @@ func (c *Channel) longPollRequest(ctx context.Context) error {
432423
}
433424
}
434425

435-
// OnPushData handles incoming push data and triggers appropriate events
436426
func (c *Channel) onPushData(dataBytes []byte) error {
437427
// Log received chunk
438428
log.Printf("Received chunk:\n%s", string(dataBytes))
439429

440-
// Update metrics
441-
// c.receivedChunks.Add(float64(len(dataBytes)))
442-
443430
// Process chunks
444431
chunks := c.chunkParser.GetChunks(dataBytes)
445432

@@ -478,7 +465,7 @@ func (c *Channel) onPushData(dataBytes []byte) error {
478465

479466
dataArray := innerArray[1]
480467

481-
log.Printf("Chunk contains data array with id %d:\n%v", arrayID, dataArray)
468+
log.Printf("Chunk contains data array with id %f:\n%v", arrayID, dataArray)
482469

483470
// Fire receive array event
484471
c.OnReceiveArray.Fire(dataArray)
@@ -491,7 +478,6 @@ func (c *Channel) onPushData(dataBytes []byte) error {
491478
return nil
492479
}
493480

494-
// Helper functions (implementations not shown for brevity)
495481
func uniqueID() string {
496482
// Implementation of _unique_id from Python code
497483
return base64.RawURLEncoding.EncodeToString([]byte(fmt.Sprintf("%d", rand.Int63())))

0 commit comments

Comments
 (0)