2022-03-27 12:26:25 +13:00
|
|
|
package room
|
|
|
|
|
|
|
|
import (
|
2024-05-05 10:03:32 +12:00
|
|
|
"encoding/json"
|
|
|
|
"io"
|
2022-03-27 12:26:25 +13:00
|
|
|
"net/http"
|
|
|
|
|
2024-05-05 10:03:32 +12:00
|
|
|
"github.com/demodesk/neko/pkg/auth"
|
|
|
|
"github.com/demodesk/neko/pkg/types"
|
2022-07-14 10:58:22 +12:00
|
|
|
"github.com/demodesk/neko/pkg/utils"
|
2022-03-27 12:26:25 +13:00
|
|
|
)
|
|
|
|
|
|
|
|
func (h *RoomHandler) settingsGet(w http.ResponseWriter, r *http.Request) error {
|
|
|
|
settings := h.sessions.Settings()
|
|
|
|
return utils.HttpSuccess(w, settings)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *RoomHandler) settingsSet(w http.ResponseWriter, r *http.Request) error {
|
2024-05-05 10:03:32 +12:00
|
|
|
session, _ := auth.GetSession(r)
|
2022-03-27 12:26:25 +13:00
|
|
|
|
2024-05-05 10:03:32 +12:00
|
|
|
// We read the request body first and unmashal it inside the UpdateSettingsFunc
|
|
|
|
// to ensure atomicity of the operation.
|
|
|
|
body, err := io.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
return utils.HttpBadRequest("unable to read request body").WithInternalErr(err)
|
2022-03-27 12:26:25 +13:00
|
|
|
}
|
|
|
|
|
2024-05-05 10:03:32 +12:00
|
|
|
h.sessions.UpdateSettingsFunc(session, func(settings *types.Settings) bool {
|
|
|
|
err = json.Unmarshal(body, settings)
|
|
|
|
return err == nil
|
|
|
|
})
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return utils.HttpBadRequest("unable to parse provided data").WithInternalErr(err)
|
|
|
|
}
|
2022-03-27 12:26:25 +13:00
|
|
|
|
|
|
|
return utils.HttpSuccess(w)
|
|
|
|
}
|