databag/net/server/internal/api_getChannelTopic.go

118 lines
2.7 KiB
Go
Raw Normal View History

2022-02-18 20:21:15 +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-02-18 20:21:15 +00:00
)
2022-07-27 05:48:50 +00:00
//GetChannelTopic retrieves channel topic
2022-02-18 20:21:15 +00:00
func GetChannelTopic(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
topicID := params["topicID"]
2022-02-18 20:21:15 +00:00
2022-07-27 05:43:39 +00:00
channelSlot, _, code, err := getChannelSlot(r, false)
2022-07-22 19:28:14 +00:00
if err != nil {
ErrResponse(w, code, err)
return
}
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
// load topic
var topicSlot store.TopicSlot
if err = store.DB.Preload("Topic.Assets").Where("channel_id = ? AND topic_slot_id = ?", channelSlot.Channel.ID, topicID).First(&topicSlot).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
} else {
code = http.StatusInternalServerError
}
2022-07-22 20:23:17 +00:00
ErrResponse(w, code, err)
2022-07-22 19:28:14 +00:00
return
}
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
WriteResponse(w, getTopicModel(&topicSlot))
2022-02-18 20:21:15 +00:00
}
2022-11-11 23:26:43 +00:00
func isMember(guid string, members []store.Member) bool {
for _, member := range members {
if guid == member.Card.GUID {
2022-07-22 19:28:14 +00:00
return true
}
}
return false
2022-02-18 20:21:15 +00:00
}
func isViewer(guid string, groups []store.Group) bool {
2022-07-22 19:28:14 +00:00
for _, group := range groups {
for _, card := range group.Cards {
if guid == card.GUID {
return true
}
}
}
return false
2022-02-18 20:21:15 +00:00
}
2022-07-27 05:43:39 +00:00
func getChannelSlot(r *http.Request, member bool) (slot store.ChannelSlot, guid string, code int, err error) {
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
channelID := params["channelID"]
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
// validate contact access
var account *store.Account
tokenType := ParamTokenType(r)
if tokenType == APPTokenAgent {
account, code, err = ParamAgentToken(r, false)
if err != nil {
return
}
guid = account.GUID
} else if tokenType == APPTokenContact {
var card *store.Card
card, code, err = ParamContactToken(r, true)
if err != nil {
return
}
account = &card.Account
guid = card.GUID
} else {
err = errors.New("unknown token type")
code = http.StatusBadRequest
return
}
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
// load channel
2022-11-11 23:26:43 +00:00
if err = store.DB.Preload("Account").Preload("Channel.Members.Card").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelID).First(&slot).Error; err != nil {
2022-07-22 19:28:14 +00:00
if errors.Is(err, gorm.ErrRecordNotFound) {
code = http.StatusNotFound
} else {
code = http.StatusInternalServerError
}
return
}
if slot.Channel == nil {
err = errors.New("referenced empty channel")
code = http.StatusNotFound
return
}
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
// validate access to channel
if tokenType == APPTokenContact {
2022-11-11 23:26:43 +00:00
if member && !isMember(guid, slot.Channel.Members) {
2022-07-22 19:28:14 +00:00
err = errors.New("contact is not a channel member")
code = http.StatusUnauthorized
return
2022-11-11 23:26:43 +00:00
} else if !isViewer(guid, slot.Channel.Groups) && !isMember(guid, slot.Channel.Members) {
2022-07-22 19:28:14 +00:00
err = errors.New("contact is not a channel viewer")
code = http.StatusUnauthorized
return
}
}
2022-02-18 20:21:15 +00:00
2022-07-22 19:28:14 +00:00
return
2022-02-18 20:21:15 +00:00
}