databag/net/server/internal/api_addChannel.go

96 lines
2.6 KiB
Go
Raw Normal View History

2022-02-15 22:30:04 +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-15 22:30:04 +00:00
)
2022-07-25 18:44:33 +00:00
//AddChannel adds a channel to account specified by qgent query param
2022-02-15 22:30:04 +00:00
func AddChannel(w http.ResponseWriter, r *http.Request) {
2022-07-22 19:28:14 +00:00
account, code, err := ParamAgentToken(r, false)
if err != nil {
ErrResponse(w, code, err)
return
}
2022-02-15 22:30:04 +00:00
2022-07-22 19:28:14 +00:00
var params ChannelParams
if err := ParseRequest(r, w, &params); err != nil {
ErrResponse(w, http.StatusBadRequest, err)
return
}
2022-02-15 22:30:04 +00:00
2022-07-22 19:28:14 +00:00
cards := []*store.Card{}
slot := &store.ChannelSlot{}
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-02-15 22:30:04 +00:00
2022-07-22 19:28:14 +00:00
channel := &store.Channel{}
channel.AccountID = account.ID
channel.Data = params.Data
channel.DataType = params.DataType
channel.DetailRevision = account.ChannelRevision + 1
channel.TopicRevision = account.ChannelRevision + 1
if res := tx.Save(channel).Error; res != nil {
return res
}
2022-02-15 22:30:04 +00:00
2022-07-22 19:28:14 +00:00
slot.ChannelSlotID = uuid.New().String()
slot.AccountID = account.ID
slot.ChannelID = channel.ID
slot.Revision = account.ChannelRevision + 1
slot.Channel = channel
if res := tx.Save(slot).Error; res != nil {
return res
}
for _, cardID := range params.Cards {
cardSlot := store.CardSlot{}
if res := tx.Preload("Card").Where("account_id = ? AND card_slot_id = ?", account.ID, cardID).First(&cardSlot).Error; res != nil {
return res
}
2022-11-11 23:26:43 +00:00
member := &store.Member{}
member.ChannelID = channel.ID
member.CardID = cardSlot.Card.ID
member.Card = *cardSlot.Card
member.Channel = channel
member.PushEnabled = true
if res := tx.Save(member).Error; res != nil {
2022-07-22 19:28:14 +00:00
return res
}
cards = append(cards, cardSlot.Card)
}
2022-04-06 21:02:18 +00:00
2022-07-22 19:28:14 +00:00
for _, groupID := range params.Groups {
groupSlot := store.GroupSlot{}
if res := tx.Preload("Group").Where("account_id = ? AND group_slot_id = ?", account.ID, groupID).First(&groupSlot).Error; res != nil {
return res
}
if res := tx.Model(&slot.Channel).Association("Groups").Append(groupSlot.Group); res != nil {
return res
}
}
2022-04-06 21:02:18 +00:00
2022-07-22 19:28:14 +00:00
if res := tx.Model(&account).Update("channel_revision", account.ChannelRevision+1).Error; res != nil {
return res
}
2022-04-06 21:02:18 +00:00
2022-07-22 19:28:14 +00:00
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-02-15 22:30:04 +00:00
2022-07-22 19:28:14 +00:00
SetStatus(account)
for _, card := range cards {
SetContactChannelNotification(account, card)
2022-11-15 06:25:15 +00:00
SetContactPushNotification(card, "content.addChannel." + params.DataType)
2022-07-22 19:28:14 +00:00
}
video := getBoolConfigValue(CNFEnableVideo, true);
audio := getBoolConfigValue(CNFEnableAudio, true);
image := getBoolConfigValue(CNFEnableImage, true);
WriteResponse(w, getChannelModel(slot, true, true, image, audio, video))
2022-02-15 22:30:04 +00:00
}