mirror of
https://github.com/balzack/databag.git
synced 2025-04-22 01:25:17 +00:00
addind endpoints to support notification config
This commit is contained in:
parent
7fde0558bb
commit
887fd65c75
74
doc/api.oa3
74
doc/api.oa3
@ -534,7 +534,35 @@ paths:
|
||||
description: authentication error
|
||||
'500':
|
||||
description: internal server error
|
||||
|
||||
|
||||
/account/notification:
|
||||
put:
|
||||
tags:
|
||||
- account
|
||||
description: Set whether account should receive push notifications.
|
||||
operationId: set-account-notification
|
||||
parameters:
|
||||
- name: agent
|
||||
in: query
|
||||
description: agent token
|
||||
required: false
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
'201':
|
||||
description: success
|
||||
'401':
|
||||
description: permission denied
|
||||
'405':
|
||||
description: failed to confirm
|
||||
'500':
|
||||
description: internal server error
|
||||
requestBody:
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: boolean
|
||||
|
||||
/account/searchable:
|
||||
put:
|
||||
tags:
|
||||
@ -2494,7 +2522,46 @@ paths:
|
||||
description: internal server error
|
||||
|
||||
/content/channels/{channelId}/notification:
|
||||
put:
|
||||
get:
|
||||
tags:
|
||||
- content
|
||||
description: Get notification enabled state for the specified channel
|
||||
operationId: get-channel-notifications
|
||||
parameters:
|
||||
- name: channelId
|
||||
in: path
|
||||
description: specified channel id
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
- name: agent
|
||||
in: query
|
||||
description: access token granted to agent
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
- name: contact
|
||||
in: query
|
||||
description: access token granted to contact
|
||||
required: true
|
||||
schema:
|
||||
type: string
|
||||
responses:
|
||||
'200':
|
||||
description: success
|
||||
content:
|
||||
application/json:
|
||||
schema:
|
||||
type: boolean
|
||||
'401':
|
||||
description: permission denied
|
||||
'404':
|
||||
description: field, channel not found
|
||||
'410':
|
||||
description: account disabled
|
||||
'500':
|
||||
description: internal server error
|
||||
put:
|
||||
tags:
|
||||
- content
|
||||
description: Set notification enabled state for the specified channel
|
||||
@ -3577,6 +3644,7 @@ components:
|
||||
- storageAvailable
|
||||
- forwardingAddress
|
||||
- searchable
|
||||
- pushEnabled
|
||||
properties:
|
||||
disabled:
|
||||
type: boolean
|
||||
@ -3590,6 +3658,8 @@ components:
|
||||
type: string
|
||||
searchable:
|
||||
type: boolean
|
||||
pushEnabled:
|
||||
type: boolean
|
||||
|
||||
AccountProfile:
|
||||
type: object
|
||||
|
@ -29,6 +29,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
|
||||
|
||||
WriteResponse(w, status)
|
||||
}
|
||||
|
58
net/server/internal/api_getChannelNotification.go
Normal file
58
net/server/internal/api_getChannelNotification.go
Normal file
@ -0,0 +1,58 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"databag/internal/store"
|
||||
"errors"
|
||||
"github.com/gorilla/mux"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//GetChannelNotifications gets enabled state of channel
|
||||
func GetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// get referenced channel
|
||||
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)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
|
||||
// return notification status
|
||||
WriteResponse(w, account.PushEnabled)
|
||||
} else if tokenType == APPTokenContact {
|
||||
card, code, err := ParamContactToken(r, true)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
|
||||
// update member notification status
|
||||
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) {
|
||||
ErrResponse(w, http.StatusNotFound, err)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
// return notification status
|
||||
WriteResponse(w, member.PushEnabled)
|
||||
} else {
|
||||
ErrResponse(w, http.StatusUnauthorized, errors.New("invalid access token"))
|
||||
}
|
||||
}
|
41
net/server/internal/api_setAccountNotification.go
Normal file
41
net/server/internal/api_setAccountNotification.go
Normal file
@ -0,0 +1,41 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"databag/internal/store"
|
||||
"gorm.io/gorm"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//SetAccountNotification sets whether notifications should be received
|
||||
func SetAccountNotification(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
account, code, err := ParamAgentToken(r, true)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
return
|
||||
}
|
||||
|
||||
var flag bool
|
||||
if err := ParseRequest(r, w, &flag); err != nil {
|
||||
ErrResponse(w, http.StatusBadRequest, err)
|
||||
return
|
||||
}
|
||||
|
||||
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)
|
||||
return res
|
||||
}
|
||||
if res := tx.Model(&account).Update("account_revision", account.AccountRevision+1).Error; res != nil {
|
||||
return res
|
||||
}
|
||||
return nil
|
||||
})
|
||||
if err != nil {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
|
||||
SetStatus(account)
|
||||
WriteResponse(w, nil)
|
||||
}
|
52
net/server/internal/api_setChannelNotification.go
Normal file
52
net/server/internal/api_setChannelNotification.go
Normal file
@ -0,0 +1,52 @@
|
||||
package databag
|
||||
|
||||
import (
|
||||
"databag/internal/store"
|
||||
"errors"
|
||||
"github.com/gorilla/mux"
|
||||
"net/http"
|
||||
)
|
||||
|
||||
//SetChannelNotifications enables or disables notifcation
|
||||
func SetChannelNotification(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
// get referenced channel
|
||||
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)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
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 {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
return
|
||||
}
|
||||
} else if tokenType == APPTokenContact {
|
||||
card, code, err := ParamContactToken(r, true)
|
||||
if err != nil {
|
||||
ErrResponse(w, code, err)
|
||||
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 {
|
||||
ErrResponse(w, http.StatusInternalServerError, err)
|
||||
}
|
||||
} else {
|
||||
ErrResponse(w, http.StatusUnauthorized, errors.New("invalid access token"))
|
||||
return
|
||||
}
|
||||
}
|
@ -30,6 +30,8 @@ type AccountStatus struct {
|
||||
ForwardingAddress string `json:"forwardingAddress"`
|
||||
|
||||
Searchable bool `json:"searchable"`
|
||||
|
||||
PushEnabled bool `json:"pushEnabled"`
|
||||
}
|
||||
|
||||
//Announce initial message sent on websocket
|
||||
|
@ -181,6 +181,13 @@ var endpoints = routes{
|
||||
SetAccountLogin,
|
||||
},
|
||||
|
||||
route{
|
||||
"SetAccountNotification",
|
||||
strings.ToUpper("Get"),
|
||||
"/account/notification",
|
||||
SetAccountNotification,
|
||||
},
|
||||
|
||||
route{
|
||||
"SetAccountSerchable",
|
||||
strings.ToUpper("Put"),
|
||||
@ -692,6 +699,20 @@ var endpoints = routes{
|
||||
SetChannelGroup,
|
||||
},
|
||||
|
||||
route{
|
||||
"GetChannelNotification",
|
||||
strings.ToUpper("Get"),
|
||||
"/content/channels/{channelID}/notification",
|
||||
GetChannelNotification,
|
||||
},
|
||||
|
||||
route{
|
||||
"SetChannelNotification",
|
||||
strings.ToUpper("Put"),
|
||||
"/content/channels/{channelID}/notification",
|
||||
SetChannelNotification,
|
||||
},
|
||||
|
||||
route{
|
||||
"SetChannelSubject",
|
||||
strings.ToUpper("Put"),
|
||||
|
@ -80,6 +80,7 @@ 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
|
||||
@ -238,7 +239,6 @@ type Channel struct {
|
||||
DetailRevision int64 `gorm:"not null"`
|
||||
DataType string `gorm:"index"`
|
||||
Data string
|
||||
ChannelPush bool
|
||||
HostPush bool
|
||||
Created int64 `gorm:"autoCreateTime"`
|
||||
Updated int64 `gorm:"autoUpdateTime"`
|
||||
|
Loading…
x
Reference in New Issue
Block a user