mirror of
https://github.com/matrix-org/dendrite.git
synced 2025-12-18 12:23:09 -06:00
Implement pushrules API
This commit is contained in:
parent
c31cb02271
commit
c9e1c0cb19
175
clientapi/routing/pushrules.go
Normal file
175
clientapi/routing/pushrules.go
Normal file
|
|
@ -0,0 +1,175 @@
|
||||||
|
package routing
|
||||||
|
|
||||||
|
import (
|
||||||
|
"encoding/json"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/authtypes"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/auth/storage/accounts"
|
||||||
|
"github.com/matrix-org/dendrite/clientapi/jsonerror"
|
||||||
|
"github.com/matrix-org/gomatrixserverlib"
|
||||||
|
"github.com/matrix-org/util"
|
||||||
|
"net/http"
|
||||||
|
)
|
||||||
|
|
||||||
|
type PushCondition struct {
|
||||||
|
Kind string `json:"kind"`
|
||||||
|
Key string `json:"key"`
|
||||||
|
Pattern string `json:"pattern"`
|
||||||
|
Is string `json:"is"`
|
||||||
|
}
|
||||||
|
|
||||||
|
type PushRule struct {
|
||||||
|
Actions interface{} `json:"actions"`
|
||||||
|
Default bool `json:"default"`
|
||||||
|
Enabled bool `json:"enabled"`
|
||||||
|
RuleId string `json:"rule_id"`
|
||||||
|
Conditions []PushCondition `json:"conditions"`
|
||||||
|
Pattern string `json:"pattern"`
|
||||||
|
}
|
||||||
|
type Global struct {
|
||||||
|
Content []PushRule `json:"content"`
|
||||||
|
Override []PushRule `json:"override"`
|
||||||
|
Room []PushRule `json:"room"`
|
||||||
|
Sender []PushRule `json:"sender"`
|
||||||
|
Underride []PushRule `json:"underride"`
|
||||||
|
}
|
||||||
|
type RuleSet struct {
|
||||||
|
Global Global `json:"global"`
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPushRuleSet(
|
||||||
|
req *http.Request,
|
||||||
|
dev *authtypes.Device,
|
||||||
|
accountDB accounts.Database,
|
||||||
|
) util.JSONResponse {
|
||||||
|
localpart, _, err := gomatrixserverlib.SplitID('@', dev.UserID)
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
data, err := accountDB.GetAccountDataByType(req.Context(), localpart, "", "m.push_rules")
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("m.push_rules data not found")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
pushRuleSet := RuleSet{Global{
|
||||||
|
Content: []PushRule{},
|
||||||
|
Override: []PushRule{},
|
||||||
|
Room: []PushRule{},
|
||||||
|
Sender: []PushRule{},
|
||||||
|
Underride: []PushRule{},
|
||||||
|
}}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data.Content, &pushRuleSet)
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("Could not unmarshal pushrules data")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: pushRuleSet,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func GetPushRule(
|
||||||
|
req *http.Request, dev *authtypes.Device, accountDB accounts.Database,
|
||||||
|
kind string, ruleID string,
|
||||||
|
) util.JSONResponse {
|
||||||
|
localpart, _, err := gomatrixserverlib.SplitID('@', dev.UserID)
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("gomatrixserverlib.SplitID failed")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
data, err := accountDB.GetAccountDataByType(req.Context(), localpart, "", "m.push_rules")
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("m.push_rules data not found")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
pushRuleSet := RuleSet{Global{
|
||||||
|
Content: []PushRule{},
|
||||||
|
Override: []PushRule{},
|
||||||
|
Room: []PushRule{},
|
||||||
|
Sender: []PushRule{},
|
||||||
|
Underride: []PushRule{},
|
||||||
|
}}
|
||||||
|
|
||||||
|
err = json.Unmarshal(data.Content, &pushRuleSet)
|
||||||
|
if err != nil {
|
||||||
|
util.GetLogger(req.Context()).WithError(err).Error("Could not unmarshal pushrules data")
|
||||||
|
return jsonerror.InternalServerError()
|
||||||
|
}
|
||||||
|
|
||||||
|
switch kind {
|
||||||
|
case "override":
|
||||||
|
for _, pushRule := range pushRuleSet.Global.Override {
|
||||||
|
if pushRule.RuleId == ruleID {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: pushRule,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
JSON: jsonerror.NotFound("Not found"),
|
||||||
|
}
|
||||||
|
case "underride":
|
||||||
|
for _, pushRule := range pushRuleSet.Global.Underride {
|
||||||
|
if pushRule.RuleId == ruleID {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: pushRule,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
JSON: jsonerror.NotFound("Not found"),
|
||||||
|
}
|
||||||
|
case "sender":
|
||||||
|
for _, pushRule := range pushRuleSet.Global.Sender {
|
||||||
|
if pushRule.RuleId == ruleID {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: pushRule,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
JSON: jsonerror.NotFound("Not found"),
|
||||||
|
}
|
||||||
|
case "room":
|
||||||
|
for _, pushRule := range pushRuleSet.Global.Room {
|
||||||
|
if pushRule.RuleId == ruleID {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: pushRule,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
JSON: jsonerror.NotFound("Not found"),
|
||||||
|
}
|
||||||
|
case "content":
|
||||||
|
for _, pushRule := range pushRuleSet.Global.Content {
|
||||||
|
if pushRule.RuleId == ruleID {
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusOK,
|
||||||
|
JSON: pushRule,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusNotFound,
|
||||||
|
JSON: jsonerror.NotFound("Not found"),
|
||||||
|
}
|
||||||
|
default:
|
||||||
|
return util.JSONResponse{
|
||||||
|
Code: http.StatusBadRequest,
|
||||||
|
JSON: jsonerror.NotFound("Unrecognised request"),
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -15,7 +15,6 @@
|
||||||
package routing
|
package routing
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
|
|
@ -255,21 +254,19 @@ func Setup(
|
||||||
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)
|
||||||
|
|
||||||
r0mux.Handle("/pushrules/",
|
r0mux.Handle("/pushrules/",
|
||||||
common.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse {
|
common.MakeAuthAPI("push_rules", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||||
// TODO: Implement push rules API
|
|
||||||
res := json.RawMessage(`{
|
return GetPushRuleSet(req, device, accountDB)
|
||||||
"global": {
|
}),
|
||||||
"content": [],
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
"override": [],
|
|
||||||
"room": [],
|
r0mux.Handle("/pushrules/global/{kind}/{ruleId}",
|
||||||
"sender": [],
|
common.MakeAuthAPI("push_rules", authData, func(req *http.Request, device *authtypes.Device) util.JSONResponse {
|
||||||
"underride": []
|
vars, err := common.URLDecodeMapValues(mux.Vars(req))
|
||||||
}
|
if err != nil {
|
||||||
}`)
|
return util.ErrorResponse(err)
|
||||||
return util.JSONResponse{
|
|
||||||
Code: http.StatusOK,
|
|
||||||
JSON: &res,
|
|
||||||
}
|
}
|
||||||
|
return GetPushRule(req, device, accountDB, vars["kind"], vars["ruleId"])
|
||||||
}),
|
}),
|
||||||
).Methods(http.MethodGet, http.MethodOptions)
|
).Methods(http.MethodGet, http.MethodOptions)
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue