Add avatars to HTML

This commit is contained in:
Thomas Miceli
2023-03-20 13:30:25 +01:00
parent 7b27db8849
commit f49f4ca10d
11 changed files with 73 additions and 52 deletions

View File

@ -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

View File

@ -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:])

View File

@ -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
}

View File

@ -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())))

View File

@ -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)
}

View File

@ -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")),
}