Skip to content
This repository was archived by the owner on Nov 25, 2024. It is now read-only.

Commit c90e528

Browse files
committed
Add internal API QueryMembershipForUser
Signed-off-by: Anant Prakash <[email protected]>
1 parent c87fb4d commit c90e528

File tree

2 files changed

+87
-0
lines changed
  • src/github.com/matrix-org/dendrite/roomserver

2 files changed

+87
-0
lines changed

src/github.com/matrix-org/dendrite/roomserver/api/query.go

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,25 @@ type QueryEventsByIDResponse struct {
104104
Events []gomatrixserverlib.Event `json:"events"`
105105
}
106106

107+
// QueryMembershipForUserRequest is a request to QueryMembership
108+
type QueryMembershipForUserRequest struct {
109+
// ID of the room to fetch membership from
110+
RoomID string `json:"room_id"`
111+
// ID of the user sending the request
112+
Sender string `json:"sender"`
113+
}
114+
115+
// QueryMembershipForUserResponse is a response to QueryMembership
116+
type QueryMembershipForUserResponse struct {
117+
// The EventID of the latest "m.room.member" event for the sender,
118+
// if HasBeenInRoom is true.
119+
EventID string `json:"event_id"`
120+
// True if the user has been in room before and has either stayed in it or left it.
121+
HasBeenInRoom bool `json:"has_been_in_room"`
122+
// True if the user is in room.
123+
IsInRoom bool `json:"is_in_room"`
124+
}
125+
107126
// QueryMembershipsForRoomRequest is a request to QueryMembershipsForRoom
108127
type QueryMembershipsForRoomRequest struct {
109128
// If true, only returns the membership events of "join" membership
@@ -222,6 +241,13 @@ type RoomserverQueryAPI interface {
222241
response *QueryEventsByIDResponse,
223242
) error
224243

244+
// Query the membership event for an user for a room.
245+
QueryMembershipForUser(
246+
ctx context.Context,
247+
request *QueryMembershipForUserRequest,
248+
response *QueryMembershipForUserResponse,
249+
) error
250+
225251
// Query a list of membership events for a room
226252
QueryMembershipsForRoom(
227253
ctx context.Context,
@@ -269,6 +295,9 @@ const RoomserverQueryStateAfterEventsPath = "/api/roomserver/queryStateAfterEven
269295
// RoomserverQueryEventsByIDPath is the HTTP path for the QueryEventsByID API.
270296
const RoomserverQueryEventsByIDPath = "/api/roomserver/queryEventsByID"
271297

298+
// RoomserverQueryMembershipForUserPath is the HTTP path for the QueryMembershipForUser API.
299+
const RoomserverQueryMembershipForUserPath = "/api/roomserver/queryMembershipForUser"
300+
272301
// RoomserverQueryMembershipsForRoomPath is the HTTP path for the QueryMembershipsForRoom API
273302
const RoomserverQueryMembershipsForRoomPath = "/api/roomserver/queryMembershipsForRoom"
274303

@@ -337,6 +366,19 @@ func (h *httpRoomserverQueryAPI) QueryEventsByID(
337366
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
338367
}
339368

369+
// QueryMembershipForUser implements RoomserverQueryAPI
370+
func (h *httpRoomserverQueryAPI) QueryMembershipForUser(
371+
ctx context.Context,
372+
request *QueryMembershipForUserRequest,
373+
response *QueryMembershipForUserResponse,
374+
) error {
375+
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMembershipForUser")
376+
defer span.Finish()
377+
378+
apiURL := h.roomserverURL + RoomserverQueryMembershipForUserPath
379+
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
380+
}
381+
340382
// QueryMembershipsForRoom implements RoomserverQueryAPI
341383
func (h *httpRoomserverQueryAPI) QueryMembershipsForRoom(
342384
ctx context.Context,

src/github.com/matrix-org/dendrite/roomserver/query/query.go

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -227,6 +227,37 @@ func (r *RoomserverQueryAPI) loadEvents(
227227
return result, nil
228228
}
229229

230+
// QueryMembershipForUser implements api.RoomserverQueryAPI
231+
func (r *RoomserverQueryAPI) QueryMembershipForUser(
232+
ctx context.Context,
233+
request *api.QueryMembershipForUserRequest,
234+
response *api.QueryMembershipForUserResponse,
235+
) error {
236+
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
237+
if err != nil {
238+
return err
239+
}
240+
241+
membershipEventNID, stillInRoom, err := r.DB.GetMembership(ctx, roomNID, request.Sender)
242+
if err != nil {
243+
return err
244+
}
245+
246+
if membershipEventNID == 0 {
247+
response.HasBeenInRoom = false
248+
return nil
249+
}
250+
251+
response.IsInRoom = stillInRoom
252+
eventIDMap, err := r.DB.EventIDs(ctx, []types.EventNID{membershipEventNID})
253+
if err != nil {
254+
return err
255+
}
256+
257+
response.EventID = eventIDMap[membershipEventNID]
258+
return nil
259+
}
260+
230261
// QueryMembershipsForRoom implements api.RoomserverQueryAPI
231262
func (r *RoomserverQueryAPI) QueryMembershipsForRoom(
232263
ctx context.Context,
@@ -593,6 +624,20 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
593624
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
594625
}),
595626
)
627+
servMux.Handle(
628+
api.RoomserverQueryMembershipForUserPath,
629+
common.MakeInternalAPI("QueryMembershipForUser", func(req *http.Request) util.JSONResponse {
630+
var request api.QueryMembershipForUserRequest
631+
var response api.QueryMembershipForUserResponse
632+
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
633+
return util.ErrorResponse(err)
634+
}
635+
if err := r.QueryMembershipForUser(req.Context(), &request, &response); err != nil {
636+
return util.ErrorResponse(err)
637+
}
638+
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
639+
}),
640+
)
596641
servMux.Handle(
597642
api.RoomserverQueryMembershipsForRoomPath,
598643
common.MakeInternalAPI("queryMembershipsForRoom", func(req *http.Request) util.JSONResponse {

0 commit comments

Comments
 (0)