~aw/fishbb

9f07f3c8cbd5dd4938fa472e8e591045971317af — alex wennerberg a month ago 3d95d60
update username route
10 files changed, 15 insertions(+), 36 deletions(-)

M db.go
D gemtext.go
M go.mod
M go.sum
M user.go
M views/forum.html
M views/index.html
M views/search.html
M views/thread.html
M web.go
M db.go => db.go +1 -1
@@ 118,7 118,7 @@ func prepareStatements(db *sql.DB) {
		select users.id,username,email,role,active,about,website,users.created, count(1)
		from users 
		left join posts on users.id = posts.authorid
		where users.id = ?  
		where users.username = ?  
		group by users.id
		`)
	stmtGetUsers = prepare(db, `

D gemtext.go => gemtext.go +0 -15
@@ 1,15 0,0 @@
package main

import (
	"html/template"
	"strings"

	"git.sr.ht/~aw/gmi2html"
)

func (p Post) Render() template.HTML {
	r := strings.NewReader(p.Content)
	g := gmi2html.NewReader(r)
	g.NestedBlocks = true
	return template.HTML(g.HTMLString())
}

M go.mod => go.mod +1 -1
@@ 8,13 8,13 @@ require (
)

require (
	git.sr.ht/~aw/gmi2html v0.1.3
	github.com/BurntSushi/toml v1.4.0
	github.com/alexedwards/argon2id v1.0.0
	github.com/go-chi/chi/v5 v5.1.0
	github.com/go-chi/httplog/v2 v2.0.11
	github.com/go-chi/httprate v0.9.0
	github.com/k3a/html2text v1.2.1
	github.com/yuin/goldmark v1.4.13
	golang.org/x/oauth2 v0.21.0
)


M go.sum => go.sum +1 -2
@@ 1,7 1,5 @@
cloud.google.com/go/compute/metadata v0.3.0 h1:Tz+eQXMEqDIKRsmY3cHTL6FVaynIjX2QxYC4trgAKZc=
cloud.google.com/go/compute/metadata v0.3.0/go.mod h1:zFmK7XCadkQkj6TtorcaGlCW1hT1fIilQDwofLpJ20k=
git.sr.ht/~aw/gmi2html v0.1.3 h1:Gj2vcmI/HqSs+9t05s0y/9n8o7swXSnJH4H+AQSu1Sc=
git.sr.ht/~aw/gmi2html v0.1.3/go.mod h1:WXvBJ0UXRsTcMcm/jmE1yJXz4bxUZDdFqv8HDcIueE0=
github.com/BurntSushi/toml v1.4.0 h1:kuoIxZQy2WRRk1pttg9asf+WVv6tWQuBNVmK8+nqPr0=
github.com/BurntSushi/toml v1.4.0/go.mod h1:ukJfTF/6rtPPRCnwkur4qwRxa8vTRFBF0uk2lLoLwho=
github.com/alexedwards/argon2id v1.0.0 h1:wJzDx66hqWX7siL/SRUmgz3F8YMrd/nfX/xHHcQQP0w=


@@ 28,6 26,7 @@ github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d h1:zE9ykE
github.com/smartystreets/assertions v0.0.0-20180927180507-b2de0cb4f26d/go.mod h1:OnSkiWE9lh6wB0YB77sQom3nweQdgAjqCqsofrRNTgc=
github.com/smartystreets/goconvey v1.6.4 h1:fv0U8FUIMPNf1L9lnHLvLhgicrIVChEkdzIKYqbNC9s=
github.com/smartystreets/goconvey v1.6.4/go.mod h1:syvi0/a8iFYH4r/RixwvyeAJjdLS9QV7WQ/tjFTllLA=
github.com/yuin/goldmark v1.4.13 h1:fVcFKWvrslecOb/tg+Cc05dkeYx540o0FuFt3nUVDoE=
github.com/yuin/goldmark v1.4.13/go.mod h1:6yULJ656Px+3vBD8DxQVa3kxgyrAnzto9xy5taEt/CY=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20210921155107-089bfa567519/go.mod h1:GvvjBRRGRdwPK5ydBHafDWAxML/pGHZbMvKqRZ5+Abc=

M user.go => user.go +2 -2
@@ 71,8 71,8 @@ func createOAuthUser(email string, active bool, provider string) error {
	return err
}

func getUser(id int) (User, error) {
	row := stmtGetUser.QueryRow(id)
func getUser(username string) (User, error) {
	row := stmtGetUser.QueryRow(username)
	var u User
	err := row.Scan(&u.ID, &u.Username, &u.Email, &u.Role, &u.Active, &u.About, &u.Website, &u.Created, &u.Posts)
	if err != nil {

M views/forum.html => views/forum.html +2 -2
@@ 16,7 16,7 @@
		<a class="title-link" href="/f/{{$.Forum.Slug}}/{{.ID}}">{{.Title}}</a><br>
		<div class="flex-between">
		<div>
			by <a href="/user/{{.Author.ID}}">{{.Author.Username}}</a> <span class="text-alt" title="{{.Created}}">{{ timeago .Created }}</span>
			by <a href="/u/{{.Author.Username}}">{{.Author.Username}}</a> <span class="text-alt" title="{{.Created}}">{{ timeago .Created }}</span>
		</div>
	  {{ if and $.User $.User.Role.ModLevel }}
	  <span>


@@ 29,7 29,7 @@
	<td style="text-align:center;">{{.Replies}}</td>
    <td>
		<a href="/f/general/{{.ID}}#{{.Latest.ID}}">{{ timeago .Latest.Created }}</a>
		by <a href="/user/{{.Latest.Author.ID}}">{{.Latest.Author.Username}}</a>
		by <a href="/u/{{.Latest.Author.Username}}">{{.Latest.Author.Username}}</a>
    </td>
  </tr>
  {{ end }}

M views/index.html => views/index.html +1 -1
@@ 10,7 10,7 @@
	<td style="text-align:center;">{{.ThreadCount}}</td>
    <td>
		<a href="/f/{{.Slug}}/{{.LastPost.ThreadID}}?p={{.LastPost.ID}}">{{ timeago .LastPost.Created }}</a> 
		by <a href="/user/{{.LastPost.Author.ID}}">{{.LastPost.Author.Username}}</a><br>
		by <a href="/u/{{.LastPost.Author.Username}}">{{.LastPost.Author.Username}}</a><br>
		in <a href="/f/{{.Slug}}/{{.LastPost.ThreadID}}">{{.LastPost.ThreadTitle}}</a><br>
    </td>
  </tr>

M views/search.html => views/search.html +1 -1
@@ 4,7 4,7 @@
{{ range .Posts }}
<div class="shortpost">
	<img style="vertical-align:middle" src="/a?a={{.Author.Username}}" width=18>
	<a href="/user/{{.Author.ID}}">{{.Author.Username}}</a> <a href="#{{.ID}}" title="Link to this post">#</a> 
	<a href="/u/{{.Author.Username}}">{{.Author.Username}}</a> <a href="#{{.ID}}" title="Link to this post">#</a> 
	{{ .Preview }}
</div>
{{ end }}

M views/thread.html => views/thread.html +1 -1
@@ 14,7 14,7 @@
  <div class="post-meta">
	  <div>
  <img  style="vertical-align:middle" src="/a?a={{.Author.Username}}" width=18>
	  <a href="/user/{{.Author.ID}}">{{.Author.Username}}</a> <a href="#{{.ID}}"
	  <a href="/u/{{.Author.Username}}">{{.Author.Username}}</a> <a href="#{{.ID}}"
	  title="Link to this post">#</a> <span class="text-alt" title="{{ .Created
	  }}">{{ timeago .Created}}
	  {{ if .Edited }}<em>(edited {{timeago .Edited}})</em>{{end}}

M web.go => web.go +5 -10
@@ 383,13 383,8 @@ func loadTemplates() *template.Template {
func userPage(w http.ResponseWriter, r *http.Request) {
	tmpl := make(map[string]any)

	uid, err := strconv.Atoi(r.PathValue("userid"))
	if err != nil {
		err = fmt.Errorf("failed to parse user ID from path: %w", err)
		serverError(w, r, err)
		return
	}
	info, err := getUser(uid)
	uname := r.PathValue("username")
	info, err := getUser(uname)
	if err != nil {
		serverError(w, r, err)
		return


@@ 403,7 398,7 @@ func userPage(w http.ResponseWriter, r *http.Request) {
func mePage(w http.ResponseWriter, r *http.Request) {
	tmpl := make(map[string]any)
	u := GetUserInfo(r)
	info, err := getUser(u.UserID)
	info, err := getUser(u.Username)
	if err != nil {
		err = fmt.Errorf("failed to get user %d: %w", u.UserID, err)
		serverError(w, r, err)


@@ 470,7 465,7 @@ func doUpdateMe(w http.ResponseWriter, r *http.Request) {
		return
	}
	tmpl := make(map[string]any)
	info, _ := getUser(u.UserID)
	info, _ := getUser(u.Username)
	tmpl["UserInfo"] = info
	tmpl["Notice"] = "Updated!"
	serveHTML(w, r, "me", tmpl)


@@ 620,7 615,7 @@ func serve() {
	r.HandleFunc("/", indexPage)
	r.HandleFunc("GET /f/{forum}", forumPage)
	r.HandleFunc("GET /f/{forum}/{threadid}", threadPage)
	r.HandleFunc("GET /user/{userid}", userPage)
	r.HandleFunc("GET /u/{username}", userPage)
	r.HandleFunc("GET /login", loginPage)
	// TODO limit registration successes
	// TODO csrf wrap?