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
|
|
|
)
|
|
|
|
|
2022-07-25 18:44:33 +00:00
|
|
|
//AddChannelTopic adds a topic to a channel through either contact or agent query param
|
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-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-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-03-03 08:03:32 +00:00
|
|
|
|
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-03-03 08:03:32 +00:00
|
|
|
|
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)
|
2022-11-15 18:42:16 +00:00
|
|
|
notify := make(map[string]store.Card)
|
2022-11-11 23:26:43 +00:00
|
|
|
for _, member := range channelSlot.Channel.Members {
|
|
|
|
cards[member.Card.GUID] = member.Card
|
2022-11-15 18:42:16 +00:00
|
|
|
if member.PushEnabled && member.Card.GUID != guid {
|
|
|
|
notify[member.Card.GUID] = member.Card
|
|
|
|
}
|
2022-07-22 19:28:14 +00:00
|
|
|
}
|
|
|
|
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)
|
|
|
|
}
|
2022-11-15 18:42:16 +00:00
|
|
|
for _, card := range notify {
|
|
|
|
SetContactPushNotification(&card, "content.addChannelTopic." + channelSlot.Channel.DataType)
|
|
|
|
}
|
2022-11-15 20:31:10 +00:00
|
|
|
if act.GUID != guid {
|
|
|
|
go SendPushEvent(*act, "content.addChannelTopic." + channelSlot.Channel.DataType)
|
|
|
|
}
|
|
|
|
|
2022-07-22 19:28:14 +00:00
|
|
|
WriteResponse(w, getTopicModel(topicSlot))
|
2022-02-18 07:56:30 +00:00
|
|
|
}
|