MM-17912: Allow searching for files through plugin API (#13647)

* constants and options for getting files

* Method to get files with options

* Add i18n strings for en

* Add API methods for getting files with options

* gofmt -s file

* explicitly set create at in tests

* use greater than nanosecond time difference for tests

* use gte instead of gt for getting files by created time

* use created at time as default sort order for getting file infos

* use explicit inline strings instead of format strings

* join tables only when required

* use if as secondary sort, and update tests

* update field docs to reflect previous changes

* make page and perPage get options as required

* add json struct tags to GetFileOptions

* bump minimum server versioni

* remove sorting by username and channelname

* use bool for sort order type

* use FileInfo prefix instead of just File

* clearer comments

* use zero-based page numbering

* test filtering by user and channel

* remove unnecessary whitespace

* use int instead of uint for page and perPage arguments

Co-authored-by: mattermod <mattermod@users.noreply.github.com>
Этот коммит содержится в:
Ashish Bhate
2020-02-15 01:51:54 +05:30
коммит произвёл GitHub
родитель ed52acd89c
Коммит 3fc5287f6b
12 изменённых файлов: 418 добавлений и 0 удалений

Просмотреть файл

@@ -5,6 +5,7 @@ package sqlstore
import (
"database/sql"
"fmt"
"net/http"
sq "github.com/Masterminds/squirrel"
@@ -95,6 +96,75 @@ func (fs SqlFileInfoStore) Get(id string) (*model.FileInfo, *model.AppError) {
return info, nil
}
func (fs SqlFileInfoStore) GetWithOptions(page, perPage int, opt *model.GetFileInfosOptions) ([]*model.FileInfo, *model.AppError) {
if perPage < 0 || page < 0 {
return nil, model.NewAppError("SqlFileInfoStore.GetWithOptions",
"store.sql_file_info.get_with_options.app_error", nil, fmt.Sprintf("page=%d and perPage=%d must be non-negative", page, perPage), http.StatusBadRequest)
}
if perPage == 0 {
return nil, nil
}
if opt == nil {
opt = &model.GetFileInfosOptions{}
}
query := fs.getQueryBuilder().
Select("FileInfo.*").
From("FileInfo")
if len(opt.ChannelIds) > 0 {
query = query.Join("Posts ON FileInfo.PostId = Posts.Id").
Where(sq.Eq{"Posts.ChannelId": opt.ChannelIds})
}
if len(opt.UserIds) > 0 {
query = query.Where(sq.Eq{"FileInfo.CreatorId": opt.UserIds})
}
if opt.Since > 0 {
query = query.Where(sq.GtOrEq{"FileInfo.CreateAt": opt.Since})
}
if !opt.IncludeDeleted {
query = query.Where("FileInfo.DeleteAt = 0")
}
if opt.SortBy == "" {
opt.SortBy = model.FILEINFO_SORT_BY_CREATED
}
sortDirection := "ASC"
if opt.SortDescending {
sortDirection = "DESC"
}
switch opt.SortBy {
case model.FILEINFO_SORT_BY_CREATED:
query = query.OrderBy("FileInfo.CreateAt " + sortDirection)
case model.FILEINFO_SORT_BY_SIZE:
query = query.OrderBy("FileInfo.Size " + sortDirection)
default:
return nil, model.NewAppError("SqlFileInfoStore.GetWithOptions",
"store.sql_file_info.get_with_options.app_error", nil, "invalid sort option", http.StatusBadRequest)
}
query = query.OrderBy("FileInfo.Id ASC") // secondary sort for sort stability
query = query.Limit(uint64(perPage)).Offset(uint64(perPage * page))
queryString, args, err := query.ToSql()
if err != nil {
return nil, model.NewAppError("SqlFileInfoStore.GetWithOptions",
"store.sql.build_query.app_error", nil, err.Error(), http.StatusInternalServerError)
}
var infos []*model.FileInfo
if _, err := fs.GetReplica().Select(&infos, queryString, args...); err != nil {
return nil, model.NewAppError("SqlFileInfoStore.GetWithOptions",
"store.sql_file_info.get_with_options.app_error", nil, err.Error(), http.StatusInternalServerError)
}
return infos, nil
}
func (fs SqlFileInfoStore) GetByPath(path string) (*model.FileInfo, *model.AppError) {
info := &model.FileInfo{}