mirror of
https://github.com/gotify/server.git
synced 2026-05-06 21:42:07 +08:00
Add default message priority for applications
Co-authored-by: Jannis Mattheis <contact@jmattheis.de>
This commit is contained in:
@@ -44,6 +44,10 @@ type ApplicationParams struct {
|
||||
//
|
||||
// example: Backup server for the interwebs
|
||||
Description string `form:"description" query:"description" json:"description"`
|
||||
// The default priority of messages sent by this application. Defaults to 0.
|
||||
//
|
||||
// example: 5
|
||||
DefaultPriority int `form:"defaultPriority" query:"defaultPriority" json:"defaultPriority"`
|
||||
}
|
||||
|
||||
// CreateApplication creates an application and returns the access token.
|
||||
@@ -83,11 +87,12 @@ func (a *ApplicationAPI) CreateApplication(ctx *gin.Context) {
|
||||
applicationParams := ApplicationParams{}
|
||||
if err := ctx.Bind(&applicationParams); err == nil {
|
||||
app := model.Application{
|
||||
Name: applicationParams.Name,
|
||||
Description: applicationParams.Description,
|
||||
Token: auth.GenerateNotExistingToken(generateApplicationToken, a.applicationExists),
|
||||
UserID: auth.GetUserID(ctx),
|
||||
Internal: false,
|
||||
Name: applicationParams.Name,
|
||||
Description: applicationParams.Description,
|
||||
DefaultPriority: applicationParams.DefaultPriority,
|
||||
Token: auth.GenerateNotExistingToken(generateApplicationToken, a.applicationExists),
|
||||
UserID: auth.GetUserID(ctx),
|
||||
Internal: false,
|
||||
}
|
||||
|
||||
if success := successOrAbort(ctx, 500, a.DB.CreateApplication(&app)); !success {
|
||||
@@ -245,6 +250,7 @@ func (a *ApplicationAPI) UpdateApplication(ctx *gin.Context) {
|
||||
if err := ctx.Bind(&applicationParams); err == nil {
|
||||
app.Description = applicationParams.Description
|
||||
app.Name = applicationParams.Name
|
||||
app.DefaultPriority = applicationParams.DefaultPriority
|
||||
|
||||
if success := successOrAbort(ctx, 500, a.DB.UpdateApplication(app)); !success {
|
||||
return
|
||||
|
||||
@@ -92,7 +92,7 @@ func (s *ApplicationSuite) Test_ensureApplicationHasCorrectJsonRepresentation()
|
||||
Image: "asd",
|
||||
Internal: true,
|
||||
}
|
||||
test.JSONEquals(s.T(), actual, `{"id":1,"token":"Aasdasfgeeg","name":"myapp","description":"mydesc", "image": "asd", "internal":true}`)
|
||||
test.JSONEquals(s.T(), actual, `{"id":1,"token":"Aasdasfgeeg","name":"myapp","description":"mydesc", "image": "asd", "internal":true, "defaultPriority":0}`)
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) Test_CreateApplication_expectBadRequestOnEmptyName() {
|
||||
@@ -527,6 +527,29 @@ func (s *ApplicationSuite) Test_UpdateApplicationName_expectSuccess() {
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) Test_UpdateApplicationDefaultPriority_expectSuccess() {
|
||||
s.db.User(5).NewAppWithToken(2, "app-2")
|
||||
|
||||
test.WithUser(s.ctx, 5)
|
||||
s.withFormData("name=name&description=&defaultPriority=4")
|
||||
s.ctx.Params = gin.Params{{Key: "id", Value: "2"}}
|
||||
s.a.UpdateApplication(s.ctx)
|
||||
|
||||
expected := &model.Application{
|
||||
ID: 2,
|
||||
Token: "app-2",
|
||||
UserID: 5,
|
||||
Name: "name",
|
||||
Description: "",
|
||||
DefaultPriority: 4,
|
||||
}
|
||||
|
||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||
if app, err := s.db.GetApplicationByID(2); assert.NoError(s.T(), err) {
|
||||
assert.Equal(s.T(), expected, app)
|
||||
}
|
||||
}
|
||||
|
||||
func (s *ApplicationSuite) Test_UpdateApplication_preservesImage() {
|
||||
app := s.db.User(5).NewAppWithToken(2, "app-2")
|
||||
app.Image = "existing.png"
|
||||
|
||||
@@ -371,6 +371,11 @@ func (a *MessageAPI) CreateMessage(ctx *gin.Context) {
|
||||
if strings.TrimSpace(message.Title) == "" {
|
||||
message.Title = application.Name
|
||||
}
|
||||
|
||||
if message.Priority == nil {
|
||||
message.Priority = &application.DefaultPriority
|
||||
}
|
||||
|
||||
message.Date = timeNow()
|
||||
message.ID = 0
|
||||
msgInternal := toInternalMessage(&message)
|
||||
@@ -388,9 +393,12 @@ func toInternalMessage(msg *model.MessageExternal) *model.Message {
|
||||
ApplicationID: msg.ApplicationID,
|
||||
Message: msg.Message,
|
||||
Title: msg.Title,
|
||||
Priority: msg.Priority,
|
||||
Date: msg.Date,
|
||||
}
|
||||
if msg.Priority != nil {
|
||||
res.Priority = *msg.Priority
|
||||
}
|
||||
|
||||
if msg.Extras != nil {
|
||||
res.Extras, _ = json.Marshal(msg.Extras)
|
||||
}
|
||||
@@ -403,7 +411,7 @@ func toExternalMessage(msg *model.Message) *model.MessageExternal {
|
||||
ApplicationID: msg.ApplicationID,
|
||||
Message: msg.Message,
|
||||
Title: msg.Title,
|
||||
Priority: msg.Priority,
|
||||
Priority: &msg.Priority,
|
||||
Date: msg.Date,
|
||||
}
|
||||
if len(msg.Extras) != 0 {
|
||||
|
||||
@@ -53,7 +53,7 @@ func (s *MessageSuite) Test_ensureCorrectJsonRepresentation() {
|
||||
|
||||
actual := &model.PagedMessages{
|
||||
Paging: model.Paging{Limit: 5, Since: 122, Size: 5, Next: "http://example.com/message?limit=5&since=122"},
|
||||
Messages: []*model.MessageExternal{{ID: 55, ApplicationID: 2, Message: "hi", Title: "hi", Date: t, Priority: 4, Extras: map[string]interface{}{
|
||||
Messages: []*model.MessageExternal{{ID: 55, ApplicationID: 2, Message: "hi", Title: "hi", Date: t, Priority: intPtr(4), Extras: map[string]interface{}{
|
||||
"test::string": "string",
|
||||
"test::array": []interface{}{1, 2, 3},
|
||||
"test::int": 1,
|
||||
@@ -331,7 +331,29 @@ func (s *MessageSuite) Test_CreateMessage_onJson_allParams() {
|
||||
|
||||
msgs, err := s.db.GetMessagesByApplication(7)
|
||||
assert.NoError(s.T(), err)
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 7, Title: "mytitle", Message: "mymessage", Priority: 1, Date: t}
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 7, Title: "mytitle", Message: "mymessage", Priority: intPtr(1), Date: t}
|
||||
assert.Len(s.T(), msgs, 1)
|
||||
assert.Equal(s.T(), expected, toExternalMessage(msgs[0]))
|
||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||
assert.Equal(s.T(), expected, s.notifiedMessage)
|
||||
}
|
||||
|
||||
func (s *MessageSuite) Test_CreateMessage_WithDefaultPriority() {
|
||||
t, _ := time.Parse("2006/01/02", "2017/01/02")
|
||||
|
||||
timeNow = func() time.Time { return t }
|
||||
defer func() { timeNow = time.Now }()
|
||||
|
||||
auth.RegisterAuthentication(s.ctx, nil, 4, "app-token")
|
||||
s.db.User(4).AppWithTokenAndDefaultPriority(8, "app-token", 5)
|
||||
s.ctx.Request = httptest.NewRequest("POST", "/message", strings.NewReader(`{"title": "mytitle", "message": "mymessage"}`))
|
||||
s.ctx.Request.Header.Set("Content-Type", "application/json")
|
||||
|
||||
s.a.CreateMessage(s.ctx)
|
||||
|
||||
msgs, err := s.db.GetMessagesByApplication(8)
|
||||
assert.NoError(s.T(), err)
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 8, Title: "mytitle", Message: "mymessage", Priority: intPtr(5), Date: t}
|
||||
assert.Len(s.T(), msgs, 1)
|
||||
assert.Equal(s.T(), expected, toExternalMessage(msgs[0]))
|
||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||
@@ -352,7 +374,7 @@ func (s *MessageSuite) Test_CreateMessage_WithTitle() {
|
||||
|
||||
msgs, err := s.db.GetMessagesByApplication(5)
|
||||
assert.NoError(s.T(), err)
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 5, Title: "mytitle", Message: "mymessage", Date: t}
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 5, Title: "mytitle", Message: "mymessage", Date: t, Priority: intPtr(0)}
|
||||
assert.Len(s.T(), msgs, 1)
|
||||
assert.Equal(s.T(), expected, toExternalMessage(msgs[0]))
|
||||
assert.Equal(s.T(), 200, s.recorder.Code)
|
||||
@@ -446,6 +468,7 @@ func (s *MessageSuite) Test_CreateMessage_WithExtras() {
|
||||
Message: "mymessage",
|
||||
Title: "msg with extras",
|
||||
Date: t,
|
||||
Priority: intPtr(0),
|
||||
Extras: map[string]interface{}{
|
||||
"gotify::test": map[string]interface{}{
|
||||
"string": "test",
|
||||
@@ -492,7 +515,7 @@ func (s *MessageSuite) Test_CreateMessage_onQueryData() {
|
||||
|
||||
s.a.CreateMessage(s.ctx)
|
||||
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 2, Title: "mytitle", Message: "mymessage", Priority: 1, Date: t}
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 2, Title: "mytitle", Message: "mymessage", Priority: intPtr(1), Date: t}
|
||||
|
||||
msgs, err := s.db.GetMessagesByApplication(2)
|
||||
assert.NoError(s.T(), err)
|
||||
@@ -515,7 +538,7 @@ func (s *MessageSuite) Test_CreateMessage_onFormData() {
|
||||
|
||||
s.a.CreateMessage(s.ctx)
|
||||
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 99, Title: "mytitle", Message: "mymessage", Priority: 1, Date: t}
|
||||
expected := &model.MessageExternal{ID: 1, ApplicationID: 99, Title: "mytitle", Message: "mymessage", Priority: intPtr(1), Date: t}
|
||||
msgs, err := s.db.GetMessagesByApplication(99)
|
||||
assert.NoError(s.T(), err)
|
||||
assert.Len(s.T(), msgs, 1)
|
||||
@@ -528,3 +551,7 @@ func (s *MessageSuite) withURL(scheme, host, path, query string) {
|
||||
s.ctx.Request.URL = &url.URL{Path: path, RawQuery: query}
|
||||
s.ctx.Set("location", &url.URL{Scheme: scheme, Host: host})
|
||||
}
|
||||
|
||||
func intPtr(x int) *int {
|
||||
return &x
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user