mirror of
https://github.com/pushbits/server.git
synced 2025-04-29 18:26:49 +02:00
move errors to new package to break import cycle
This commit is contained in:
parent
5f5d941bc4
commit
dafc815be5
8 changed files with 195 additions and 20 deletions
168
app/test_notifications.py
Normal file
168
app/test_notifications.py
Normal file
|
@ -0,0 +1,168 @@
|
|||
import requests, json
|
||||
|
||||
token = 'AxZTwBZXs2YK1J5IZa0fyCcfHz9cYM9VjaO627eMSbAXAMdwFluGZSOTB4Vk3RHY'
|
||||
|
||||
url = 'https://push.remote.alexanderebhart.de/message?token=' + token
|
||||
headers = {'content-type': 'application/json'}
|
||||
|
||||
messages = [
|
||||
{
|
||||
"message": "<hr>",
|
||||
"title": "NEW TEST",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"message": "line <br> break",
|
||||
"title": "Linebreak single br",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"message": "line \n break",
|
||||
"title": "Linebreak single n",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"message": """line
|
||||
break""",
|
||||
"title": "Linebreak native",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"message": "# Heading 1\n\n###### Heading 6\n\nAlt-H1\n=====\n\n**bold**\n\n_curly_\n\n~~Strike through~~\n\n[Link](google.com)\n\n* list1 \n* list2\n\n1. another list\n2. numerated\n\n\n\n`some inline code`\n\n```\nA code block\n```\n\n> Blockquote",
|
||||
"title": "Markdown styling",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/markdown"
|
||||
}
|
||||
}
|
||||
},
|
||||
{
|
||||
"message": "<font data-mx-color='#AA0000'>Red</font>",
|
||||
"title": "HTML",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
}
|
||||
},
|
||||
]
|
||||
|
||||
priorities = [
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "Prio 0",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
"priority": 0
|
||||
},
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "Prio 3",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
"priority": 3
|
||||
},
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "Prio 4",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
"priority": 4
|
||||
},
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "Prio 15",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
"priority": 15
|
||||
},
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "Prio 21",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
"priority": 21
|
||||
},
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "Prio -5",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
"priority": -5
|
||||
},
|
||||
{
|
||||
"message": "Some text",
|
||||
"title": "No Prio",
|
||||
"extras":
|
||||
{
|
||||
"client::display":
|
||||
{
|
||||
"contentType": "text/html"
|
||||
}
|
||||
},
|
||||
|
||||
},
|
||||
]
|
||||
|
||||
|
||||
for data in priorities:
|
||||
x = requests.post(url, data = json.dumps(data), headers=headers)
|
||||
print(x.text)
|
|
@ -1 +1 @@
|
|||
find . -name '*.go' -printf '%h\n' | sort -u | xargs -n1 -P1 go test -cover
|
||||
find . -name '*.go' -printf '%h\n' | sort -u | xargs -n1 -P1 go test -cover
|
||||
|
|
11
go.sum
11
go.sum
|
@ -109,6 +109,17 @@ golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c h1:VwygUrnw9jn88c4u8GD3rZQbq
|
|||
golang.org/x/sys v0.0.0-20210124154548-22da62e12c0c/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210330210617-4fbd30eecc44/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
|
||||
golang.org/x/sys v0.0.0-20210510120138-977fb7262007/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139 h1:C+AwYEtBp/VQwoLntUmQ/yx3MS9vmZaKNdw5eOpoQe8=
|
||||
golang.org/x/sys v0.0.0-20210608053332-aa57babbf139/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
|
||||
golang.org/x/term v0.0.0-20201117132131-f5c789dd3221/go.mod h1:Nr5EML6q2oocZ2LXRh80K7BxOlk5/8JxuGnuhpl+muw=
|
||||
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
|
||||
golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk=
|
||||
golang.org/x/text v0.3.3/go.mod h1:5Zoc/QRtKVWzQhOtBMvqHzDpF6irO9z98xDceosuGiQ=
|
||||
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
|
||||
golang.org/x/tools v0.0.0-20191119224855-298f0cb1881e/go.mod h1:b+2E5dAYhXwXZwtnZ6UAqBI28+e2cm9otk0dWdXHAEo=
|
||||
golang.org/x/tools v0.1.0/go.mod h1:xkSsbof2nBLbhDlRMhhhyNLN/zl3eTqcnHD5viDpcZ0=
|
||||
golang.org/x/xerrors v0.0.0-20190717185122-a985d3407aa7/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191011141410-1b5146add898/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1 h1:go1bK/D/BFZV2I8cIQd1NKEZ+0owSTG1fDTci4IqFcE=
|
||||
golang.org/x/xerrors v0.0.0-20200804184101-5ec99f83aff1/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
|
||||
|
|
|
@ -1,5 +0,0 @@
|
|||
package api
|
||||
|
||||
import "errors"
|
||||
|
||||
var ErrorMessageNotFound = errors.New("message not found")
|
|
@ -5,6 +5,7 @@ import (
|
|||
"net/http"
|
||||
|
||||
"github.com/pushbits/server/internal/authentication"
|
||||
"github.com/pushbits/server/internal/pberrors"
|
||||
|
||||
"github.com/gin-gonic/gin"
|
||||
)
|
||||
|
@ -13,7 +14,7 @@ func successOrAbort(ctx *gin.Context, code int, err error) bool {
|
|||
if err != nil {
|
||||
// If we know the error force error code
|
||||
switch err {
|
||||
case ErrorMessageNotFound:
|
||||
case pberrors.ErrorMessageNotFound:
|
||||
ctx.AbortWithError(http.StatusNotFound, err)
|
||||
default:
|
||||
ctx.AbortWithError(code, err)
|
||||
|
|
|
@ -8,8 +8,8 @@ import (
|
|||
|
||||
"github.com/gomarkdown/markdown"
|
||||
"github.com/matrix-org/gomatrix"
|
||||
"github.com/pushbits/server/internal/api"
|
||||
"github.com/pushbits/server/internal/model"
|
||||
"github.com/pushbits/server/internal/pberrors"
|
||||
)
|
||||
|
||||
// MessageFormat is a matrix message format
|
||||
|
@ -77,7 +77,7 @@ func (d *Dispatcher) DeleteNotification(a *model.Application, n *model.DeleteNot
|
|||
|
||||
if err != nil {
|
||||
log.Println(err)
|
||||
return api.ErrorMessageNotFound
|
||||
return pberrors.ErrorMessageNotFound
|
||||
}
|
||||
|
||||
oldBody, oldFormattedBody, err = bodiesFromMessage(deleteMessage)
|
||||
|
@ -182,7 +182,7 @@ func (d *Dispatcher) getMessage(a *model.Application, id string) (gomatrix.Event
|
|||
}
|
||||
start = messages.End
|
||||
}
|
||||
return gomatrix.Event{}, api.ErrorMessageNotFound
|
||||
return gomatrix.Event{}, pberrors.ErrorMessageNotFound
|
||||
}
|
||||
|
||||
// Replaces the content of a matrix message
|
||||
|
@ -254,19 +254,19 @@ func bodiesFromMessage(message gomatrix.Event) (body, formattedBody string, err
|
|||
body, ok := val.(string)
|
||||
|
||||
if !ok {
|
||||
return "", "", api.ErrorMessageNotFound
|
||||
return "", "", pberrors.ErrorMessageNotFound
|
||||
}
|
||||
|
||||
formattedBody = body
|
||||
|
||||
} else {
|
||||
return "", "", api.ErrorMessageNotFound
|
||||
return "", "", pberrors.ErrorMessageNotFound
|
||||
}
|
||||
|
||||
if val, ok := message.Content["formatted_body"]; ok {
|
||||
body, ok := val.(string)
|
||||
if !ok {
|
||||
return "", "", api.ErrorMessageNotFound
|
||||
return "", "", pberrors.ErrorMessageNotFound
|
||||
}
|
||||
|
||||
formattedBody = body
|
||||
|
|
6
internal/pberrors/errors.go
Normal file
6
internal/pberrors/errors.go
Normal file
|
@ -0,0 +1,6 @@
|
|||
package pberrors
|
||||
|
||||
import "errors"
|
||||
|
||||
// ErrorMessageNotFound indicates that a message does not exist
|
||||
var ErrorMessageNotFound = errors.New("message not found")
|
|
@ -7,11 +7,5 @@ import (
|
|||
|
||||
// GetMatrixDispatcher creates and returns a matrix dispatcher
|
||||
func GetMatrixDispatcher(homeserver, username, password string, confCrypto configuration.CryptoConfig) (*dispatcher.Dispatcher, error) {
|
||||
db, err := GetEmptyDatabase(confCrypto)
|
||||
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return dispatcher.Create(db, homeserver, username, password, configuration.Formatting{})
|
||||
return dispatcher.Create(homeserver, username, password, configuration.Formatting{})
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue