[feature] Add partial text search for accounts + statuses (#1836)

This commit is contained in:
tobi 2023-06-21 18:26:40 +02:00 committed by GitHub
commit 831ae09f8b
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
30 changed files with 3834 additions and 669 deletions

View file

@ -18,10 +18,7 @@
package search
import (
"errors"
"fmt"
"net/http"
"strconv"
"github.com/gin-gonic/gin"
apimodel "github.com/superseriousbusiness/gotosocial/internal/api/model"
@ -40,6 +37,98 @@ import (
// tags:
// - search
//
// produces:
// - application/json
//
// parameters:
// -
// name: max_id
// type: string
// description: >-
// Return only items *OLDER* than the given max ID.
// The item with the specified ID will not be included in the response.
// Currently only used if 'type' is set to a specific type.
// in: query
// required: false
// -
// name: min_id
// type: string
// description: >-
// Return only items *immediately newer* than the given min ID.
// The item with the specified ID will not be included in the response.
// Currently only used if 'type' is set to a specific type.
// in: query
// required: false
// -
// name: limit
// type: integer
// description: Number of each type of item to return.
// default: 20
// maximum: 40
// minimum: 1
// in: query
// required: false
// -
// name: offset
// type: integer
// description: >-
// Page number of results to return (starts at 0).
// This parameter is currently not used, page by selecting
// a specific query type and using maxID and minID instead.
// default: 0
// maximum: 10
// minimum: 0
// in: query
// required: false
// -
// name: q
// type: string
// description: |-
// Query string to search for. This can be in the following forms:
// - `@[username]` -- search for an account with the given username on any domain. Can return multiple results.
// - @[username]@[domain]` -- search for a remote account with exact username and domain. Will only ever return 1 result at most.
// - `https://example.org/some/arbitrary/url` -- search for an account OR a status with the given URL. Will only ever return 1 result at most.
// - any arbitrary string -- search for accounts or statuses containing the given string. Can return multiple results.
// in: query
// required: true
// -
// name: type
// type: string
// description: |-
// Type of item to return. One of:
// - `` -- empty string; return any/all results.
// - `accounts` -- return account(s).
// - `statuses` -- return status(es).
// - `hashtags` -- return hashtag(s).
// If `type` is specified, paging can be performed using max_id and min_id parameters.
// If `type` is not specified, see the `offset` parameter for paging.
// in: query
// -
// name: resolve
// type: boolean
// description: >-
// If searching query is for `@[username]@[domain]`, or a URL, allow the GoToSocial
// instance to resolve the search by making calls to remote instances (webfinger, ActivityPub, etc).
// default: false
// in: query
// -
// name: following
// type: boolean
// description: >-
// If search type includes accounts, and search query is an arbitrary string, show only accounts
// that the requesting account follows. If this is set to `true`, then the GoToSocial instance will
// enhance the search by also searching within account notes, not just in usernames and display names.
// default: false
// in: query
// -
// name: exclude_unreviewed
// type: boolean
// description: >-
// If searching for hashtags, exclude those not yet approved by instance admin.
// Currently this parameter is unused.
// default: false
// in: query
//
// security:
// - OAuth2 Bearer:
// - read:search
@ -74,93 +163,55 @@ func (m *Module) SearchGETHandler(c *gin.Context) {
return
}
excludeUnreviewed := false
excludeUnreviewedString := c.Query(ExcludeUnreviewedKey)
if excludeUnreviewedString != "" {
var err error
excludeUnreviewed, err = strconv.ParseBool(excludeUnreviewedString)
if err != nil {
err := fmt.Errorf("error parsing %s: %s", ExcludeUnreviewedKey, err)
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
}
}
query := c.Query(QueryKey)
if query == "" {
err := errors.New("query parameter q was empty")
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
limit, errWithCode := apiutil.ParseLimit(c.Query(apiutil.LimitKey), 20, 40, 1)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
resolve := false
resolveString := c.Query(ResolveKey)
if resolveString != "" {
var err error
resolve, err = strconv.ParseBool(resolveString)
if err != nil {
err := fmt.Errorf("error parsing %s: %s", ResolveKey, err)
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
}
offset, errWithCode := apiutil.ParseSearchOffset(c.Query(apiutil.SearchOffsetKey), 0, 10, 0)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
limit := 2
limitString := c.Query(LimitKey)
if limitString != "" {
i, err := strconv.ParseInt(limitString, 10, 32)
if err != nil {
err := fmt.Errorf("error parsing %s: %s", LimitKey, err)
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
}
limit = int(i)
}
if limit > 40 {
limit = 40
}
if limit < 1 {
limit = 1
query, errWithCode := apiutil.ParseSearchQuery(c.Query(apiutil.SearchQueryKey))
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
offset := 0
offsetString := c.Query(OffsetKey)
if offsetString != "" {
i, err := strconv.ParseInt(offsetString, 10, 32)
if err != nil {
err := fmt.Errorf("error parsing %s: %s", OffsetKey, err)
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
}
offset = int(i)
resolve, errWithCode := apiutil.ParseSearchResolve(c.Query(apiutil.SearchResolveKey), false)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
following := false
followingString := c.Query(FollowingKey)
if followingString != "" {
var err error
following, err = strconv.ParseBool(followingString)
if err != nil {
err := fmt.Errorf("error parsing %s: %s", FollowingKey, err)
apiutil.ErrorHandler(c, gtserror.NewErrorBadRequest(err, err.Error()), m.processor.InstanceGetV1)
return
}
following, errWithCode := apiutil.ParseSearchFollowing(c.Query(apiutil.SearchFollowingKey), false)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
searchQuery := &apimodel.SearchQuery{
AccountID: c.Query(AccountIDKey),
MaxID: c.Query(MaxIDKey),
MinID: c.Query(MinIDKey),
Type: c.Query(TypeKey),
ExcludeUnreviewed: excludeUnreviewed,
Query: query,
Resolve: resolve,
excludeUnreviewed, errWithCode := apiutil.ParseSearchExcludeUnreviewed(c.Query(apiutil.SearchExcludeUnreviewedKey), false)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return
}
searchRequest := &apimodel.SearchRequest{
MaxID: c.Query(apiutil.MaxIDKey),
MinID: c.Query(apiutil.MinIDKey),
Limit: limit,
Offset: offset,
Query: query,
QueryType: c.Query(apiutil.SearchTypeKey),
Resolve: resolve,
Following: following,
ExcludeUnreviewed: excludeUnreviewed,
}
results, errWithCode := m.processor.SearchGet(c.Request.Context(), authed, searchQuery)
results, errWithCode := m.processor.Search().Get(c.Request.Context(), authed.Account, searchRequest)
if errWithCode != nil {
apiutil.ErrorHandler(c, errWithCode, m.processor.InstanceGetV1)
return