2022-02-18 07:56:30 +00:00
|
|
|
package databag
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
"github.com/google/uuid"
|
|
|
|
"databag/internal/store"
|
|
|
|
)
|
|
|
|
|
|
|
|
func AddChannelTopic(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
2022-03-01 18:35:14 +00:00
|
|
|
confirm := r.FormValue("confirm")
|
|
|
|
|
2022-02-18 07:56:30 +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
|
|
|
channelSlot, guid, err, code := getChannelSlot(r, true)
|
2022-02-18 20:21:15 +00:00
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, code, err)
|
2022-02-18 07:56:30 +00:00
|
|
|
return
|
|
|
|
}
|
2022-02-18 20:21:15 +00:00
|
|
|
act := &channelSlot.Account
|
2022-02-18 07:56:30 +00:00
|
|
|
|
|
|
|
topicSlot := &store.TopicSlot{}
|
2022-02-18 20:21:15 +00:00
|
|
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
2022-02-18 07:56:30 +00:00
|
|
|
|
2022-07-22 17:15:44 +00:00
|
|
|
topicSlot.TopicSlotID = uuid.New().String()
|
2022-03-03 08:03:32 +00:00
|
|
|
topicSlot.AccountID = act.ID
|
|
|
|
topicSlot.ChannelID = channelSlot.Channel.ID
|
|
|
|
topicSlot.Revision = act.ChannelRevision + 1
|
|
|
|
if res := tx.Save(topicSlot).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-02-18 07:56:30 +00:00
|
|
|
topic := &store.Topic{}
|
2022-03-10 06:18:37 +00:00
|
|
|
topic.AccountID = act.ID
|
2022-03-03 05:46:21 +00:00
|
|
|
topic.ChannelID = channelSlot.Channel.ID
|
2022-03-03 08:03:32 +00:00
|
|
|
topic.TopicSlotID = topicSlot.ID
|
2022-02-18 07:56:30 +00:00
|
|
|
topic.Data = subject.Data
|
|
|
|
topic.DataType = subject.DataType
|
2022-07-22 17:15:44 +00:00
|
|
|
topic.GUID = guid
|
2022-02-18 07:56:30 +00:00
|
|
|
topic.DetailRevision = act.ChannelRevision + 1
|
|
|
|
topic.TagRevision = act.ChannelRevision + 1
|
2022-03-01 18:35:14 +00:00
|
|
|
if confirm == "true" {
|
2022-07-22 17:52:13 +00:00
|
|
|
topic.Status = APPTopicConfirmed
|
2022-03-01 18:35:14 +00:00
|
|
|
} else {
|
2022-07-22 17:52:13 +00:00
|
|
|
topic.Status = AppTopicUnconfirmed
|
2022-03-01 18:35:14 +00:00
|
|
|
}
|
2022-02-18 07:56:30 +00:00
|
|
|
if res := tx.Save(topic).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-03-03 08:03:32 +00:00
|
|
|
|
2022-02-18 07:56:30 +00:00
|
|
|
topicSlot.Topic = topic
|
|
|
|
|
|
|
|
// update parent revision
|
2022-03-04 20:03:33 +00:00
|
|
|
if res := tx.Model(&channelSlot.Channel).Update("topic_revision", act.ChannelRevision + 1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-02-18 07:56:30 +00:00
|
|
|
if res := tx.Model(&channelSlot).Update("revision", act.ChannelRevision + 1).Error; res != nil {
|
|
|
|
return res
|
|
|
|
}
|
2022-02-18 20:21:15 +00:00
|
|
|
if res := tx.Model(act).Update("channel_revision", act.ChannelRevision + 1).Error; res != nil {
|
2022-02-18 07:56:30 +00:00
|
|
|
return res
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
ErrResponse(w, http.StatusInternalServerError, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// determine affected contact list
|
|
|
|
cards := make(map[string]store.Card)
|
|
|
|
for _, card := range channelSlot.Channel.Cards {
|
2022-07-22 17:15:44 +00:00
|
|
|
cards[card.GUID] = card
|
2022-02-18 07:56:30 +00:00
|
|
|
}
|
|
|
|
for _, group := range channelSlot.Channel.Groups {
|
|
|
|
for _, card := range group.Cards {
|
2022-07-22 17:15:44 +00:00
|
|
|
cards[card.GUID] = card
|
2022-02-18 07:56:30 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
SetStatus(act)
|
|
|
|
for _, card := range cards {
|
|
|
|
SetContactChannelNotification(act, &card)
|
|
|
|
}
|
2022-02-18 20:21:15 +00:00
|
|
|
WriteResponse(w, getTopicModel(topicSlot))
|
2022-02-18 07:56:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|