Skip to content

Commit 38681fc

Browse files
committed
fix
1 parent 475431f commit 38681fc

File tree

3 files changed

+24
-2
lines changed

3 files changed

+24
-2
lines changed

service/messages.go

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,7 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
344344

345345
// resolveMatrixUser resolves an identifier to a valid Matrix user ID.
346346
// If the identifier is already a valid Matrix user ID (starts with @), it's returned as-is.
347+
// If the identifier is in format "username@domain", extracts the username part.
347348
// Otherwise, it tries to look up the identifier in the mapping store with the following logic:
348349
// - First tries to match the identifier as the main number
349350
// - If no match, tries to find the identifier in any entry's sub_numbers array
@@ -358,6 +359,12 @@ func (s *MessageService) resolveMatrixUser(identifier string) id.UserID {
358359
return id.UserID(identifier)
359360
}
360361

362+
// Extract username from "username@domain" format if present
363+
if idx := strings.Index(identifier, "@"); idx > 0 {
364+
identifier = identifier[:idx]
365+
logger.Debug().Str("extracted_username", identifier).Msg("extracted username from user@domain format")
366+
}
367+
361368
// Try to look up in mappings (e.g., phone number to Matrix user)
362369
if entry, ok := s.getMapping(identifier); ok && entry.MatrixID != "" {
363370
logger.Debug().Str("original_identifier", identifier).Str("resolved_user", entry.MatrixID).Msg("identifier resolved from mapping")

service/messages_cache_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,7 @@ func TestResolveRoomIDToOtherIdentifierCacheBehavior(t *testing.T) {
114114
svc.roomAliasesCache.Set(string(roomID), []string{"user1|user2"})
115115

116116
// Call resolveRoomIDToOtherIdentifier
117-
result := svc.resolveRoomIDToOtherIdentifier(nil, roomID, myMatrixID)
117+
result := svc.resolveRoomIDToOtherIdentifier(context.TODO(), roomID, myMatrixID)
118118

119119
// Should resolve to 201 (the mapped number for @user2:server)
120120
assert.Equal(t, "201", result)

service/messages_test.go

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -328,7 +328,22 @@ func TestResolveMatrixUser_SubNumbers(t *testing.T) {
328328
assert.Equal(t, "", string(result), "should return empty string if no mapping found")
329329
})
330330

331-
// Test case 6: Case insensitivity
331+
// Test case 6: Extract username from user@domain format - but still need to resolve via mapping
332+
// This test verifies that the username extraction works for the identifier processing,
333+
// but the actual resolution still depends on having a mapping with that number
334+
t.Run("extract username from user@domain format", func(t *testing.T) {
335+
svc := NewMessageService(nil, nil, NewTestConfig())
336+
// When the auth service creates a mapping, it will use the user_name to look up
337+
// But in resolveMatrixUser, we only look up by number or try to convert to int
338+
// So we test that user@domain gets extracted to "user" but it won't resolve
339+
// unless there's a numeric mapping. This is the core fix - preventing the
340+
// "could not resolve" warning for user@domain format identifiers.
341+
result := svc.resolveMatrixUser("[email protected]")
342+
// Should return empty string since no mapping exists with that username
343+
assert.Equal(t, "", string(result), "should extract username from user@domain format but return empty if no mapping")
344+
})
345+
346+
// Test case 7: Case insensitive sub_number resolution
332347
t.Run("case insensitive sub_number resolution", func(t *testing.T) {
333348
svc := NewMessageService(nil, nil, NewTestConfig())
334349
svc.SaveMapping(&models.MappingRequest{

0 commit comments

Comments
 (0)