gtsocial-umbx

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

notificationsget.go (4636B)


      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 notifications
     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 // NotificationsGETHandler swagger:operation GET /api/v1/notifications notifications
     32 //
     33 // Get notifications for currently authorized user.
     34 //
     35 // The notifications 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 // Example:
     39 //
     40 // ```
     41 // <https://example.org/api/v1/notifications?limit=80&max_id=01FC0SKA48HNSVR6YKZCQGS2V8>; rel="next", <https://example.org/api/v1/notifications?limit=80&since_id=01FC0SKW5JK2Q4EVAV2B462YY0>; rel="prev"
     42 // ````
     43 //
     44 //	---
     45 //	tags:
     46 //	- notifications
     47 //
     48 //	produces:
     49 //	- application/json
     50 //
     51 //	parameters:
     52 //	-
     53 //		name: max_id
     54 //		type: string
     55 //		description: >-
     56 //			Return only notifications *OLDER* than the given max notification ID.
     57 //			The notification with the specified ID will not be included in the response.
     58 //		in: query
     59 //		required: false
     60 //	-
     61 //		name: since_id
     62 //		type: string
     63 //		description: >-
     64 //			Return only notifications *newer* than the given since notification ID.
     65 //			The notification with the specified ID will not be included in the response.
     66 //		in: query
     67 //	-
     68 //		name: min_id
     69 //		type: string
     70 //		description: >-
     71 //			Return only notifications *immediately newer* than the given since notification ID.
     72 //			The notification with the specified ID will not be included in the response.
     73 //		in: query
     74 //		required: false
     75 //	-
     76 //		name: limit
     77 //		type: integer
     78 //		description: Number of notifications to return.
     79 //		default: 20
     80 //		in: query
     81 //		required: false
     82 //	-
     83 //		name: exclude_types
     84 //		type: array
     85 //		items:
     86 //			type: string
     87 //			description: Array of types of notifications to exclude (follow, favourite, reblog, mention, poll, follow_request)
     88 //		in: query
     89 //		required: false
     90 //
     91 //	security:
     92 //	- OAuth2 Bearer:
     93 //		- read:notifications
     94 //
     95 //	responses:
     96 //		'200':
     97 //			headers:
     98 //				Link:
     99 //					type: string
    100 //					description: Links to the next and previous queries.
    101 //			name: notifications
    102 //			description: Array of notifications.
    103 //			schema:
    104 //				type: array
    105 //				items:
    106 //					"$ref": "#/definitions/notification"
    107 //		'400':
    108 //			description: bad request
    109 //		'401':
    110 //			description: unauthorized
    111 //		'404':
    112 //			description: not found
    113 //		'406':
    114 //			description: not acceptable
    115 //		'500':
    116 //			description: internal server error
    117 func (m *Module) NotificationsGETHandler(c *gin.Context) {
    118 	authed, err := oauth.Authed(c, true, true, true, true)
    119 	if err != nil {
    120 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
    121 		return
    122 	}
    123 
    124 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
    125 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
    126 		return
    127 	}
    128 
    129 	limit := 20
    130 	limitString := c.Query(LimitKey)
    131 	if limitString != "" {
    132 		i, err := strconv.ParseInt(limitString, 10, 32)
    133 		if err != nil {
    134 			err := fmt.Errorf("error parsing %s: %s", LimitKey, err)
    135 			apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
    136 			return
    137 		}
    138 		limit = int(i)
    139 	}
    140 
    141 	resp, errWithCode := m.processor.Timeline().NotificationsGet(
    142 		c.Request.Context(),
    143 		authed,
    144 		c.Query(MaxIDKey),
    145 		c.Query(SinceIDKey),
    146 		c.Query(MinIDKey),
    147 		limit,
    148 		c.QueryArray(ExcludeTypesKey),
    149 	)
    150 	if errWithCode != nil {
    151 		apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    152 		return
    153 	}
    154 
    155 	if resp.LinkHeader != "" {
    156 		c.Header("Link", resp.LinkHeader)
    157 	}
    158 	c.JSON(http.StatusOK, resp.Items)
    159 }