package clientapi import ( "net/http" "github.com/dariuszjaniak/warp/server/internal/httputil" "github.com/dariuszjaniak/warp/server/internal/storage" "github.com/dariuszjaniak/warp/server/internal/syncapi" "github.com/dariuszjaniak/warp/server/internal/userapi" ) // handleRelations implements // GET /_matrix/client/v1/rooms/{roomID}/relations/{eventID}[/{relType}[/{eventType}]]. // // The three routes share the handler; the narrower ones only fill in more // of the path. func (c *ClientAPI) handleRelations(w http.ResponseWriter, r *http.Request, auth userapi.Authenticated) error { roomID, err := pathRoomID(r) if err != nil { return err } eventID := r.PathValue("eventID") if eventID == "" { return httputil.MissingParam("An event ID is required") } query := r.URL.Query() direction := storage.DirectionBackward switch query.Get("dir") { case "f": direction = storage.DirectionForward case "b", "": default: return httputil.InvalidParam("dir must be \"b\" or \"f\"") } limit, err := intParam(query.Get("limit")) if err != nil { return err } recurse, err := boolParam(query.Get("recurse")) if err != nil { return err } resp, err := c.sync.Relations(r.Context(), syncapi.RelationsRequest{ RoomID: roomID, EventID: eventID, User: auth.UserID, RelType: r.PathValue("relType"), EventType: r.PathValue("eventType"), From: query.Get("from"), To: query.Get("to"), Direction: direction, Limit: limit, Recurse: recurse, }) if err != nil { return syncError(err) } httputil.WriteJSON(w, http.StatusOK, resp) return nil } // handleThreads implements GET /_matrix/client/v1/rooms/{roomID}/threads. func (c *ClientAPI) handleThreads(w http.ResponseWriter, r *http.Request, auth userapi.Authenticated) error { roomID, err := pathRoomID(r) if err != nil { return err } query := r.URL.Query() limit, err := intParam(query.Get("limit")) if err != nil { return err } resp, err := c.sync.Threads(r.Context(), syncapi.ThreadsRequest{ RoomID: roomID, User: auth.UserID, Include: query.Get("include"), From: query.Get("from"), Limit: limit, }) if err != nil { return syncError(err) } httputil.WriteJSON(w, http.StatusOK, resp) return nil }