mirror of
https://git.vectorsigma.ru/public/photoview.git
synced 2026-08-03 18:29:17 +00:00
Fix gqlgen (#1105)
This commit is contained in:
@@ -1,80 +1,26 @@
|
||||
package resolvers
|
||||
|
||||
// This file will be automatically regenerated based on the schema, any resolver implementations
|
||||
// will be copied through when generating and any unknown code will be moved to the end.
|
||||
// Code generated by github.com/99designs/gqlgen version v0.17.55
|
||||
|
||||
import (
|
||||
"context"
|
||||
"os"
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
api "github.com/photoview/photoview/api/graphql"
|
||||
"github.com/photoview/photoview/api/graphql/auth"
|
||||
"github.com/photoview/photoview/api/graphql/models"
|
||||
"github.com/photoview/photoview/api/graphql/models/actions"
|
||||
"github.com/photoview/photoview/api/scanner"
|
||||
"github.com/photoview/photoview/api/scanner/face_detection"
|
||||
"github.com/photoview/photoview/api/utils"
|
||||
"github.com/pkg/errors"
|
||||
"golang.org/x/crypto/bcrypt"
|
||||
"gorm.io/gorm"
|
||||
)
|
||||
|
||||
type userResolver struct {
|
||||
*Resolver
|
||||
}
|
||||
|
||||
func (r *Resolver) User() api.UserResolver {
|
||||
return &userResolver{r}
|
||||
}
|
||||
|
||||
func (r *queryResolver) User(ctx context.Context, order *models.Ordering,
|
||||
paginate *models.Pagination) ([]*models.User, error) {
|
||||
|
||||
var users []*models.User
|
||||
|
||||
if err := models.FormatSQL(r.DB(ctx).Model(models.User{}), order, paginate).Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
func (r *userResolver) Albums(ctx context.Context, user *models.User) ([]*models.Album, error) {
|
||||
user.FillAlbums(r.DB(ctx))
|
||||
|
||||
pointerAlbums := make([]*models.Album, len(user.Albums))
|
||||
for i, album := range user.Albums {
|
||||
pointerAlbums[i] = &album
|
||||
}
|
||||
|
||||
return pointerAlbums, nil
|
||||
}
|
||||
|
||||
func (r *userResolver) RootAlbums(ctx context.Context, user *models.User) (albums []*models.Album, err error) {
|
||||
db := r.DB(ctx)
|
||||
|
||||
err = db.Model(&user).
|
||||
Where("albums.parent_album_id NOT IN (?)",
|
||||
db.Table("user_albums").
|
||||
Select("albums.id").
|
||||
Joins("JOIN albums ON albums.id = user_albums.album_id AND user_albums.user_id = ?", user.ID),
|
||||
).Or("albums.parent_album_id IS NULL").Order("path ASC").
|
||||
Association("Albums").Find(&albums)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
func (r *queryResolver) MyUser(ctx context.Context) (*models.User, error) {
|
||||
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, auth.ErrUnauthorized
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func (r *mutationResolver) AuthorizeUser(ctx context.Context, username string,
|
||||
password string) (*models.AuthorizeResult, error) {
|
||||
// AuthorizeUser is the resolver for the authorizeUser field.
|
||||
func (r *mutationResolver) AuthorizeUser(ctx context.Context, username string, password string) (*models.AuthorizeResult, error) {
|
||||
db := r.DB(ctx)
|
||||
user, err := models.AuthorizeUser(db, username, password)
|
||||
if err != nil {
|
||||
@@ -106,8 +52,8 @@ func (r *mutationResolver) AuthorizeUser(ctx context.Context, username string,
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *mutationResolver) InitialSetupWizard(ctx context.Context, username string, password string,
|
||||
rootPath string) (*models.AuthorizeResult, error) {
|
||||
// InitialSetupWizard is the resolver for the initialSetupWizard field.
|
||||
func (r *mutationResolver) InitialSetupWizard(ctx context.Context, username string, password string, rootPath string) (*models.AuthorizeResult, error) {
|
||||
db := r.DB(ctx)
|
||||
siteInfo, err := models.GetSiteInfo(db)
|
||||
if err != nil {
|
||||
@@ -159,53 +105,8 @@ func (r *mutationResolver) InitialSetupWizard(ctx context.Context, username stri
|
||||
}, nil
|
||||
}
|
||||
|
||||
func (r *queryResolver) MyUserPreferences(ctx context.Context) (*models.UserPreferences, error) {
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, auth.ErrUnauthorized
|
||||
}
|
||||
|
||||
userPref := models.UserPreferences{
|
||||
UserID: user.ID,
|
||||
}
|
||||
if err := r.DB(ctx).Where("user_id = ?", user.ID).FirstOrCreate(&userPref).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userPref, nil
|
||||
}
|
||||
|
||||
func (r *mutationResolver) ChangeUserPreferences(ctx context.Context, language *string) (*models.UserPreferences, error) {
|
||||
db := r.DB(ctx)
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, auth.ErrUnauthorized
|
||||
}
|
||||
|
||||
var langTrans *models.LanguageTranslation = nil
|
||||
if language != nil {
|
||||
lng := models.LanguageTranslation(*language)
|
||||
langTrans = &lng
|
||||
}
|
||||
|
||||
var userPref models.UserPreferences
|
||||
if err := db.Where("user_id = ?", user.ID).FirstOrInit(&userPref).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
userPref.UserID = user.ID
|
||||
userPref.Language = langTrans
|
||||
|
||||
if err := db.Save(&userPref).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userPref, nil
|
||||
}
|
||||
|
||||
// Admin queries
|
||||
func (r *mutationResolver) UpdateUser(ctx context.Context, id int, username *string, password *string,
|
||||
admin *bool) (*models.User, error) {
|
||||
// UpdateUser is the resolver for the updateUser field.
|
||||
func (r *mutationResolver) UpdateUser(ctx context.Context, id int, username *string, password *string, admin *bool) (*models.User, error) {
|
||||
db := r.DB(ctx)
|
||||
|
||||
if username == nil && password == nil && admin == nil {
|
||||
@@ -236,15 +137,14 @@ func (r *mutationResolver) UpdateUser(ctx context.Context, id int, username *str
|
||||
}
|
||||
|
||||
if err := db.Save(&user).Error; err != nil {
|
||||
return nil, errors.Wrap(err, "failed to update user")
|
||||
return nil, fmt.Errorf("failed to update user: %w", err)
|
||||
}
|
||||
|
||||
return &user, nil
|
||||
}
|
||||
|
||||
func (r *mutationResolver) CreateUser(ctx context.Context, username string, password *string,
|
||||
admin bool) (*models.User, error) {
|
||||
|
||||
// CreateUser is the resolver for the createUser field.
|
||||
func (r *mutationResolver) CreateUser(ctx context.Context, username string, password *string, admin bool) (*models.User, error) {
|
||||
var user *models.User
|
||||
|
||||
transactionError := r.DB(ctx).Transaction(func(tx *gorm.DB) error {
|
||||
@@ -264,10 +164,12 @@ func (r *mutationResolver) CreateUser(ctx context.Context, username string, pass
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// DeleteUser is the resolver for the deleteUser field.
|
||||
func (r *mutationResolver) DeleteUser(ctx context.Context, id int) (*models.User, error) {
|
||||
return actions.DeleteUser(r.DB(ctx), id)
|
||||
}
|
||||
|
||||
// UserAddRootPath is the resolver for the userAddRootPath field.
|
||||
func (r *mutationResolver) UserAddRootPath(ctx context.Context, id int, rootPath string) (*models.Album, error) {
|
||||
db := r.DB(ctx)
|
||||
|
||||
@@ -286,6 +188,7 @@ func (r *mutationResolver) UserAddRootPath(ctx context.Context, id int, rootPath
|
||||
return newAlbum, nil
|
||||
}
|
||||
|
||||
// UserRemoveRootAlbum is the resolver for the userRemoveRootAlbum field.
|
||||
func (r *mutationResolver) UserRemoveRootAlbum(ctx context.Context, userID int, albumID int) (*models.Album, error) {
|
||||
db := r.DB(ctx)
|
||||
|
||||
@@ -339,44 +242,101 @@ func (r *mutationResolver) UserRemoveRootAlbum(ctx context.Context, userID int,
|
||||
return &album, nil
|
||||
}
|
||||
|
||||
func cleanup(tx *gorm.DB, albumID int, childAlbumIDs []int) ([]int, error) {
|
||||
var userAlbumCount int
|
||||
var deletedAlbumIDs []int = nil
|
||||
// ChangeUserPreferences is the resolver for the changeUserPreferences field.
|
||||
func (r *mutationResolver) ChangeUserPreferences(ctx context.Context, language *string) (*models.UserPreferences, error) {
|
||||
db := r.DB(ctx)
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, auth.ErrUnauthorized
|
||||
}
|
||||
|
||||
if err := tx.Raw("SELECT COUNT(user_id) FROM user_albums WHERE album_id = ?",
|
||||
albumID).Scan(&userAlbumCount).Error; err != nil {
|
||||
var langTrans *models.LanguageTranslation = nil
|
||||
if language != nil {
|
||||
lng := models.LanguageTranslation(*language)
|
||||
langTrans = &lng
|
||||
}
|
||||
|
||||
var userPref models.UserPreferences
|
||||
if err := db.Where("user_id = ?", user.ID).FirstOrInit(&userPref).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if userAlbumCount == 0 {
|
||||
deletedAlbumIDs = append(childAlbumIDs, albumID)
|
||||
childAlbumIDs = nil
|
||||
// Delete albums from database
|
||||
if err := tx.Delete(&models.Album{}, "id IN (?)", deletedAlbumIDs).Error; err != nil {
|
||||
deletedAlbumIDs = nil
|
||||
return nil, err
|
||||
}
|
||||
userPref.UserID = user.ID
|
||||
userPref.Language = langTrans
|
||||
|
||||
if err := db.Save(&userPref).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return deletedAlbumIDs, nil
|
||||
|
||||
return &userPref, nil
|
||||
}
|
||||
|
||||
func clearCacheAndReloadFaces(db *gorm.DB, deletedAlbumIDs []int) error {
|
||||
if deletedAlbumIDs != nil {
|
||||
// Delete albums from cache
|
||||
for _, id := range deletedAlbumIDs {
|
||||
cacheAlbumPath := path.Join(utils.MediaCachePath(), strconv.Itoa(id))
|
||||
// User is the resolver for the user field.
|
||||
func (r *queryResolver) User(ctx context.Context, order *models.Ordering, paginate *models.Pagination) ([]*models.User, error) {
|
||||
var users []*models.User
|
||||
|
||||
if err := os.RemoveAll(cacheAlbumPath); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
// Reload faces as media might have been deleted
|
||||
if face_detection.GlobalFaceDetector != nil {
|
||||
if err := face_detection.GlobalFaceDetector.ReloadFacesFromDatabase(db); err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
if err := models.FormatSQL(r.DB(ctx).Model(models.User{}), order, paginate).Find(&users).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil
|
||||
|
||||
return users, nil
|
||||
}
|
||||
|
||||
// MyUser is the resolver for the myUser field.
|
||||
func (r *queryResolver) MyUser(ctx context.Context) (*models.User, error) {
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, auth.ErrUnauthorized
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
// MyUserPreferences is the resolver for the myUserPreferences field.
|
||||
func (r *queryResolver) MyUserPreferences(ctx context.Context) (*models.UserPreferences, error) {
|
||||
user := auth.UserFromContext(ctx)
|
||||
if user == nil {
|
||||
return nil, auth.ErrUnauthorized
|
||||
}
|
||||
|
||||
userPref := models.UserPreferences{
|
||||
UserID: user.ID,
|
||||
}
|
||||
if err := r.DB(ctx).Where("user_id = ?", user.ID).FirstOrCreate(&userPref).Error; err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &userPref, nil
|
||||
}
|
||||
|
||||
// Albums is the resolver for the albums field.
|
||||
func (r *userResolver) Albums(ctx context.Context, obj *models.User) ([]*models.Album, error) {
|
||||
obj.FillAlbums(r.DB(ctx))
|
||||
|
||||
pointerAlbums := make([]*models.Album, len(obj.Albums))
|
||||
for i, album := range obj.Albums {
|
||||
pointerAlbums[i] = &album
|
||||
}
|
||||
|
||||
return pointerAlbums, nil
|
||||
}
|
||||
|
||||
// RootAlbums is the resolver for the rootAlbums field.
|
||||
func (r *userResolver) RootAlbums(ctx context.Context, obj *models.User) (albums []*models.Album, err error) {
|
||||
db := r.DB(ctx)
|
||||
|
||||
err = db.Model(obj).
|
||||
Where("albums.parent_album_id NOT IN (?)",
|
||||
db.Table("user_albums").
|
||||
Select("albums.id").
|
||||
Joins("JOIN albums ON albums.id = user_albums.album_id AND user_albums.user_id = ?", obj.ID),
|
||||
).Or("albums.parent_album_id IS NULL").Order("path ASC").
|
||||
Association("Albums").Find(&albums)
|
||||
|
||||
return
|
||||
}
|
||||
|
||||
// User returns api.UserResolver implementation.
|
||||
func (r *Resolver) User() api.UserResolver { return &userResolver{r} }
|
||||
|
||||
type userResolver struct{ *Resolver }
|
||||
|
||||
Reference in New Issue
Block a user