mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
fixing notification status endpoints
This commit is contained in:
parent
c8f8ef7a97
commit
9959541094
@ -15,13 +15,6 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
params := mux.Vars(r)
|
params := mux.Vars(r)
|
||||||
channelID := params["channelID"]
|
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)
|
tokenType := ParamTokenType(r)
|
||||||
if tokenType == APPTokenAgent {
|
if tokenType == APPTokenAgent {
|
||||||
account, code, err := ParamAgentToken(r, false)
|
account, code, err := ParamAgentToken(r, false)
|
||||||
@ -31,8 +24,8 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
// get channel entry
|
// get channel entry
|
||||||
channel := store.Channel{}
|
slot := store.ChannelSlot{}
|
||||||
if store.DB.Model(&channel).Where("channel_id = ? AND account_id = ?", channelID, account.ID).First(&channel).Error != nil {
|
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) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
ErrResponse(w, http.StatusNotFound, err)
|
ErrResponse(w, http.StatusNotFound, err)
|
||||||
} else {
|
} else {
|
||||||
@ -40,9 +33,13 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if (slot.Channel == nil) {
|
||||||
|
ErrResponse(w, http.StatusNotFound, errors.New("referenced empty channel"));
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
// retrun notification status
|
// return notification status
|
||||||
WriteResponse(w, channel.HostPush)
|
WriteResponse(w, slot.Channel.HostPush)
|
||||||
} else if tokenType == APPTokenContact {
|
} else if tokenType == APPTokenContact {
|
||||||
card, code, err := ParamContactToken(r, true)
|
card, code, err := ParamContactToken(r, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -50,9 +47,24 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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
|
// get member entry
|
||||||
member := store.Member{}
|
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) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
ErrResponse(w, http.StatusNotFound, err)
|
ErrResponse(w, http.StatusNotFound, err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -24,7 +24,7 @@ func SetChannelCard(w http.ResponseWriter, r *http.Request) {
|
|||||||
|
|
||||||
// load referenced channel
|
// load referenced channel
|
||||||
var channelSlot store.ChannelSlot
|
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) {
|
if !errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
ErrResponse(w, http.StatusInternalServerError, err)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
} else {
|
} else {
|
||||||
|
@ -3,6 +3,7 @@ package databag
|
|||||||
import (
|
import (
|
||||||
"databag/internal/store"
|
"databag/internal/store"
|
||||||
"errors"
|
"errors"
|
||||||
|
"gorm.io/gorm"
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"net/http"
|
"net/http"
|
||||||
)
|
)
|
||||||
@ -29,8 +30,23 @@ func SetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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
|
// 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)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@ -41,8 +57,23 @@ func SetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
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
|
// 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)
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
Reference in New Issue
Block a user