Add routes for subscriptions

This commit is contained in:
kolaente 2021-02-13 17:40:27 +01:00
parent 3d94b489cd
commit 27be382cac
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
5 changed files with 410 additions and 0 deletions

View File

@ -72,6 +72,21 @@ func getEntityTypeFromString(entityType string) SubscriptionEntityType {
return SubscriptionEntityUnknown
}
// Create subscribes the current user to an entity
// @Summary Subscribes the current user to an entity.
// @Description Subscribes the current user to an entity.
// @tags subscriptions
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param entity path string true "The entity the user subscribes to. Can be either `namespace`, `list` or `task`."
// @Param entityID path string true "The numeric id of the entity to subscribe to."
// @Success 200 {object} models.Subscription "The subscription"
// @Failure 403 {object} web.HTTPError "The user does not have access to subscribe to this entity."
// @Failure 412 {object} web.HTTPError "The subscription already exists."
// @Failure 412 {object} web.HTTPError "The subscription entity is invalid."
// @Failure 500 {object} models.Message "Internal error"
// @Router /subscriptions/{entity}/{entityID} [put]
func (sb *Subscription) Create(s *xorm.Session, auth web.Auth) (err error) {
// Rights method alread does the validation of the entity type so we don't need to do that here
@ -98,6 +113,20 @@ func (sb *Subscription) Create(s *xorm.Session, auth web.Auth) (err error) {
return
}
// Delete unsubscribes the current user to an entity
// @Summary Unsubscribe the current user from an entity.
// @Description Unsubscribes the current user to an entity.
// @tags subscriptions
// @Accept json
// @Produce json
// @Security JWTKeyAuth
// @Param entity path string true "The entity the user subscribed to. Can be either `namespace`, `list` or `task`."
// @Param entityID path string true "The numeric id of the subscribed entity to."
// @Success 200 {object} models.Subscription "The subscription"
// @Failure 403 {object} web.HTTPError "The user does not have access to subscribe to this entity."
// @Failure 404 {object} web.HTTPError "The subscription does not exist."
// @Failure 500 {object} models.Message "Internal error"
// @Router /subscriptions/{entity}/{entityID} [delete]
func (sb *Subscription) Delete(s *xorm.Session, auth web.Auth) (err error) {
_, err = s.
Where("entity_id = ? AND entity_type = ? AND user_id = ?", sb.EntityID, sb.EntityType, sb.UserID).

View File

@ -59,6 +59,7 @@ func (sb *Subscription) CanDelete(s *xorm.Session, a web.Auth) (can bool, err er
if err != nil {
return false, err
}
// TODO: Return a 404 if the subscription does not exist
return realSb.UserID == a.GetID(), nil
}

View File

@ -3991,6 +3991,128 @@ var doc = `{
}
}
},
"/subscriptions/{entity}/{entityID}": {
"put": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Subscribes the current user to an entity.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Subscribes the current user to an entity.",
"parameters": [
{
"type": "string",
"description": "The entity the user subscribes to. Can be either ` + "`" + `namespace` + "`" + `, ` + "`" + `list` + "`" + ` or ` + "`" + `task` + "`" + `.",
"name": "entity",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The numeric id of the entity to subscribe to.",
"name": "entityID",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "The subscription",
"schema": {
"$ref": "#/definitions/models.Subscription"
}
},
"403": {
"description": "The user does not have access to subscribe to this entity.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"412": {
"description": "The subscription entity is invalid.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
},
"delete": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Unsubscribes the current user to an entity.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Unsubscribe the current user from an entity.",
"parameters": [
{
"type": "string",
"description": "The entity the user subscribed to. Can be either ` + "`" + `namespace` + "`" + `, ` + "`" + `list` + "`" + ` or ` + "`" + `task` + "`" + `.",
"name": "entity",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The numeric id of the subscribed entity to.",
"name": "entityID",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "The subscription",
"schema": {
"$ref": "#/definitions/models.Subscription"
}
},
"403": {
"description": "The user does not have access to subscribe to this entity.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"404": {
"description": "The subscription does not exist.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
}
},
"/tasks/all": {
"get": {
"security": [
@ -7419,6 +7541,27 @@ var doc = `{
}
}
},
"models.Subscription": {
"type": "object",
"properties": {
"created": {
"description": "A timestamp when this subscription was created. You cannot change this value.",
"type": "string"
},
"entity_id": {
"description": "The id of the entity to subscribe to.",
"type": "integer"
},
"id": {
"description": "The numeric ID of the subscription",
"type": "integer"
},
"user": {
"description": "The user who made this subscription",
"$ref": "#/definitions/user.User"
}
}
},
"models.Task": {
"type": "object",
"properties": {

View File

@ -3974,6 +3974,128 @@
}
}
},
"/subscriptions/{entity}/{entityID}": {
"put": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Subscribes the current user to an entity.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Subscribes the current user to an entity.",
"parameters": [
{
"type": "string",
"description": "The entity the user subscribes to. Can be either `namespace`, `list` or `task`.",
"name": "entity",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The numeric id of the entity to subscribe to.",
"name": "entityID",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "The subscription",
"schema": {
"$ref": "#/definitions/models.Subscription"
}
},
"403": {
"description": "The user does not have access to subscribe to this entity.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"412": {
"description": "The subscription entity is invalid.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
},
"delete": {
"security": [
{
"JWTKeyAuth": []
}
],
"description": "Unsubscribes the current user to an entity.",
"consumes": [
"application/json"
],
"produces": [
"application/json"
],
"tags": [
"subscriptions"
],
"summary": "Unsubscribe the current user from an entity.",
"parameters": [
{
"type": "string",
"description": "The entity the user subscribed to. Can be either `namespace`, `list` or `task`.",
"name": "entity",
"in": "path",
"required": true
},
{
"type": "string",
"description": "The numeric id of the subscribed entity to.",
"name": "entityID",
"in": "path",
"required": true
}
],
"responses": {
"200": {
"description": "The subscription",
"schema": {
"$ref": "#/definitions/models.Subscription"
}
},
"403": {
"description": "The user does not have access to subscribe to this entity.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"404": {
"description": "The subscription does not exist.",
"schema": {
"$ref": "#/definitions/web.HTTPError"
}
},
"500": {
"description": "Internal error",
"schema": {
"$ref": "#/definitions/models.Message"
}
}
}
}
},
"/tasks/all": {
"get": {
"security": [
@ -7402,6 +7524,27 @@
}
}
},
"models.Subscription": {
"type": "object",
"properties": {
"created": {
"description": "A timestamp when this subscription was created. You cannot change this value.",
"type": "string"
},
"entity_id": {
"description": "The id of the entity to subscribe to.",
"type": "integer"
},
"id": {
"description": "The numeric ID of the subscription",
"type": "integer"
},
"user": {
"description": "The user who made this subscription",
"$ref": "#/definitions/user.User"
}
}
},
"models.Task": {
"type": "object",
"properties": {

View File

@ -490,6 +490,21 @@ definitions:
description: A timestamp when this filter was last updated. You cannot change this value.
type: string
type: object
models.Subscription:
properties:
created:
description: A timestamp when this subscription was created. You cannot change this value.
type: string
entity_id:
description: The id of the entity to subscribe to.
type: integer
id:
description: The numeric ID of the subscription
type: integer
user:
$ref: '#/definitions/user.User'
description: The user who made this subscription
type: object
models.Task:
properties:
assignees:
@ -3660,6 +3675,85 @@ paths:
summary: Get an auth token for a share
tags:
- sharing
/subscriptions/{entity}/{entityID}:
delete:
consumes:
- application/json
description: Unsubscribes the current user to an entity.
parameters:
- description: The entity the user subscribed to. Can be either `namespace`, `list` or `task`.
in: path
name: entity
required: true
type: string
- description: The numeric id of the subscribed entity to.
in: path
name: entityID
required: true
type: string
produces:
- application/json
responses:
"200":
description: The subscription
schema:
$ref: '#/definitions/models.Subscription'
"403":
description: The user does not have access to subscribe to this entity.
schema:
$ref: '#/definitions/web.HTTPError'
"404":
description: The subscription does not exist.
schema:
$ref: '#/definitions/web.HTTPError'
"500":
description: Internal error
schema:
$ref: '#/definitions/models.Message'
security:
- JWTKeyAuth: []
summary: Unsubscribe the current user from an entity.
tags:
- subscriptions
put:
consumes:
- application/json
description: Subscribes the current user to an entity.
parameters:
- description: The entity the user subscribes to. Can be either `namespace`, `list` or `task`.
in: path
name: entity
required: true
type: string
- description: The numeric id of the entity to subscribe to.
in: path
name: entityID
required: true
type: string
produces:
- application/json
responses:
"200":
description: The subscription
schema:
$ref: '#/definitions/models.Subscription'
"403":
description: The user does not have access to subscribe to this entity.
schema:
$ref: '#/definitions/web.HTTPError'
"412":
description: The subscription entity is invalid.
schema:
$ref: '#/definitions/web.HTTPError'
"500":
description: Internal error
schema:
$ref: '#/definitions/models.Message'
security:
- JWTKeyAuth: []
summary: Subscribes the current user to an entity.
tags:
- subscriptions
/tasks/{ID}:
get:
consumes: