Search (#36)
First implementation of search functionality for remote account and status lookups.
This commit is contained in:
88
internal/api/client/search/search.go
Normal file
88
internal/api/client/search/search.go
Normal file
@ -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
|
||||
}
|
151
internal/api/client/search/searchget.go
Normal file
151
internal/api/client/search/searchget.go
Normal file
@ -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)
|
||||
}
|
52
internal/api/model/search.go
Normal file
52
internal/api/model/search.go
Normal file
@ -0,0 +1,52 @@
|
||||
/*
|
||||
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 model
|
||||
|
||||
// SearchQuery corresponds to search parameters as submitted through the client API.
|
||||
// See https://docs.joinmastodon.org/methods/search/
|
||||
type SearchQuery struct {
|
||||
// If provided, statuses returned will be authored only by this account
|
||||
AccountID string
|
||||
// Return results older than this id
|
||||
MaxID string
|
||||
// Return results immediately newer than this id
|
||||
MinID string
|
||||
// Enum(accounts, hashtags, statuses)
|
||||
Type string
|
||||
// Filter out unreviewed tags? Defaults to false. Use true when trying to find trending tags.
|
||||
ExcludeUnreviewed bool
|
||||
// The search query
|
||||
Query string
|
||||
// Attempt WebFinger lookup. Defaults to false.
|
||||
Resolve bool
|
||||
// Maximum number of results to load, per type. Defaults to 20. Max 40.
|
||||
Limit int
|
||||
// Offset in search results. Used for pagination. Defaults to 0.
|
||||
Offset int
|
||||
// Only include accounts that the user is following. Defaults to false.
|
||||
Following bool
|
||||
}
|
||||
|
||||
// SearchResult corresponds to a search result, containing accounts, statuses, and hashtags.
|
||||
// See https://docs.joinmastodon.org/methods/search/
|
||||
type SearchResult struct {
|
||||
Accounts []Account `json:"accounts"`
|
||||
Statuses []Status `json:"statuses"`
|
||||
Hashtags []Tag `json:"hashtags"`
|
||||
}
|
Reference in New Issue
Block a user