Add support for custom alerts

This commit is contained in:
Knut Ahlers 2020-11-23 11:34:08 +01:00
parent 1569df222e
commit 5600bff429
Signed by: luzifer
GPG key ID: 0066F03ED215AD7D
2 changed files with 39 additions and 5 deletions

33
api.go
View file

@ -2,6 +2,7 @@ package main
import ( import (
"crypto/sha256" "crypto/sha256"
"encoding/json"
"fmt" "fmt"
"net/http" "net/http"
"strings" "strings"
@ -89,12 +90,38 @@ var upgrader = websocket.Upgrader{
} }
func registerAPI(r *mux.Router) { func registerAPI(r *mux.Router) {
r.HandleFunc("/api/follows/clear-last", handleSetLastFollower) r.HandleFunc("/api/custom-alert", handleCustomAlert).Methods(http.MethodPost)
r.HandleFunc("/api/follows/set-last/{name}", handleSetLastFollower) r.HandleFunc("/api/follows/clear-last", handleSetLastFollower).Methods(http.MethodPut)
r.HandleFunc("/api/subscribe", handleUpdateSocket) r.HandleFunc("/api/follows/set-last/{name}", handleSetLastFollower).Methods(http.MethodPut)
r.HandleFunc("/api/subscribe", handleUpdateSocket).Methods(http.MethodGet)
r.HandleFunc("/api/webhook/{type}", handleWebHookPush) r.HandleFunc("/api/webhook/{type}", handleWebHookPush)
} }
func handleCustomAlert(w http.ResponseWriter, r *http.Request) {
var alert struct {
Sound *string `json:"sound"`
Text string `json:"text"`
Title string `json:"title"`
}
if err := json.NewDecoder(r.Body).Decode(&alert); err != nil {
http.Error(w, errors.Wrap(err, "parse request body").Error(), http.StatusBadRequest)
return
}
if alert.Title == "" || alert.Text == "" {
http.Error(w, "empty title or text", http.StatusBadRequest)
return
}
if err := subscriptions.SendAllSockets("alert", alert); err != nil {
http.Error(w, errors.Wrap(err, "send to sockets").Error(), http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusCreated)
}
func handleSetLastFollower(w http.ResponseWriter, r *http.Request) { func handleSetLastFollower(w http.ResponseWriter, r *http.Request) {
name := mux.Vars(r)["name"] name := mux.Vars(r)["name"]

View file

@ -52,11 +52,11 @@ const app = new Vue({
this.sound.src = soundUrl this.sound.src = soundUrl
}, },
showAlert(title, text) { showAlert(title, text, variant) {
this.$bvToast.toast(text, { this.$bvToast.toast(text, {
title, title,
toaster: 'b-toaster-top-right', toaster: 'b-toaster-top-right',
variant: 'primary', variant: variant || 'primary',
}) })
}, },
@ -83,6 +83,13 @@ const app = new Vue({
} }
switch (data.type) { switch (data.type) {
case 'alert':
this.showAlert(data.payload.title, data.payload.text, data.payload.variant)
if (data.payload.sound) {
this.playSound(data.payload.sound)
}
break
case 'store': case 'store':
this.store = data.payload this.store = data.payload
break break