Add favorites filter for timeline

This commit is contained in:
viktorstrate
2021-02-07 17:13:27 +01:00
parent d5ace2c0f8
commit 4fdb9357ea
8 changed files with 138 additions and 35 deletions

View File

@@ -152,7 +152,7 @@ type ComplexityRoot struct {
MyAlbums func(childComplexity int, filter *models.Filter, onlyRoot *bool, showEmpty *bool, onlyWithFavorites *bool) int
MyMedia func(childComplexity int, filter *models.Filter) int
MyMediaGeoJSON func(childComplexity int) int
MyTimeline func(childComplexity int) int
MyTimeline func(childComplexity int, onlyFavorites *bool) int
MyUser func(childComplexity int) int
Search func(childComplexity int, query string, limitMedia *int, limitAlbums *int) int
ShareToken func(childComplexity int, token string, password *string) int
@@ -272,7 +272,7 @@ type QueryResolver interface {
MyMedia(ctx context.Context, filter *models.Filter) ([]*models.Media, error)
Media(ctx context.Context, id int) (*models.Media, error)
MediaList(ctx context.Context, ids []int) ([]*models.Media, error)
MyTimeline(ctx context.Context) ([]*models.TimelineGroup, error)
MyTimeline(ctx context.Context, onlyFavorites *bool) ([]*models.TimelineGroup, error)
MyMediaGeoJSON(ctx context.Context) (interface{}, error)
MapboxToken(ctx context.Context) (*string, error)
ShareToken(ctx context.Context, token string, password *string) (*models.ShareToken, error)
@@ -945,7 +945,12 @@ func (e *executableSchema) Complexity(typeName, field string, childComplexity in
break
}
return e.complexity.Query.MyTimeline(childComplexity), true
args, err := ec.field_Query_myTimeline_args(context.TODO(), rawArgs)
if err != nil {
return 0, false
}
return e.complexity.Query.MyTimeline(childComplexity, args["onlyFavorites"].(*bool)), true
case "Query.myUser":
if e.complexity.Query.MyUser == nil {
@@ -1395,7 +1400,7 @@ type Query {
"Get a list of media by their ids, user must own the media or be admin"
mediaList(ids: [ID!]!): [Media!]!
myTimeline: [TimelineGroup!]!
myTimeline(onlyFavorites: Boolean): [TimelineGroup!]!
"Get media owned by the logged in user, returned in GeoJson format"
myMediaGeoJson: Any!
@@ -2187,6 +2192,21 @@ func (ec *executionContext) field_Query_myMedia_args(ctx context.Context, rawArg
return args, nil
}
func (ec *executionContext) field_Query_myTimeline_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
var err error
args := map[string]interface{}{}
var arg0 *bool
if tmp, ok := rawArgs["onlyFavorites"]; ok {
ctx := graphql.WithPathContext(ctx, graphql.NewPathWithField("onlyFavorites"))
arg0, err = ec.unmarshalOBoolean2ᚖbool(ctx, tmp)
if err != nil {
return nil, err
}
}
args["onlyFavorites"] = arg0
return args, nil
}
func (ec *executionContext) field_Query_search_args(ctx context.Context, rawArgs map[string]interface{}) (map[string]interface{}, error) {
var err error
args := map[string]interface{}{}
@@ -5205,9 +5225,16 @@ func (ec *executionContext) _Query_myTimeline(ctx context.Context, field graphql
}
ctx = graphql.WithFieldContext(ctx, fc)
rawArgs := field.ArgumentMap(ec.Variables)
args, err := ec.field_Query_myTimeline_args(ctx, rawArgs)
if err != nil {
ec.Error(ctx, err)
return graphql.Null
}
fc.Args = args
resTmp, err := ec.ResolverMiddleware(ctx, func(rctx context.Context) (interface{}, error) {
ctx = rctx // use context from middleware stack in children
return ec.resolvers.Query().MyTimeline(rctx)
return ec.resolvers.Query().MyTimeline(rctx, args["onlyFavorites"].(*bool))
})
if err != nil {
ec.Error(ctx, err)

View File

@@ -4,17 +4,22 @@ import (
"context"
"time"
"github.com/photoview/photoview/api/graphql/auth"
"github.com/photoview/photoview/api/graphql/models"
"gorm.io/gorm"
)
func (r *queryResolver) MyTimeline(ctx context.Context) ([]*models.TimelineGroup, error) {
func (r *queryResolver) MyTimeline(ctx context.Context, onlyFavorites *bool) ([]*models.TimelineGroup, error) {
user := auth.UserFromContext(ctx)
if user == nil {
return nil, auth.ErrUnauthorized
}
var timelineGroups []*models.TimelineGroup
transactionError := r.Database.Transaction(func(tx *gorm.DB) error {
// album_id, year, month, day
rows, err := tx.Select(
daysQuery := tx.Select(
"albums.id AS album_id",
"YEAR(media.date_shot) AS year",
"MONTH(media.date_shot) AS month",
@@ -22,7 +27,13 @@ func (r *queryResolver) MyTimeline(ctx context.Context) ([]*models.TimelineGroup
).
Table("media").
Joins("JOIN albums ON media.album_id = albums.id").
Group("albums.id, YEAR(media.date_shot), MONTH(media.date_shot), DAY(media.date_shot)").
Where("albums.id IN (?)", tx.Table("user_albums").Select("user_albums.album_id").Where("user_id = ?", user.ID))
if onlyFavorites != nil && *onlyFavorites == true {
daysQuery.Where("media.id IN (?)", tx.Table("user_media_data").Select("user_media_data.media_id").Where("user_media_data.user_id = ?", user.ID).Where("user_media_data.favorite = 1"))
}
rows, err := daysQuery.Group("albums.id, YEAR(media.date_shot), MONTH(media.date_shot), DAY(media.date_shot)").
Order("media.date_shot DESC").
Rows()
@@ -59,23 +70,21 @@ func (r *queryResolver) MyTimeline(ctx context.Context) ([]*models.TimelineGroup
// Fill media
var groupMedia []*models.Media
err := tx.Model(&models.Media{}).
mediaQuery := tx.Model(&models.Media{}).
Where("album_id = ? AND YEAR(date_shot) = ? AND MONTH(date_shot) = ? AND DAY(date_shot) = ?", group.albumID, group.year, group.month, group.day).
Order("date_shot DESC").
Limit(5).
Find(&groupMedia).Error
Order("date_shot DESC")
if err != nil {
if onlyFavorites != nil && *onlyFavorites == true {
mediaQuery.Where("media.id IN (?)", tx.Table("user_media_data").Select("user_media_data.media_id").Where("user_media_data.user_id = ?", user.ID).Where("user_media_data.favorite = 1"))
}
if err := mediaQuery.Limit(5).Find(&groupMedia).Error; err != nil {
return err
}
// Get total media count
var totalMedia int64
err = tx.Model(&models.Media{}).
Where("album_id = ? AND YEAR(date_shot) = ? AND MONTH(date_shot) = ? AND DAY(date_shot) = ?", group.albumID, group.year, group.month, group.day).
Count(&totalMedia).Error
if err != nil {
if err := mediaQuery.Count(&totalMedia).Error; err != nil {
return err
}

View File

@@ -44,7 +44,7 @@ type Query {
"Get a list of media by their ids, user must own the media or be admin"
mediaList(ids: [ID!]!): [Media!]!
myTimeline: [TimelineGroup!]!
myTimeline(onlyFavorites: Boolean): [TimelineGroup!]!
"Get media owned by the logged in user, returned in GeoJson format"
myMediaGeoJson: Any!