gtsocial-umbx

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

reportsget.go (5083B)


      1 // GoToSocial
      2 // Copyright (C) GoToSocial Authors admin@gotosocial.org
      3 // SPDX-License-Identifier: AGPL-3.0-or-later
      4 //
      5 // This program is free software: you can redistribute it and/or modify
      6 // it under the terms of the GNU Affero General Public License as published by
      7 // the Free Software Foundation, either version 3 of the License, or
      8 // (at your option) any later version.
      9 //
     10 // This program is distributed in the hope that it will be useful,
     11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
     12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
     13 // GNU Affero General Public License for more details.
     14 //
     15 // You should have received a copy of the GNU Affero General Public License
     16 // along with this program.  If not, see <http://www.gnu.org/licenses/>.
     17 
     18 package reports
     19 
     20 import (
     21 	"fmt"
     22 	"net/http"
     23 	"strconv"
     24 
     25 	"github.com/gin-gonic/gin"
     26 	apiutil "github.com/superseriousbusiness/gotosocial/internal/api/util"
     27 	"github.com/superseriousbusiness/gotosocial/internal/gtserror"
     28 	"github.com/superseriousbusiness/gotosocial/internal/oauth"
     29 )
     30 
     31 // ReportsGETHandler swagger:operation GET /api/v1/reports reports
     32 //
     33 // See reports created by the requesting account.
     34 //
     35 // The reports will be returned in descending chronological order (newest first), with sequential IDs (bigger = newer).
     36 //
     37 // The next and previous queries can be parsed from the returned Link header.
     38 //
     39 // Example:
     40 //
     41 // ```
     42 // <https://example.org/api/v1/reports?limit=20&max_id=01FC0SKA48HNSVR6YKZCQGS2V8>; rel="next", <https://example.org/api/v1/reports?limit=20&min_id=01FC0SKW5JK2Q4EVAV2B462YY0>; rel="prev"
     43 // ````
     44 //
     45 //	---
     46 //	tags:
     47 //	- reports
     48 //
     49 //	produces:
     50 //	- application/json
     51 //
     52 //	parameters:
     53 //	-
     54 //		name: resolved
     55 //		type: boolean
     56 //		description: >-
     57 //			If set to true, only resolved reports will be returned.
     58 //			If false, only unresolved reports will be returned.
     59 //			If unset, reports will not be filtered on their resolved status.
     60 //		in: query
     61 //	-
     62 //		name: target_account_id
     63 //		type: string
     64 //		description: Return only reports that target the given account id.
     65 //		in: query
     66 //	-
     67 //		name: max_id
     68 //		type: string
     69 //		description: >-
     70 //			Return only reports *OLDER* than the given max ID.
     71 //			The report with the specified ID will not be included in the response.
     72 //		in: query
     73 //	-
     74 //		name: since_id
     75 //		type: string
     76 //		description: >-
     77 //			Return only reports *NEWER* than the given since ID.
     78 //			The report with the specified ID will not be included in the response.
     79 //			This parameter is functionally equivalent to min_id.
     80 //		in: query
     81 //	-
     82 //		name: min_id
     83 //		type: string
     84 //		description: >-
     85 //			Return only reports *NEWER* than the given min ID.
     86 //			The report with the specified ID will not be included in the response.
     87 //			This parameter is functionally equivalent to since_id.
     88 //		in: query
     89 //	-
     90 //		name: limit
     91 //		type: integer
     92 //		description: >-
     93 //			Number of reports to return.
     94 //			If less than 1, will be clamped to 1.
     95 //			If more than 100, will be clamped to 100.
     96 //		default: 20
     97 //		in: query
     98 //
     99 //	security:
    100 //	- OAuth2 Bearer:
    101 //		- read:reports
    102 //
    103 //	responses:
    104 //		'200':
    105 //			name: reports
    106 //			description: Array of reports.
    107 //			schema:
    108 //				type: array
    109 //				items:
    110 //					"$ref": "#/definitions/report"
    111 //		'400':
    112 //			description: bad request
    113 //		'401':
    114 //			description: unauthorized
    115 //		'404':
    116 //			description: not found
    117 //		'406':
    118 //			description: not acceptable
    119 //		'500':
    120 //			description: internal server error
    121 func (m *Module) ReportsGETHandler(c *gin.Context) {
    122 	authed, err := oauth.Authed(c, true, true, true, true)
    123 	if err != nil {
    124 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
    125 		return
    126 	}
    127 
    128 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
    129 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
    130 		return
    131 	}
    132 
    133 	var resolved *bool
    134 	if resolvedString := c.Query(ResolvedKey); resolvedString != "" {
    135 		i, err := strconv.ParseBool(resolvedString)
    136 		if err != nil {
    137 			err := fmt.Errorf("error parsing %s: %s", ResolvedKey, err)
    138 			apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    139 			return
    140 		}
    141 		resolved = &i
    142 	}
    143 
    144 	limit := 20
    145 	if limitString := c.Query(LimitKey); limitString != "" {
    146 		i, err := strconv.Atoi(limitString)
    147 		if err != nil {
    148 			err := fmt.Errorf("error parsing %s: %s", LimitKey, err)
    149 			apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    150 			return
    151 		}
    152 
    153 		// normalize
    154 		if i <= 0 {
    155 			i = 1
    156 		} else if i >= 100 {
    157 			i = 100
    158 		}
    159 		limit = i
    160 	}
    161 
    162 	resp, errWithCode := m.processor.Report().GetMultiple(c.Request.Context(), authed.Account, resolved, c.Query(TargetAccountIDKey), c.Query(MaxIDKey), c.Query(SinceIDKey), c.Query(MinIDKey), limit)
    163 	if errWithCode != nil {
    164 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    165 		return
    166 	}
    167 
    168 	if resp.LinkHeader != "" {
    169 		c.Header("Link", resp.LinkHeader)
    170 	}
    171 	c.JSON(http.StatusOK, resp.Items)
    172 }