Skip to content
This repository has been archived by the owner on Feb 23, 2023. It is now read-only.

Commit

Permalink
Adds a better /pushrules/ GET response.
Browse files Browse the repository at this point in the history
  • Loading branch information
tommie committed Oct 2, 2021
1 parent 4e72dd2 commit abce199
Show file tree
Hide file tree
Showing 3 changed files with 371 additions and 21 deletions.
32 changes: 20 additions & 12 deletions clientapi/routing/routing.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
package routing

import (
"encoding/json"
"net/http"
"strings"

Expand Down Expand Up @@ -531,20 +530,29 @@ func Setup(
).Methods(http.MethodGet, http.MethodPost, http.MethodOptions)

r0mux.Handle("/pushrules/",
httputil.MakeExternalAPI("push_rules", func(req *http.Request) util.JSONResponse {
httputil.MakeAuthAPI("push_rules", userAPI, func(req *http.Request, device *userapi.Device) util.JSONResponse {
const pushRulesType = "m.push_rules"

// TODO: Implement push rules API
res := json.RawMessage(`{
"global": {
"content": [],
"override": [],
"room": [],
"sender": [],
"underride": []
}
}`)
dataReq := userapi.QueryAccountDataRequest{
UserID: device.UserID,
DataType: pushRulesType,
}
var dataRes userapi.QueryAccountDataResponse
if err := userAPI.QueryAccountData(req.Context(), &dataReq, &dataRes); err != nil {
util.GetLogger(req.Context()).WithError(err).Error("userAPI.QueryAccountData failed")
return util.ErrorResponse(err)
}
data, ok := dataRes.GlobalAccountData[pushRulesType]
if !ok {
return util.JSONResponse{
Code: http.StatusNotFound,
JSON: jsonerror.NotFound("data not found"),
}
}
return util.JSONResponse{
Code: http.StatusOK,
JSON: &res,
JSON: data,
}
}),
).Methods(http.MethodGet, http.MethodOptions)
Expand Down
345 changes: 345 additions & 0 deletions userapi/storage/accounts/common/pushrules.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,345 @@
package common

import (
"encoding/json"
"strings"
"text/template"
)

func DefaultPushRules(userID, serverName string) (json.RawMessage, error) {
var sb strings.Builder
if err := pushRulesTmpl.Execute(&sb, map[string]string{"UserID": userID, "ServerName": serverName}); err != nil {
return nil, err
}
return json.RawMessage(sb.String()), nil
}

// pushRulesTmpl is a simple value of m.push_rules. It was
// snapshotted from a default Element Web account on matrix.org.
var pushRulesTmpl = template.Must(template.New("").Parse(`{
"device" : {},
"global" : {
"content" : [
{
"actions" : [
"notify",
{
"set_tweak" : "sound",
"value" : "default"
},
{
"set_tweak" : "highlight"
}
],
"default" : true,
"enabled" : true,
"pattern" : "{{.Device.UserID}}",
"rule_id" : ".m.rule.contains_user_name"
}
],
"override" : [
{
"actions" : [
"dont_notify"
],
"conditions" : [],
"default" : true,
"enabled" : false,
"rule_id" : ".m.rule.master"
},
{
"actions" : [
"dont_notify"
],
"conditions" : [
{
"key" : "content.msgtype",
"kind" : "event_match",
"pattern" : "m.notice"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.suppress_notices"
},
{
"actions" : [
"notify",
{
"set_tweak" : "sound",
"value" : "default"
},
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.member"
},
{
"key" : "content.membership",
"kind" : "event_match",
"pattern" : "invite"
},
{
"key" : "state_key",
"kind" : "event_match",
"pattern" : "@{{.Device.UserID}}:{{.ServerName}}"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.invite_for_me"
},
{
"actions" : [
"dont_notify"
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.member"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.member_event"
},
{
"actions" : [
"notify",
{
"set_tweak" : "sound",
"value" : "default"
},
{
"set_tweak" : "highlight"
}
],
"conditions" : [
{
"kind" : "contains_display_name"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.contains_display_name"
},
{
"actions" : [
"notify",
{
"set_tweak" : "highlight",
"value" : true
}
],
"conditions" : [
{
"key" : "content.body",
"kind" : "event_match",
"pattern" : "@room"
},
{
"key" : "room",
"kind" : "sender_notification_permission"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.roomnotif"
},
{
"actions" : [
"notify",
{
"set_tweak" : "highlight",
"value" : true
}
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.tombstone"
},
{
"key" : "state_key",
"kind" : "event_match",
"pattern" : ""
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.tombstone"
},
{
"actions" : [
"dont_notify"
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.reaction"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.reaction"
}
],
"room" : [],
"sender" : [],
"underride" : [
{
"actions" : [
"notify",
{
"set_tweak" : "sound",
"value" : "ring"
},
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.call.invite"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.call"
},
{
"actions" : [
"notify",
{
"set_tweak" : "sound",
"value" : "default"
},
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"is" : "2",
"kind" : "room_member_count"
},
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.message"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.room_one_to_one"
},
{
"actions" : [
"notify",
{
"set_tweak" : "sound",
"value" : "default"
},
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"is" : "2",
"kind" : "room_member_count"
},
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.encrypted"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.encrypted_room_one_to_one"
},
{
"actions" : [
"notify",
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.message"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.message"
},
{
"actions" : [
"notify",
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "m.room.encrypted"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".m.rule.encrypted"
},
{
"actions" : [
"notify",
{
"set_tweak" : "highlight",
"value" : false
}
],
"conditions" : [
{
"key" : "type",
"kind" : "event_match",
"pattern" : "im.vector.modular.widgets"
},
{
"key" : "content.type",
"kind" : "event_match",
"pattern" : "jitsi"
},
{
"key" : "state_key",
"kind" : "event_match",
"pattern" : "*"
}
],
"default" : true,
"enabled" : true,
"rule_id" : ".im.vector.jitsi"
}
]
}
}`))
Loading

0 comments on commit abce199

Please sign in to comment.