Bluemage/go/api/images_list.go

110 lines
2.3 KiB
Go
Raw Normal View History

2024-08-24 19:21:01 +07:00
package api
import (
"context"
"fmt"
"github.com/tigorlazuardi/bluemage/go/gen/jet/model"
"github.com/tigorlazuardi/bluemage/go/pkg/telemetry"
. "github.com/go-jet/jet/v2/sqlite"
. "github.com/tigorlazuardi/bluemage/go/gen/jet/table"
)
type ImageListRequest struct {
Subreddits []string
Devices []string
Limit int64
After int64
Before int64
OrderBy string
Sort Sort
Search string
NSFW *bool
Blacklist *bool
}
// CREATE VIRTUAL TABLE images_fts5 USING fts5(
//
// post_author,
// post_title,
// post_name,
// image_relative_path,
// post_url,
// post_author_url,
// content='images',
// content_rowid='id'
//
// );
//
// matching list:
//
// - Author's name
// - Post Title
// - Post ID
// - Author's ID
// - Image filename (url path suffix to get this image from web browser)
// - Image original url
// - Post url
// - Author's url
var imagesFTSBM25 = fmt.Sprintf("bm25(%s, 25, 20, 10, 5, 3, 3)", ImagesFts5.TableName())
func (request ImageListRequest) Statement() SelectStatement {
cond := Bool(true)
from := Images
if request.NSFW != nil {
n := *request.NSFW
if n {
cond.AND(Images.Nsfw.EQ(Int(1)))
} else {
cond.AND(Images.Nsfw.EQ(Int(0)))
}
}
if request.Blacklist != nil {
b := *request.Blacklist
if b {
cond.AND(Images.Blacklisted.EQ(Int(1)))
} else {
cond.AND(Images.Blacklisted.EQ(Int(0)))
}
}
if request.After > 0 {
cond.AND(Images.CreatedAt.LT_EQ(Int(request.After)))
}
if request.Before > 0 {
cond.AND(Images.CreatedAt.GT_EQ(Int(request.Before)))
}
stmt := SELECT(Images.AllColumns).FROM(from).WHERE(cond)
if request.Limit > 0 {
stmt.LIMIT(request.Limit)
}
if len(request.Search) > 0 {
from.INNER_JOIN(ImagesFts5, Images.PostName.EQ(ImagesFts5.PostName))
cond.AND(ImagesFts5.ImagesFts5.EQ(String(request.Search)))
return stmt.ORDER_BY(RawString(imagesFTSBM25).ASC())
}
if request.OrderBy == "" {
return stmt.ORDER_BY(Images.CreatedAt.DESC())
}
orderBy := StringColumn(request.OrderBy)
if request.Sort == SortDesc {
return stmt.ORDER_BY(orderBy.DESC())
}
return stmt.ORDER_BY(orderBy.ASC())
}
func (api *API) ImageList(ctx context.Context, request ImageListRequest) (images []*model.Images, err error) {
ctx, span := tracer.Start(ctx, "ImageList")
defer func() { telemetry.EndWithStatus(span, err) }()
return images, err
}