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

more content and member links on the dashboard #155

Merged
merged 1 commit into from
Apr 15, 2021
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
36 changes: 30 additions & 6 deletions web/handlers/admin/dashboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,10 @@ import (
"net/http"

"go.mindeco.de/http/render"
refs "go.mindeco.de/ssb-refs"

"github.com/ssb-ngi-pointer/go-ssb-room/internal/network"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/roomstate"
weberrors "github.com/ssb-ngi-pointer/go-ssb-room/web/errors"
)
Expand All @@ -17,31 +20,52 @@ type dashboardHandler struct {
flashes *weberrors.FlashHelper

roomState *roomstate.Manager
netInfo network.ServerEndpointDetails
dbs Databases
}

func (h dashboardHandler) overview(w http.ResponseWriter, req *http.Request) (interface{}, error) {
roomRef := h.netInfo.RoomID.Ref()

onlineRefs := h.roomState.List()
onlineCount := len(onlineRefs)

onlineMembers := make([]roomdb.Member, len(onlineRefs))
for i := range onlineRefs {
ref, err := refs.ParseFeedRef(onlineRefs[i])
if err != nil {
return nil, fmt.Errorf("failed to parse online ref: %w", err)
}
onlineMembers[i], err = h.dbs.Members.GetByFeed(req.Context(), *ref)
if err != nil {
// TODO: do we want to show "external users" (non-members) on the dashboard?
return nil, fmt.Errorf("failed to lookup online member: %w", err)
}
}

onlineCount := len(onlineMembers)

memberCount, err := h.dbs.Members.Count(req.Context())
if err != nil {
return nil, fmt.Errorf("failed to count members: %w", err)
}

inviteCount, err := h.dbs.Invites.Count(req.Context())
if err != nil {
return nil, fmt.Errorf("failed to count invites: %w", err)
}

deniedCount, err := h.dbs.DeniedKeys.Count(req.Context())
if err != nil {
return nil, fmt.Errorf("failed to count denied keys: %w", err)
}

pageData := map[string]interface{}{
"OnlineRefs": onlineRefs,
"OnlineCount": onlineCount,
"MemberCount": memberCount,
"InviteCount": inviteCount,
"DeniedCount": deniedCount,
"RoomRef": roomRef,
"OnlineMembers": onlineMembers,
"OnlineCount": onlineCount,
"MemberCount": memberCount,
"InviteCount": inviteCount,
"DeniedCount": deniedCount,
}

pageData["Flashes"], err = h.flashes.GetAll(w, req)
Expand Down
6 changes: 4 additions & 2 deletions web/handlers/admin/handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import (
"github.com/vcraescu/go-paginator/v2/view"
"go.mindeco.de/http/render"

"github.com/ssb-ngi-pointer/go-ssb-room/internal/network"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
"github.com/ssb-ngi-pointer/go-ssb-room/roomstate"
weberrors "github.com/ssb-ngi-pointer/go-ssb-room/web/errors"
Expand Down Expand Up @@ -58,7 +59,7 @@ type Databases struct {
// Handler supplies the elevated access pages to known users.
// It is not registering on the mux router like other pages to clean up the authorize flow.
func Handler(
domainName string,
netInfo network.ServerEndpointDetails,
r *render.Renderer,
roomState *roomstate.Manager,
fh *weberrors.FlashHelper,
Expand All @@ -69,6 +70,7 @@ func Handler(
var dashboardHandler = dashboardHandler{
r: r,
flashes: fh,
netInfo: netInfo,

dbs: dbs,
roomState: roomState,
Expand Down Expand Up @@ -126,7 +128,7 @@ func Handler(
db: dbs.Invites,
config: dbs.Config,

domainName: domainName,
domainName: netInfo.Domain,
}

mux.HandleFunc("/invites", r.HTML("admin/invite-list.tmpl", ih.overview))
Expand Down
2 changes: 1 addition & 1 deletion web/handlers/admin/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestDashoard(t *testing.T) {
ts := newSession(t)
a := assert.New(t)

testRef := refs.FeedRef{Algo: "test", ID: bytes.Repeat([]byte{0}, 16)}
testRef := refs.FeedRef{Algo: "ed25519", ID: bytes.Repeat([]byte{0}, 32)}
ts.RoomState.AddEndpoint(testRef, nil) // 1 online
ts.MembersDB.CountReturns(4, nil) // 4 members
ts.InvitesDB.CountReturns(3, nil) // 3 invites
Expand Down
16 changes: 11 additions & 5 deletions web/handlers/admin/setup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
package admin

import (
"bytes"
"context"
"crypto/rand"
"net/http"
Expand All @@ -18,7 +19,9 @@ import (
"go.mindeco.de/http/render"
"go.mindeco.de/http/tester"
"go.mindeco.de/logging/logtest"
refs "go.mindeco.de/ssb-refs"

"github.com/ssb-ngi-pointer/go-ssb-room/internal/network"
"github.com/ssb-ngi-pointer/go-ssb-room/internal/randutil"
"github.com/ssb-ngi-pointer/go-ssb-room/internal/repo"
"github.com/ssb-ngi-pointer/go-ssb-room/roomdb"
Expand All @@ -33,8 +36,8 @@ import (
)

type testSession struct {
Domain string
Mux *http.ServeMux
netInfo network.ServerEndpointDetails
Mux *http.ServeMux

Client *tester.Tester

Expand Down Expand Up @@ -71,7 +74,10 @@ func newSession(t *testing.T) *testSession {
ctx := context.TODO()
ts.RoomState = roomstate.NewManager(ctx, log)

ts.Domain = randutil.String(10)
ts.netInfo = network.ServerEndpointDetails{
Domain: randutil.String(10),
RoomID: refs.FeedRef{Algo: "ed25519", ID: bytes.Repeat([]byte{0}, 32)},
}

// instantiate the urlTo helper (constructs urls for us!)
// the cookiejar in our custom http/tester needs a non-empty domain and scheme
Expand All @@ -82,7 +88,7 @@ func newSession(t *testing.T) *testSession {
if testURL == nil {
t.Fatalf("no URL for %s", name)
}
testURL.Host = ts.Domain
testURL.Host = ts.netInfo.Domain
testURL.Scheme = "https" // fake
return testURL
}
Expand Down Expand Up @@ -139,7 +145,7 @@ func newSession(t *testing.T) *testSession {
}

handler := Handler(
ts.Domain,
ts.netInfo,
r,
ts.RoomState,
flashHelper,
Expand Down
4 changes: 2 additions & 2 deletions web/handlers/auth_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ func TestFallbackAuthWorks(t *testing.T) {
{"title", "AdminDashboardTitle"},
})

testRef := refs.FeedRef{Algo: "test", ID: bytes.Repeat([]byte{0}, 16)}
testRef := refs.FeedRef{Algo: "ed25519", ID: bytes.Repeat([]byte{0}, 32)}
ts.RoomState.AddEndpoint(testRef, nil)

html, resp = ts.Client.GetHTML(dashboardURL)
Expand All @@ -189,7 +189,7 @@ func TestFallbackAuthWorks(t *testing.T) {
{"title", "AdminDashboardTitle"},
})

testRef2 := refs.FeedRef{Algo: "test", ID: bytes.Repeat([]byte{1}, 16)}
testRef2 := refs.FeedRef{Algo: "ed25519", ID: bytes.Repeat([]byte{1}, 32)}
ts.RoomState.AddEndpoint(testRef2, nil)

html, resp = ts.Client.GetHTML(dashboardURL)
Expand Down
2 changes: 1 addition & 1 deletion web/handlers/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -233,7 +233,7 @@ func New(

// all the admin routes
adminHandler := admin.Handler(
netInfo.Domain,
netInfo,
r,
roomState,
flashHelper,
Expand Down
2 changes: 1 addition & 1 deletion web/i18n/defaults/active.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ AuthFallbackInstruct = "This method is an acceptable fallback, if you have a use
#########################

AdminDashboardTitle = "Dashboard"
AdminDashboardWelcome = "Welcome to your dashboard"
AdminDashboardRoomID = "This room's ID is"

# privacy modes
###############
Expand Down
23 changes: 20 additions & 3 deletions web/templates/admin/dashboard.tmpl
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@
>{{i18n "AdminDashboardTitle"}}</h1>

{{ template "flashes" . }}


<div class="mt-6 flex flex-row justify-start items-center py-3 px-4 border-gray-200 border-2 rounded-3xl text-gray-500">
<span class="break-all"
>{{i18n "AdminDashboardRoomID"}} <span class="font-mono">{{.RoomRef}}</span>
</span>
</div>

<div class="flex flex-col-reverse sm:flex-row justify-start items-stretch ">
<div class="sm:mr-4 mt-6 py-6 px-4 border-gray-200 border-2 rounded-3xl flex flex-col justify-start items-start">
<div class="grid grid-rows-2 grid-flow-col gap-x-4 gap-y-0">
Expand Down Expand Up @@ -79,11 +85,22 @@
<div class="ml-11 h-8 w-0.5 bg-gray-200"></div>
{{end}}

{{range .OnlineRefs}}
{{range .OnlineMembers}}
{{$member := .PubKey.Ref}}
{{$memberIsAlias := false}}
{{range $index, $alias := .Aliases}}
{{if eq $index 0}}
{{$member = $alias.Name}}
{{$memberIsAlias = true}}
{{end}}
{{end}}
<div class="ml-11 h-8 w-0.5 bg-gray-200"></div>
<div class="ml-11 relative h-3">
<div class="absolute inline-flex w-3 h-3 bg-green-500 rounded-full -left-1 -ml-px"></div>
<div class="absolute w-44 sm:w-auto -top-1.5 ml-5 pl-1 font-mono truncate flex-auto text-gray-700">{{.}}</div>
<a
href="{{urlTo "admin:member:details" "id" .ID}}"
class="absolute w-44 sm:w-auto -top-1.5 ml-5 pl-1 font-mono truncate flex-auto text-gray-700 hover:underline"
>{{$member}}</a>
</div>
{{end}}
</div>
Expand Down