package server
import (
"fmt"
"net/http"
"net/url"
"prism/service/notification"
)
func (s *Server) handleFragmentApps(w http.ResponseWriter, r *http.Request) {
mappings, err := s.store.GetAllMappings()
if err != nil {
s.logger.Error("Failed to get mappings", "error", err)
http.Error(w, "Internal server error", http.StatusInternalServerError)
return
}
w.Header().Set("Content-Type", "text/html")
_, _ = fmt.Fprint(w, s.getAppsListHTML(mappings)) //nolint:errcheck
}
func (s *Server) getAppsListHTML(mappings []notification.Mapping) string {
if len(mappings) == 0 {
return `
No apps registered yet. See real-world examples to get started.
`
}
signalLinked := false
if s.integrations.Signal != nil {
handlers := s.integrations.Signal.GetHandlers()
if handlers != nil {
account, _ := handlers.GetClient().GetLinkedAccount()
signalLinked = account != nil
}
}
telegramConfigured := s.cfg.IsTelegramEnabled() && s.cfg.TelegramChatID != 0
var html string
html += ``
for _, m := range mappings {
isSignal := m.Channel == notification.ChannelSignal
isWebPush := m.Channel == notification.ChannelWebPush
isTelegram := m.Channel == notification.ChannelTelegram
channelBadge := ""
channelTooltip := ""
if isSignal && m.Signal != nil && m.Signal.GroupID != "" {
channelBadge = "Signal"
channelTooltip = fmt.Sprintf(`Group ID: %s`, m.Signal.GroupID)
} else if isWebPush && m.WebPush != nil && m.WebPush.Endpoint != "" {
channelBadge = "WebPush"
channelTooltip = fmt.Sprintf(`%s`, m.WebPush.Endpoint)
} else if isTelegram && telegramConfigured {
channelBadge = "Telegram"
} else {
channelBadge = "Not Configured"
if isSignal {
channelTooltip = `No Signal group created yet. Will auto-create on first notification.`
} else if isTelegram {
channelTooltip = `Set TELEGRAM_CHAT_ID in .env`
} else {
channelTooltip = `No WebPush endpoint registered.`
}
}
itemHTML := fmt.Sprintf(`-
%s
`, m.AppName)
if channelBadge != "Not Configured" {
itemHTML += fmt.Sprintf(`%s%s`, m.Channel, channelBadge, channelTooltip)
} else {
itemHTML += fmt.Sprintf(`%s%s`, channelBadge, channelTooltip)
}
if m.WebPush != nil && isWebPush {
if u, err := url.Parse(m.WebPush.Endpoint); err == nil {
itemHTML += fmt.Sprintf(`%s`, u.Hostname())
}
}
itemHTML += `
`
var channelOptions string
optionCount := 0
if signalLinked {
channelOptions += fmt.Sprintf(``, map[bool]string{true: " selected", false: ""}[isSignal])
optionCount++
}
if telegramConfigured {
channelOptions += fmt.Sprintf(``, map[bool]string{true: " selected", false: ""}[isTelegram])
optionCount++
}
if m.WebPush != nil && m.WebPush.Endpoint != "" {
channelOptions += fmt.Sprintf(``, map[bool]string{true: " selected", false: ""}[isWebPush])
optionCount++
}
if optionCount > 1 {
itemHTML += fmt.Sprintf(``, m.AppName, channelOptions)
}
itemHTML += fmt.Sprintf(`
`, m.AppName)
html += itemHTML
}
html += `
`
return html
}