Skip to content

Commit 1ad1fc6

Browse files
committed
fix: simply fetch logic
1 parent a45f4c1 commit 1ad1fc6

File tree

3 files changed

+13
-50
lines changed

3 files changed

+13
-50
lines changed

README.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,4 +81,5 @@ Implemented APIs:
8181

8282
The following features are not yet implemented:
8383

84-
- implement password validation on send messages, currently the password is ignored
84+
- sendMessage: implement password validation on send messages, currently the password is ignored
85+
- fetchMessages: implement filtering messages by timestamp, currently last 100 messages are fetched

matrix/client.go

Lines changed: 3 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -98,12 +98,11 @@ func (mc *MatrixClient) SendMessage(ctx context.Context, userID id.UserID, roomI
9898
}
9999

100100
// Sync performs a sync for the specified user to fetch messages.
101-
// If filterAfterEventID is provided (non-empty), uses post-processing to filter events.
102-
func (mc *MatrixClient) Sync(ctx context.Context, userID id.UserID, since, filterAfterEventID string) (*mautrix.RespSync, error) {
101+
func (mc *MatrixClient) Sync(ctx context.Context, userID id.UserID) (*mautrix.RespSync, error) {
103102
mc.mu.Lock()
104103
defer mc.mu.Unlock()
105104

106-
logger.Debug().Str("user_id", string(userID)).Str("since", since).Str("filter_after_event_id", filterAfterEventID).Msg("matrix: performing sync")
105+
logger.Debug().Str("user_id", string(userID)).Msg("matrix: performing sync")
107106

108107
mc.cli.UserID = userID
109108

@@ -127,37 +126,12 @@ func (mc *MatrixClient) Sync(ctx context.Context, userID id.UserID, since, filte
127126
logger.Debug().Str("user_id", string(userID)).Str("filter", filterJSON).Msg("matrix: using message filter for sync")
128127

129128
// The SyncRequest method signature: SyncRequest(ctx, timeoutMS, since, filter, fullState, setPresence)
130-
resp, err := mc.cli.SyncRequest(ctx, 30000, since, filterJSON, false, event.PresenceOnline)
129+
resp, err := mc.cli.SyncRequest(ctx, 30000, "", filterJSON, false, event.PresenceOnline)
131130
if err != nil {
132131
logger.Error().Str("user_id", string(userID)).Err(err).Msg("matrix: sync failed")
133132
return nil, err
134133
}
135134

136-
// Post-process: If filterAfterEventID is provided, filter room timelines to only include events after it
137-
// This is necessary because the Matrix spec doesn't support server-side filtering by event ID
138-
if filterAfterEventID != "" {
139-
logger.Debug().Str("user_id", string(userID)).Str("filter_after_event_id", filterAfterEventID).Msg("matrix: filtering sync results by event ID")
140-
for roomID, room := range resp.Rooms.Join {
141-
foundEvent := false
142-
// Find the index of the target event
143-
for j, evt := range room.Timeline.Events {
144-
if string(evt.ID) == filterAfterEventID {
145-
// Keep only events after this one
146-
room.Timeline.Events = room.Timeline.Events[j+1:]
147-
resp.Rooms.Join[roomID] = room
148-
logger.Debug().Str("user_id", string(userID)).Str("room_id", roomID.String()).Int("events_removed", j+1).Msg("matrix: filtered room timeline")
149-
foundEvent = true
150-
break
151-
}
152-
}
153-
if !foundEvent && len(room.Timeline.Events) > 0 {
154-
// Event not found in this timeline window - the requested event is older than what we got
155-
// Keep all events since they're all newer than the requested event
156-
logger.Debug().Str("user_id", string(userID)).Str("room_id", roomID.String()).Msg("matrix: filter event not found in timeline, keeping all events")
157-
}
158-
}
159-
}
160-
161135
logger.Debug().Str("user_id", string(userID)).Int("rooms", len(resp.Rooms.Join)).Msg("matrix: sync completed")
162136
return resp, nil
163137
}

service/messages.go

Lines changed: 8 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -125,35 +125,21 @@ func (s *MessageService) SendMessage(ctx context.Context, req *models.SendMessag
125125
func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMessagesRequest) (*models.FetchMessagesResponse, error) {
126126
logger.Debug().Interface("request", req).Msg("fetch messages request received")
127127

128-
// The user to impersonate is taken from the 'Username' field.
128+
// The username sent by the app is always an extension number like 91201
129129
userID := s.resolveMatrixUser(strings.TrimSpace(req.Username))
130130
if userID == "" {
131131
logger.Warn().Msg("fetch messages: empty user ID")
132132
return nil, ErrAuthentication
133133
}
134134

135-
since := req.LastID
136-
filterAfterEventID := ""
135+
logger.Debug().Str("user_id", string(userID)).Msg("syncing messages from matrix")
137136

138-
// Acrobits might send a Matrix Event ID (starts with $) as last_id.
139-
// Matrix Sync requires a stream token (usually starts with s).
140-
// If we get an Event ID, we must perform an initial sync (empty since)
141-
// and let the Matrix client filter the results to return only messages after that event.
142-
if strings.HasPrefix(since, "$") {
143-
logger.Debug().Str("last_id", since).Msg("received event ID as last_id, performing initial sync with event filtering")
144-
filterAfterEventID = since
145-
since = ""
146-
}
147-
148-
logger.Debug().Str("user_id", string(userID)).Str("since", since).Msg("syncing messages from matrix")
149-
150-
resp, err := s.matrixClient.Sync(ctx, userID, since, filterAfterEventID)
137+
resp, err := s.matrixClient.Sync(ctx, userID)
151138
if err != nil {
152139
// If the token is invalid (e.g. expired or from a different session), retry with a full sync.
153140
if strings.Contains(err.Error(), "Invalid stream token") || strings.Contains(err.Error(), "M_UNKNOWN") {
154141
logger.Warn().Err(err).Msg("invalid stream token, retrying with full sync")
155-
since = ""
156-
resp, err = s.matrixClient.Sync(ctx, userID, since, filterAfterEventID)
142+
resp, err = s.matrixClient.Sync(ctx, userID)
157143
}
158144
}
159145
if err != nil {
@@ -163,7 +149,7 @@ func (s *MessageService) FetchMessages(ctx context.Context, req *models.FetchMes
163149

164150
received, sent := make([]models.SMS, 0, 8), make([]models.SMS, 0, 8)
165151

166-
// Resolve the caller's identifier (e.g. "91201")
152+
// Resolve the caller's identifier (e.g. "91201" -> "201")
167153
callerIdentifier := s.resolveMatrixIDToIdentifier(string(userID))
168154

169155
for _, room := range resp.Rooms.Join {
@@ -295,7 +281,9 @@ func (s *MessageService) resolveMatrixIDToIdentifier(matrixID string) string {
295281

296282
// No mapping found, return the original Matrix ID
297283
return matrixID
298-
} // resolveRoomIDToOtherIdentifier finds the identifier of the "other" participant in a room.
284+
}
285+
286+
// resolveRoomIDToOtherIdentifier finds the identifier of the "other" participant in a room.
299287
func (s *MessageService) resolveRoomIDToOtherIdentifier(ctx context.Context, roomID id.RoomID, myMatrixID string) string {
300288
aliases := s.matrixClient.GetRoomAliases(ctx, roomID)
301289
my := strings.TrimSpace(myMatrixID)

0 commit comments

Comments
 (0)