databag/net/server/internal/api_removeChannel.go

150 lines
4.3 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:39:39 +00:00
//RemoveChannel removes channel from account
2022-03-03 08:46:32 +00:00
func RemoveChannel(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
var err error
var code int
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
channelID := params["channelID"]
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// validate contact access
var account *store.Account
var contact *store.Card
tokenType := ParamTokenType(r)
if tokenType == APPTokenAgent {
account, code, err = ParamAgentToken(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
} else if tokenType == APPTokenContact {
contact, code, err = ParamContactToken(r, true)
if err != nil {
ErrResponse(w, code, err)
return
}
account = &contact.Account
} else {
err = errors.New("unknown token type")
code = http.StatusBadRequest
2022-07-22 20:23:17 +00:00
ErrResponse(w, code, err)
2022-07-22 19:28:14 +00:00
return
}
2022-03-03 08:46:32 +00:00
2022-07-22 19:28:14 +00:00
// load channel
var slot store.ChannelSlot
2022-11-11 23:26:43 +00:00
if err = store.DB.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) {
ErrResponse(w, http.StatusNotFound, err)
} else {
ErrResponse(w, http.StatusInternalServerError, err)
}
return
}
if slot.Channel == nil {
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"))
return
}
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)
2022-11-11 23:26:43 +00:00
for _, member := range slot.Channel.Members {
cards[member.Card.GUID] = member.Card
2022-07-22 19:28:14 +00:00
}
for _, group := range slot.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
if contact == nil {
err = store.DB.Transaction(func(tx *gorm.DB) error {
if res := tx.Model(&slot.Channel).Association("Groups").Clear(); res != nil {
return res
}
slot.Channel.Groups = []store.Group{}
2022-11-15 18:04:45 +00:00
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Member{}).Error; res != nil {
return res
}
2022-11-11 23:26:43 +00:00
slot.Channel.Members = []store.Member{}
2022-07-22 19:28:14 +00:00
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Tag{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.TagSlot{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Asset{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.Topic{}).Error; res != nil {
return res
}
if res := tx.Where("channel_id = ?", slot.Channel.ID).Delete(&store.TopicSlot{}).Error; res != nil {
return res
}
slot.Channel.Topics = []store.Topic{}
if res := tx.Delete(&slot.Channel).Error; res != nil {
return res
}
slot.Channel = nil
if res := tx.Model(&slot).Update("channel_id", 0).Error; res != nil {
return res
}
if res := tx.Model(&slot).Update("revision", account.ChannelRevision+1).Error; res != nil {
return res
}
if res := tx.Model(account).Update("channel_revision", account.ChannelRevision+1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-05-12 06:33:00 +00:00
2022-07-22 19:28:14 +00:00
// cleanup file assets
go garbageCollect(account)
} else {
if _, member := cards[contact.GUID]; !member {
ErrResponse(w, http.StatusNotFound, errors.New("member channel not found"))
return
}
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-11-11 23:26:43 +00:00
if res := tx.Where("channel_id = ? AND card_id = ?", slot.Channel.ID, contact.ID).Delete(&store.Member{}).Error; res != nil {
2022-07-22 19:28:14 +00:00
return res
}
2022-11-11 23:26:43 +00:00
if res := tx.Model(&store.Channel{}).Where("id = ?", slot.Channel.ID).Update("detail_revision", account.ChannelRevision+1).Error; res != nil {
return res
}
if res := tx.Model(&store.ChannelSlot{}).Where("id = ?", slot.ID).Update("revision", account.ChannelRevision+1).Error; res != nil {
return res
}
2022-07-22 19:28:14 +00:00
if res := tx.Model(&account).Update("channel_revision", account.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
SetStatus(account)
for _, card := range cards {
SetContactChannelNotification(account, &card)
}
WriteResponse(w, nil)
2022-03-03 08:46:32 +00:00
}