2022-02-19 06:33:13 +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-19 06:33:13 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func SetChannelTopicConfirmed(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-19 06:33:13 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
var status string
|
|
|
|
if err := ParseRequest(r, w, &status); err != nil {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if !AppTopicStatus(status) {
|
|
|
|
ErrResponse(w, http.StatusBadRequest, errors.New("unknown status"))
|
|
|
|
return
|
|
|
|
}
|
2022-02-19 06:33:13 +00:00
|
|
|
|
2022-07-27 05:43:39 +00:00
|
|
|
channelSlot, _, 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-02-19 06:33:13 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
// load topic
|
|
|
|
var topicSlot store.TopicSlot
|
|
|
|
if err = store.DB.Preload("Topic").Where("channel_id = ? AND topic_slot_id = ?", channelSlot.Channel.ID, topicID).First(&topicSlot).Error; err != nil {
|
|
|
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
|
|
|
ErrResponse(w, http.StatusNotFound, err)
|
|
|
|
} else {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if topicSlot.Topic == nil {
|
|
|
|
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty slot"))
|
|
|
|
return
|
|
|
|
}
|
2022-02-19 06:33:13 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
|
|
|
if res := tx.Model(&topicSlot.Topic).Update("status", status).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
if res := tx.Model(&topicSlot.Topic).Update("detail_revision", act.ChannelRevision+1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
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-02-19 06:33:13 +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-02-19 06:33:13 +00:00
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
SetStatus(act)
|
|
|
|
for _, card := range cards {
|
|
|
|
SetContactChannelNotification(act, &card)
|
|
|
|
}
|
|
|
|
WriteResponse(w, getTopicModel(&topicSlot))
|
2022-02-19 06:33:13 +00:00
|
|
|
}
|