Skip to content

Commit 8d211d7

Browse files
committed
fix: map user on fetch messages
1 parent e1a9a4b commit 8d211d7

File tree

1 file changed

+23
-2
lines changed

1 file changed

+23
-2
lines changed

service/messages.go

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ func (s *MessageService) SendMessage(ctx context.Context, req *models.SendMessag
116116
// FetchMessages translates Matrix /sync into the Acrobits fetch_messages response.
117117
func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMessagesRequest) (*models.FetchMessagesResponse, error) {
118118
// The user to impersonate is taken from the 'Username' field.
119-
userID := id.UserID(req.Username)
119+
userID := s.resolveMatrixUser(strings.TrimSpace(req.Username))
120120
if userID == "" {
121121
logger.Warn().Msg("fetch messages: empty user ID")
122122
return nil, ErrAuthentication
@@ -137,7 +137,9 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
137137
continue
138138
}
139139
msg := convertEvent(evt)
140-
if isSentBy(msg.Sender, req.Username) {
140+
// Remap sender from Matrix ID to SMS number if mapping exists
141+
msg.Sender = s.remapMatrixToSMS(msg.Sender)
142+
if isSentBy(msg.Sender, string(userID)) {
141143
sent = append(sent, msg)
142144
} else {
143145
received = append(received, msg)
@@ -177,6 +179,25 @@ func (s *MessageService) resolveMatrixUser(identifier string) id.UserID {
177179
return ""
178180
}
179181

182+
// remapMatrixToSMS attempts to remap a Matrix user ID to an SMS number if a mapping exists.
183+
// If no mapping is found, returns the original Matrix ID.
184+
func (s *MessageService) remapMatrixToSMS(matrixID string) string {
185+
matrixID = strings.TrimSpace(matrixID)
186+
187+
// Search through all mappings to find one where MatrixID matches
188+
s.mu.RLock()
189+
defer s.mu.RUnlock()
190+
for _, entry := range s.mappings {
191+
if strings.EqualFold(entry.MatrixID, matrixID) {
192+
logger.Debug().Str("matrix_id", matrixID).Str("sms_number", entry.SMSNumber).Msg("remapped matrix id to sms number")
193+
return entry.SMSNumber
194+
}
195+
}
196+
197+
// No mapping found, return the original Matrix ID
198+
return matrixID
199+
}
200+
180201
func (s *MessageService) ensureDirectRoom(ctx context.Context, actingUserID, targetUserID id.UserID) (id.RoomID, error) {
181202
// Use both user IDs to create a consistent mapping key for the DM.
182203
key := fmt.Sprintf("%s|%s", actingUserID, targetUserID)

0 commit comments

Comments
 (0)