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

Disable Gravatar #37

Merged
merged 5 commits into from
May 26, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
Next Next commit
Change email fetching
  • Loading branch information
thomiceli committed May 24, 2023
commit 0a90bbddd9493e986554f88085aaeb3b4c270715
7 changes: 3 additions & 4 deletions internal/git/commands.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,7 @@ func GetFileContent(user string, gist string, revision string, filename string,
return truncateCommandOutput(stdout, maxBytes)
}

func GetLog(user string, gist string, skip int) ([]*Commit, []string, error) {
func GetLog(user string, gist string, skip int) ([]*Commit, error) {
repositoryPath := RepositoryPath(user, gist)

cmd := exec.Command(
Expand All @@ -125,12 +125,11 @@ func GetLog(user string, gist string, skip int) ([]*Commit, []string, error) {
stdout, _ := cmd.StdoutPipe()
err := cmd.Start()
if err != nil {
return nil, nil, err
return nil, err
}
defer cmd.Wait()

commits, emails := parseLog(stdout, 2<<18)
return commits, emails, nil
return parseLog(stdout, 2<<18), nil
}

func CloneTmp(user string, gist string, gistTmpId string, email string) error {
Expand Down
6 changes: 2 additions & 4 deletions internal/git/output_parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,11 +61,10 @@ func truncateCommandOutput(out io.Reader, maxBytes int64) (string, bool, error)
return string(buf), truncated, nil
}

func parseLog(out io.Reader, maxBytes int) ([]*Commit, []string) {
func parseLog(out io.Reader, maxBytes int) []*Commit {
scanner := bufio.NewScanner(out)

var commits []*Commit
var emails []string
var currentCommit *Commit
var currentFile *File
var isContent bool
Expand All @@ -87,7 +86,6 @@ func parseLog(out io.Reader, maxBytes int) ([]*Commit, []string) {

scanner.Scan()
currentCommit.AuthorEmail = string(scanner.Bytes()[2:])
emails = append(emails, currentCommit.AuthorEmail)

scanner.Scan()
currentCommit.Timestamp = string(scanner.Bytes()[2:])
Expand Down Expand Up @@ -180,7 +178,7 @@ func parseLog(out io.Reader, maxBytes int) ([]*Commit, []string) {

}

return commits, emails
return commits
}

func ParseCsv(file *File) (*CsvFile, error) {
Expand Down
2 changes: 1 addition & 1 deletion internal/models/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ func (gist *Gist) File(revision string, filename string, truncate bool) (*git.Fi
}, err
}

func (gist *Gist) Log(skip int) ([]*git.Commit, []string, error) {
func (gist *Gist) Log(skip int) ([]*git.Commit, error) {
return git.GetLog(gist.User.Username, gist.Uuid, skip)
}

Expand Down
15 changes: 12 additions & 3 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,19 +82,28 @@ func GetUserById(userId uint) (*User, error) {
return user, err
}

func GetUsersFromEmails(emails []string) (map[string]*User, error) {
func GetUsersFromEmails(emailsSet map[string]struct{}) (map[string]*User, error) {
var users []*User
userMap := make(map[string]*User)

emails := make([]string, 0, len(emailsSet))
for email := range emailsSet {
emails = append(emails, email)
}

err := db.
Where("email IN ?", emails).
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are emails normalized in db or will this create issues with capitalization? A normalizedEmail field in db may be good otherwise? For this use-case and when looking up by email in the future.

Copy link
Owner Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You're right it might create issues ; the current email field in db should contain only lowercase emails, and the fetched emails in commits would be set as lowercase.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think so. I also think it's best to save the original email also, and not just a normalized one--hence why I would recommend a normalizedEmail field rather than normalizing the current email field. Emails can technically be case sensitive and it could technically screw with future email notification/sending functionality if the original is not kept.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Your way of doing it makes good enough sense for the scale of this project. Same with not bothering to care about migrating old emails in db to lowercase. Maybe in a few years it's a different story. :)

Find(&users).Error

if err != nil {
return nil, err
}

userMap := make(map[string]*User)
for _, user := range users {
userMap[user.Email] = user
}

return userMap, err
return userMap, nil
}

func SSHKeyExistsForUser(sshKey string, userId uint) (*SSHKey, error) {
Expand Down
12 changes: 10 additions & 2 deletions internal/web/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -177,7 +177,7 @@ func revisions(ctx echo.Context) error {

pageInt := getPage(ctx)

commits, emails, err := gist.Log((pageInt - 1) * 10)
commits, err := gist.Log((pageInt - 1) * 10)
if err != nil {
return errorRes(500, "Error fetching commits log", err)
}
Expand All @@ -186,7 +186,15 @@ func revisions(ctx echo.Context) error {
return errorRes(404, "Page not found", nil)
}

emailsUsers, err := models.GetUsersFromEmails(emails)
emailsSet := map[string]struct{}{}
for _, commit := range commits {
if commit.AuthorEmail == "" {
continue
}
emailsSet[commit.AuthorEmail] = struct{}{}
}

emailsUsers, err := models.GetUsersFromEmails(emailsSet)
if err != nil {
return errorRes(500, "Error fetching users emails", err)
}
Expand Down
13 changes: 9 additions & 4 deletions internal/web/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,17 +79,15 @@ var fm = template.FuncMap{
return "https://www.gravatar.com/avatar/" + user.MD5Hash + "?d=identicon&s=200"
}

if dev {
return "http:https://localhost:16157/default.png"
}
return "/" + manifestEntries["default.png"].File
return defaultAvatar()
},
"asset": func(jsfile string) string {
if dev {
return "http:https://localhost:16157/" + jsfile
}
return "/" + manifestEntries[jsfile].File
},
"defaultAvatar": defaultAvatar,
}

var EmbedFS fs.FS
Expand Down Expand Up @@ -371,3 +369,10 @@ func parseManifestEntries() {
log.Fatal().Err(err).Msg("Failed to unmarshal manifest.json")
}
}

func defaultAvatar() string {
if dev {
return "http:https://localhost:16157/default.png"
}
return "/" + manifestEntries["default.png"].File
}
5 changes: 3 additions & 2 deletions templates/pages/revisions.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,9 @@ <h3 class="text-sm py-2 flex-auto">
<svg xmlns="http:https://www.w3.org/2000/svg" class="h-3 w-3 mr-1 inline" fill="none" viewBox="0 0 24 24" stroke="currentColor" stroke-width="2">
<path stroke-linecap="round" stroke-linejoin="round" d="M13 5l7 7-7 7M5 5l7 7-7 7" />
</svg>
<img class="h-5 w-5 rounded-full inline" src="{{ avatarUrl (index $.emails $commit.AuthorEmail) $.DisableGravatar }}" alt="" />
<span class="font-bold">{{ $commit.AuthorName }}</span> revised this gist <span class="moment-timestamp font-bold">{{ $commit.Timestamp }}</span>. <a href="/{{ $.gist.User.Username }}/{{ $.gist.Uuid }}/rev/{{ $commit.Hash }}">Go to revision</a></h3>
{{ $user := (index $.emails $commit.AuthorEmail) }}
<img class="h-5 w-5 rounded-full inline" src="{{if $user }}{{ avatarUrl $user $.DisableGravatar }}{{else}}{{defaultAvatar}}{{end}}" alt="" />
<span class="font-bold">{{if $user}}<a href="/{{$user.Username}}" class="text-slate-300 hover:text-slate-300 hover:underline">{{ $commit.AuthorName }}</a>{{else}}{{ $commit.AuthorName }}{{end}}</span> revised this gist <span class="moment-timestamp font-bold">{{ $commit.Timestamp }}</span>. <a href="/{{ $.gist.User.Username }}/{{ $.gist.Uuid }}/rev/{{ $commit.Hash }}">Go to revision</a></h3>
{{ if ne $commit.Changed "" }}
<p class="text-sm float-right py-2">
<svg xmlns="http:https://www.w3.org/2000/svg" fill="none" viewBox="0 0 24 24" stroke-width="1.5" stroke="currentColor" class="w-5 h-5 inline-flex">
Expand Down