From 99595410944d77aec2caf1f677edf70ed71f8888 Mon Sep 17 00:00:00 2001 From: Roland Osborne Date: Mon, 14 Nov 2022 13:29:27 -0800 Subject: [PATCH] fixing notification status endpoints --- .../internal/api_getChannelNotification.go | 36 ++++++++++++------- net/server/internal/api_setChannelCard.go | 2 +- .../internal/api_setChannelNotification.go | 35 ++++++++++++++++-- 3 files changed, 58 insertions(+), 15 deletions(-) diff --git a/net/server/internal/api_getChannelNotification.go b/net/server/internal/api_getChannelNotification.go index 7701467e..1bd1b6a3 100644 --- a/net/server/internal/api_getChannelNotification.go +++ b/net/server/internal/api_getChannelNotification.go @@ -15,13 +15,6 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) { params := mux.Vars(r) channelID := params["channelID"] - // get enabled state - var flag bool - if err := ParseRequest(r, w, &flag); err != nil { - ErrResponse(w, http.StatusBadRequest, err) - return - } - tokenType := ParamTokenType(r) if tokenType == APPTokenAgent { account, code, err := ParamAgentToken(r, false) @@ -31,8 +24,8 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) { } // get channel entry - channel := store.Channel{} - if store.DB.Model(&channel).Where("channel_id = ? AND account_id = ?", channelID, account.ID).First(&channel).Error != nil { + slot := store.ChannelSlot{} + if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, account.ID).First(&slot).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { ErrResponse(w, http.StatusNotFound, err) } else { @@ -40,9 +33,13 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) { } return } + if (slot.Channel == nil) { + ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel")); + return; + } - // retrun notification status - WriteResponse(w, channel.HostPush) + // return notification status + WriteResponse(w, slot.Channel.HostPush) } else if tokenType == APPTokenContact { card, code, err := ParamContactToken(r, true) if err != nil { @@ -50,9 +47,24 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) { return } + // get channel entry + slot := store.ChannelSlot{} + if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, card.Account.ID).First(&slot).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + ErrResponse(w, http.StatusNotFound, err) + } else { + ErrResponse(w, http.StatusInternalServerError, err) + } + return + } + if (slot.Channel == nil) { + ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel")); + return; + } + // get member entry member := store.Member{} - if store.DB.Model(&member).Where("channel_id ? AND card_id = ?", channelID, card.ID).First(&member).Error != nil { + if err := store.DB.Model(&member).Where("channel_id = ? AND card_id = ?", slot.Channel.ID, card.ID).First(&member).Error; err != nil { if errors.Is(err, gorm.ErrRecordNotFound) { ErrResponse(w, http.StatusNotFound, err) } else { diff --git a/net/server/internal/api_setChannelCard.go b/net/server/internal/api_setChannelCard.go index 3e33ea79..0f0ffba9 100644 --- a/net/server/internal/api_setChannelCard.go +++ b/net/server/internal/api_setChannelCard.go @@ -24,7 +24,7 @@ func SetChannelCard(w http.ResponseWriter, r *http.Request) { // load referenced channel var channelSlot store.ChannelSlot - if err := store.DB.Preload("Channel.Cards.CardSlot").Preload("Channel.Groups.GroupSlot").Preload("Channel.Groups.Cards").Where("account_id = ? AND channel_slot_id = ?", account.ID, channelID).First(&channelSlot).Error; err != nil { + 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 { if !errors.Is(err, gorm.ErrRecordNotFound) { ErrResponse(w, http.StatusInternalServerError, err) } else { diff --git a/net/server/internal/api_setChannelNotification.go b/net/server/internal/api_setChannelNotification.go index 9cefc794..7a8ec7a5 100644 --- a/net/server/internal/api_setChannelNotification.go +++ b/net/server/internal/api_setChannelNotification.go @@ -3,6 +3,7 @@ package databag import ( "databag/internal/store" "errors" + "gorm.io/gorm" "github.com/gorilla/mux" "net/http" ) @@ -29,8 +30,23 @@ func SetChannelNotification(w http.ResponseWriter, r *http.Request) { return } + // get channel entry + slot := store.ChannelSlot{} + if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, account.ID).First(&slot).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + ErrResponse(w, http.StatusNotFound, err) + } else { + ErrResponse(w, http.StatusInternalServerError, err) + } + return + } + if (slot.Channel == nil) { + ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel")); + return; + } + // update host notification status - if err = store.DB.Model(&store.Channel{}).Where("account_id = ? AND id = ?", account.ID, channelID).Update("host_push", flag).Error; err != nil { + if err = store.DB.Model(&store.Channel{}).Where("account_id = ? AND id = ?", account.ID, slot.Channel.ID).Update("host_push", flag).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) return } @@ -41,8 +57,23 @@ func SetChannelNotification(w http.ResponseWriter, r *http.Request) { return } + // get channel entry + slot := store.ChannelSlot{} + if err := store.DB.Model(&slot).Preload("Channel").Where("channel_slot_id = ? AND account_id = ?", channelID, card.Account.ID).First(&slot).Error; err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + ErrResponse(w, http.StatusNotFound, err) + } else { + ErrResponse(w, http.StatusInternalServerError, err) + } + return + } + if (slot.Channel == nil) { + ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel")); + return; + } + // update member notification status - if err := store.DB.Model(&store.Member{}).Where("channel_id = ? AND card_id = ?", channelID, card.ID).Update("push_enabled", flag).Error; err != nil { + if err := store.DB.Model(&store.Member{}).Where("channel_id = ? AND card_id = ?", slot.Channel.ID, card.ID).Update("push_enabled", flag).Error; err != nil { ErrResponse(w, http.StatusInternalServerError, err) } } else {