databag/net/server/internal/api_setChannelCard.go

106 lines
3.2 KiB
Go
Raw Normal View History

2022-02-16 08:00:07 +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-16 08:00:07 +00:00
)
2022-07-29 21:57:25 +00:00
//SetChannelCard adds contact to channel membership
2022-02-16 08:00:07 +00:00
func SetChannelCard(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-16 08:00:07 +00:00
2022-07-22 19:28:14 +00:00
// scan parameters
params := mux.Vars(r)
channelID := params["channelID"]
cardID := params["cardID"]
2022-02-16 08:00:07 +00:00
2022-07-22 19:28:14 +00:00
// load referenced channel
var channelSlot store.ChannelSlot
2022-11-14 21:29:27 +00:00
if err := store.DB.Preload("Channel.Members.Card.CardSlot").Preload("Channel.Groups.GroupSlot").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelID).First(&channelSlot).Error; err != nil {
2022-07-22 19:28:14 +00:00
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if channelSlot.Channel == nil {
ErrResponse(w, http.StatusNotFound, errors.New("channel has been deleted"))
return
}
2022-02-16 08:00:07 +00:00
2022-07-22 19:28:14 +00:00
// load referenced card
var cardSlot store.CardSlot
if err := store.DB.Preload("Card.CardSlot").Where("account_id = ? AND card_slot_id = ?", account.ID, cardID).First(&cardSlot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
ErrResponse(w, http.StatusNotFound, err)
}
return
}
if cardSlot.Card == nil {
ErrResponse(w, http.StatusNotFound, errors.New("card has been deleted"))
return
}
2022-02-16 08:00:07 +00:00
2022-07-22 19:28:14 +00:00
// determine contact list
cards := 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-07-22 19:28:14 +00:00
}
for _, group := range channelSlot.Channel.Groups {
for _, card := range group.Cards {
cards[card.GUID] = card
}
}
cards[cardSlot.Card.GUID] = *cardSlot.Card
2022-02-17 08:30:33 +00:00
2022-07-22 19:28:14 +00:00
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
2022-11-11 23:26:43 +00:00
member := &store.Member{}
member.ChannelID = channelSlot.Channel.ID
member.CardID = cardSlot.Card.ID
member.Channel = channelSlot.Channel
member.Card = *cardSlot.Card
member.PushEnabled = true
if res := tx.Save(member).Error; res != nil {
2022-07-22 19:28:14 +00:00
return res
}
2022-11-11 23:26:43 +00:00
if res := tx.Model(&store.Channel{}).Where("id = ?", channelSlot.Channel.ID).Update("detail_revision", account.ChannelRevision+1).Error; res != nil {
2022-07-22 19:28:14 +00:00
return res
}
2022-11-11 23:26:43 +00:00
if res := tx.Model(&store.ChannelSlot{}).Where("id = ?", channelSlot.ID).Update("revision", account.ChannelRevision+1).Error; res != nil {
2022-07-22 19:28:14 +00:00
return res
}
if res := tx.Model(&account).Update("channel_revision", account.ChannelRevision+1).Error; res != nil {
return res
}
return nil
})
if err != nil {
ErrResponse(w, http.StatusInternalServerError, err)
return
}
2022-02-16 08:00:07 +00:00
2022-07-22 19:28:14 +00:00
// notify contacts of content change
SetStatus(account)
for _, card := range cards {
SetContactChannelNotification(account, &card)
}
2022-11-15 06:25:15 +00:00
SetContactPushNotification(cardSlot.Card, "content.addChannel." + channelSlot.Channel.DataType)
video := getBoolConfigValue(CNFEnableVideo, true);
audio := getBoolConfigValue(CNFEnableAudio, true);
image := getBoolConfigValue(CNFEnableImage, true);
WriteResponse(w, getChannelModel(&channelSlot, true, true, image, audio, video))
2022-02-16 08:00:07 +00:00
}