neko/internal/api/members/controler.go

115 lines
2.3 KiB
Go
Raw Normal View History

2020-11-22 11:42:34 +13:00
package members
2020-11-22 11:39:24 +13:00
import (
"net/http"
2020-11-26 10:39:54 +13:00
"demodesk/neko/internal/types"
2021-02-15 02:40:17 +13:00
"demodesk/neko/internal/utils"
2020-11-22 11:39:24 +13:00
)
type MemberDataPayload struct {
2020-11-26 10:39:54 +13:00
ID string `json:"id"`
2020-12-09 03:14:20 +13:00
*types.MemberProfile
2020-11-22 12:06:55 +13:00
}
2020-12-09 04:05:54 +13:00
func (h *MembersHandler) membersList(w http.ResponseWriter, r *http.Request) {
members := []MemberDataPayload{}
2021-03-14 09:11:48 +13:00
for _, session := range h.sessions.List() {
2020-12-09 04:05:54 +13:00
profile := session.GetProfile()
members = append(members, MemberDataPayload{
ID: session.ID(),
MemberProfile: &profile,
})
}
utils.HttpSuccess(w, members)
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersCreate(w http.ResponseWriter, r *http.Request) {
data := &MemberDataPayload{
2020-12-09 03:37:48 +13:00
// default values
2020-12-09 03:14:20 +13:00
MemberProfile: &types.MemberProfile{
2021-02-15 02:40:17 +13:00
IsAdmin: false,
CanLogin: true,
CanConnect: true,
CanWatch: true,
CanHost: true,
2020-12-09 03:14:20 +13:00
CanAccessClipboard: true,
},
2020-12-09 02:13:12 +13:00
}
2020-11-22 12:06:55 +13:00
if !utils.HttpJsonRequest(w, r, data) {
return
}
2020-11-22 11:39:24 +13:00
2020-12-09 02:13:12 +13:00
if data.Secret == "" {
2020-12-07 09:10:45 +13:00
utils.HttpBadRequest(w, "Secret cannot be empty.")
return
}
2020-12-09 02:13:12 +13:00
if data.Name == "" {
2020-12-07 09:10:45 +13:00
utils.HttpBadRequest(w, "Name cannot be empty.")
return
}
2020-12-09 02:13:12 +13:00
if data.ID == "" {
2020-12-03 04:50:30 +13:00
var err error
2020-12-09 02:13:12 +13:00
if data.ID, err = utils.NewUID(32); err != nil {
2020-12-03 04:50:30 +13:00
utils.HttpInternalServerError(w, err)
return
}
} else {
2020-12-09 02:13:12 +13:00
if _, ok := h.sessions.Get(data.ID); ok {
2020-12-03 04:50:30 +13:00
utils.HttpBadRequest(w, "Member ID already exists.")
return
}
2020-11-26 10:39:54 +13:00
}
2021-03-14 09:43:36 +13:00
// TODO: Refactor.
// data.ID
session, err := h.sessions.Create(*data.MemberProfile)
2020-12-06 11:05:46 +13:00
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w, MemberDataPayload{
2020-11-26 10:39:54 +13:00
ID: session.ID(),
2020-11-22 12:06:55 +13:00
})
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersRead(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2020-12-09 03:37:48 +13:00
profile := member.GetProfile()
2020-11-22 11:39:24 +13:00
2020-12-09 03:37:48 +13:00
utils.HttpSuccess(w, profile)
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersUpdate(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2020-12-09 03:37:48 +13:00
profile := member.GetProfile()
2020-12-07 09:10:45 +13:00
2020-12-09 03:14:20 +13:00
if !utils.HttpJsonRequest(w, r, &profile) {
2020-12-09 02:13:12 +13:00
return
2020-12-07 09:10:45 +13:00
}
2020-12-09 03:14:20 +13:00
if err := h.sessions.Update(member.ID(), profile); err != nil {
2020-12-06 10:18:45 +13:00
utils.HttpInternalServerError(w, err)
return
}
2020-12-03 05:43:34 +13:00
utils.HttpSuccess(w)
2020-11-22 11:39:24 +13:00
}
2020-11-22 11:42:34 +13:00
func (h *MembersHandler) membersDelete(w http.ResponseWriter, r *http.Request) {
2020-11-22 11:39:24 +13:00
member := GetMember(r)
2020-11-26 10:39:54 +13:00
if err := h.sessions.Delete(member.ID()); err != nil {
2020-11-29 03:00:21 +13:00
utils.HttpInternalServerError(w, err)
2020-11-26 10:39:54 +13:00
return
}
utils.HttpSuccess(w)
2020-11-22 11:39:24 +13:00
}