Skip to content

Commit d9390e6

Browse files
committed
feat: fetch_messages, remap to user name
1 parent 0b22940 commit d9390e6

File tree

2 files changed

+25
-9
lines changed

2 files changed

+25
-9
lines changed

models/mapping.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ type MappingRequest struct {
55
SMSNumber string `json:"sms_number"`
66
MatrixID string `json:"matrix_id,omitempty"`
77
RoomID string `json:"room_id"`
8+
UserName string `json:"user_name,omitempty"`
89
}
910

1011
// MappingResponse is returned once a mapping has been created or looked up.

service/messages.go

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ type mappingEntry struct {
3838
SMSNumber string
3939
MatrixID string
4040
RoomID id.RoomID
41+
UserName string
4142
UpdatedAt time.Time
4243
}
4344

@@ -137,8 +138,8 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
137138
continue
138139
}
139140
msg := convertEvent(evt)
140-
// Remap sender from Matrix ID to SMS number if mapping exists
141-
msg.Sender = s.remapMatrixToSMS(msg.Sender)
141+
// Remap sender from Matrix ID to User name if mapping exists
142+
msg.Sender = s.remapMatrixToUserName(msg.Sender)
142143
if isSentBy(msg.Sender, string(userID)) {
143144
sent = append(sent, msg)
144145
} else {
@@ -179,18 +180,31 @@ func (s *MessageService) resolveMatrixUser(identifier string) id.UserID {
179180
return ""
180181
}
181182

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 {
183+
// remapMatrixToUserName attempts to remap a Matrix user ID to a configured user name if a mapping exists.
184+
// If a user name is present in the mapping, it is returned. Otherwise, if an SMS number is
185+
// configured for the mapping that is a plausible phone number it will be returned. If no mapping
186+
// is found, returns the original Matrix ID.
187+
func (s *MessageService) remapMatrixToUserName(matrixID string) string {
185188
matrixID = strings.TrimSpace(matrixID)
186189

187190
// Search through all mappings to find one where MatrixID matches
188191
s.mu.RLock()
189192
defer s.mu.RUnlock()
190193
for _, entry := range s.mappings {
191194
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
195+
if entry.UserName != "" {
196+
logger.Debug().Str("matrix_id", matrixID).Str("user_name", entry.UserName).Msg("remapped matrix id to user name")
197+
return entry.UserName
198+
}
199+
// Fall back to SMS number if it looks like a phone number
200+
if isPhoneNumber(entry.SMSNumber) {
201+
logger.Debug().Str("matrix_id", matrixID).Str("sms_number", entry.SMSNumber).Msg("remapped matrix id to sms number (fallback)")
202+
return entry.SMSNumber
203+
}
204+
// If we have a MatrixID stored in the entry, prefer returning that normalized value
205+
if entry.MatrixID != "" {
206+
return entry.MatrixID
207+
}
194208
}
195209
}
196210

@@ -310,8 +324,8 @@ func (s *MessageService) SaveMapping(req *models.MappingRequest) (*models.Mappin
310324
// LoadMappingsFromFile loads mappings from a JSON file in the format:
311325
//
312326
// [
313-
// {"sms_number": "91201", "matrix_id": "@giacomo:synapse.gs.nethserver.net", "room_id": "!giacomo-room:synapse.gs.nethserver.net"},
314-
// {"sms_number": "91202", "matrix_id": "@mario:synapse.gs.nethserver.net", "room_id": "!mario-room:synapse.gs.nethserver.net"}
327+
// {"sms_number": "91201", "matrix_id": "@giacomo:synapse.gs.nethserver.net", "room_id": "!giacomo-room:synapse.gs.nethserver.net", "user_name": "Giacomo Rossi"},
328+
// {"sms_number": "91202", "matrix_id": "@mario:synapse.gs.nethserver.net", "room_id": "!mario-room:synapse.gs.nethserver.net", "user_name": "Mario Bianchi"}
315329
// ]
316330
//
317331
// This is typically called at startup if MAPPING_FILE environment variable is set.
@@ -335,6 +349,7 @@ func (s *MessageService) LoadMappingsFromFile(filePath string) error {
335349
SMSNumber: req.SMSNumber,
336350
MatrixID: req.MatrixID,
337351
RoomID: id.RoomID(req.RoomID),
352+
UserName: req.UserName,
338353
UpdatedAt: s.now(),
339354
}
340355
s.setMapping(entry)

0 commit comments

Comments
 (0)