databag/net/server/internal/api_setChannelTopicSubject.go

106 lines
2.8 KiB
Go
Raw Normal View History

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
)
2022-07-29 21:57:25 +00:00
//SetChannelTopicSubject sets subject of channel topic created by invoker
2022-02-19 06:33:13 +00:00
func SetChannelTopicSubject(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
topicID := params["topicID"]
confirm := r.FormValue("confirm")
2022-02-19 06:33:13 +00:00
2022-07-22 19:28:14 +00:00
var subject Subject
if err := ParseRequest(r, w, &subject); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
2022-02-19 06:33:13 +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-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
}
2022-02-19 06:33:13 +00:00
2022-07-22 19:28:14 +00:00
// can only update subject if creator
if topicSlot.Topic.GUID != guid {
ErrResponse(w, http.StatusUnauthorized, errors.New("topic not created by you"))
return
}
2022-02-28 22:59:29 +00:00
2022-07-22 19:28:14 +00:00
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-02-19 06:33:13 +00:00
2022-07-22 19:28:14 +00:00
if res := tx.Model(topicSlot.Topic).Update("data", subject.Data).Error; res != nil {
return res
}
if res := tx.Model(topicSlot.Topic).Update("data_type", subject.DataType).Error; res != nil {
return res
}
if confirm == "true" {
if res := tx.Model(topicSlot.Topic).Update("status", APPTopicConfirmed).Error; res != nil {
return res
}
}
if confirm == "false" {
2022-07-22 20:55:26 +00:00
if res := tx.Model(topicSlot.Topic).Update("status", APPTopicUnconfirmed).Error; res != nil {
2022-07-22 19:28:14 +00:00
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
}