Skip to content

Commit 67b706b

Browse files
committed
feat: retrieve full number/matrix map
1 parent fa16a17 commit 67b706b

File tree

4 files changed

+67
-5
lines changed

4 files changed

+67
-5
lines changed

api/routes.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ import (
55
"net/http"
66
"strings"
77

8+
"github.com/labstack/echo/v4"
89
"github.com/nethesis/matrix2acrobits/models"
910
"github.com/nethesis/matrix2acrobits/service"
10-
"github.com/labstack/echo/v4"
1111
)
1212

1313
const adminTokenHeader = "X-Super-Admin-Token"
@@ -75,7 +75,12 @@ func (h handler) getMapping(c echo.Context) error {
7575

7676
smsNumber := strings.TrimSpace(c.QueryParam("sms_number"))
7777
if smsNumber == "" {
78-
return echo.NewHTTPError(http.StatusBadRequest, "sms_number query parameter is required")
78+
// return full mappings list when sms_number is not provided
79+
respList, err := h.svc.ListMappings()
80+
if err != nil {
81+
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
82+
}
83+
return c.JSON(http.StatusOK, respList)
7984
}
8085

8186
resp, err := h.svc.LookupMapping(smsNumber)

docs/openapi.yaml

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -146,15 +146,19 @@ paths:
146146
name: sms_number
147147
schema:
148148
type: string
149-
required: true
150-
description: The SMS number or identifier previously mapped.
149+
required: false
150+
description: The SMS number or identifier previously mapped. If omitted, the endpoint returns the full list of mappings.
151151
responses:
152152
'200':
153153
description: Mapping found
154154
content:
155155
application/json:
156156
schema:
157-
$ref: '#/components/schemas/MappingResponse'
157+
oneOf:
158+
- $ref: '#/components/schemas/MappingResponse'
159+
- type: array
160+
items:
161+
$ref: '#/components/schemas/MappingResponse'
158162
'401':
159163
description: Invalid admin token.
160164
'404':

service/messages.go

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,17 @@ func (s *MessageService) LookupMapping(smsNumber string) (*models.MappingRespons
200200
return s.buildMappingResponse(entry), nil
201201
}
202202

203+
// ListMappings returns all stored mappings as MappingResponse slices.
204+
func (s *MessageService) ListMappings() ([]*models.MappingResponse, error) {
205+
s.mu.RLock()
206+
defer s.mu.RUnlock()
207+
out := make([]*models.MappingResponse, 0, len(s.mappings))
208+
for _, entry := range s.mappings {
209+
out = append(out, s.buildMappingResponse(entry))
210+
}
211+
return out, nil
212+
}
213+
203214
// SaveMapping persists a new SMS-to-Matrix mapping via the admin API.
204215
func (s *MessageService) SaveMapping(req *models.MappingRequest) (*models.MappingResponse, error) {
205216
smsNumber := strings.TrimSpace(req.SMSNumber)

service/messages_test.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package service
33
import (
44
"testing"
55

6+
"github.com/nethesis/matrix2acrobits/models"
67
"github.com/stretchr/testify/assert"
78
)
89

@@ -152,3 +153,44 @@ func TestMapAuthErr(t *testing.T) {
152153
})
153154
}
154155
}
156+
157+
func TestListMappings(t *testing.T) {
158+
svc := NewMessageService(nil)
159+
160+
// Seed two mappings
161+
svc.setMapping(mappingEntry{
162+
SMSNumber: "+111",
163+
MatrixID: "@alice:example.com",
164+
RoomID: "!room1:example.com",
165+
})
166+
svc.setMapping(mappingEntry{
167+
SMSNumber: "+222",
168+
MatrixID: "@bob:example.com",
169+
RoomID: "!room2:example.com",
170+
})
171+
172+
list, err := svc.ListMappings()
173+
assert.NoError(t, err)
174+
// We expect two mappings; order is not guaranteed.
175+
assert.Len(t, list, 2)
176+
177+
// Build a map for easy assertions
178+
m := make(map[string]*models.MappingResponse)
179+
for _, it := range list {
180+
m[it.SMSNumber] = it
181+
}
182+
183+
if v, ok := m["+111"]; ok {
184+
assert.Equal(t, "@alice:example.com", v.MatrixID)
185+
assert.Equal(t, "!room1:example.com", v.RoomID)
186+
} else {
187+
t.Fatalf("missing mapping for +111")
188+
}
189+
190+
if v, ok := m["+222"]; ok {
191+
assert.Equal(t, "@bob:example.com", v.MatrixID)
192+
assert.Equal(t, "!room2:example.com", v.RoomID)
193+
} else {
194+
t.Fatalf("missing mapping for +222")
195+
}
196+
}

0 commit comments

Comments
 (0)