Skip to content

Commit f3c3c90

Browse files
committed
fix: add auto join for messages
1 parent 35b23e0 commit f3c3c90

File tree

1 file changed

+25
-7
lines changed

1 file changed

+25
-7
lines changed

matrix/client.go

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -27,8 +27,10 @@ type Config struct {
2727
// Note: The underlying mautrix client is stateful for impersonation in this version.
2828
// A mutex is used to make operations thread-safe.
2929
type MatrixClient struct {
30-
cli *mautrix.Client
31-
mu sync.Mutex
30+
cli *mautrix.Client
31+
homeserverURL string
32+
homeserverName string
33+
mu sync.Mutex
3234
}
3335

3436
// NewClient creates a MatrixClient authenticated as an Application Service.
@@ -57,8 +59,19 @@ func NewClient(cfg Config) (*MatrixClient, error) {
5759
// This flag enables the `user_id` query parameter for impersonation.
5860
client.SetAppServiceUserID = true
5961

62+
// Extract homeserver name from URL (e.g., https://synapse.example.com -> synapse.example.com)
63+
homeserverName := strings.TrimPrefix(cfg.HomeserverURL, "https://")
64+
homeserverName = strings.TrimPrefix(homeserverName, "http://")
65+
homeserverName = strings.TrimSuffix(homeserverName, "/")
66+
// Remove path if present
67+
if idx := strings.Index(homeserverName, "/"); idx > 0 {
68+
homeserverName = homeserverName[:idx]
69+
}
70+
6071
return &MatrixClient{
61-
cli: client,
72+
cli: client,
73+
homeserverURL: cfg.HomeserverURL,
74+
homeserverName: homeserverName,
6275
}, nil
6376
}
6477

@@ -136,13 +149,18 @@ func (mc *MatrixClient) JoinRoom(ctx context.Context, userID id.UserID, roomID i
136149
req := &mautrix.ReqJoinRoom{}
137150

138151
if idx := strings.Index(roomIDStr, ":"); idx > 0 && idx < len(roomIDStr)-1 {
139-
homeserver := roomIDStr[idx+1:]
140-
if homeserver != "" {
141-
req.Via = []string{homeserver}
142-
logger.Debug().Str("user_id", string(userID)).Str("room_id", roomIDStr).Str("homeserver", homeserver).Msg("matrix: joining room with homeserver hint")
152+
roomServerName := roomIDStr[idx+1:]
153+
154+
// Only add Via if the room is on a different homeserver (federated)
155+
if roomServerName != "" && roomServerName != mc.homeserverName {
156+
req.Via = []string{roomServerName}
157+
logger.Debug().Str("user_id", string(userID)).Str("room_id", roomIDStr).Str("room_server", roomServerName).Str("local_server", mc.homeserverName).Msg("matrix: joining federated room with homeserver hint")
158+
} else {
159+
logger.Debug().Str("user_id", string(userID)).Str("room_id", roomIDStr).Msg("matrix: joining local room without via")
143160
}
144161
}
145162

163+
logger.Debug().Str("user_id", string(userID)).Str("room_id", roomIDStr).Bool("has_via", len(req.Via) > 0).Msg("matrix: calling JoinRoom")
146164
return mc.cli.JoinRoom(ctx, roomIDStr, req)
147165
}
148166

0 commit comments

Comments
 (0)