diff options
| author | 2021-05-29 19:39:43 +0200 | |
|---|---|---|
| committer | 2021-05-29 19:39:43 +0200 | |
| commit | 1fe5e36ac3a631a53724fe99583b7f11baa32c53 (patch) | |
| tree | 40ae75c3613c9c6915273f98fe7ce41046d11381 /internal/api/client | |
| parent | federate account updates (diff) | |
| download | gotosocial-1fe5e36ac3a631a53724fe99583b7f11baa32c53.tar.xz | |
Search (#36)
First implementation of search functionality for remote account and status lookups.
Diffstat (limited to 'internal/api/client')
| -rw-r--r-- | internal/api/client/search/search.go | 88 | ||||
| -rw-r--r-- | internal/api/client/search/searchget.go | 151 | 
2 files changed, 239 insertions, 0 deletions
diff --git a/internal/api/client/search/search.go b/internal/api/client/search/search.go new file mode 100644 index 000000000..b89ae1a74 --- /dev/null +++ b/internal/api/client/search/search.go @@ -0,0 +1,88 @@ +/* +   GoToSocial +   Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + +   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/>. +*/ + +package search + +import ( +	"net/http" + +	"github.com/sirupsen/logrus" +	"github.com/superseriousbusiness/gotosocial/internal/api" +	"github.com/superseriousbusiness/gotosocial/internal/config" +	"github.com/superseriousbusiness/gotosocial/internal/message" +	"github.com/superseriousbusiness/gotosocial/internal/router" +) + +const ( +	// BasePath is the base path for serving v1 of the search API +	BasePathV1 = "/api/v1/search" + +	// BasePathV2 is the base path for serving v2 of the search API +	BasePathV2 = "/api/v2/search" + +	// AccountIDKey -- If provided, statuses returned will be authored only by this account +	AccountIDKey = "account_id" +	// MaxIDKey -- Return results older than this id +	MaxIDKey = "max_id" +	// MinIDKey -- Return results immediately newer than this id +	MinIDKey = "min_id" +	// TypeKey -- Enum(accounts, hashtags, statuses) +	TypeKey = "type" +	// ExcludeUnreviewedKey -- Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags. +	ExcludeUnreviewedKey = "exclude_unreviewed" +	// QueryKey -- The search query +	QueryKey = "q" +	// ResolveKey -- Attempt WebFinger lookup. Defaults to false. +	ResolveKey = "resolve" +	// LimitKey -- Maximum number of results to load, per type. Defaults to 20. Max 40. +	LimitKey = "limit" +	// OffsetKey -- Offset in search results. Used for pagination. Defaults to 0. +	OffsetKey = "offset" +	// FollowingKey -- Only include accounts that the user is following. Defaults to false. +	FollowingKey = "following" + +	// TypeAccounts -- +	TypeAccounts = "accounts" +	// TypeHashtags -- +	TypeHashtags = "hashtags" +	// TypeStatuses -- +	TypeStatuses = "statuses" +) + +// Module implements the ClientAPIModule interface for everything related to searching +type Module struct { +	config    *config.Config +	processor message.Processor +	log       *logrus.Logger +} + +// New returns a new search module +func New(config *config.Config, processor message.Processor, log *logrus.Logger) api.ClientModule { +	return &Module{ +		config:    config, +		processor: processor, +		log:       log, +	} +} + +// Route attaches all routes from this module to the given router +func (m *Module) Route(r router.Router) error { +	r.AttachHandler(http.MethodGet, BasePathV1, m.SearchGETHandler) +	r.AttachHandler(http.MethodGet, BasePathV2, m.SearchGETHandler) +	return nil +} diff --git a/internal/api/client/search/searchget.go b/internal/api/client/search/searchget.go new file mode 100644 index 000000000..1e2694673 --- /dev/null +++ b/internal/api/client/search/searchget.go @@ -0,0 +1,151 @@ +/* +   GoToSocial +   Copyright (C) 2021 GoToSocial Authors admin@gotosocial.org + +   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/>. +*/ + +package search + +import ( +	"fmt" +	"net/http" +	"strconv" + +	"github.com/gin-gonic/gin" +	"github.com/sirupsen/logrus" +	"github.com/superseriousbusiness/gotosocial/internal/api/model" +	"github.com/superseriousbusiness/gotosocial/internal/oauth" +) + +// SearchGETHandler handles searches for local and remote accounts, statuses, and hashtags. +// It corresponds to the mastodon endpoint described here: https://docs.joinmastodon.org/methods/search/ +func (m *Module) SearchGETHandler(c *gin.Context) { +	l := m.log.WithFields(logrus.Fields{ +		"func":        "SearchGETHandler", +		"request_uri": c.Request.RequestURI, +		"user_agent":  c.Request.UserAgent(), +		"origin_ip":   c.ClientIP(), +	}) +	l.Debugf("entering function") + +	authed, err := oauth.Authed(c, true, true, true, true) // we don't really need an app here but we want everything else +	if err != nil { +		l.Errorf("error authing search request: %s", err) +		c.JSON(http.StatusBadRequest, gin.H{"error": "not authed"}) +		return +	} + +	accountID := c.Query(AccountIDKey) +	maxID := c.Query(MaxIDKey) +	minID := c.Query(MinIDKey) +	searchType := c.Query(TypeKey) + +	excludeUnreviewed := false +	excludeUnreviewedString := c.Query(ExcludeUnreviewedKey) +	if excludeUnreviewedString != "" { +		var err error +		excludeUnreviewed, err = strconv.ParseBool(excludeUnreviewedString) +		if err != nil { +			c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("couldn't parse param %s: %s", excludeUnreviewedString, err)}) +			return +		} +	} + +	query := c.Query(QueryKey) +	if query == "" { +		c.JSON(http.StatusBadRequest, gin.H{"error": "query parameter q was empty"}) +		return +	} + +	resolve := false +	resolveString := c.Query(ResolveKey) +	if resolveString != "" { +		var err error +		resolve, err = strconv.ParseBool(resolveString) +		if err != nil { +			c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("couldn't parse param %s: %s", resolveString, err)}) +			return +		} +	} + +	limit := 20 +	limitString := c.Query(LimitKey) +	if limitString != "" { +		i, err := strconv.ParseInt(limitString, 10, 64) +		if err != nil { +			l.Debugf("error parsing limit string: %s", err) +			c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse limit query param"}) +			return +		} +		limit = int(i) +	} +	if limit > 40 { +		limit = 40 +	} +	if limit < 1 { +		limit = 1 +	} + +	offset := 0 +	offsetString := c.Query(OffsetKey) +	if offsetString != "" { +		i, err := strconv.ParseInt(offsetString, 10, 64) +		if err != nil { +			l.Debugf("error parsing offset string: %s", err) +			c.JSON(http.StatusBadRequest, gin.H{"error": "couldn't parse offset query param"}) +			return +		} +		offset = int(i) +	} +	if limit > 40 { +		limit = 40 +	} +	if limit < 1 { +		limit = 1 +	} + +	following := false +	followingString := c.Query(FollowingKey) +	if followingString != "" { +		var err error +		following, err = strconv.ParseBool(followingString) +		if err != nil { +			c.JSON(http.StatusBadRequest, gin.H{"error": fmt.Sprintf("couldn't parse param %s: %s", followingString, err)}) +			return +		} +	} + +	searchQuery := &model.SearchQuery{ +		AccountID:         accountID, +		MaxID:             maxID, +		MinID:             minID, +		Type:              searchType, +		ExcludeUnreviewed: excludeUnreviewed, +		Query:             query, +		Resolve:           resolve, +		Limit:             limit, +		Offset:            offset, +		Following:         following, +	} + +	results, errWithCode := m.processor.SearchGet(authed, searchQuery) +	if errWithCode != nil { +		l.Debugf("error searching: %s", errWithCode.Error()) +		c.JSON(errWithCode.Code(), gin.H{"error": errWithCode.Safe()}) +		return +	} + +	c.JSON(http.StatusOK, results) +}  | 
