Skip to content

Commit 35b23e0

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

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

matrix/client.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"errors"
66
"fmt"
77
"net/http"
8+
"strings"
89
"sync"
910
"time"
1011

@@ -128,7 +129,21 @@ func (mc *MatrixClient) JoinRoom(ctx context.Context, userID id.UserID, roomID i
128129
defer mc.mu.Unlock()
129130

130131
mc.cli.UserID = userID
131-
return mc.cli.JoinRoom(ctx, string(roomID), nil)
132+
133+
// Extract homeserver from room ID for federated joins
134+
// Room ID format: !opaque:homeserver.name
135+
roomIDStr := string(roomID)
136+
req := &mautrix.ReqJoinRoom{}
137+
138+
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")
143+
}
144+
}
145+
146+
return mc.cli.JoinRoom(ctx, roomIDStr, req)
132147
}
133148

134149
// CreateRoom creates a new room impersonating the specified userID.

service/messages.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func (s *MessageService) SendMessage(ctx context.Context, req *models.SendMessag
7878

7979
logger.Debug().Str("user_id", string(userID)).Str("room_id", string(roomID)).Msg("sending message to room")
8080

81+
// Ensure the sender is a member of the room
82+
_, err = s.matrixClient.JoinRoom(ctx, userID, roomID)
83+
if err != nil {
84+
logger.Error().Str("user_id", string(userID)).Str("room_id", string(roomID)).Err(err).Msg("failed to join room")
85+
return nil, fmt.Errorf("send message: %w", err)
86+
}
87+
8188
content := &event.MessageEventContent{
8289
MsgType: event.MsgText,
8390
Body: req.SMSBody,
@@ -326,7 +333,7 @@ func mapAuthErr(err error) error {
326333
if errors.Is(err, ErrAuthentication) {
327334
return err
328335
}
329-
if errors.Is(err, mautrix.MUnknownToken) || errors.Is(err, mautrix.MMissingToken) || errors.Is(err, mautrix.MForbidden) {
336+
if errors.Is(err, mautrix.MUnknownToken) || errors.Is(err, mautrix.MMissingToken) {
330337
return fmt.Errorf("%w", ErrAuthentication)
331338
}
332339
return err

0 commit comments

Comments
 (0)