From f49f4ca10dbe0daeb80f0cb0162b0aab9df65b5c Mon Sep 17 00:00:00 2001 From: Thomas Miceli Date: Mon, 20 Mar 2023 13:30:25 +0100 Subject: [PATCH] Add avatars to HTML --- internal/git/commands.go | 27 +++++++++++---------------- internal/git/output_parser.go | 16 ++++++++++------ internal/models/gist.go | 2 +- internal/web/config.go | 2 -- internal/web/gist.go | 27 ++++++++++++++------------- internal/web/run.go | 8 ++++++-- templates/base/base_header.html | 4 ++-- templates/pages/all.html | 22 +++++++++++++++++----- templates/pages/forks.html | 3 +++ templates/pages/likes.html | 5 +++-- templates/pages/revisions.html | 9 ++++++--- 11 files changed, 73 insertions(+), 52 deletions(-) diff --git a/internal/git/commands.go b/internal/git/commands.go index 4532ca6..bd4e1e0 100644 --- a/internal/git/commands.go +++ b/internal/git/commands.go @@ -1,6 +1,7 @@ package git import ( + "fmt" "io" "opengist/internal/config" "os" @@ -114,7 +115,7 @@ func GetLog(user string, gist string, skip string) ([]*Commit, error) { "-p", "--skip", skip, - "--format=format:c %H%na %aN%nt %at", + "--format=format:c %H%na %aN%nm %ae%nt %at", "--shortstat", "HEAD", ) @@ -146,12 +147,6 @@ func CloneTmp(user string, gist string, gistTmpId string) error { return err } - cmd = exec.Command("git", "config", "user.name", user) - cmd.Dir = tmpRepositoryPath - if err = cmd.Run(); err != nil { - return err - } - // remove every file (and not the .git directory!) cmd = exec.Command("find", ".", "-maxdepth", "1", "-type", "f", "-delete") cmd.Dir = tmpRepositoryPath @@ -167,13 +162,6 @@ func ForkClone(userSrc string, gistSrc string, userDst string, gistDst string) e return err } - cmd = exec.Command("git", "config", "user.name", userDst) - cmd.Dir = repositoryPathDst - err := cmd.Run() - if err != nil { - return err - } - return copyFiles(repositoryPathDst) } @@ -200,8 +188,15 @@ func AddAll(gistTmpId string) error { return cmd.Run() } -func CommitRepository(gistTmpId string) error { - cmd := exec.Command("git", "commit", "--allow-empty", "-m", `"Opengist commit"`) +func CommitRepository(gistTmpId string, authorName string, authorEmail string) error { + cmd := exec.Command("git", + "commit", + "--allow-empty", + "-m", + "Opengist commit", + "--author", + fmt.Sprintf("%s <%s>", authorName, authorEmail), + ) tmpPath := TmpRepositoryPath(gistTmpId) cmd.Dir = tmpPath diff --git a/internal/git/output_parser.go b/internal/git/output_parser.go index 39e467c..4075838 100644 --- a/internal/git/output_parser.go +++ b/internal/git/output_parser.go @@ -26,11 +26,12 @@ type CsvFile struct { } type Commit struct { - Hash string - Author string - Timestamp string - Changed string - Files []File + Hash string + AuthorName string + AuthorEmail string + Timestamp string + Changed string + Files []File } func truncateCommandOutput(out io.Reader, maxBytes int64) (string, bool, error) { @@ -75,7 +76,10 @@ func parseLog(out io.Reader) []*Commit { currentCommit = &Commit{Hash: string(scanner.Bytes()[2:]), Files: []File{}} scanner.Scan() - currentCommit.Author = string(scanner.Bytes()[2:]) + currentCommit.AuthorName = string(scanner.Bytes()[2:]) + + scanner.Scan() + currentCommit.AuthorEmail = string(scanner.Bytes()[2:]) scanner.Scan() currentCommit.Timestamp = string(scanner.Bytes()[2:]) diff --git a/internal/models/gist.go b/internal/models/gist.go index 98cb27d..676a860 100644 --- a/internal/models/gist.go +++ b/internal/models/gist.go @@ -251,7 +251,7 @@ func (gist *Gist) AddAndCommitFiles(files *[]FileDTO) error { return err } - if err := git.CommitRepository(gist.Uuid); err != nil { + if err := git.CommitRepository(gist.Uuid, gist.User.Username, gist.User.Email); err != nil { return err } diff --git a/internal/web/config.go b/internal/web/config.go index 042d796..8f0879c 100644 --- a/internal/web/config.go +++ b/internal/web/config.go @@ -32,8 +32,6 @@ func emailProcess(ctx echo.Context) error { email := ctx.FormValue("email") var hash string - fmt.Println() - if email == "" { // generate random md5 string hash = fmt.Sprintf("%x", md5.Sum([]byte(time.Now().String()))) diff --git a/internal/web/gist.go b/internal/web/gist.go index 58181a1..24364f6 100644 --- a/internal/web/gist.go +++ b/internal/web/gist.go @@ -71,7 +71,8 @@ func gistInit(next echo.HandlerFunc) echo.HandlerFunc { func allGists(ctx echo.Context) error { var err error - fromUser := ctx.Param("user") + fromUserStr := ctx.Param("user") + userLogged := getUserLogged(ctx) pageInt := getPage(ctx) @@ -99,30 +100,30 @@ func allGists(ctx echo.Context) error { } else { currentUserId = 0 } - if fromUser == "" { + if fromUserStr == "" { setData(ctx, "htmlTitle", "All gists") - fromUser = "all" + fromUserStr = "all" gists, err = models.GetAllGistsForCurrentUser(currentUserId, pageInt-1, sort, order) } else { - setData(ctx, "htmlTitle", "All gists from "+fromUser) - setData(ctx, "fromUser", fromUser) + setData(ctx, "htmlTitle", "All gists from "+fromUserStr) - var exists bool - if exists, err = models.UserExists(fromUser); err != nil { + fromUser, err := models.GetUserByUsername(fromUserStr) + if err != nil { + if errors.Is(err, gorm.ErrRecordNotFound) { + return notFound("User not found") + } return errorRes(500, "Error fetching user", err) } + setData(ctx, "fromUser", fromUser) - if !exists { - return notFound("User not found") - } - - gists, err = models.GetAllGistsFromUser(fromUser, currentUserId, pageInt-1, sort, order) + gists, err = models.GetAllGistsFromUser(fromUserStr, currentUserId, pageInt-1, sort, order) } + if err != nil { return errorRes(500, "Error fetching gists", err) } - if err = paginate(ctx, gists, pageInt, 10, "gists", fromUser, 2, "&sort="+sort+"&order="+order); err != nil { + if err = paginate(ctx, gists, pageInt, 10, "gists", fromUserStr, 2, "&sort="+sort+"&order="+order); err != nil { return errorRes(404, "Page not found", nil) } diff --git a/internal/web/run.go b/internal/web/run.go index 96e6c5e..edc4c14 100644 --- a/internal/web/run.go +++ b/internal/web/run.go @@ -2,6 +2,7 @@ package web import ( "context" + "crypto/md5" "fmt" "github.com/gorilla/sessions" "github.com/labstack/echo/v4" @@ -100,8 +101,11 @@ func Start() { "slug": func(s string) string { return strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-") }, - "avatarUrl": func(user *models.User) string { - return "https://www.gravatar.com/avatar/" + user.MD5Hash + "?d=identicon&s=200" + "avatarUrl": func(userHash string) string { + return "https://www.gravatar.com/avatar/" + userHash + "?d=identicon&s=200" + }, + "emailToMD5": func(email string) string { + return fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(email))))) }, }).ParseGlob("templates/*/*.html")), } diff --git a/templates/base/base_header.html b/templates/base/base_header.html index f937a0e..855bdde 100644 --- a/templates/base/base_header.html +++ b/templates/base/base_header.html @@ -58,7 +58,7 @@ {{ if .userLogged.IsAdmin }} {{ end }} - +

{{ .userLogged.Username }}

@@ -90,7 +90,7 @@
All {{ if .userLogged }} My gists - Config + Settings {{ if .userLogged.IsAdmin }} Admin diff --git a/templates/pages/all.html b/templates/pages/all.html index 21b283b..1dd2be2 100644 --- a/templates/pages/all.html +++ b/templates/pages/all.html @@ -2,7 +2,19 @@
-

All gists {{if .fromUser}} from {{.fromUser}} {{end}}

+ {{if .fromUser}} +
+
+ +
+
+

{{.fromUser.Username}}

+

Joined {{.fromUser.CreatedAt}}

+
+
+ {{ else }} +

All gists

+ {{ end }}
@@ -16,21 +28,21 @@