sending card notification to specific channels

This commit is contained in:
Roland Osborne 2022-02-12 08:28:36 -08:00
parent ea17a74c99
commit 9533f2035d
2 changed files with 11 additions and 37 deletions

View File

@ -22,7 +22,7 @@ func RemoveCard(w http.ResponseWriter, r *http.Request) {
// load referenced card
var slot store.CardSlot
if err := store.DB.Preload("Card.Groups").Preload("Card.Channels.ChannelSlot").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).First(&slot).Error; err != nil {
if err := store.DB.Preload("Card.Groups").Preload("Card.Channels.Cards").Preload("Card.Channels.ChannelSlot").Where("account_id = ? AND card_slot_id = ?", account.ID, cardId).First(&slot).Error; err != nil {
if !errors.Is(err, gorm.ErrRecordNotFound) {
ErrResponse(w, http.StatusInternalServerError, err)
} else {
@ -35,6 +35,9 @@ func RemoveCard(w http.ResponseWriter, r *http.Request) {
return
}
// cards to update
cards := make(map[string]*store.Card)
// save and update contact revision
err = store.DB.Transaction(func(tx *gorm.DB) error {
for _, channel := range slot.Card.Channels {
@ -44,6 +47,9 @@ func RemoveCard(w http.ResponseWriter, r *http.Request) {
if res := tx.Model(&channel.ChannelSlot).Update("revision", account.ChannelRevision + 1).Error; res != nil {
return res
}
for _, card := range channel.Cards {
cards[card.Guid] = &card
}
}
if res := tx.Model(&slot.Card).Association("Groups").Clear(); res != nil {
return res
@ -71,7 +77,9 @@ func RemoveCard(w http.ResponseWriter, r *http.Request) {
return
}
SetChannelNotification(account)
for _, card := range cards {
SetContactChannelNotification(account, card)
}
SetStatus(account)
WriteResponse(w, nil);
}

View File

@ -161,8 +161,7 @@ func SetContactViewNotification(account *store.Account, card *store.Card) {
}
// notify single card of channel change:
// for each card of groups in updated channel data
// for each card of group set or cleared from channel (updates data)
// for each card in updated channel data
func SetContactChannelNotification(account *store.Account, card *store.Card) {
if card.Status != APP_CARDCONNECTED {
@ -184,36 +183,3 @@ func SetContactChannelNotification(account *store.Account, card *store.Card) {
}
}
// notify all cards of channel change
// when card is deleted
func SetChannelNotification(account *store.Account) {
// select all connected cards
var cards []store.Card
if err := store.DB.Where("account_id = ? AND status = ?", account.Guid, APP_CARDCONNECTED).Find(&cards).Error; err != nil {
ErrMsg(err)
return
}
// add new notification for each card
err := store.DB.Transaction(func(tx *gorm.DB) error {
for _, card := range cards {
notification := &store.Notification{
Node: card.Node,
Module: APP_NOTIFYCHANNEL,
Token: card.OutToken,
Revision: account.ChannelRevision,
}
if err := tx.Save(notification).Error; err != nil {
return err
}
notify <- notification
}
return nil
})
if err != nil {
ErrMsg(err)
}
}