databag/net/server/internal/api_removeChannelTopicAsset.go

96 lines
2.4 KiB
Go
Raw Normal View History

2022-03-02 21:19:16 +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-02 21:19:16 +00:00
)
2022-07-29 21:50:40 +00:00
//RemoveChannelTopicAsset removes topic asset if invoker created topic
2022-03-02 21:19:16 +00:00
func RemoveChannelTopicAsset(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
topicID := params["topicID"]
assetID := params["assetID"]
2022-03-02 21:19:16 +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-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
// load asset
var asset store.Asset
if err = store.DB.Preload("Topic.TopicSlot").Where("channel_id = ? AND asset_id = ?", channelSlot.Channel.ID, assetID).First(&asset).Error; err != nil {
if errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if asset.Topic.TopicSlot.TopicSlotID != topicID {
ErrResponse(w, http.StatusNotFound, errors.New("invalid topic"))
return
}
2022-03-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
// can only update topic if creator
if asset.Topic.GUID != guid {
ErrResponse(w, http.StatusUnauthorized, errors.New("topic not created by you"))
return
}
2022-03-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
// delete asset record
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Delete(&asset).Error; res != nil {
return res
}
if res := tx.Model(&asset.Topic).Update("detail_revision", act.ChannelRevision+1).Error; res != nil {
return res
}
if res := tx.Model(&asset.Topic.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-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
// cleanup files from deleted record
go garbageCollect(act)
2022-03-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
// determine affected contact list
cards := make(map[string]store.Card)
2022-11-11 23:26:43 +00:00
for _, member := range channelSlot.Channel.Members {
cards[member.Card.GUID] = member.Card
2022-07-22 19:28:14 +00:00
}
for _, group := range channelSlot.Channel.Groups {
for _, card := range group.Cards {
cards[card.GUID] = card
}
}
2022-03-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
// notify
SetStatus(act)
for _, card := range cards {
SetContactChannelNotification(act, &card)
}
2022-03-02 21:19:16 +00:00
2022-07-22 19:28:14 +00:00
WriteResponse(w, nil)
2022-03-02 21:19:16 +00:00
}