Skip to content

Commit

Permalink
add some gravatar ideas
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanfaerman committed Feb 1, 2024
1 parent 0c7bd53 commit 36ff084
Show file tree
Hide file tree
Showing 11 changed files with 299 additions and 12 deletions.
29 changes: 28 additions & 1 deletion internal/models/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,14 @@ const (
AccountKindClub
)

var AccountAnonymous = &Account{
ID: -1,
Name: "Anonymous",
CreatedAt: time.Now(),
}

type Account struct {
ID int64
ID int64 `validate:"gte=0"`

Name string `validate:"required"`
About string
Expand All @@ -26,6 +32,14 @@ type Account struct {
Deleted bool
}

func (m *Account) IsAnonymous() bool {
return m.ID < 0
}

func (m *Account) InsertAllowed() bool {
return !m.IsAnonymous()
}

func (u *Account) Emails() ([]Email, error) {
var emails []Email
rows, err := global.dao.GetEmailsForAccount(context.Background(), u.ID)
Expand Down Expand Up @@ -53,6 +67,19 @@ func (u *Account) Emails() ([]Email, error) {
return emails, nil
}

func (m *Account) PrimaryEmail() (Email, error) {
emails, err := m.Emails()
if err != nil {
return Email{}, err
}
for _, email := range emails {
if email.IsPrimary {
return email, nil
}
}
return Email{}, errors.New("no primary email")
}

func FindAccountByID(ctx context.Context, id int64) (*Account, error) {
raw, err := global.dao.GetAccount(ctx, id)
if err != nil {
Expand Down
37 changes: 37 additions & 0 deletions internal/services/account.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,13 @@ package services

import (
"context"
"crypto/sha256"
"database/sql"
"errors"
"fmt"
"strings"

"github.com/davecgh/go-spew/spew"
"github.com/ryanfaerman/netctl/hamdb"
"github.com/ryanfaerman/netctl/internal/dao"
"github.com/ryanfaerman/netctl/internal/models"
Expand Down Expand Up @@ -157,3 +161,36 @@ func (s account) Update(ctx context.Context, m *models.Account) error {
})
return err
}

func (s account) AvatarURLForCallsign(ctx context.Context, callsign string) (string, error) {
m, err := s.FindByCallsign(ctx, callsign)
if err != nil {
return "", err
}
email, err := m.PrimaryEmail()
if err != nil {
return "", err
}

h := sha256.New()
h.Write([]byte(strings.TrimSpace(strings.ToLower(email.Address))))
return "https://www.gravatar.com/avatar/" + string(h.Sum(nil)), nil
}

func (s account) AvatarURLForAccount(ctx context.Context) (string, error) {
m, err := Session.GetAccount(ctx)
if err != nil {
return "", err
}
email, err := m.PrimaryEmail()
if err != nil {
return "", err
}
spew.Dump(email.Address)

h := sha256.New()
h.Write([]byte(strings.TrimSpace(strings.ToLower(email.Address))))
sum := fmt.Sprintf("%x", h.Sum(nil))
spew.Dump(sum)
return "https://www.gravatar.com/avatar/" + string(sum), nil
}
1 change: 1 addition & 0 deletions internal/services/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ type ctxKey int
const (
ctxKeyCSRF ctxKey = iota
ctxKeyUser
ctxKeyAccount
)

var setupOnce sync.Once
Expand Down
11 changes: 10 additions & 1 deletion internal/services/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,21 @@ func (session) Destroy(ctx context.Context) error {

var ErrNoAccountInSession = errors.New("no account in session")

// TODO: use middleware to add account to the context, possibly the session
func (session) GetAccount(ctx context.Context) (*models.Account, error) {
if account, ok := ctx.Value(ctxKeyAccount).(*models.Account); ok {
return account, nil
}

id, ok := global.session.Get(ctx, "account_id").(int64)
if !ok {
return nil, ErrNoAccountInSession
}
return Account.FindByID(ctx, id)
account, err := Account.FindByID(ctx, id)
if err != nil {
return models.AccountAnonymous, err
}
return account, nil
}

func (s session) MustGetAccount(ctx context.Context) *models.Account {
Expand Down
8 changes: 7 additions & 1 deletion internal/views/account.templ
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,12 @@ templ (v Account) Profile() {
<div class="grid-container split">
<div>
<h1>{ v.Account.Name }</h1>
@UserGravatar()
@Image(ImageAttrs{
URL: selfGravatarURL(ctx),
Width: 250,
Height: 250,
})
@templ.Raw(v.Account.About)
</div>
<div>
Expand All @@ -37,7 +43,7 @@ templ (v Account) Edit() {
@Page(v.Nav()) {
<div class="grid-container split">
<div>
"Editing { v.Account.Name }!"
Editing { v.Account.Name }!
@v.EditForm()
</div>
<div>
Expand Down
18 changes: 15 additions & 3 deletions internal/views/account_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

37 changes: 37 additions & 0 deletions internal/views/image.templ
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package views

import "fmt"

type ImageAttrs struct {
URL string
Classes templ.Attributes
Width int
Height int
Units string
Extra templ.Attributes
}

func (v ImageAttrs) units() string {
if v.Units != "" {
return v.Units
}
return "px"
}

func (v ImageAttrs) width() string {
return fmt.Sprintf("%d%s", v.Width, v.units())
}

func (v ImageAttrs) height() string {
return fmt.Sprintf("%d%s", v.Height, v.units())
}

templ Image(attrs ImageAttrs) {
<img
src={ attrs.URL }
class={ attrs.Classes }
width={ attrs.width() }
height={ attrs.height() }
{ attrs.Extra... }
/>
}
106 changes: 106 additions & 0 deletions internal/views/image_templ.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

28 changes: 22 additions & 6 deletions internal/views/tools.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ package views

import (
"bytes"
"crypto/sha256"
"context"
"encoding/gob"
"strings"
"sync"

"github.com/essentialkaos/branca/v2"
"github.com/ryanfaerman/netctl/config"
"github.com/ryanfaerman/netctl/internal/models"
"github.com/ryanfaerman/netctl/internal/services"
)

var (
Expand Down Expand Up @@ -61,8 +62,23 @@ func DecodeInputAttrs(encoded string) (InputAttrs, error) {
return i, nil
}

func gravatarURL(email string) string {
h := sha256.New()
h.Write([]byte(strings.TrimSpace(strings.ToLower(email))))
return "https://www.gravatar.com/avatar/" + string(h.Sum(nil)) + "?s=80"
func selfGravatarURL(ctx context.Context) string {
u, err := services.Account.AvatarURLForAccount(ctx)
if err != nil {
return ""
}
return u
}

func callsignGravatarURL(ctx context.Context, callsign string) string {
u, err := services.Account.AvatarURLForCallsign(ctx, callsign)
if err != nil {
return ""
}
return u
}

func CurrentAccount(ctx context.Context) *models.Account {
m, _ := services.Session.GetAccount(ctx)
return m
}
4 changes: 4 additions & 0 deletions internal/views/tools.templ
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,7 @@ templ CSRFValue() {
templ MetaVersion() {
<meta name="version" content={ version.Version.String() }/>
}

templ UserGravatar() {
<img src={ selfGravatarURL(ctx) } class="user-gravatar"/>
}
Loading

0 comments on commit 36ff084

Please sign in to comment.