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
Next Next commit
Disable Gravatar
  • Loading branch information
thomiceli committed May 17, 2023
commit c564bbef41e2ca752a1c0e92cacba3521f29e22e
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ A self-hosted pastebin **powered by Git**. [Try it here](https://opengist.thomic
* Editor with indentation mode & size ; drag and drop files
* Download raw files or as a ZIP archive
* OAuth2 login with GitHub and Gitea
* Avatars
* Avatars via Gravatar or OAuth2 providers
* Responsive UI
* Enable or disable signups
* Restrict or unrestrict snippets visibility to anonymous users
Expand Down
2 changes: 1 addition & 1 deletion fs_embed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ package main

import "embed"

//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg
//go:embed templates/*/*.html public/manifest.json public/assets/*.js public/assets/*.css public/assets/*.svg public/assets/*.png
var dirFS embed.FS
7 changes: 4 additions & 3 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, error) {
func GetLog(user string, gist string, skip int) ([]*Commit, []string, error) {
repositoryPath := RepositoryPath(user, gist)

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

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

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

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

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

scanner.Scan()
currentCommit.AuthorEmail = string(scanner.Bytes()[2:])
emails = append(emails, currentCommit.AuthorEmail)
thomiceli marked this conversation as resolved.
Show resolved Hide resolved

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

}

return commits
return commits, emails
}

func ParseCsv(file *File) (*CsvFile, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/models/admin_setting.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const (
SettingDisableSignup = "disable-signup"
SettingRequireLogin = "require-login"
SettingDisableLoginForm = "disable-login-form"
SettingDisableGravatar = "disable-gravatar"
)

func GetSetting(key string) (string, error) {
Expand Down
1 change: 1 addition & 0 deletions internal/models/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ func Setup(dbpath string) error {
SettingDisableSignup: "0",
SettingRequireLogin: "0",
SettingDisableLoginForm: "0",
SettingDisableGravatar: "0",
})
}

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, error) {
func (gist *Gist) Log(skip int) ([]*git.Commit, []string, error) {
return git.GetLog(gist.User.Username, gist.Uuid, skip)
}

Expand Down
16 changes: 16 additions & 0 deletions internal/models/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ type User struct {
CreatedAt int64
Email string
MD5Hash string // for gravatar, if no Email is specified, the value is random
AvatarURL string
GithubID string
GiteaID string

Expand Down Expand Up @@ -81,6 +82,21 @@ func GetUserById(userId uint) (*User, error) {
return user, err
}

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

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

for _, user := range users {
userMap[user.Email] = user
}

return userMap, err
}

func SSHKeyExistsForUser(sshKey string, userId uint) (*SSHKey, error) {
key := new(SSHKey)
err := db.
Expand Down
44 changes: 43 additions & 1 deletion internal/web/auth.go
thomiceli marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package web
import (
"context"
"crypto/md5"
"encoding/json"
"errors"
"fmt"
"github.com/labstack/echo/v4"
Expand Down Expand Up @@ -139,12 +140,14 @@ func oauthCallback(ctx echo.Context) error {

currUser := getUserLogged(ctx)
if currUser != nil {
// if user is logged in, link account to user
// if user is logged in, link account to user and update its avatar URL
switch user.Provider {
case "github":
currUser.GithubID = user.UserID
currUser.AvatarURL = getAvatarUrlFromProvider("github", user.UserID)
case "gitea":
currUser.GiteaID = user.UserID
currUser.AvatarURL = getAvatarUrlFromProvider("gitea", user.NickName)
}

if err = currUser.Update(); err != nil {
Expand Down Expand Up @@ -172,11 +175,14 @@ func oauthCallback(ctx echo.Context) error {
MD5Hash: fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(user.Email))))),
}

// set provider id and avatar URL
switch user.Provider {
case "github":
userDB.GithubID = user.UserID
userDB.AvatarURL = getAvatarUrlFromProvider("github", user.UserID)
case "gitea":
userDB.GiteaID = user.UserID
userDB.AvatarURL = getAvatarUrlFromProvider("gitea", user.NickName)
}

if err = userDB.Create(); err != nil {
Expand Down Expand Up @@ -328,3 +334,39 @@ func trimGiteaUrl() string {

return giteaUrl
}

func getAvatarUrlFromProvider(provider string, identifier string) string {
fmt.Println("getAvatarUrlFromProvider", provider, identifier)
switch provider {
case "github":
return "https://avatars.githubusercontent.com/u/" + identifier + "?v=4"
case "gitea":
resp, err := http.Get("https://gitea.com/api/v1/users/" + identifier)
if err != nil {
log.Error().Err(err).Msg("Cannot get user from Gitea")
return ""
}
defer resp.Body.Close()

body, err := io.ReadAll(resp.Body)
if err != nil {
log.Error().Err(err).Msg("Cannot read Gitea response body")
return ""
}

var result map[string]interface{}
err = json.Unmarshal(body, &result)
if err != nil {
log.Error().Err(err).Msg("Cannot unmarshal Gitea response body")
return ""
}

field, ok := result["avatar_url"]
if !ok {
log.Error().Msg("Field 'avatar_url' not found in Gitea JSON response")
return ""
}
return field.(string)
}
return ""
}
8 changes: 7 additions & 1 deletion 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, err := gist.Log((pageInt - 1) * 10)
commits, emails, err := gist.Log((pageInt - 1) * 10)
if err != nil {
return errorRes(500, "Error fetching commits log", err)
}
Expand All @@ -186,8 +186,14 @@ func revisions(ctx echo.Context) error {
return errorRes(404, "Page not found", nil)
}

emailsUsers, err := models.GetUsersFromEmails(emails)
thomiceli marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return errorRes(500, "Error fetching users emails", err)
}

setData(ctx, "page", "revisions")
setData(ctx, "revision", "HEAD")
setData(ctx, "emails", emailsUsers)
setData(ctx, "htmlTitle", "Revision of "+gist.Title)

return html(ctx, "revisions.html")
Expand Down
19 changes: 13 additions & 6 deletions internal/web/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package web

import (
"context"
"crypto/md5"
"encoding/json"
"fmt"
"github.com/gorilla/sessions"
Expand Down Expand Up @@ -71,11 +70,19 @@ var fm = template.FuncMap{
"slug": func(s string) string {
return strings.Trim(re.ReplaceAllString(strings.ToLower(s), "-"), "-")
},
"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)))))
"avatarUrl": func(user *models.User, noGravatar bool) string {
if user.AvatarURL != "" {
return user.AvatarURL
}

if user.MD5Hash != "" && !noGravatar {
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
},
"asset": func(jsfile string) string {
if dev {
Expand Down
Binary file added public/default.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
1 change: 1 addition & 0 deletions public/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'highlight.js/styles/tokyo-night-dark.css';
import './style.css';
import './markdown.css';
import './favicon.svg';
import './default.png';
import moment from 'moment';
import md from 'markdown-it';
import hljs from 'highlight.js';
Expand Down
11 changes: 11 additions & 0 deletions templates/pages/admin_settings.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,17 @@
</button>
</div>
</li>
<li class="list-none gap-x-4 py-5">
<div class="flex items-center justify-between">
<span class="flex flex-grow flex-col">
<span class="text-sm font-medium leading-6 text-slate-300">Disable Gravatar</span>
<span class="text-sm text-gray-400">Disable the usage of Gravatar as an avatar provider.</span>
</span>
<button type="button" id="disable-gravatar" data-bool="{{ .DisableGravatar }}" class="toggle-button {{ if .DisableGravatar }}bg-primary-600{{else}}bg-gray-400{{end}} relative inline-flex h-6 w-11 ml-4 flex-shrink-0 cursor-pointer rounded-full border-2 border-transparent transition-colors duration-200 ease-in-out focus:outline-none focus:ring-2 focus:ring-primary-600 focus:ring-offset-2" role="switch" aria-checked="false" aria-labelledby="availability-label" aria-describedby="availability-description">
<span aria-hidden="true" class="{{ if .DisableGravatar }}translate-x-5{{else}}translate-x-0{{end}} pointer-events-none inline-block h-5 w-5 transform rounded-full bg-white shadow ring-0 transition duration-200 ease-in-out"></span>
</button>
</div>
</li>
</ul>
{{ .csrfHtml }}
</div>
Expand Down
2 changes: 1 addition & 1 deletion templates/pages/all.html
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
{{if .fromUser}}
<div class="flex items-center">
<div class="flex-shrink-0">
<img class="h-12 w-12 rounded-md mr-2 border border-gray-700" src="{{ avatarUrl .fromUser.MD5Hash }}" alt="">
<img class="h-12 w-12 rounded-md mr-2 border border-gray-700" src="{{ avatarUrl .fromUser .DisableGravatar }}" alt="">
</div>
<div>
<h1 class="text-2xl font-bold leading-tight">{{.fromUser.Username}}</h1>
Expand Down
2 changes: 1 addition & 1 deletion templates/pages/forks.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ <h3 class="text-xl font-bold leading-tight break-all py-2">Forks</h3>
{{ range $gist := .forks }}
<li class="flex py-4">
<a href="/{{ $gist.User.Username }}">
<img class="h-12 w-12 rounded-md mr-2 border border-gray-700" src="{{ avatarUrl $gist.User.MD5Hash }}" alt="">
<img class="h-12 w-12 rounded-md mr-2 border border-gray-700" src="{{ avatarUrl $gist.User $.DisableGravatar }}" alt="">
</a>
<div>
<a href="/{{ $gist.User.Username }}" class="text-sm font-medium text-slate-300">{{ $gist.User.Username }}</a>
Expand Down
2 changes: 1 addition & 1 deletion templates/pages/likes.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ <h3 class="text-xl font-bold leading-tight break-all py-2">Likes</h3>
{{ range $user := .likers }}
<div class="relative flex items-center space-x-3 rounded-lg border border-gray-600 bg-gray-800 px-6 py-5 shadow-sm focus-within:ring-1 focus-within:border-primary-500 focus-within:ring-primary-500 hover:border-gray-400">
<div class="min-w-0 flex">
<img class="h-12 w-12 rounded-md mr-2 border border-gray-700" src="{{ avatarUrl $user.MD5Hash }}" alt="">
<img class="h-12 w-12 rounded-md mr-2 border border-gray-700" src="{{ avatarUrl $user $.DisableGravatar }}" alt="">
<a href="/{{ $user.Username }}" class="focus:outline-none">
<span class="absolute inset-0" aria-hidden="true"></span>
<p class="text-sm font-medium text-slate-300 align-middle">{{ $user.Username }}</p>
Expand Down
2 changes: 1 addition & 1 deletion templates/pages/revisions.html
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ <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 (emailToMD5 $commit.AuthorEmail) }}" alt="" />
<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>
{{ if ne $commit.Changed "" }}
<p class="text-sm float-right py-2">
Expand Down