databag/net/server/internal/api_getChannelDetail.go

68 lines
1.8 KiB
Go
Raw Normal View History

2022-05-09 21:08:54 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"errors"
"github.com/gorilla/mux"
"gorm.io/gorm"
"net/http"
2022-05-09 21:08:54 +00:00
)
2022-07-27 05:43:39 +00:00
//GetChannelDetail retrieve channel top level attributes
2022-05-09 21:08:54 +00:00
func GetChannelDetail(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
channelID := params["channelID"]
2022-05-09 21:08:54 +00:00
2022-07-22 19:28:14 +00:00
var guid string
var act *store.Account
tokenType := ParamTokenType(r)
if tokenType == APPTokenAgent {
account, code, err := ParamAgentToken(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
act = account
} else if tokenType == APPTokenContact {
card, code, err := ParamContactToken(r, true)
if err != nil {
ErrResponse(w, code, err)
return
}
act = &card.Account
guid = card.GUID
} else {
ErrResponse(w, http.StatusBadRequest, errors.New("unknown token type"))
return
}
2022-05-09 21:08:54 +00:00
2022-07-22 19:28:14 +00:00
// load channel
var slot store.ChannelSlot
if err := store.DB.Preload("Channel.Cards.CardSlot").Preload("Channel.Groups.Cards").Preload("Channel.Groups.GroupSlot").Where("account_id = ? AND channel_slot_id = ?", act.ID, channelID).First(&slot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
2022-05-09 21:08:54 +00:00
video := getBoolConfigValue(CNFEnableVideo, true);
audio := getBoolConfigValue(CNFEnableAudio, true);
image := getBoolConfigValue(CNFEnableImage, true);
2022-07-22 19:28:14 +00:00
// return model data
if guid != "" {
if isChannelShared(guid, slot.Channel) {
WriteResponse(w, getChannelDetailModel(&slot, false, image, audio, video))
2022-07-22 19:28:14 +00:00
} else {
ErrResponse(w, http.StatusNotFound, errors.New("channel not shared with requestor"))
return
}
} else {
WriteResponse(w, getChannelDetailModel(&slot, true, image, audio, video))
2022-07-22 19:28:14 +00:00
}
2022-05-09 21:08:54 +00:00
}