| 
									
										
										
										
											2023-03-12 16:00:57 +01:00
										 |  |  | // GoToSocial | 
					
						
							|  |  |  | // Copyright (C) GoToSocial Authors admin@gotosocial.org | 
					
						
							|  |  |  | // SPDX-License-Identifier: AGPL-3.0-or-later | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is free software: you can redistribute it and/or modify | 
					
						
							|  |  |  | // it under the terms of the GNU Affero General Public License as published by | 
					
						
							|  |  |  | // the Free Software Foundation, either version 3 of the License, or | 
					
						
							|  |  |  | // (at your option) any later version. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // This program is distributed in the hope that it will be useful, | 
					
						
							|  |  |  | // but WITHOUT ANY WARRANTY; without even the implied warranty of | 
					
						
							|  |  |  | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the | 
					
						
							|  |  |  | // GNU Affero General Public License for more details. | 
					
						
							|  |  |  | // | 
					
						
							|  |  |  | // You should have received a copy of the GNU Affero General Public License | 
					
						
							|  |  |  | // along with this program.  If not, see <http://www.gnu.org/licenses/>. | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 
 | 
					
						
							|  |  |  | package api | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | import ( | 
					
						
							| 
									
										
										
										
											2023-01-08 11:43:08 +00:00
										 |  |  | 	"time" | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-01-03 10:50:59 +00:00
										 |  |  | 	"github.com/gin-gonic/gin" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/accounts" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/admin" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/apps" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/blocks" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/bookmarks" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/customemojis" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/favourites" | 
					
						
							| 
									
										
										
										
											2023-02-05 10:50:09 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/featuredtags" | 
					
						
							| 
									
										
										
										
											2024-03-06 02:15:58 -08:00
										 |  |  | 	filtersV1 "github.com/superseriousbusiness/gotosocial/internal/api/client/filters/v1" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/followrequests" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/instance" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/lists" | 
					
						
							| 
									
										
										
										
											2023-07-29 03:49:14 -07:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/markers" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/media" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/notifications" | 
					
						
							| 
									
										
										
										
											2023-11-08 14:32:17 +00:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/polls" | 
					
						
							| 
									
										
										
										
											2023-05-06 17:42:58 +02:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/preferences" | 
					
						
							| 
									
										
										
										
											2023-01-23 13:14:21 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/reports" | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/search" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/statuses" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/streaming" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/timelines" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/api/client/user" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/db" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/middleware" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/processing" | 
					
						
							|  |  |  | 	"github.com/superseriousbusiness/gotosocial/internal/router" | 
					
						
							|  |  |  | ) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | type Client struct { | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | 	processor *processing.Processor | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	db        db.DB | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	accounts       *accounts.Module       // api/v1/accounts | 
					
						
							|  |  |  | 	admin          *admin.Module          // api/v1/admin | 
					
						
							|  |  |  | 	apps           *apps.Module           // api/v1/apps | 
					
						
							|  |  |  | 	blocks         *blocks.Module         // api/v1/blocks | 
					
						
							|  |  |  | 	bookmarks      *bookmarks.Module      // api/v1/bookmarks | 
					
						
							|  |  |  | 	customEmojis   *customemojis.Module   // api/v1/custom_emojis | 
					
						
							|  |  |  | 	favourites     *favourites.Module     // api/v1/favourites | 
					
						
							| 
									
										
										
										
											2023-02-05 10:50:09 +01:00
										 |  |  | 	featuredTags   *featuredtags.Module   // api/v1/featured_tags | 
					
						
							| 
									
										
										
										
											2024-03-06 02:15:58 -08:00
										 |  |  | 	filtersV1      *filtersV1.Module      // api/v1/filters | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	followRequests *followrequests.Module // api/v1/follow_requests | 
					
						
							|  |  |  | 	instance       *instance.Module       // api/v1/instance | 
					
						
							|  |  |  | 	lists          *lists.Module          // api/v1/lists | 
					
						
							| 
									
										
										
										
											2023-07-29 03:49:14 -07:00
										 |  |  | 	markers        *markers.Module        // api/v1/markers | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	media          *media.Module          // api/v1/media, api/v2/media | 
					
						
							|  |  |  | 	notifications  *notifications.Module  // api/v1/notifications | 
					
						
							| 
									
										
										
										
											2023-11-08 14:32:17 +00:00
										 |  |  | 	polls          *polls.Module          // api/v1/polls | 
					
						
							| 
									
										
										
										
											2023-05-06 17:42:58 +02:00
										 |  |  | 	preferences    *preferences.Module    // api/v1/preferences | 
					
						
							| 
									
										
										
										
											2023-01-23 13:14:21 +01:00
										 |  |  | 	reports        *reports.Module        // api/v1/reports | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	search         *search.Module         // api/v1/search, api/v2/search | 
					
						
							|  |  |  | 	statuses       *statuses.Module       // api/v1/statuses | 
					
						
							|  |  |  | 	streaming      *streaming.Module      // api/v1/streaming | 
					
						
							|  |  |  | 	timelines      *timelines.Module      // api/v1/timelines | 
					
						
							|  |  |  | 	user           *user.Module           // api/v1/user | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-11-13 19:48:51 +01:00
										 |  |  | func (c *Client) Route(r *router.Router, m ...gin.HandlerFunc) { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	// create a new group on the top level client 'api' prefix | 
					
						
							|  |  |  | 	apiGroup := r.AttachGroup("api") | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// attach non-global middlewares appropriate to the client api | 
					
						
							| 
									
										
										
										
											2023-01-03 10:50:59 +00:00
										 |  |  | 	apiGroup.Use(m...) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	apiGroup.Use( | 
					
						
							|  |  |  | 		middleware.TokenCheck(c.db, c.processor.OAuthValidateBearerToken), | 
					
						
							| 
									
										
										
										
											2023-07-13 21:27:25 +02:00
										 |  |  | 		middleware.CacheControl(middleware.CacheControlConfig{ | 
					
						
							|  |  |  | 			// Never cache client api responses. | 
					
						
							|  |  |  | 			Directives: []string{"no-store"}, | 
					
						
							|  |  |  | 		}), | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	) | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 	// for each client api module, pass it the Handle function | 
					
						
							|  |  |  | 	// so that the module can attach its routes to this group | 
					
						
							|  |  |  | 	h := apiGroup.Handle | 
					
						
							|  |  |  | 	c.accounts.Route(h) | 
					
						
							|  |  |  | 	c.admin.Route(h) | 
					
						
							|  |  |  | 	c.apps.Route(h) | 
					
						
							|  |  |  | 	c.blocks.Route(h) | 
					
						
							|  |  |  | 	c.bookmarks.Route(h) | 
					
						
							|  |  |  | 	c.customEmojis.Route(h) | 
					
						
							|  |  |  | 	c.favourites.Route(h) | 
					
						
							| 
									
										
										
										
											2023-02-05 10:50:09 +01:00
										 |  |  | 	c.featuredTags.Route(h) | 
					
						
							| 
									
										
										
										
											2024-03-06 02:15:58 -08:00
										 |  |  | 	c.filtersV1.Route(h) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	c.followRequests.Route(h) | 
					
						
							|  |  |  | 	c.instance.Route(h) | 
					
						
							|  |  |  | 	c.lists.Route(h) | 
					
						
							| 
									
										
										
										
											2023-07-29 03:49:14 -07:00
										 |  |  | 	c.markers.Route(h) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	c.media.Route(h) | 
					
						
							|  |  |  | 	c.notifications.Route(h) | 
					
						
							| 
									
										
										
										
											2023-11-08 14:32:17 +00:00
										 |  |  | 	c.polls.Route(h) | 
					
						
							| 
									
										
										
										
											2023-05-06 17:42:58 +02:00
										 |  |  | 	c.preferences.Route(h) | 
					
						
							| 
									
										
										
										
											2023-01-23 13:14:21 +01:00
										 |  |  | 	c.reports.Route(h) | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	c.search.Route(h) | 
					
						
							|  |  |  | 	c.statuses.Route(h) | 
					
						
							|  |  |  | 	c.streaming.Route(h) | 
					
						
							|  |  |  | 	c.timelines.Route(h) | 
					
						
							|  |  |  | 	c.user.Route(h) | 
					
						
							|  |  |  | } | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2023-02-22 16:05:26 +01:00
										 |  |  | func NewClient(db db.DB, p *processing.Processor) *Client { | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 	return &Client{ | 
					
						
							|  |  |  | 		processor: p, | 
					
						
							|  |  |  | 		db:        db, | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  | 		accounts:       accounts.New(p), | 
					
						
							|  |  |  | 		admin:          admin.New(p), | 
					
						
							|  |  |  | 		apps:           apps.New(p), | 
					
						
							|  |  |  | 		blocks:         blocks.New(p), | 
					
						
							|  |  |  | 		bookmarks:      bookmarks.New(p), | 
					
						
							|  |  |  | 		customEmojis:   customemojis.New(p), | 
					
						
							|  |  |  | 		favourites:     favourites.New(p), | 
					
						
							| 
									
										
										
										
											2023-02-05 10:50:09 +01:00
										 |  |  | 		featuredTags:   featuredtags.New(p), | 
					
						
							| 
									
										
										
										
											2024-03-06 02:15:58 -08:00
										 |  |  | 		filtersV1:      filtersV1.New(p), | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		followRequests: followrequests.New(p), | 
					
						
							|  |  |  | 		instance:       instance.New(p), | 
					
						
							|  |  |  | 		lists:          lists.New(p), | 
					
						
							| 
									
										
										
										
											2023-07-29 03:49:14 -07:00
										 |  |  | 		markers:        markers.New(p), | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		media:          media.New(p), | 
					
						
							|  |  |  | 		notifications:  notifications.New(p), | 
					
						
							| 
									
										
										
										
											2023-11-08 14:32:17 +00:00
										 |  |  | 		polls:          polls.New(p), | 
					
						
							| 
									
										
										
										
											2023-05-06 17:42:58 +02:00
										 |  |  | 		preferences:    preferences.New(p), | 
					
						
							| 
									
										
										
										
											2023-01-23 13:14:21 +01:00
										 |  |  | 		reports:        reports.New(p), | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		search:         search.New(p), | 
					
						
							|  |  |  | 		statuses:       statuses.New(p), | 
					
						
							| 
									
										
										
										
											2023-01-08 11:43:08 +00:00
										 |  |  | 		streaming:      streaming.New(p, time.Second*30, 4096), | 
					
						
							| 
									
										
										
										
											2023-01-02 13:10:50 +01:00
										 |  |  | 		timelines:      timelines.New(p), | 
					
						
							|  |  |  | 		user:           user.New(p), | 
					
						
							|  |  |  | 	} | 
					
						
							|  |  |  | } |