mapper page

This commit is contained in:
Michael Zhang 2021-07-22 03:32:53 -05:00
parent 8f2d1791e1
commit c3a321b50a
Signed by: michael
GPG key ID: BDA47A31A3C8EE6B
10 changed files with 155 additions and 37 deletions

View file

@ -6,7 +6,6 @@ package db
// channel/<channel_id>/tracks/<mapper_id> -> priority // channel/<channel_id>/tracks/<mapper_id> -> priority
import ( import (
"fmt"
"strconv" "strconv"
"github.com/pkg/errors" "github.com/pkg/errors"
@ -59,7 +58,15 @@ func (db *Db) SaveBeatmap(beatmap Beatmapset) {
func (db *Db) IterTrackedBeatmapsets(limit int, fn func(beatmapset Beatmapset) error) (err error) { func (db *Db) IterTrackedBeatmapsets(limit int, fn func(beatmapset Beatmapset) error) (err error) {
var beatmapsets []Beatmapset var beatmapsets []Beatmapset
db.gorm.Preload(clause.Associations).Limit(limit).Order("last_updated desc").Find(&beatmapsets) db.gorm.Preload(clause.Associations).Limit(limit).Order("last_updated desc").Find(&beatmapsets)
fmt.Println("HELLOSU", beatmapsets) for _, beatmapset := range beatmapsets {
fn(beatmapset)
}
return
}
func (db *Db) IterTrackedBeatmapsetsBy(mapperId int, fn func(beatmapset Beatmapset) error) (err error) {
var beatmapsets []Beatmapset
db.gorm.Where("mapper_id = ?", mapperId).Order("last_updated desc").Find(&beatmapsets)
for _, beatmapset := range beatmapsets { for _, beatmapset := range beatmapsets {
fn(beatmapset) fn(beatmapset)
} }

View file

@ -65,3 +65,17 @@ func (web *Web) loginCallback(c *gin.Context) {
c.Redirect(http.StatusTemporaryRedirect, "/") c.Redirect(http.StatusTemporaryRedirect, "/")
} }
func isLoggedIn(c *gin.Context) bool {
session := sessions.Default(c)
var accessToken string
loggedIn := false
accessTokenI := session.Get("access_token")
if accessTokenI != nil {
accessToken = accessTokenI.(string)
if len(accessToken) > 0 {
loggedIn = true
}
}
return loggedIn
}

38
web/mapper.go Normal file
View file

@ -0,0 +1,38 @@
package web
import (
"net/http"
"strconv"
"subscribe-bot/db"
"github.com/gin-gonic/gin"
"github.com/pkg/errors"
)
func (web *Web) mapperIndex(c *gin.Context) (err error) {
userId := c.Param("userId")
mapperId, err := strconv.Atoi(userId)
if err != nil {
err = errors.Wrap(err, "id is not an integer")
return
}
mapper, err := web.db.GetUser(mapperId)
if err != nil {
err = errors.Wrap(err, "failed to get user from mapper-index")
return
}
beatmapsets := make([]db.Beatmapset, 0)
web.db.IterTrackedBeatmapsetsBy(mapperId, func(beatmapset db.Beatmapset) error {
beatmapsets = append(beatmapsets, beatmapset)
return nil
})
web.render(c, http.StatusOK, "mapper-index.html", gin.H{
"Mapper": mapper,
"Beatmapsets": beatmapsets,
})
return
}

30
web/render.go Normal file
View file

@ -0,0 +1,30 @@
package web
import (
"log"
"net/http"
"github.com/gin-gonic/gin"
)
func (web *Web) errorWrap(fn func(*gin.Context) error) func(*gin.Context) {
return func(c *gin.Context) {
err := fn(c)
if err != nil {
c.String(http.StatusInternalServerError, "error")
log.Println("fatal error", err)
}
}
}
func (web *Web) render(c *gin.Context, code int, tmpl string, obj gin.H) {
base := gin.H{
"IsLoggedIn": isLoggedIn(c),
}
for key, val := range obj {
base[key] = val
}
c.HTML(code, tmpl, base)
}

View file

@ -17,7 +17,7 @@ import (
"github.com/go-git/go-git/v5/plumbing/object" "github.com/go-git/go-git/v5/plumbing/object"
) )
func (web *Web) mapVersions(c *gin.Context) { func (web *Web) mapVersions(c *gin.Context) (err error) {
userId := c.Param("userId") userId := c.Param("userId")
mapId := c.Param("mapId") mapId := c.Param("mapId")
@ -56,14 +56,15 @@ func (web *Web) mapVersions(c *gin.Context) {
}) })
} }
c.HTML(http.StatusOK, "map-version.html", gin.H{ web.render(c, http.StatusOK, "map-version.html", gin.H{
"Beatmapset": bs, "Beatmapset": bs,
"LoggedIn": isLoggedIn(c), "LoggedIn": isLoggedIn(c),
"Versions": versions, "Versions": versions,
}) })
return
} }
func (web *Web) mapPatch(c *gin.Context) { func (web *Web) mapPatch(c *gin.Context) (err error) {
userId := c.Param("userId") userId := c.Param("userId")
mapId := c.Param("mapId") mapId := c.Param("mapId")
hash := c.Param("hash") hash := c.Param("hash")
@ -77,9 +78,10 @@ func (web *Web) mapPatch(c *gin.Context) {
patch, _ := parent.Patch(commit) patch, _ := parent.Patch(commit)
c.String(http.StatusOK, "text/plain", patch.String()) c.String(http.StatusOK, "text/plain", patch.String())
return
} }
func (web *Web) mapZip(c *gin.Context) { func (web *Web) mapZip(c *gin.Context) (err error) {
userId := c.Param("userId") userId := c.Param("userId")
mapId := c.Param("mapId") mapId := c.Param("mapId")
hash := c.Param("hash") hash := c.Param("hash")
@ -111,4 +113,5 @@ func (web *Web) mapZip(c *gin.Context) {
return false return false
}) })
return
} }

View file

@ -1,21 +1,34 @@
:root {
--primary-color: #09c;
}
body, html { body, html {
font-family: Arial, Helvetica, sans-serif; font-family: Arial, Helvetica, sans-serif;
} }
.title a { .title a {
color: black; color: black;
text-decoration: none; text-decoration: none;
} }
.nav-bar { .nav-bar {
background-color: #09c; background-color: var(--primary-color);
padding: 5px; padding: 5px;
} }
.nav-bar a { .nav-bar a {
color: white; color: white;
padding: 7px; padding: 7px;
margin: 0px 3px; margin: 0px 3px;
}
a {
color: var(--primary-color);
text-decoration: none;
}
a:hover {
text-decoration: underline;
} }
/* containers */ /* containers */

View file

@ -17,8 +17,8 @@ Currently tracking <b>{{ .TotalMaps }}</b> maps from <b>{{ .TotalUsers }}</b> us
{{ range .Beatmapsets }} {{ range .Beatmapsets }}
<tr> <tr>
<td> <td>
<a href="https://osu.ppy.sh/s/{{ .ID }}" target="_blank">osu</a> <a href="https://osu.ppy.sh/s/{{ .ID }}" target="_blank">[osu]</a>
<a href="/map/{{ .MapperID }}/{{ .ID }}/versions">versions</a> <a href="/map/{{ .MapperID }}/{{ .ID }}/versions">[versions]</a>
</td> </td>
<td>{{ .Artist }} - {{ .Title }}</td> <td>{{ .Artist }} - {{ .Title }}</td>
<td> <td>

View file

@ -22,9 +22,9 @@
<tr> <tr>
<td><span title="{{ .Date }}">{{ .HumanDate }}</span></td> <td><span title="{{ .Date }}">{{ .HumanDate }}</span></td>
<td> <td>
<a href="zip/{{ .Hash }}" target="_blank">zip</a> <a href="zip/{{ .Hash }}" target="_blank">[zip]</a>
{{ if .HasParent }} {{ if .HasParent }}
<a href="patch/{{ .Hash }}" target="_blank">patch</a> <a href="patch/{{ .Hash }}" target="_blank">[patch]</a>
{{ end }} {{ end }}
</td> </td>
<td><pre>{{ .Summary }}</pre></td> <td><pre>{{ .Summary }}</pre></td>

View file

@ -0,0 +1,27 @@
{{ define "content" }}
<h3>Tracked Maps by <a href="https://osu.ppy.sh/u/{{ .Mapper.ID }}">{{ .Mapper.Username }}</a></h3>
<table class="table-auto">
<thead>
<th>Links</th>
<th>Title</th>
<th>Updated</th>
</thead>
<tbody>
{{ range .Beatmapsets }}
<tr>
<td>
<a href="https://osu.ppy.sh/s/{{ .ID }}" target="_blank">[osu]</a>
<a href="/map/{{ .MapperID }}/{{ .ID }}/versions">[versions]</a>
</td>
<td>{{ .Artist }} - {{ .Title }}</td>
<td>
{{ .LastUpdated | humanize }}
</td>
</tr>
{{ end }}
</tbody>
</table>
{{ end }}

View file

@ -70,15 +70,15 @@ func (web *Web) Run() {
r.GET("/login", web.login) r.GET("/login", web.login)
r.GET("/login/callback", web.loginCallback) r.GET("/login/callback", web.loginCallback)
r.GET("/map/:userId/:mapId/versions", web.mapVersions) r.GET("/map/:userId", web.errorWrap(web.mapperIndex))
r.GET("/map/:userId/:mapId/patch/:hash", web.mapPatch) r.GET("/map/:userId/:mapId/versions", web.errorWrap(web.mapVersions))
r.GET("/map/:userId/:mapId/zip/:hash", web.mapZip) r.GET("/map/:userId/:mapId/patch/:hash", web.errorWrap(web.mapPatch))
r.GET("/map/:userId/:mapId/zip/:hash", web.errorWrap(web.mapZip))
r.GET("/", func(c *gin.Context) { r.GET("/", func(c *gin.Context) {
beatmapSets := web.listRepos() beatmapSets := web.listRepos()
stats := web.db.GetStats() stats := web.db.GetStats()
c.HTML(http.StatusOK, "index.html", gin.H{ web.render(c, http.StatusOK, "index.html", gin.H{
"LoggedIn": isLoggedIn(c),
"Beatmapsets": beatmapSets, "Beatmapsets": beatmapSets,
"TotalMaps": stats.TotalMaps, "TotalMaps": stats.TotalMaps,
"TotalUsers": stats.TotalUsers, "TotalUsers": stats.TotalUsers,
@ -89,20 +89,6 @@ func (web *Web) Run() {
r.Run(addr) r.Run(addr)
} }
func isLoggedIn(c *gin.Context) bool {
session := sessions.Default(c)
var accessToken string
loggedIn := false
accessTokenI := session.Get("access_token")
if accessTokenI != nil {
accessToken = accessTokenI.(string)
if len(accessToken) > 0 {
loggedIn = true
}
}
return loggedIn
}
func (web *Web) listRepos() []db.Beatmapset { func (web *Web) listRepos() []db.Beatmapset {
beatmapSets := make([]db.Beatmapset, 0) beatmapSets := make([]db.Beatmapset, 0)
web.db.IterTrackedBeatmapsets(10, func(beatmapset db.Beatmapset) error { web.db.IterTrackedBeatmapsets(10, func(beatmapset db.Beatmapset) error {