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 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
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
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
7 changes: 7 additions & 0 deletions internal/models/migration.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ func ApplyMigrations(db *gorm.DB) error {
Func func(*gorm.DB) error
}{
{1, v1_modifyConstraintToSSHKeys},
{2, v2_lowercaseEmails},
// Add more migrations here as needed
}

Expand Down Expand Up @@ -94,3 +95,9 @@ func v1_modifyConstraintToSSHKeys(db *gorm.DB) error {
renameSQL := `ALTER TABLE ssh_keys_temp RENAME TO ssh_keys;`
return db.Exec(renameSQL).Error
}

func v2_lowercaseEmails(db *gorm.DB) error {
// Copy the lowercase emails into the new column
copySQL := `UPDATE users SET email = lower(email);`
return db.Exec(copySQL).Error
}
35 changes: 33 additions & 2 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,30 @@ func GetUserById(userId uint) (*User, error) {
return user, err
}

func GetUsersFromEmails(emailsSet map[string]struct{}) (map[string]*User, error) {
var users []*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, nil
}

func SSHKeyExistsForUser(sshKey string, userId uint) (*SSHKey, error) {
key := new(SSHKey)
err := db.
Expand Down Expand Up @@ -135,9 +160,15 @@ func (user *User) HasLiked(gist *Gist) (bool, error) {
func (user *User) DeleteProviderID(provider string) error {
switch provider {
case "github":
return db.Model(&user).Update("github_id", nil).Error
return db.Model(&user).
Update("github_id", nil).
Update("avatar_url", nil).
Error
case "gitea":
return db.Model(&user).Update("gitea_id", nil).Error
return db.Model(&user).
Update("gitea_id", nil).
Update("avatar_url", nil).
Error
}

return nil
Expand Down
44 changes: 43 additions & 1 deletion internal/web/auth.go
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 ""
}
14 changes: 14 additions & 0 deletions internal/web/gist.go
Original file line number Diff line number Diff line change
Expand Up @@ -186,8 +186,22 @@ func revisions(ctx echo.Context) error {
return errorRes(404, "Page not found", nil)
}

emailsSet := map[string]struct{}{}
for _, commit := range commits {
if commit.AuthorEmail == "" {
continue
}
emailsSet[strings.ToLower(commit.AuthorEmail)] = struct{}{}
}

emailsUsers, err := models.GetUsersFromEmails(emailsSet)
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
24 changes: 18 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,18 +70,24 @@ 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"
}

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 @@ -364,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
}
2 changes: 1 addition & 1 deletion internal/web/settings.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func emailProcess(ctx echo.Context) error {
hash = fmt.Sprintf("%x", md5.Sum([]byte(strings.ToLower(strings.TrimSpace(email)))))
}

user.Email = email
user.Email = strings.ToLower(email)
user.MD5Hash = hash

if err := user.Update(); err != nil {
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
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 (emailToMD5 $commit.AuthorEmail) }}" 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