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
|
||||
func GetAccountStatus(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
account, code, err := ParamAgentToken(r, true)
|
||||
session, code, err := GetSession(r)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
account := session.Account
|
||||
|
||||
var assets []store.Asset
|
||||
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.ForwardingAddress = account.Forward
|
||||
status.Searchable = account.Searchable
|
||||
status.PushEnabled = account.PushEnabled
|
||||
status.PushEnabled = session.PushEnabled
|
||||
|
||||
WriteResponse(w, status)
|
||||
}
|
||||
|
@ -30,8 +30,19 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// return notification status
|
||||
WriteResponse(w, account.PushEnabled)
|
||||
// get channel entry
|
||||
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 {
|
||||
card, code, err := ParamContactToken(r, true)
|
||||
if err != nil {
|
||||
@ -39,7 +50,7 @@ func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// update member notification status
|
||||
// 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 errors.Is(err, gorm.ErrRecordNotFound) {
|
||||
|
@ -9,7 +9,7 @@ import (
|
||||
//SetAccountNotification sets whether notifications should be received
|
||||
func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
account, code, err := ParamAgentToken(r, true)
|
||||
session, code, err := GetSession(r)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
@ -22,11 +22,10 @@ func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
err = store.DB.Transaction(func(tx *gorm.DB) error {
|
||||
if res := tx.Model(account).Update("push_enabled", flag).Error; res != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, res)
|
||||
if res := tx.Model(session).Update("push_enabled", flag).Error; res != nil {
|
||||
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 nil
|
||||
@ -36,6 +35,6 @@ func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
SetStatus(account)
|
||||
SetStatus(&session.Account)
|
||||
WriteResponse(w, nil)
|
||||
}
|
||||
|
@ -26,10 +26,20 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) {
|
||||
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{}
|
||||
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 {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
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 {
|
||||
return http.StatusInternalServerError, err
|
||||
}
|
||||
|
||||
// send push notification for each
|
||||
@ -37,6 +47,6 @@ func SetPushEvent(w http.ResponseWriter, r *http.Request) {
|
||||
PrintMsg(message);
|
||||
}
|
||||
|
||||
WriteResponse(w, nil)
|
||||
return http.StatusOK, nil
|
||||
}
|
||||
|
||||
|
@ -95,6 +95,31 @@ func ParamAdminToken(r *http.Request) (int, error) {
|
||||
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
|
||||
func ParamAgentToken(r *http.Request, detail bool) (*store.Account, int, error) {
|
||||
|
||||
|
@ -80,7 +80,6 @@ type Account struct {
|
||||
Updated int64 `gorm:"autoUpdateTime"`
|
||||
Disabled bool `gorm:"not null;default:false"`
|
||||
Searchable bool `gorm:"not null;default:false"`
|
||||
PushEnabled bool `gorm:"not null;default:true"`
|
||||
Forward string
|
||||
AccountDetail AccountDetail
|
||||
Apps []App
|
||||
|
Loading…
Reference in New Issue
Block a user