[roomserver] Add internal API QueryMembershipForUser (#544)
* Add internal API QueryMembershipForUser Signed-off-by: Anant Prakash <anantprakashjsr@gmail.com> * Change sender to user_id
This commit is contained in:
parent
74b319c42a
commit
c8feee7354
2 changed files with 87 additions and 0 deletions
|
@ -104,6 +104,25 @@ type QueryEventsByIDResponse struct {
|
|||
Events []gomatrixserverlib.Event `json:"events"`
|
||||
}
|
||||
|
||||
// QueryMembershipForUserRequest is a request to QueryMembership
|
||||
type QueryMembershipForUserRequest struct {
|
||||
// ID of the room to fetch membership from
|
||||
RoomID string `json:"room_id"`
|
||||
// ID of the user for whom membership is requested
|
||||
UserID string `json:"user_id"`
|
||||
}
|
||||
|
||||
// QueryMembershipForUserResponse is a response to QueryMembership
|
||||
type QueryMembershipForUserResponse struct {
|
||||
// The EventID of the latest "m.room.member" event for the sender,
|
||||
// if HasBeenInRoom is true.
|
||||
EventID string `json:"event_id"`
|
||||
// True if the user has been in room before and has either stayed in it or left it.
|
||||
HasBeenInRoom bool `json:"has_been_in_room"`
|
||||
// True if the user is in room.
|
||||
IsInRoom bool `json:"is_in_room"`
|
||||
}
|
||||
|
||||
// QueryMembershipsForRoomRequest is a request to QueryMembershipsForRoom
|
||||
type QueryMembershipsForRoomRequest struct {
|
||||
// If true, only returns the membership events of "join" membership
|
||||
|
@ -222,6 +241,13 @@ type RoomserverQueryAPI interface {
|
|||
response *QueryEventsByIDResponse,
|
||||
) error
|
||||
|
||||
// Query the membership event for an user for a room.
|
||||
QueryMembershipForUser(
|
||||
ctx context.Context,
|
||||
request *QueryMembershipForUserRequest,
|
||||
response *QueryMembershipForUserResponse,
|
||||
) error
|
||||
|
||||
// Query a list of membership events for a room
|
||||
QueryMembershipsForRoom(
|
||||
ctx context.Context,
|
||||
|
@ -269,6 +295,9 @@ const RoomserverQueryStateAfterEventsPath = "/api/roomserver/queryStateAfterEven
|
|||
// RoomserverQueryEventsByIDPath is the HTTP path for the QueryEventsByID API.
|
||||
const RoomserverQueryEventsByIDPath = "/api/roomserver/queryEventsByID"
|
||||
|
||||
// RoomserverQueryMembershipForUserPath is the HTTP path for the QueryMembershipForUser API.
|
||||
const RoomserverQueryMembershipForUserPath = "/api/roomserver/queryMembershipForUser"
|
||||
|
||||
// RoomserverQueryMembershipsForRoomPath is the HTTP path for the QueryMembershipsForRoom API
|
||||
const RoomserverQueryMembershipsForRoomPath = "/api/roomserver/queryMembershipsForRoom"
|
||||
|
||||
|
@ -337,6 +366,19 @@ func (h *httpRoomserverQueryAPI) QueryEventsByID(
|
|||
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
// QueryMembershipForUser implements RoomserverQueryAPI
|
||||
func (h *httpRoomserverQueryAPI) QueryMembershipForUser(
|
||||
ctx context.Context,
|
||||
request *QueryMembershipForUserRequest,
|
||||
response *QueryMembershipForUserResponse,
|
||||
) error {
|
||||
span, ctx := opentracing.StartSpanFromContext(ctx, "QueryMembershipForUser")
|
||||
defer span.Finish()
|
||||
|
||||
apiURL := h.roomserverURL + RoomserverQueryMembershipForUserPath
|
||||
return postJSON(ctx, span, h.httpClient, apiURL, request, response)
|
||||
}
|
||||
|
||||
// QueryMembershipsForRoom implements RoomserverQueryAPI
|
||||
func (h *httpRoomserverQueryAPI) QueryMembershipsForRoom(
|
||||
ctx context.Context,
|
||||
|
|
|
@ -227,6 +227,37 @@ func (r *RoomserverQueryAPI) loadEvents(
|
|||
return result, nil
|
||||
}
|
||||
|
||||
// QueryMembershipForUser implements api.RoomserverQueryAPI
|
||||
func (r *RoomserverQueryAPI) QueryMembershipForUser(
|
||||
ctx context.Context,
|
||||
request *api.QueryMembershipForUserRequest,
|
||||
response *api.QueryMembershipForUserResponse,
|
||||
) error {
|
||||
roomNID, err := r.DB.RoomNID(ctx, request.RoomID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
membershipEventNID, stillInRoom, err := r.DB.GetMembership(ctx, roomNID, request.UserID)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
if membershipEventNID == 0 {
|
||||
response.HasBeenInRoom = false
|
||||
return nil
|
||||
}
|
||||
|
||||
response.IsInRoom = stillInRoom
|
||||
eventIDMap, err := r.DB.EventIDs(ctx, []types.EventNID{membershipEventNID})
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
response.EventID = eventIDMap[membershipEventNID]
|
||||
return nil
|
||||
}
|
||||
|
||||
// QueryMembershipsForRoom implements api.RoomserverQueryAPI
|
||||
func (r *RoomserverQueryAPI) QueryMembershipsForRoom(
|
||||
ctx context.Context,
|
||||
|
@ -593,6 +624,20 @@ func (r *RoomserverQueryAPI) SetupHTTP(servMux *http.ServeMux) {
|
|||
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverQueryMembershipForUserPath,
|
||||
common.MakeInternalAPI("QueryMembershipForUser", func(req *http.Request) util.JSONResponse {
|
||||
var request api.QueryMembershipForUserRequest
|
||||
var response api.QueryMembershipForUserResponse
|
||||
if err := json.NewDecoder(req.Body).Decode(&request); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
if err := r.QueryMembershipForUser(req.Context(), &request, &response); err != nil {
|
||||
return util.ErrorResponse(err)
|
||||
}
|
||||
return util.JSONResponse{Code: http.StatusOK, JSON: &response}
|
||||
}),
|
||||
)
|
||||
servMux.Handle(
|
||||
api.RoomserverQueryMembershipsForRoomPath,
|
||||
common.MakeInternalAPI("queryMembershipsForRoom", func(req *http.Request) util.JSONResponse {
|
||||
|
|
Loading…
Reference in a new issue