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
import (
"fmt"
"strconv"
"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) {
var beatmapsets []Beatmapset
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 {
fn(beatmapset)
}

View file

@ -65,3 +65,17 @@ func (web *Web) loginCallback(c *gin.Context) {
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"
)
func (web *Web) mapVersions(c *gin.Context) {
func (web *Web) mapVersions(c *gin.Context) (err error) {
userId := c.Param("userId")
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,
"LoggedIn": isLoggedIn(c),
"Versions": versions,
})
return
}
func (web *Web) mapPatch(c *gin.Context) {
func (web *Web) mapPatch(c *gin.Context) (err error) {
userId := c.Param("userId")
mapId := c.Param("mapId")
hash := c.Param("hash")
@ -77,9 +78,10 @@ func (web *Web) mapPatch(c *gin.Context) {
patch, _ := parent.Patch(commit)
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")
mapId := c.Param("mapId")
hash := c.Param("hash")
@ -111,4 +113,5 @@ func (web *Web) mapZip(c *gin.Context) {
return false
})
return
}

View file

@ -1,3 +1,7 @@
:root {
--primary-color: #09c;
}
body, html {
font-family: Arial, Helvetica, sans-serif;
}
@ -8,7 +12,7 @@ body, html {
}
.nav-bar {
background-color: #09c;
background-color: var(--primary-color);
padding: 5px;
}
@ -18,6 +22,15 @@ body, html {
margin: 0px 3px;
}
a {
color: var(--primary-color);
text-decoration: none;
}
a:hover {
text-decoration: underline;
}
/* containers */
.container {

View file

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

View file

@ -22,9 +22,9 @@
<tr>
<td><span title="{{ .Date }}">{{ .HumanDate }}</span></td>
<td>
<a href="zip/{{ .Hash }}" target="_blank">zip</a>
<a href="zip/{{ .Hash }}" target="_blank">[zip]</a>
{{ if .HasParent }}
<a href="patch/{{ .Hash }}" target="_blank">patch</a>
<a href="patch/{{ .Hash }}" target="_blank">[patch]</a>
{{ end }}
</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/callback", web.loginCallback)
r.GET("/map/:userId/:mapId/versions", web.mapVersions)
r.GET("/map/:userId/:mapId/patch/:hash", web.mapPatch)
r.GET("/map/:userId/:mapId/zip/:hash", web.mapZip)
r.GET("/map/:userId", web.errorWrap(web.mapperIndex))
r.GET("/map/:userId/:mapId/versions", web.errorWrap(web.mapVersions))
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) {
beatmapSets := web.listRepos()
stats := web.db.GetStats()
c.HTML(http.StatusOK, "index.html", gin.H{
"LoggedIn": isLoggedIn(c),
web.render(c, http.StatusOK, "index.html", gin.H{
"Beatmapsets": beatmapSets,
"TotalMaps": stats.TotalMaps,
"TotalUsers": stats.TotalUsers,
@ -89,20 +89,6 @@ func (web *Web) Run() {
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 {
beatmapSets := make([]db.Beatmapset, 0)
web.db.IterTrackedBeatmapsets(10, func(beatmapset db.Beatmapset) error {