2023-09-02 22:30:57 +00:00
|
|
|
package db
|
2023-03-14 15:22:52 +00:00
|
|
|
|
|
|
|
import (
|
2023-12-15 01:14:59 +00:00
|
|
|
"fmt"
|
2024-01-04 02:38:15 +00:00
|
|
|
"github.com/alecthomas/chroma/v2"
|
|
|
|
"github.com/alecthomas/chroma/v2/lexers"
|
2023-12-19 01:47:14 +00:00
|
|
|
"github.com/dustin/go-humanize"
|
2024-01-04 02:38:15 +00:00
|
|
|
"github.com/rs/zerolog/log"
|
|
|
|
"github.com/thomiceli/opengist/internal/index"
|
2023-03-18 15:18:24 +00:00
|
|
|
"os/exec"
|
2024-01-04 02:38:15 +00:00
|
|
|
"path/filepath"
|
2023-03-18 23:27:30 +00:00
|
|
|
"strings"
|
2023-03-14 15:22:52 +00:00
|
|
|
"time"
|
2023-12-15 01:14:59 +00:00
|
|
|
|
|
|
|
"github.com/thomiceli/opengist/internal/git"
|
|
|
|
"gorm.io/gorm"
|
|
|
|
)
|
|
|
|
|
|
|
|
type Visibility int
|
|
|
|
|
|
|
|
const (
|
|
|
|
PublicVisibility Visibility = iota
|
|
|
|
UnlistedVisibility
|
|
|
|
PrivateVisibility
|
2023-03-14 15:22:52 +00:00
|
|
|
)
|
|
|
|
|
2023-12-15 01:14:59 +00:00
|
|
|
func (v Visibility) Next() Visibility {
|
|
|
|
switch v {
|
|
|
|
case PublicVisibility:
|
|
|
|
return UnlistedVisibility
|
|
|
|
case UnlistedVisibility:
|
|
|
|
return PrivateVisibility
|
|
|
|
default:
|
|
|
|
return PublicVisibility
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func ParseVisibility[T string | int](v T) (Visibility, error) {
|
|
|
|
switch s := fmt.Sprint(v); s {
|
2024-01-29 23:07:57 +00:00
|
|
|
case "0", "public":
|
2023-12-15 01:14:59 +00:00
|
|
|
return PublicVisibility, nil
|
2024-01-29 23:07:57 +00:00
|
|
|
case "1", "unlisted":
|
2023-12-15 01:14:59 +00:00
|
|
|
return UnlistedVisibility, nil
|
2024-01-29 23:07:57 +00:00
|
|
|
case "2", "private":
|
2023-12-15 01:14:59 +00:00
|
|
|
return PrivateVisibility, nil
|
|
|
|
default:
|
|
|
|
return -1, fmt.Errorf("unknown visibility %q", s)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
type Gist struct {
|
|
|
|
ID uint `gorm:"primaryKey"`
|
|
|
|
Uuid string
|
2023-03-17 13:56:39 +00:00
|
|
|
Title string
|
2023-12-26 02:24:04 +00:00
|
|
|
URL string
|
2023-03-14 15:22:52 +00:00
|
|
|
Preview string
|
|
|
|
PreviewFilename string
|
2023-03-17 13:56:39 +00:00
|
|
|
Description string
|
2023-12-15 01:14:59 +00:00
|
|
|
Private Visibility // 0: public, 1: unlisted, 2: private
|
2023-03-14 15:22:52 +00:00
|
|
|
UserID uint
|
2023-03-17 13:56:39 +00:00
|
|
|
User User
|
2023-03-14 15:22:52 +00:00
|
|
|
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
|
|
|
}
|
|
|
|
|
2023-06-21 16:19:17 +00:00
|
|
|
type Like struct {
|
|
|
|
UserID uint `gorm:"primaryKey"`
|
|
|
|
GistID uint `gorm:"primaryKey"`
|
|
|
|
CreatedAt int64
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) BeforeDelete(tx *gorm.DB) error {
|
2023-03-14 23:52:24 +00:00
|
|
|
// Decrement fork counter if the gist was forked
|
|
|
|
err := tx.Model(&Gist{}).
|
|
|
|
Omit("updated_at").
|
2023-03-17 13:56:39 +00:00
|
|
|
Where("id = ?", gist.ForkedID).
|
2023-03-14 23:52:24 +00:00
|
|
|
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-12-26 02:24:04 +00:00
|
|
|
Where("(gists.uuid = ? OR gists.url = ?) AND users.username like ?", gistUuid, gistUuid, user).
|
2023-03-14 15:22:52 +00:00
|
|
|
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) {
|
2023-03-17 13:56:39 +00:00
|
|
|
var gists []*Gist
|
2023-03-14 15:22:52 +00:00
|
|
|
err := db.Preload("User").
|
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("id asc").
|
2023-03-17 13:56:39 +00:00
|
|
|
Find(&gists).Error
|
2023-03-14 15:22:52 +00:00
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
return gists, err
|
2023-03-14 15:22:52 +00:00
|
|
|
}
|
|
|
|
|
2023-06-21 16:19:17 +00:00
|
|
|
func GetAllGistsFromSearch(currentUserId uint, query string, offset int, sort string, order string) ([]*Gist, error) {
|
2023-03-14 15:22:52 +00:00
|
|
|
var gists []*Gist
|
2023-03-14 22:26:39 +00:00
|
|
|
err := db.Preload("User").Preload("Forked.User").
|
2023-09-02 01:58:37 +00:00
|
|
|
Where("((gists.private = 0) or (gists.private > 0 and gists.user_id = ?))", currentUserId).
|
2023-06-21 16:19:17 +00:00
|
|
|
Where("gists.title like ? or gists.description like ?", "%"+query+"%", "%"+query+"%").
|
2023-03-14 15:22:52 +00:00
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("gists." + sort + "_at " + order).
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
2023-06-21 16:19:17 +00:00
|
|
|
func gistsFromUserStatement(fromUserId uint, currentUserId uint) *gorm.DB {
|
|
|
|
return db.Preload("User").Preload("Forked.User").
|
2023-09-02 01:58:37 +00:00
|
|
|
Where("((gists.private = 0) or (gists.private > 0 and gists.user_id = ?))", currentUserId).
|
2023-06-21 16:19:17 +00:00
|
|
|
Where("users.id = ?", fromUserId).
|
|
|
|
Joins("join users on gists.user_id = users.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGistsFromUser(fromUserId uint, currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
|
|
|
err := gistsFromUserStatement(fromUserId, currentUserId).Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("gists." + sort + "_at " + order).
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CountAllGistsFromUser(fromUserId uint, currentUserId uint) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := gistsFromUserStatement(fromUserId, currentUserId).Model(&Gist{}).Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func likedStatement(fromUserId uint, currentUserId uint) *gorm.DB {
|
|
|
|
return db.Preload("User").Preload("Forked.User").
|
2023-09-02 01:58:37 +00:00
|
|
|
Where("((gists.private = 0) or (gists.private > 0 and gists.user_id = ?))", currentUserId).
|
2023-06-21 16:19:17 +00:00
|
|
|
Where("likes.user_id = ?", fromUserId).
|
|
|
|
Joins("join likes on gists.id = likes.gist_id").
|
|
|
|
Joins("join users on likes.user_id = users.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGistsLikedByUser(fromUserId uint, currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
|
|
|
err := likedStatement(fromUserId, currentUserId).Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("gists." + sort + "_at " + order).
|
|
|
|
Find(&gists).Error
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CountAllGistsLikedByUser(fromUserId uint, currentUserId uint) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := likedStatement(fromUserId, currentUserId).Model(&Gist{}).Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func forkedStatement(fromUserId uint, currentUserId uint) *gorm.DB {
|
|
|
|
return db.Preload("User").Preload("Forked.User").
|
2023-09-02 01:58:37 +00:00
|
|
|
Where("gists.forked_id is not null and ((gists.private = 0) or (gists.private > 0 and gists.user_id = ?))", currentUserId).
|
2023-06-21 16:19:17 +00:00
|
|
|
Where("gists.user_id = ?", fromUserId).
|
|
|
|
Joins("join users on gists.user_id = users.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGistsForkedByUser(fromUserId uint, currentUserId uint, offset int, sort string, order string) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
|
|
|
err := forkedStatement(fromUserId, currentUserId).Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("gists." + sort + "_at " + order).
|
|
|
|
Find(&gists).Error
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func CountAllGistsForkedByUser(fromUserId uint, currentUserId uint) (int64, error) {
|
|
|
|
var count int64
|
|
|
|
err := forkedStatement(fromUserId, currentUserId).Model(&Gist{}).Count(&count).Error
|
|
|
|
return count, err
|
|
|
|
}
|
|
|
|
|
2023-03-19 00:51:25 +00:00
|
|
|
func GetAllGistsRows() ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
|
|
|
err := db.Table("gists").
|
|
|
|
Preload("User").
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
func GetAllGistsVisibleByUser(userId uint) ([]uint, error) {
|
|
|
|
var gists []uint
|
|
|
|
|
|
|
|
err := db.Table("gists").
|
|
|
|
Where("gists.private = 0 or gists.user_id = ?", userId).
|
|
|
|
Pluck("gists.id", &gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
|
|
|
func GetAllGistsByIds(ids []uint) ([]*Gist, error) {
|
|
|
|
var gists []*Gist
|
|
|
|
err := db.Preload("User").Preload("Forked.User").
|
|
|
|
Where("id in ?", ids).
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) Create() 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
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) CreateForked() error {
|
2023-03-14 15:22:52 +00:00
|
|
|
return db.Create(&gist).Error
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) Update() 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
|
|
|
}
|
|
|
|
|
2024-01-02 03:01:45 +00:00
|
|
|
func (gist *Gist) UpdateNoTimestamps() error {
|
|
|
|
return db.Omit("forked_id", "updated_at").Save(&gist).Error
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) Delete() error {
|
2023-09-09 17:39:57 +00:00
|
|
|
err := gist.DeleteRepository()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-14 15:22:52 +00:00
|
|
|
return db.Delete(&gist).Error
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) SetLastActiveNow() error {
|
2023-03-14 15:22:52 +00:00
|
|
|
return db.Model(&Gist{}).
|
2023-03-17 13:56:39 +00:00
|
|
|
Where("id = ?", gist.ID).
|
2023-03-14 15:22:52 +00:00
|
|
|
Update("updated_at", time.Now().Unix()).Error
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) AppendUserLike(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)
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) RemoveUserLike(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-17 13:56:39 +00:00
|
|
|
func (gist *Gist) IncrementForkCount() error {
|
2023-03-14 22:26:39 +00:00
|
|
|
return db.Model(&gist).Omit("updated_at").Update("nb_forks", gist.NbForks+1).Error
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) GetForkParent(user *User) (*Gist, error) {
|
2023-03-14 22:26:39 +00:00
|
|
|
fork := new(Gist)
|
|
|
|
err := db.Preload("User").
|
|
|
|
Where("forked_id = ? and user_id = ?", gist.ID, user.ID).
|
|
|
|
First(&fork).Error
|
|
|
|
return fork, err
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) GetUsersLikes(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-17 13:56:39 +00:00
|
|
|
func (gist *Gist) GetForks(currentUserId uint, offset int) ([]*Gist, error) {
|
2023-03-14 22:26:39 +00:00
|
|
|
var gists []*Gist
|
|
|
|
err := db.Model(&gist).Preload("User").
|
|
|
|
Where("forked_id = ?", gist.ID).
|
2023-09-02 01:58:37 +00:00
|
|
|
Where("(gists.private = 0) or (gists.private > 0 and gists.user_id = ?)", currentUserId).
|
2023-03-14 22:26:39 +00:00
|
|
|
Limit(11).
|
|
|
|
Offset(offset * 10).
|
|
|
|
Order("updated_at desc").
|
|
|
|
Find(&gists).Error
|
|
|
|
|
|
|
|
return gists, err
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
func (gist *Gist) CanWrite(user *User) bool {
|
2023-03-14 15:22:52 +00:00
|
|
|
return !(user == nil) && (gist.UserID == user.ID)
|
|
|
|
}
|
2023-03-17 13:56:39 +00:00
|
|
|
|
2023-03-18 15:18:24 +00:00
|
|
|
func (gist *Gist) InitRepository() error {
|
|
|
|
return git.InitRepository(gist.User.Username, gist.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) DeleteRepository() error {
|
|
|
|
return git.DeleteRepository(gist.User.Username, gist.Uuid)
|
|
|
|
}
|
|
|
|
|
2023-12-27 11:11:02 +00:00
|
|
|
func (gist *Gist) Files(revision string, truncate bool) ([]*git.File, error) {
|
2023-12-30 22:46:14 +00:00
|
|
|
filesCat, err := git.CatFileBatch(gist.User.Username, gist.Uuid, revision, truncate)
|
2023-03-18 15:18:24 +00:00
|
|
|
if err != nil {
|
|
|
|
// if the revision or the file do not exist
|
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok && exiterr.ExitCode() == 128 {
|
2023-12-27 11:11:02 +00:00
|
|
|
return nil, &git.RevisionNotFoundError{}
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-12-30 22:46:14 +00:00
|
|
|
var files []*git.File
|
|
|
|
for _, fileCat := range filesCat {
|
|
|
|
files = append(files, &git.File{
|
|
|
|
Filename: fileCat.Name,
|
|
|
|
Size: fileCat.Size,
|
|
|
|
HumanSize: humanize.IBytes(fileCat.Size),
|
|
|
|
Content: fileCat.Content,
|
|
|
|
Truncated: fileCat.Truncated,
|
|
|
|
})
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|
|
|
|
return files, err
|
|
|
|
}
|
|
|
|
|
2023-03-18 22:18:20 +00:00
|
|
|
func (gist *Gist) File(revision string, filename string, truncate bool) (*git.File, error) {
|
2023-03-18 15:18:24 +00:00
|
|
|
content, truncated, err := git.GetFileContent(gist.User.Username, gist.Uuid, revision, filename, truncate)
|
|
|
|
|
|
|
|
// if the revision or the file do not exist
|
|
|
|
if exiterr, ok := err.(*exec.ExitError); ok && exiterr.ExitCode() == 128 {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
2023-12-21 19:00:04 +00:00
|
|
|
var size uint64
|
2023-12-19 01:47:14 +00:00
|
|
|
|
|
|
|
size, err = git.GetFileSize(gist.User.Username, gist.Uuid, revision, filename)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-03-18 22:18:20 +00:00
|
|
|
return &git.File{
|
2023-03-18 15:18:24 +00:00
|
|
|
Filename: filename,
|
2023-12-21 19:00:04 +00:00
|
|
|
Size: size,
|
|
|
|
HumanSize: humanize.IBytes(size),
|
2023-03-18 15:18:24 +00:00
|
|
|
Content: content,
|
|
|
|
Truncated: truncated,
|
|
|
|
}, err
|
|
|
|
}
|
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
func (gist *Gist) FileNames(revision string) ([]string, error) {
|
|
|
|
return git.GetFilesOfRepository(gist.User.Username, gist.Uuid, revision)
|
|
|
|
}
|
|
|
|
|
2023-04-16 14:14:12 +00:00
|
|
|
func (gist *Gist) Log(skip int) ([]*git.Commit, error) {
|
2023-03-18 22:18:20 +00:00
|
|
|
return git.GetLog(gist.User.Username, gist.Uuid, skip)
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) NbCommits() (string, error) {
|
2023-09-16 22:59:47 +00:00
|
|
|
return git.CountCommits(gist.User.Username, gist.Uuid)
|
2023-03-18 15:18:24 +00:00
|
|
|
}
|
|
|
|
|
2023-03-18 22:23:23 +00:00
|
|
|
func (gist *Gist) AddAndCommitFiles(files *[]FileDTO) error {
|
2023-12-26 02:23:47 +00:00
|
|
|
if err := git.CloneTmp(gist.User.Username, gist.Uuid, gist.Uuid, gist.User.Email, true); err != nil {
|
2023-03-18 15:18:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range *files {
|
|
|
|
if err := git.SetFileContent(gist.Uuid, file.Filename, file.Content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.AddAll(gist.Uuid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-03-20 12:30:25 +00:00
|
|
|
if err := git.CommitRepository(gist.Uuid, gist.User.Username, gist.User.Email); err != nil {
|
2023-12-26 02:23:47 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return git.Push(gist.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) AddAndCommitFile(file *FileDTO) error {
|
|
|
|
if err := git.CloneTmp(gist.User.Username, gist.Uuid, gist.Uuid, gist.User.Email, false); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.SetFileContent(gist.Uuid, file.Filename, file.Content); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.AddAll(gist.Uuid); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := git.CommitRepository(gist.Uuid, gist.User.Username, gist.User.Email); err != nil {
|
2023-03-18 15:18:24 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return git.Push(gist.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) ForkClone(username string, uuid string) error {
|
|
|
|
return git.ForkClone(gist.User.Username, gist.Uuid, username, uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) UpdateServerInfo() error {
|
|
|
|
return git.UpdateServerInfo(gist.User.Username, gist.Uuid)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) RPC(service string) ([]byte, error) {
|
|
|
|
return git.RPC(gist.User.Username, gist.Uuid, service)
|
|
|
|
}
|
|
|
|
|
2024-01-02 03:01:45 +00:00
|
|
|
func (gist *Gist) UpdatePreviewAndCount(withTimestampUpdate bool) error {
|
2023-03-18 23:27:30 +00:00
|
|
|
filesStr, err := git.GetFilesOfRepository(gist.User.Username, gist.Uuid, "HEAD")
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
gist.NbFiles = len(filesStr)
|
|
|
|
|
|
|
|
if len(filesStr) == 0 {
|
|
|
|
gist.Preview = ""
|
|
|
|
gist.PreviewFilename = ""
|
|
|
|
} else {
|
|
|
|
file, err := gist.File("HEAD", filesStr[0], true)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
split := strings.Split(file.Content, "\n")
|
|
|
|
if len(split) > 10 {
|
|
|
|
gist.Preview = strings.Join(split[:10], "\n")
|
|
|
|
} else {
|
|
|
|
gist.Preview = file.Content
|
|
|
|
}
|
|
|
|
|
|
|
|
gist.PreviewFilename = file.Filename
|
|
|
|
}
|
|
|
|
|
2024-01-02 03:01:45 +00:00
|
|
|
if withTimestampUpdate {
|
|
|
|
return gist.Update()
|
|
|
|
}
|
|
|
|
return gist.UpdateNoTimestamps()
|
2023-03-18 23:27:30 +00:00
|
|
|
}
|
|
|
|
|
2023-12-21 19:00:04 +00:00
|
|
|
func (gist *Gist) VisibilityStr() string {
|
|
|
|
switch gist.Private {
|
|
|
|
case PublicVisibility:
|
|
|
|
return "public"
|
|
|
|
case UnlistedVisibility:
|
|
|
|
return "unlisted"
|
|
|
|
case PrivateVisibility:
|
|
|
|
return "private"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-12-26 02:24:04 +00:00
|
|
|
func (gist *Gist) Identifier() string {
|
|
|
|
if gist.URL != "" {
|
|
|
|
return gist.URL
|
|
|
|
}
|
|
|
|
return gist.Uuid
|
|
|
|
}
|
|
|
|
|
2024-01-04 02:38:15 +00:00
|
|
|
func (gist *Gist) GetLanguagesFromFiles() ([]string, error) {
|
|
|
|
files, err := gist.Files("HEAD", true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
languages := make([]string, 0, len(files))
|
|
|
|
for _, file := range files {
|
|
|
|
var lexer chroma.Lexer
|
|
|
|
if lexer = lexers.Get(file.Filename); lexer == nil {
|
|
|
|
lexer = lexers.Fallback
|
|
|
|
}
|
|
|
|
|
|
|
|
fileType := lexer.Config().Name
|
|
|
|
if lexer.Config().Name == "fallback" || lexer.Config().Name == "plaintext" {
|
|
|
|
fileType = "Text"
|
|
|
|
}
|
|
|
|
|
|
|
|
languages = append(languages, fileType)
|
|
|
|
}
|
|
|
|
|
|
|
|
return languages, nil
|
|
|
|
}
|
|
|
|
|
2023-03-17 13:56:39 +00:00
|
|
|
// -- DTO -- //
|
|
|
|
|
|
|
|
type GistDTO struct {
|
2023-12-15 01:14:59 +00:00
|
|
|
Title string `validate:"max=250" form:"title"`
|
|
|
|
Description string `validate:"max=1000" form:"description"`
|
2023-12-26 02:24:04 +00:00
|
|
|
URL string `validate:"max=32,alphanumdashorempty" form:"url"`
|
2023-12-15 01:14:59 +00:00
|
|
|
Private Visibility `validate:"number,min=0,max=2" form:"private"`
|
|
|
|
Files []FileDTO `validate:"min=1,dive"`
|
|
|
|
Name []string `form:"name"`
|
|
|
|
Content []string `form:"content"`
|
2023-03-18 22:23:23 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type FileDTO struct {
|
2023-10-04 16:48:02 +00:00
|
|
|
Filename string `validate:"excludes=\x2f,excludes=\x5c,max=255"`
|
2023-03-18 22:23:23 +00:00
|
|
|
Content string `validate:"required"`
|
2023-03-17 13:56:39 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (dto *GistDTO) ToGist() *Gist {
|
|
|
|
return &Gist{
|
|
|
|
Title: dto.Title,
|
|
|
|
Description: dto.Description,
|
|
|
|
Private: dto.Private,
|
2023-12-26 02:24:04 +00:00
|
|
|
URL: dto.URL,
|
2023-03-17 13:56:39 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dto *GistDTO) ToExistingGist(gist *Gist) *Gist {
|
|
|
|
gist.Title = dto.Title
|
|
|
|
gist.Description = dto.Description
|
2023-12-26 02:24:04 +00:00
|
|
|
gist.URL = dto.URL
|
2023-03-17 13:56:39 +00:00
|
|
|
return gist
|
|
|
|
}
|
2024-01-04 02:38:15 +00:00
|
|
|
|
|
|
|
// -- Index -- //
|
|
|
|
|
|
|
|
func (gist *Gist) ToIndexedGist() (*index.Gist, error) {
|
|
|
|
files, err := gist.Files("HEAD", true)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
exts := make([]string, 0, len(files))
|
|
|
|
wholeContent := ""
|
|
|
|
for _, file := range files {
|
|
|
|
wholeContent += file.Content
|
|
|
|
exts = append(exts, filepath.Ext(file.Filename))
|
|
|
|
}
|
|
|
|
|
|
|
|
fileNames, err := gist.FileNames("HEAD")
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
langs, err := gist.GetLanguagesFromFiles()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
indexedGist := &index.Gist{
|
|
|
|
GistID: gist.ID,
|
|
|
|
Username: gist.User.Username,
|
|
|
|
Title: gist.Title,
|
|
|
|
Content: wholeContent,
|
|
|
|
Filenames: fileNames,
|
|
|
|
Extensions: exts,
|
|
|
|
Languages: langs,
|
|
|
|
CreatedAt: gist.CreatedAt,
|
|
|
|
UpdatedAt: gist.UpdatedAt,
|
|
|
|
}
|
|
|
|
|
|
|
|
return indexedGist, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) AddInIndex() {
|
|
|
|
if !index.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
indexedGist, err := gist.ToIndexedGist()
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Cannot convert gist %d to indexed gist", gist.ID)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
err = index.AddInIndex(indexedGist)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error adding gist %d to index", gist.ID)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (gist *Gist) RemoveFromIndex() {
|
|
|
|
if !index.Enabled() {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
err := index.RemoveFromIndex(gist.ID)
|
|
|
|
if err != nil {
|
|
|
|
log.Error().Err(err).Msgf("Error remove gist %d from index", gist.ID)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
}
|