Skip to content

Commit 71e39e9

Browse files
committed
fix: improve mappings for the fetch (again)
1 parent fc5ed96 commit 71e39e9

File tree

2 files changed

+97
-1
lines changed

2 files changed

+97
-1
lines changed

service/messages.go

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -223,7 +223,11 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
223223

224224
// resolveMatrixUser resolves an identifier to a valid Matrix user ID.
225225
// If the identifier is already a valid Matrix user ID (starts with @), it's returned as-is.
226-
// Otherwise, it tries to look up the identifier in the mapping store (e.g., phone number to user).
226+
// Otherwise, it tries to look up the identifier in the mapping store with the following logic:
227+
// - First tries to match the identifier as the main number
228+
// - If no match, tries to find the identifier in any entry's sub_numbers array
229+
// (if a sub_number matches, returns the matrix_id of that entry)
230+
//
227231
// Returns empty string if the identifier cannot be resolved.
228232
func (s *MessageService) resolveMatrixUser(identifier string) id.UserID {
229233
identifier = strings.TrimSpace(identifier)
@@ -239,6 +243,19 @@ func (s *MessageService) resolveMatrixUser(identifier string) id.UserID {
239243
return id.UserID(entry.MatrixID)
240244
}
241245

246+
// If not found as main number, try to find it in any sub_numbers
247+
s.mu.RLock()
248+
for _, entry := range s.mappings {
249+
for _, subNum := range entry.SubNumbers {
250+
if strings.EqualFold(strings.TrimSpace(subNum), identifier) {
251+
s.mu.RUnlock()
252+
logger.Debug().Str("original_identifier", identifier).Str("sub_number", subNum).Str("resolved_user", entry.MatrixID).Msg("identifier resolved from sub_number mapping")
253+
return id.UserID(entry.MatrixID)
254+
}
255+
}
256+
}
257+
s.mu.RUnlock()
258+
242259
// Could not resolve
243260
logger.Warn().Str("identifier", identifier).Msg("identifier could not be resolved to a Matrix user ID")
244261
return ""

service/messages_test.go

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -365,6 +365,85 @@ func TestLoadMappingsFromFile_InvalidJSON(t *testing.T) {
365365
assert.Contains(t, err.Error(), "failed to parse mapping file")
366366
}
367367

368+
func TestResolveMatrixUser_SubNumbers(t *testing.T) {
369+
// Test case 1: Resolve sub_number to matrix_id
370+
t.Run("resolve sub_number to matrix_id", func(t *testing.T) {
371+
svc := NewMessageService(nil, nil)
372+
svc.SaveMapping(&models.MappingRequest{
373+
Number: "201",
374+
MatrixID: "@giacomo:example.com",
375+
RoomID: "!room1:example.com",
376+
UserName: "Giacomo Rossi",
377+
SubNumbers: []string{"3344", "91201"},
378+
})
379+
380+
// Resolve using a sub_number
381+
result := svc.resolveMatrixUser("91201")
382+
assert.Equal(t, "@giacomo:example.com", string(result), "should resolve sub_number to matrix_id")
383+
})
384+
385+
// Test case 2: Resolve main number to matrix_id
386+
t.Run("resolve main number to matrix_id", func(t *testing.T) {
387+
svc := NewMessageService(nil, nil)
388+
svc.SaveMapping(&models.MappingRequest{
389+
Number: "202",
390+
MatrixID: "@mario:example.com",
391+
RoomID: "!room2:example.com",
392+
UserName: "Mario Bianchi",
393+
})
394+
395+
// Resolve using the main number
396+
result := svc.resolveMatrixUser("202")
397+
assert.Equal(t, "@mario:example.com", string(result), "should resolve main number to matrix_id")
398+
})
399+
400+
// Test case 3: Resolve another sub_number
401+
t.Run("resolve another sub_number", func(t *testing.T) {
402+
svc := NewMessageService(nil, nil)
403+
svc.SaveMapping(&models.MappingRequest{
404+
Number: "201",
405+
MatrixID: "@giacomo:example.com",
406+
RoomID: "!room1:example.com",
407+
UserName: "Giacomo Rossi",
408+
SubNumbers: []string{"3344", "91201"},
409+
})
410+
411+
// Resolve using a different sub_number
412+
result := svc.resolveMatrixUser("3344")
413+
assert.Equal(t, "@giacomo:example.com", string(result), "should resolve any sub_number to matrix_id")
414+
})
415+
416+
// Test case 4: Matrix ID passed directly
417+
t.Run("matrix id passed directly", func(t *testing.T) {
418+
svc := NewMessageService(nil, nil)
419+
result := svc.resolveMatrixUser("@test:example.com")
420+
assert.Equal(t, "@test:example.com", string(result), "should return matrix_id as-is if it starts with @")
421+
})
422+
423+
// Test case 5: No mapping found
424+
t.Run("no mapping found", func(t *testing.T) {
425+
svc := NewMessageService(nil, nil)
426+
result := svc.resolveMatrixUser("9999")
427+
assert.Equal(t, "", string(result), "should return empty string if no mapping found")
428+
})
429+
430+
// Test case 6: Case insensitivity
431+
t.Run("case insensitive sub_number resolution", func(t *testing.T) {
432+
svc := NewMessageService(nil, nil)
433+
svc.SaveMapping(&models.MappingRequest{
434+
Number: "201",
435+
MatrixID: "@giacomo:example.com",
436+
RoomID: "!room1:example.com",
437+
UserName: "Giacomo Rossi",
438+
SubNumbers: []string{"3344", "91201"},
439+
})
440+
441+
// Resolve with different case (though phone numbers are typically numeric)
442+
result := svc.resolveMatrixUser("91201")
443+
assert.Equal(t, "@giacomo:example.com", string(result), "should resolve case-insensitively")
444+
})
445+
}
446+
368447
func TestResolveMatrixIDToIdentifier_SubNumbers(t *testing.T) {
369448
// Test case 1: Resolve via sub_number match
370449
// When a matrix_id matches one of the sub_numbers, the main number should be returned (not the sub_number)

0 commit comments

Comments
 (0)