databag/net/server/internal/api_addChannelTopic.go

92 lines
2.2 KiB
Go
Raw Normal View History

2022-02-18 07:56:30 +00:00
package databag
import (
2022-07-22 19:28:14 +00:00
"databag/internal/store"
"github.com/google/uuid"
"gorm.io/gorm"
"net/http"
2022-02-18 07:56:30 +00:00
)
func AddChannelTopic(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
confirm := r.FormValue("confirm")
2022-03-01 18:35:14 +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-18 07:56:30 +00:00
2022-07-22 19:28:14 +00:00
channelSlot, guid, err, code := getChannelSlot(r, true)
if err != nil {
ErrResponse(w, code, err)
return
}
act := &channelSlot.Account
2022-02-18 07:56:30 +00:00
2022-07-22 19:28:14 +00:00
topicSlot := &store.TopicSlot{}
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-02-18 07:56:30 +00:00
2022-07-22 19:28:14 +00:00
topicSlot.TopicSlotID = uuid.New().String()
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-07-22 19:28:14 +00:00
topic := &store.Topic{}
topic.AccountID = act.ID
topic.ChannelID = channelSlot.Channel.ID
topic.TopicSlotID = topicSlot.ID
topic.Data = subject.Data
topic.DataType = subject.DataType
topic.GUID = guid
topic.DetailRevision = act.ChannelRevision + 1
topic.TagRevision = act.ChannelRevision + 1
if confirm == "true" {
topic.Status = APPTopicConfirmed
} else {
2022-07-22 20:55:26 +00:00
topic.Status = APPTopicUnconfirmed
2022-07-22 19:28:14 +00:00
}
if res := tx.Save(topic).Error; res != nil {
return res
}
2022-07-22 19:28:14 +00:00
topicSlot.Topic = topic
2022-02-18 07:56:30 +00:00
2022-07-22 19:28:14 +00:00
// update parent revision
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-18 07:56:30 +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-18 07:56:30 +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-18 07:56:30 +00:00
}