gtsocial-umbx

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

relationships.go (3290B)


      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 accounts
     19 
     20 import (
     21 	"errors"
     22 	"net/http"
     23 
     24 	"github.com/gin-gonic/gin"
     25 	apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
     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 // AccountRelationshipsGETHandler swagger:operation GET /api/v1/accounts/relationships accountRelationships
     32 //
     33 // See your account's relationships with the given account IDs.
     34 //
     35 //	---
     36 //	tags:
     37 //	- accounts
     38 //
     39 //	produces:
     40 //	- application/json
     41 //
     42 //	parameters:
     43 //	-
     44 //		name: id
     45 //		type: array
     46 //		items:
     47 //			type: string
     48 //		description: Account IDs.
     49 //		in: query
     50 //		required: true
     51 //
     52 //	security:
     53 //	- OAuth2 Bearer:
     54 //		- read:accounts
     55 //
     56 //	responses:
     57 //		'200':
     58 //			name: account relationships
     59 //			description: Array of account relationships.
     60 //			schema:
     61 //				type: array
     62 //				items:
     63 //					"$ref": "#/definitions/accountRelationship"
     64 //		'400':
     65 //			description: bad request
     66 //		'401':
     67 //			description: unauthorized
     68 //		'404':
     69 //			description: not found
     70 //		'406':
     71 //			description: not acceptable
     72 //		'500':
     73 //			description: internal server error
     74 func (m *Module) AccountRelationshipsGETHandler(c *gin.Context) {
     75 	authed, err := oauth.Authed(c, true, true, true, true)
     76 	if err != nil {
     77 		apiutil.ErrorHandler(c, gtserror.NewErrorUnauthorized(err, err.Error()), m.processor.InstanceGetV1)
     78 		return
     79 	}
     80 
     81 	if _, err := apiutil.NegotiateAccept(c, apiutil.JSONAcceptHeaders...); err != nil {
     82 		apiutil.ErrorHandler(c, gtserror.NewErrorNotAcceptable(err, err.Error()), m.processor.InstanceGetV1)
     83 		return
     84 	}
     85 
     86 	targetAccountIDs := c.QueryArray("id[]")
     87 	if len(targetAccountIDs) == 0 {
     88 		// check fallback -- let's be generous and see if maybe it's just set as 'id'?
     89 		id := c.Query("id")
     90 		if id == "" {
     91 			err = errors.New("no account id(s) specified in query")
     92 			apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
     93 			return
     94 		}
     95 		targetAccountIDs = append(targetAccountIDs, id)
     96 	}
     97 
     98 	relationships := []apimodel.Relationship{}
     99 
    100 	for _, targetAccountID := range targetAccountIDs {
    101 		r, errWithCode := m.processor.Account().RelationshipGet(c.Request.Context(), authed.Account, targetAccountID)
    102 		if errWithCode != nil {
    103 			apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
    104 			return
    105 		}
    106 		relationships = append(relationships, *r)
    107 	}
    108 
    109 	c.JSON(http.StatusOK, relationships)
    110 }