added notify helpers
This commit is contained in:
@@ -16,6 +16,13 @@ import (
|
|||||||
func Login(server *hws.Server, cfg *config.Config, st *store.Store, discordAPI *discord.APIClient) http.Handler {
|
func Login(server *hws.Server, cfg *config.Config, st *store.Store, discordAPI *discord.APIClient) http.Handler {
|
||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
// TODO: check DB is connected
|
||||||
|
// check discord API is working
|
||||||
|
if r.Method == "POST" {
|
||||||
|
// if either fail, notify the client that login is unavailable right now
|
||||||
|
// otherwise proceed redirect to GET method
|
||||||
|
}
|
||||||
|
// if either fail and method is GET, show service not available page
|
||||||
cookies.SetPageFrom(w, r, cfg.HWSAuth.TrustedHost)
|
cookies.SetPageFrom(w, r, cfg.HWSAuth.TrustedHost)
|
||||||
attempts, exceeded, track := st.TrackRedirect(r, "/login", 5)
|
attempts, exceeded, track := st.TrackRedirect(r, "/login", 5)
|
||||||
|
|
||||||
|
|||||||
50
internal/handlers/notifications.go
Normal file
50
internal/handlers/notifications.go
Normal file
@@ -0,0 +1,50 @@
|
|||||||
|
package handlers
|
||||||
|
|
||||||
|
import (
|
||||||
|
"net/http"
|
||||||
|
|
||||||
|
"git.haelnorr.com/h/golib/hws"
|
||||||
|
"git.haelnorr.com/h/golib/notify"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func notifyClient(
|
||||||
|
s *hws.Server,
|
||||||
|
r *http.Request,
|
||||||
|
level notify.Level,
|
||||||
|
title, message, details string,
|
||||||
|
action any,
|
||||||
|
) error {
|
||||||
|
subCookie, err := r.Cookie("ws_sub_id")
|
||||||
|
if err != nil {
|
||||||
|
return errors.Wrap(err, "r.Cookie")
|
||||||
|
}
|
||||||
|
subID := notify.Target(subCookie.Value)
|
||||||
|
nt := notify.Notification{
|
||||||
|
Target: subID,
|
||||||
|
Title: title,
|
||||||
|
Message: message,
|
||||||
|
Details: details,
|
||||||
|
Action: action,
|
||||||
|
Level: level,
|
||||||
|
}
|
||||||
|
s.NotifySub(nt)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func notifyInternalServiceError(s *hws.Server, r *http.Request, msg string, err error) error {
|
||||||
|
return notifyClient(s, r, notify.LevelError, "Internal Service Error", msg,
|
||||||
|
SerializeErrorDetails(http.StatusInternalServerError, err), nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notifyWarn(s *hws.Server, r *http.Request, title, msg string, action any) error {
|
||||||
|
return notifyClient(s, r, notify.LevelWarn, title, msg, "", action)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notifyInfo(s *hws.Server, r *http.Request, title, msg string, action any) error {
|
||||||
|
return notifyClient(s, r, notify.LevelInfo, title, msg, "", action)
|
||||||
|
}
|
||||||
|
|
||||||
|
func notifySuccess(s *hws.Server, r *http.Request, title, msg string, action any) error {
|
||||||
|
return notifyClient(s, r, notify.LevelSuccess, title, msg, "", action)
|
||||||
|
}
|
||||||
@@ -18,7 +18,7 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
func Register(
|
func Register(
|
||||||
server *hws.Server,
|
s *hws.Server,
|
||||||
auth *hwsauth.Authenticator[*db.User, bun.Tx],
|
auth *hwsauth.Authenticator[*db.User, bun.Tx],
|
||||||
conn *bun.DB,
|
conn *bun.DB,
|
||||||
cfg *config.Config,
|
cfg *config.Config,
|
||||||
@@ -42,7 +42,7 @@ func Register(
|
|||||||
store.ClearRedirectTrack(r, "/register")
|
store.ClearRedirectTrack(r, "/register")
|
||||||
|
|
||||||
throwError(
|
throwError(
|
||||||
server,
|
s,
|
||||||
w,
|
w,
|
||||||
r,
|
r,
|
||||||
http.StatusBadRequest,
|
http.StatusBadRequest,
|
||||||
@@ -69,7 +69,7 @@ func Register(
|
|||||||
defer cancel()
|
defer cancel()
|
||||||
tx, err := conn.BeginTx(ctx, nil)
|
tx, err := conn.BeginTx(ctx, nil)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
throwInternalServiceError(server, w, r, "Database transaction failed", err)
|
throwInternalServiceError(s, w, r, "Database transaction failed", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
defer tx.Rollback()
|
defer tx.Rollback()
|
||||||
@@ -83,7 +83,10 @@ func Register(
|
|||||||
username := r.FormValue("username")
|
username := r.FormValue("username")
|
||||||
user, err := registerUser(ctx, tx, username, details)
|
user, err := registerUser(ctx, tx, username, details)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
throwInternalServiceError(server, w, r, "Registration failed", err)
|
err = notifyInternalServiceError(s, r, "Registration failed", err)
|
||||||
|
if err != nil {
|
||||||
|
throwInternalServiceError(s, w, r, "Registration failed", err)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
tx.Commit()
|
tx.Commit()
|
||||||
@@ -92,7 +95,7 @@ func Register(
|
|||||||
} else {
|
} else {
|
||||||
err = auth.Login(w, r, user, true)
|
err = auth.Login(w, r, user, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
throwInternalServiceError(server, w, r, "Login failed", err)
|
throwInternalServiceError(s, w, r, "Login failed", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
pageFrom := cookies.CheckPageFrom(w, r)
|
pageFrom := cookies.CheckPageFrom(w, r)
|
||||||
|
|||||||
@@ -6,13 +6,12 @@ import (
|
|||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
|
||||||
"git.haelnorr.com/h/golib/hws"
|
"git.haelnorr.com/h/golib/hws"
|
||||||
"git.haelnorr.com/h/golib/notify"
|
|
||||||
"git.haelnorr.com/h/oslstats/internal/view/page"
|
"git.haelnorr.com/h/oslstats/internal/view/page"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Handles responses to the / path. Also serves a 404 Page for paths that
|
// Handles responses to the / path. Also serves a 404 Page for paths that
|
||||||
// don't have explicit handlers
|
// don't have explicit handlers
|
||||||
func NotifyTester(server *hws.Server) http.Handler {
|
func NotifyTester(s *hws.Server) http.Handler {
|
||||||
return http.HandlerFunc(
|
return http.HandlerFunc(
|
||||||
func(w http.ResponseWriter, r *http.Request) {
|
func(w http.ResponseWriter, r *http.Request) {
|
||||||
testErr := errors.New("This is a stack trace. No really i swear. Just pretend ok? Thanks")
|
testErr := errors.New("This is a stack trace. No really i swear. Just pretend ok? Thanks")
|
||||||
@@ -26,31 +25,24 @@ func NotifyTester(server *hws.Server) http.Handler {
|
|||||||
page.Test().Render(r.Context(), w)
|
page.Test().Render(r.Context(), w)
|
||||||
} else {
|
} else {
|
||||||
r.ParseForm()
|
r.ParseForm()
|
||||||
target := r.Form.Get("target")
|
// target := r.Form.Get("target")
|
||||||
title := r.Form.Get("title")
|
title := r.Form.Get("title")
|
||||||
level := map[string]notify.Level{
|
level := r.Form.Get("type")
|
||||||
"info": notify.LevelInfo,
|
|
||||||
"success": notify.LevelSuccess,
|
|
||||||
"warn": notify.LevelWarn,
|
|
||||||
"error": notify.LevelError,
|
|
||||||
}[r.Form.Get("type")]
|
|
||||||
message := r.Form.Get("message")
|
message := r.Form.Get("message")
|
||||||
nt := notify.Notification{
|
|
||||||
Target: notify.Target(target),
|
|
||||||
Title: title,
|
|
||||||
Message: message,
|
|
||||||
Level: level,
|
|
||||||
}
|
|
||||||
|
|
||||||
// For error level, serialize error details with code
|
var err error
|
||||||
if level == notify.LevelError {
|
switch level {
|
||||||
nt.Details = SerializeErrorDetails(500, testErr)
|
case "success":
|
||||||
|
err = notifySuccess(s, r, title, message, nil)
|
||||||
|
case "info":
|
||||||
|
err = notifyInfo(s, r, title, message, nil)
|
||||||
|
case "warn":
|
||||||
|
err = notifyWarn(s, r, title, message, nil)
|
||||||
|
case "error":
|
||||||
|
err = notifyInternalServiceError(s, r, message, testErr)
|
||||||
}
|
}
|
||||||
|
if err != nil {
|
||||||
if target == "all" {
|
throwInternalServiceError(s, w, r, "Error notifying client", err)
|
||||||
server.NotifyAll(nt)
|
|
||||||
} else {
|
|
||||||
server.NotifySub(nt)
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
|
|||||||
@@ -40,7 +40,7 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int)
|
|||||||
<button
|
<button
|
||||||
onclick={ templ.JSFuncCall("copyToClipboard", fmt.Sprintf("error-modal-ws-details-%v", id), "copyButton") }
|
onclick={ templ.JSFuncCall("copyToClipboard", fmt.Sprintf("error-modal-ws-details-%v", id), "copyButton") }
|
||||||
id="copyButton"
|
id="copyButton"
|
||||||
class="mt-2 bg-mauve text-crust px-3 py-1 rounded text-xs hover:bg-mauve/75 transition"
|
class="mt-2 bg-mauve text-crust px-3 py-1 rounded text-xs hover:bg-mauve/75 transition hover:cursor-pointer"
|
||||||
title="Copy to clipboard"
|
title="Copy to clipboard"
|
||||||
>
|
>
|
||||||
Copy
|
Copy
|
||||||
@@ -52,7 +52,7 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int)
|
|||||||
<div class="mt-6">
|
<div class="mt-6">
|
||||||
<button
|
<button
|
||||||
onclick="document.getElementById('error-modal-ws-container').innerHTML = ''"
|
onclick="document.getElementById('error-modal-ws-container').innerHTML = ''"
|
||||||
class="inline-block rounded-lg bg-mauve px-5 py-3 text-sm text-crust transition hover:bg-mauve/75"
|
class="inline-block rounded-lg bg-mauve px-5 py-3 text-sm text-crust transition hover:bg-mauve/75 hover:cursor-pointer"
|
||||||
>
|
>
|
||||||
Close Modal
|
Close Modal
|
||||||
</button>
|
</button>
|
||||||
@@ -62,4 +62,3 @@ templ ErrorModalWS(code int, stacktrace string, nt notify.Notification, id int)
|
|||||||
<script src="/static/js/copytoclipboard.js"></script>
|
<script src="/static/js/copytoclipboard.js"></script>
|
||||||
</div>
|
</div>
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -36,7 +36,6 @@ templ Test() {
|
|||||||
Target
|
Target
|
||||||
</label>
|
</label>
|
||||||
<input
|
<input
|
||||||
required
|
|
||||||
type="text"
|
type="text"
|
||||||
name="target"
|
name="target"
|
||||||
id="target"
|
id="target"
|
||||||
@@ -73,7 +72,7 @@ templ Test() {
|
|||||||
<!-- Submit Button -->
|
<!-- Submit Button -->
|
||||||
<button
|
<button
|
||||||
type="submit"
|
type="submit"
|
||||||
class="mt-2 bg-mauve text-crust font-semibold px-6 py-3 rounded-lg hover:bg-mauve/75 transition"
|
class="mt-2 bg-mauve text-crust font-semibold px-6 py-3 rounded-lg hover:bg-mauve/75 transition hover:cursor-pointer"
|
||||||
>
|
>
|
||||||
Send Notification
|
Send Notification
|
||||||
</button>
|
</button>
|
||||||
|
|||||||
Reference in New Issue
Block a user