mirror of
https://github.com/balzack/databag.git
synced 2025-02-12 03:29:16 +00:00
fixing enabled status on channel
This commit is contained in:
parent
81aefff5a7
commit
61ec0b281a
@ -8,11 +8,12 @@ import (
|
|||||||
//GetAccountStatus retrieves account state values
|
//GetAccountStatus retrieves account state values
|
||||||
func GetAccountStatus(w http.ResponseWriter, r *http.Request) {
|
func GetAccountStatus(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
account, code, err := ParamAgentToken(r, true)
|
session, code, err := GetSession(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrResponse(w, code, err)
|
ErrResponse(w, code, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
account := session.Account
|
||||||
|
|
||||||
var assets []store.Asset
|
var assets []store.Asset
|
||||||
if err = store.DB.Where("account_id = ?", account.ID).Find(&assets).Error; err != nil {
|
if err = store.DB.Where("account_id = ?", account.ID).Find(&assets).Error; err != nil {
|
||||||
@ -29,7 +30,7 @@ func GetAccountStatus(w http.ResponseWriter, r *http.Request) {
|
|||||||
status.Disabled = account.Disabled
|
status.Disabled = account.Disabled
|
||||||
status.ForwardingAddress = account.Forward
|
status.ForwardingAddress = account.Forward
|
||||||
status.Searchable = account.Searchable
|
status.Searchable = account.Searchable
|
||||||
status.PushEnabled = account.PushEnabled
|
status.PushEnabled = session.PushEnabled
|
||||||
|
|
||||||
WriteResponse(w, status)
|
WriteResponse(w, status)
|
||||||
}
|
}
|
||||||
|
@ -30,8 +30,19 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// return notification status
|
// get channel entry
|
||||||
WriteResponse(w, account.PushEnabled)
|
channel := store.Channel{}
|
||||||
|
if store.DB.Model(&channel).Where("channel_id = ? AND account_id = ?", channelID, account.ID).First(&channel).Error != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
ErrResponse(w, http.StatusNotFound, err)
|
||||||
|
} else {
|
||||||
|
ErrResponse(w, http.StatusInternalServerError, err)
|
||||||
|
}
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
// retrun notification status
|
||||||
|
WriteResponse(w, 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 {
|
||||||
@ -39,7 +50,7 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// update member notification status
|
// 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 store.DB.Model(&member).Where("channel_id ? AND card_id = ?", channelID, card.ID).First(&member).Error != nil {
|
||||||
if errors.Is(err, gorm.ErrRecordNotFound) {
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
@ -9,7 +9,7 @@ import (
|
|||||||
//SetAccountNotification sets whether notifications should be received
|
//SetAccountNotification sets whether notifications should be received
|
||||||
func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
||||||
|
|
||||||
account, code, err := ParamAgentToken(r, true)
|
session, code, err := GetSession(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ErrResponse(w, code, err)
|
ErrResponse(w, code, err)
|
||||||
return
|
return
|
||||||
@ -22,11 +22,10 @@ func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||||
if res := tx.Model(account).Update("push_enabled", flag).Error; res != nil {
|
if res := tx.Model(session).Update("push_enabled", flag).Error; res != nil {
|
||||||
ErrResponse(w, http.StatusInternalServerError, res)
|
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
if res := tx.Model(&account).Update("account_revision", account.AccountRevision+1).Error; res != nil {
|
if res := tx.Model(session.Account).Update("account_revision", session.Account.AccountRevision+1).Error; res != nil {
|
||||||
return res
|
return res
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
@ -36,6 +35,6 @@ func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
SetStatus(account)
|
SetStatus(&session.Account)
|
||||||
WriteResponse(w, nil)
|
WriteResponse(w, nil)
|
||||||
}
|
}
|
||||||
|
@ -26,10 +26,20 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if code, err := SendPushEvent(card.Account, event); err != nil {
|
||||||
|
ErrResponse(w, code, err);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
WriteResponse(w, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
//SendPushEvent delivers notification to clients
|
||||||
|
func SendPushEvent(account store.Account, event string) (int, error) {
|
||||||
|
|
||||||
messages := []push{}
|
messages := []push{}
|
||||||
if err := store.DB.Model(&store.Session{}).Select("sessions.push_token, push_events.message_title, push_events.message_body").Joins("left join push_events on push_events.session_id = session.id").Where("sessions.account_id = ? AND session.push_enabled = ?", card.Account.ID, true).Scan(messages).Error; err != nil {
|
if err := store.DB.Model(&store.Session{}).Select("sessions.push_token, push_events.message_title, push_events.message_body").Joins("left join push_events on push_events.session_id = session.id").Where("sessions.account_id = ? AND session.push_enabled = ? AND push_events.event = ?", account.ID, true, event).Scan(messages).Error; err != nil {
|
||||||
ErrResponse(w, http.StatusInternalServerError, err)
|
return http.StatusInternalServerError, err
|
||||||
return
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// send push notification for each
|
// send push notification for each
|
||||||
@ -37,6 +47,6 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) {
|
|||||||
PrintMsg(message);
|
PrintMsg(message);
|
||||||
}
|
}
|
||||||
|
|
||||||
WriteResponse(w, nil)
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -95,6 +95,31 @@ func ParamAdminToken(r *http.Request) (int, error) {
|
|||||||
return http.StatusOK, nil
|
return http.StatusOK, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
//ParamAgentToken retrieves account specified by agent query param
|
||||||
|
func GetSession(r *http.Request) (*store.Session, int, error) {
|
||||||
|
|
||||||
|
// parse authentication token
|
||||||
|
target, access, err := ParseToken(r.FormValue("agent"))
|
||||||
|
if err != nil {
|
||||||
|
return nil, http.StatusBadRequest, err
|
||||||
|
}
|
||||||
|
|
||||||
|
// find session record
|
||||||
|
var session store.Session;
|
||||||
|
if err := store.DB.Preload("Account").Where("account_id = ? AND token = ?", target, access).Find(&session).Error; err != nil {
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return nil, http.StatusNotFound, err
|
||||||
|
}
|
||||||
|
return nil, http.StatusInternalServerError, err
|
||||||
|
}
|
||||||
|
|
||||||
|
if session.Account.Disabled {
|
||||||
|
return nil, http.StatusGone, errors.New("account is inactive")
|
||||||
|
}
|
||||||
|
|
||||||
|
return &session, http.StatusOK, nil
|
||||||
|
}
|
||||||
|
|
||||||
//ParamAgentToken retrieves account specified by agent query param
|
//ParamAgentToken retrieves account specified by agent query param
|
||||||
func ParamAgentToken(r *http.Request, detail bool) (*store.Account, int, error) {
|
func ParamAgentToken(r *http.Request, detail bool) (*store.Account, int, error) {
|
||||||
|
|
||||||
|
@ -80,7 +80,6 @@ type Account struct {
|
|||||||
Updated int64 `gorm:"autoUpdateTime"`
|
Updated int64 `gorm:"autoUpdateTime"`
|
||||||
Disabled bool `gorm:"not null;default:false"`
|
Disabled bool `gorm:"not null;default:false"`
|
||||||
Searchable bool `gorm:"not null;default:false"`
|
Searchable bool `gorm:"not null;default:false"`
|
||||||
PushEnabled bool `gorm:"not null;default:true"`
|
|
||||||
Forward string
|
Forward string
|
||||||
AccountDetail AccountDetail
|
AccountDetail AccountDetail
|
||||||
Apps []App
|
Apps []App
|
||||||
|
Loading…
Reference in New Issue
Block a user