2023-03-14 15:22:52 +00:00
|
|
|
package models
|
|
|
|
|
|
|
|
import (
|
2023-03-14 23:52:24 +00:00
|
|
|
"gorm.io/gorm"
|
2023-03-14 15:22:52 +00:00
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Gist struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
Uuid string
|
|
|
|
Title string `validate:"max=50" form:"title"`
|
|
|
|
Preview string
|
|
|
|
PreviewFilename string
|
|
|
|
Description string `validate:"max=150" form:"description"`
|
|
|
|
Private bool `form:"private"`
|
|
|
|
UserID uint
|
|
|
|
User User `validate:"-"`
|
|
|
|
NbFiles int
|
|
|
|
NbLikes int
|
2023-03-14 22:26:39 +00:00
|
|
|
NbForks int
|
2023-03-14 15:22:52 +00:00
|
|
|
CreatedAt int64
|
|
|
|
UpdatedAt int64
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
Likes []User `gorm:"many2many:likes;constraint:OnUpdate:CASCADE,OnDelete:CASCADE"`
|
|
|
|
Forked *Gist `gorm:"foreignKey:ForkedID;constraint:OnUpdate:CASCADE,OnDelete:SET NULL"`
|
|
|
|
ForkedID uint
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
Files []File `gorm:"-" validate:"min=1,dive"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type File struct {
|
|
|
|
Filename string `validate:"excludes=\x2f,excludes=\x5c,max=50"`
|
|
|
|
OldFilename string `validate:"excludes=\x2f,excludes=\x5c,max=50"`
|
|
|
|
Content string `validate:"required"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type Commit struct {
|
|
|
|
Hash string
|
|
|
|
Author string
|
|
|
|
Timestamp string
|
|
|
|
Changed string
|
|
|
|
Files []File
|
|
|
|
}
|
|
|
|
|
2023-03-14 23:52:24 +00:00
|
|
|
func (g *Gist) BeforeDelete(tx *gorm.DB) error {
|
|
|
|
// Decrement fork counter if the gist was forked
|
|
|
|
err := tx.Model(&Gist{}).
|
|
|
|
Omit("updated_at").
|
|
|
|
Where("id = ?", g.ForkedID).
|
|
|
|
UpdateColumn("nb_forks", gorm.Expr("nb_forks - 1")).Error
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func GetGist(user string, gistUuid string) (*Gist, error) {
|
|
|
|
gist := new(Gist)
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Preload("User").Preload("Forked.User").
|
2023-03-14 15:22:52 +00:00
|
|
|
Where("gists.uuid = ? AND users.username like ?", gistUuid, user).
|
|
|
|
Joins("join users on gists.user_id = users.id").
|
|
|
|
First(&gist).Error
|
|
|
|
|
|
|
|
return gist, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetGistByID(gistId string) (*Gist, error) {
|
|
|
|
gist := new(Gist)
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Preload("User").Preload("Forked.User").
|
2023-03-14 15:22:52 +00:00
|
|
|
Where("gists.id = ?", gistId).
|
|
|
|
First(&gist).Error
|
|
|
|
|
|
|
|
return gist, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGistsForCurrentUser(currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Preload("User").Preload("Forked.User").
|
2023-03-14 15:22:52 +00:00
|
|
|
Where("gists.private = 0 or gists.user_id = ?", currentUserId).
|
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order(sort + "_at " + order).
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGists(offset int) ([]*Gist, error) {
|
|
|
|
var all []*Gist
|
|
|
|
err := db.Preload("User").
|
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("id asc").
|
|
|
|
Find(&all).Error
|
|
|
|
|
|
|
|
return all, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGistsFromUser(fromUser string, currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Preload("User").Preload("Forked.User").
|
2023-03-14 15:22:52 +00:00
|
|
|
Where("users.username = ? and ((gists.private = 0) or (gists.private = 1 and gists.user_id = ?))", fromUser, currentUserId).
|
|
|
|
Joins("join users on gists.user_id = users.id").
|
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("gists." + sort + "_at " + order).
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateGist(gist *Gist) error {
|
2023-03-14 22:26:39 +00:00
|
|
|
// avoids foreign key constraint error because the default value in the struct is 0
|
|
|
|
return db.Omit("forked_id").Create(&gist).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func CreateForkedGist(gist *Gist) error {
|
2023-03-14 15:22:52 +00:00
|
|
|
return db.Create(&gist).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func UpdateGist(gist *Gist) error {
|
2023-03-14 22:26:39 +00:00
|
|
|
return db.Omit("forked_id").Save(&gist).Error
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DeleteGist(gist *Gist) error {
|
|
|
|
return db.Delete(&gist).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func GistLastActiveNow(gistID uint) error {
|
|
|
|
return db.Model(&Gist{}).
|
|
|
|
Where("id = ?", gistID).
|
|
|
|
Update("updated_at", time.Now().Unix()).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func AppendUserLike(gist *Gist, user *User) error {
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Model(&gist).Omit("updated_at").Update("nb_likes", gist.NbLikes+1).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
return db.Model(&gist).Omit("updated_at").Association("Likes").Append(user)
|
|
|
|
}
|
|
|
|
|
|
|
|
func RemoveUserLike(gist *Gist, user *User) error {
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Model(&gist).Omit("updated_at").Update("nb_likes", gist.NbLikes-1).Error
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
return db.Model(&gist).Omit("updated_at").Association("Likes").Delete(user)
|
|
|
|
}
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
func IncrementGistForkCount(gist *Gist) error {
|
|
|
|
return db.Model(&gist).Omit("updated_at").Update("nb_forks", gist.NbForks+1).Error
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetForkedGist(gist *Gist, user *User) (*Gist, error) {
|
|
|
|
fork := new(Gist)
|
|
|
|
err := db.Preload("User").
|
|
|
|
Where("forked_id = ? and user_id = ?", gist.ID, user.ID).
|
|
|
|
First(&fork).Error
|
|
|
|
return fork, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetUsersLikesForGist(gist *Gist, offset int) ([]*User, error) {
|
2023-03-14 15:22:52 +00:00
|
|
|
var users []*User
|
|
|
|
err := db.Model(&gist).
|
|
|
|
Where("gist_id = ?", gist.ID).
|
|
|
|
Limit(31).
|
|
|
|
Offset(offset * 30).
|
|
|
|
Association("Likes").Find(&users)
|
|
|
|
return users, err
|
|
|
|
}
|
|
|
|
|
2023-03-14 22:26:39 +00:00
|
|
|
func GetUsersForksForGist(gist *Gist, currentUserId uint, offset int) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
|
|
|
err := db.Model(&gist).Preload("User").
|
|
|
|
Where("forked_id = ?", gist.ID).
|
|
|
|
Where("(gists.private = 0) or (gists.private = 1 and gists.user_id = ?)", currentUserId).
|
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("updated_at desc").
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
func UserCanWrite(user *User, gist *Gist) bool {
|
|
|
|
return !(user == nil) && (gist.UserID == user.ID)
|
|
|
|
}
|