Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: default visibility #155

Merged
merged 1 commit into from
Dec 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
54 changes: 44 additions & 10 deletions internal/db/gist.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,56 @@
package db

import (
"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/git"
"gorm.io/gorm"
"fmt"
"os/exec"
"strings"
"time"

"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/git"
"gorm.io/gorm"
)

type Visibility int

const (
PublicVisibility Visibility = iota
UnlistedVisibility
PrivateVisibility
)

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 {
case "0":
return PublicVisibility, nil
case "1":
return UnlistedVisibility, nil
case "2":
return PrivateVisibility, nil
default:
return -1, fmt.Errorf("unknown visibility %q", s)
}
}

type Gist struct {
ID uint `gorm:"primaryKey"`
Uuid string
Title string
Preview string
PreviewFilename string
Description string
Private int // 0: public, 1: unlisted, 2: private
Private Visibility // 0: public, 1: unlisted, 2: private
UserID uint
User User
NbFiles int
Expand Down Expand Up @@ -386,12 +420,12 @@ func (gist *Gist) UpdatePreviewAndCount() error {
// -- DTO -- //

type GistDTO struct {
Title string `validate:"max=250" form:"title"`
Description string `validate:"max=1000" form:"description"`
Private int `validate:"number,min=0,max=2" form:"private"`
Files []FileDTO `validate:"min=1,dive"`
Name []string `form:"name"`
Content []string `form:"content"`
Title string `validate:"max=250" form:"title"`
Description string `validate:"max=1000" form:"description"`
Private Visibility `validate:"number,min=0,max=2" form:"private"`
Files []FileDTO `validate:"min=1,dive"`
Name []string `form:"name"`
Content []string `form:"content"`
}

type FileDTO struct {
Expand Down
1 change: 0 additions & 1 deletion internal/db/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,6 @@ func GetUsersFromEmails(emailsSet map[string]struct{}) (map[string]*User, error)
err := db.
Where("email IN ?", emails).
Find(&users).Error

if err != nil {
return nil, err
}
Expand Down
33 changes: 16 additions & 17 deletions internal/web/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ import (
"archive/zip"
"bytes"
"errors"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/config"
"github.com/thomiceli/opengist/internal/db"
"gorm.io/gorm"
"html/template"
"net/url"
"regexp"
"strconv"
"strings"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/thomiceli/opengist/internal/config"
"github.com/thomiceli/opengist/internal/db"
"gorm.io/gorm"
)

func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
Expand All @@ -30,7 +31,7 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc {
return notFound("Gist not found")
}

if gist.Private == 2 {
if gist.Private == db.PrivateVisibility {
if currUser == nil || currUser.ID != gist.UserID {
return notFound("Gist not found")
}
Expand Down Expand Up @@ -433,7 +434,7 @@ func processCreate(ctx echo.Context) error {
}

func toggleVisibility(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)

gist.Private = (gist.Private + 1) % 3
if err := gist.Update(); err != nil {
Expand All @@ -445,7 +446,7 @@ func toggleVisibility(ctx echo.Context) error {
}

func deleteGist(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)

if err := gist.Delete(); err != nil {
return errorRes(500, "Error deleting this gist", err)
Expand All @@ -456,7 +457,7 @@ func deleteGist(ctx echo.Context) error {
}

func like(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)
currentUser := getUserLogged(ctx)

hasLiked, err := currentUser.HasLiked(gist)
Expand All @@ -482,7 +483,7 @@ func like(ctx echo.Context) error {
}

func fork(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)
currentUser := getUserLogged(ctx)

alreadyForked, err := gist.GetForkParent(currentUser)
Expand Down Expand Up @@ -535,7 +536,6 @@ func fork(ctx echo.Context) error {
func rawFile(ctx echo.Context) error {
gist := getData(ctx, "gist").(*db.Gist)
file, err := gist.File(ctx.Param("revision"), ctx.Param("file"), false)

if err != nil {
return errorRes(500, "Error getting file content", err)
}
Expand All @@ -550,7 +550,6 @@ func rawFile(ctx echo.Context) error {
func downloadFile(ctx echo.Context) error {
gist := getData(ctx, "gist").(*db.Gist)
file, err := gist.File(ctx.Param("revision"), ctx.Param("file"), false)

if err != nil {
return errorRes(500, "Error getting file content", err)
}
Expand All @@ -572,7 +571,7 @@ func downloadFile(ctx echo.Context) error {
}

func edit(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)

files, err := gist.Files("HEAD")
if err != nil {
Expand All @@ -586,8 +585,8 @@ func edit(ctx echo.Context) error {
}

func downloadZip(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
var revision = ctx.Param("revision")
gist := getData(ctx, "gist").(*db.Gist)
revision := ctx.Param("revision")

files, err := gist.Files(revision)
if err != nil {
Expand Down Expand Up @@ -631,7 +630,7 @@ func downloadZip(ctx echo.Context) error {
}

func likes(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)

pageInt := getPage(ctx)

Expand All @@ -650,7 +649,7 @@ func likes(ctx echo.Context) error {
}

func forks(ctx echo.Context) error {
var gist = getData(ctx, "gist").(*db.Gist)
gist := getData(ctx, "gist").(*db.Gist)
pageInt := getPage(ctx)

currentUser := getUserLogged(ctx)
Expand Down
17 changes: 9 additions & 8 deletions internal/web/git_http.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,6 @@ import (
"encoding/base64"
"errors"
"fmt"
"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/db"
"github.com/thomiceli/opengist/internal/git"
"github.com/thomiceli/opengist/internal/memdb"
"gorm.io/gorm"
"net/http"
"os"
"os/exec"
Expand All @@ -21,6 +14,14 @@ import (
"strconv"
"strings"
"time"

"github.com/google/uuid"
"github.com/labstack/echo/v4"
"github.com/rs/zerolog/log"
"github.com/thomiceli/opengist/internal/db"
"github.com/thomiceli/opengist/internal/git"
"github.com/thomiceli/opengist/internal/memdb"
"gorm.io/gorm"
)

var routes = []struct {
Expand Down Expand Up @@ -73,7 +74,7 @@ func gitHttp(ctx echo.Context) error {
// - user wants to clone/pull a private gist
// - gist is not found (obfuscation)
// - admin setting to require login is set to true
if isPull && gist.Private != 2 && gist.ID != 0 && !getData(ctx, "RequireLogin").(bool) {
if isPull && gist.Private != db.PrivateVisibility && gist.ID != 0 && !getData(ctx, "RequireLogin").(bool) {
return route.handler(ctx)
}

Expand Down
Loading