databag/net/server/internal/api_removeChannelTopic.go

104 lines
2.8 KiB
Go
Raw Normal View History

2022-03-03 08:46:32 +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-03-03 08:46:32 +00:00
)
2022-07-29 21:50:40 +00:00
//RemoveChannelTopic removes channel topic created by invoker or under invokers channel
2022-03-03 08:46:32 +00:00
func RemoveChannelTopic(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
channelID := params["channelID"]
topicID := params["topicID"]
2022-03-03 08:46:32 +00:00
2022-07-27 05:43:39 +00:00
channelSlot, guid, code, err := getChannelSlot(r, true)
2022-07-22 19:28:14 +00:00
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// load topic
var topicSlot store.TopicSlot
if ret := store.DB.Preload("Topic.Channel.ChannelSlot").Where("account_id = ? AND topic_slot_id = ?", act.ID, topicID).First(&topicSlot).Error; ret != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, ret)
} else {
ErrResponse(w, http.StatusInternalServerError, ret)
}
}
if topicSlot.Topic == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty topic"))
return
}
if topicSlot.Topic.Channel.ChannelSlot.ChannelSlotID != channelID {
ErrResponse(w, http.StatusNotFound, errors.New("channel topic not found"))
return
}
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// check permission
if act.GUID != guid && topicSlot.Topic.GUID != guid {
ErrResponse(w, http.StatusUnauthorized, errors.New("not creator of topic or host"))
return
}
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
if res := tx.Where("topic_id = ?", topicSlot.Topic.ID).Delete(&store.Tag{}).Error; res != nil {
return res
}
if res := tx.Where("topic_id = ?", topicSlot.Topic.ID).Delete(&store.TagSlot{}).Error; res != nil {
return res
}
if res := tx.Where("topic_id = ?", topicSlot.Topic.ID).Delete(&store.Asset{}).Error; res != nil {
return res
}
if res := tx.Delete(&topicSlot.Topic).Error; res != nil {
return res
}
topicSlot.Topic = nil
if res := tx.Model(&topicSlot).Update("revision", act.ChannelRevision+1).Error; res != nil {
return res
}
if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision+1).Error; res != nil {
return res
}
if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision+1).Error; res != nil {
return res
}
if res := tx.Model(act).Update("channel_revision", act.ChannelRevision+1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// cleanup file assets
go garbageCollect(act)
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// determine affected contact list
cards := make(map[string]store.Card)
for _, card := range channelSlot.Channel.Cards {
cards[card.GUID] = card
}
for _, group := range channelSlot.Channel.Groups {
for _, card := range group.Cards {
cards[card.GUID] = card
}
}
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
SetStatus(act)
for _, card := range cards {
SetContactChannelNotification(act, &card)
}
WriteResponse(w, nil)
2022-03-03 08:46:32 +00:00
}