diff --git a/api/message.go b/api/message.go index 185e31e9..ca818109 100644 --- a/api/message.go +++ b/api/message.go @@ -345,7 +345,7 @@ func (a *MessageAPI) DeleteMessage(ctx *gin.Context) { // description: the message to add // required: true // schema: -// $ref: "#/definitions/Message" +// $ref: "#/definitions/CreateMessage" // responses: // 200: // description: Ok @@ -364,7 +364,7 @@ func (a *MessageAPI) DeleteMessage(ctx *gin.Context) { // schema: // $ref: "#/definitions/Error" func (a *MessageAPI) CreateMessage(ctx *gin.Context) { - message := model.MessageExternal{} + message := model.CreateMessage{} if err := ctx.Bind(&message); err != nil { return } @@ -395,8 +395,6 @@ func (a *MessageAPI) CreateMessage(ctx *gin.Context) { message.Priority = &app.DefaultPriority } - message.Date = timeNow() - message.ID = 0 msgInternal := toInternalMessage(&message) if success := successOrAbort(ctx, 500, a.DB.CreateMessage(msgInternal)); !success { return @@ -405,13 +403,12 @@ func (a *MessageAPI) CreateMessage(ctx *gin.Context) { ctx.JSON(200, toExternalMessage(msgInternal)) } -func toInternalMessage(msg *model.MessageExternal) *model.Message { +func toInternalMessage(msg *model.CreateMessage) *model.Message { res := &model.Message{ - ID: msg.ID, ApplicationID: msg.ApplicationID, Message: msg.Message, Title: msg.Title, - Date: msg.Date, + Date: timeNow(), } if msg.Priority != nil { res.Priority = *msg.Priority diff --git a/docs/spec.json b/docs/spec.json index 41059517..38261bb1 100644 --- a/docs/spec.json +++ b/docs/spec.json @@ -1558,7 +1558,7 @@ "in": "body", "required": true, "schema": { - "$ref": "#/definitions/Message" + "$ref": "#/definitions/CreateMessage" } } ], @@ -2746,6 +2746,57 @@ }, "x-go-package": "github.com/gotify/server/v2/api" }, + "CreateMessage": { + "description": "The CreateMessage holds information about a message that will be sent.", + "type": "object", + "title": "CreateMessage Model", + "required": [ + "message" + ], + "properties": { + "appid": { + "description": "The application id that send this message. Always set when returned via the API.", + "type": "integer", + "format": "int64", + "x-go-name": "ApplicationID", + "example": 5 + }, + "extras": { + "description": "The extra data sent along the message.\n\nThe extra fields are stored in a key-value scheme. Only accepted in CreateMessage requests with application/json content-type.\n\nThe keys should be in the following format: \u0026lt;top-namespace\u0026gt;::[\u0026lt;sub-namespace\u0026gt;::]\u0026lt;action\u0026gt;\n\nThese namespaces are reserved and might be used in the official clients: gotify android ios web server client. Do not use them for other purposes.", + "type": "object", + "additionalProperties": {}, + "x-go-name": "Extras", + "example": { + "home::appliances::lighting::on": { + "brightness": 15 + }, + "home::appliances::thermostat::change_temperature": { + "temperature": 23 + } + } + }, + "message": { + "description": "The message. Markdown (excluding html) is allowed.", + "type": "string", + "x-go-name": "Message", + "example": "**Backup** was successfully finished." + }, + "priority": { + "description": "The priority of the message. If unset, then the default priority of the\napplication will be used.", + "type": "integer", + "format": "int64", + "x-go-name": "Priority", + "example": 2 + }, + "title": { + "description": "The title of the message.", + "type": "string", + "x-go-name": "Title", + "example": "Backup" + } + }, + "x-go-package": "github.com/gotify/server/v2/model" + }, "CreateUserExternal": { "description": "Used for user creation.", "type": "object", diff --git a/model/message.go b/model/message.go index 57a6c9f9..6f75fe27 100644 --- a/model/message.go +++ b/model/message.go @@ -64,3 +64,39 @@ type MessageExternal struct { // example: 2018-02-27T19:36:10.5045044+01:00 Date time.Time `json:"date"` } + +// CreateMessage Model +// +// The CreateMessage holds information about a message that will be sent. +// +// swagger:model CreateMessage +type CreateMessage struct { + // The application id that send this message. Always set when returned via the API. + // + // example: 5 + ApplicationID uint `json:"appid"` + // The message. Markdown (excluding html) is allowed. + // + // required: true + // example: **Backup** was successfully finished. + Message string `form:"message" query:"message" json:"message" binding:"required"` + // The title of the message. + // + // example: Backup + Title string `form:"title" query:"title" json:"title"` + // The priority of the message. If unset, then the default priority of the + // application will be used. + // + // example: 2 + Priority *int `form:"priority" query:"priority" json:"priority"` + // The extra data sent along the message. + // + // The extra fields are stored in a key-value scheme. Only accepted in CreateMessage requests with application/json content-type. + // + // The keys should be in the following format: <top-namespace>::[<sub-namespace>::]<action> + // + // These namespaces are reserved and might be used in the official clients: gotify android ios web server client. Do not use them for other purposes. + // + // example: {"home::appliances::thermostat::change_temperature":{"temperature":23},"home::appliances::lighting::on":{"brightness":15}} + Extras map[string]any `form:"-" query:"-" json:"extras,omitempty"` +}