package server
import (
"fmt"
"net/http"
"net/url"
"prism/service/notification"
"prism/service/signal"
"prism/service/util"
)
func (s *Server) handleFragmentHealth(w http.ResponseWriter, r *http.Request) {
linked := s.getLinkedAccount()
signalOk := s.signalDaemon.IsRunning()
hasProton := s.cfg.IsProtonEnabled()
w.Header().Set("Content-Type", "text/html")
statusClass := "status-error"
statusText := "Disconnected and Unlinked"
var tooltip string
if signalOk && linked != nil {
statusClass = "status-ok"
statusText = "Connected and Linked"
tooltip = fmt.Sprintf(`%s`, util.FormatPhoneNumber(linked.Number))
} else if signalOk {
statusText = "Connected and Unlinked"
}
html := fmt.Sprintf(`
Signal: %s%s
`, statusClass, statusText, tooltip)
if hasProton {
protonStatus := "Disconnected"
protonClass := "status-error"
protonTooltip := ""
if s.protonMonitor.IsConnected() {
protonStatus = "Connected"
protonClass = "status-ok"
protonTooltip = fmt.Sprintf(`
%s`, s.cfg.ProtonIMAPUsername)
}
html += fmt.Sprintf(`
Proton Mail: %s%s
`, protonClass, protonStatus, protonTooltip)
}
html += `
`
currentLinked := linked != nil
if s.lastSignalLinked == nil || *s.lastSignalLinked != currentLinked {
html += `
`
html += s.getSignalInfoHTML()
html += `
`
s.lastSignalLinked = ¤tLinked
}
// Check if apps changed
mappings, err := s.store.GetAllMappings()
if err == nil {
currentAppsCount := len(mappings)
if s.lastAppsCount == nil || *s.lastAppsCount != currentAppsCount {
html += `
`
html += s.getAppsListHTML(mappings)
html += `
`
s.lastAppsCount = ¤tAppsCount
}
}
_, _ = fmt.Fprint(w, html) //nolint:errcheck // Error writing to ResponseWriter is handled by HTTP server
}
func (s *Server) handleFragmentSignalInfo(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "text/html")
_, _ = fmt.Fprint(w, s.getSignalInfoHTML()) //nolint:errcheck
}
func (s *Server) getSignalInfoHTML() string {
if s.getLinkedAccount() != nil {
return fmt.Sprintf(`
Unlink and remove device
- Open Signal app → Settings → Linked Devices
- Find "%s" and tap it
- Tap "Unlink Device"
`, s.cfg.DeviceName)
}
return fmt.Sprintf(`%s
`, s.getQRCodeHTML())
}
func (s *Server) getLinkedAccount() *signal.AccountInfo {
client := signal.NewClient(s.cfg.SignalCLISocketPath)
account, _ := client.GetLinkedAccount() //nolint:errcheck // Nil account is valid, indicates not linked
return account
}
func (s *Server) getQRCodeHTML() string {
if s.getLinkedAccount() != nil {
return `Account already linked
`
}
qrCode, err := s.linkDevice.GenerateQR()
if err != nil {
s.logger.Error("Failed to generate QR code", "error", err)
return `Signal daemon is starting up, please refresh in a few seconds...
`
}
return fmt.Sprintf(`Scan this QR code with your Signal app:
Settings → Linked Devices → Link New Device
`, qrCode)
}
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
`
}
var html string
html += ``
for _, m := range mappings {
isSignal := m.Channel == notification.ChannelSignal
isWebPush := m.Channel == notification.ChannelWebPush
channelBadge := "Signal"
channelTooltip := ""
if isWebPush && m.WebPush != nil {
if m.WebPush.HasEncryption() {
channelBadge = "WebPush"
} else {
channelBadge = "Webhook"
}
channelTooltip = fmt.Sprintf(`%s`, m.WebPush.Endpoint)
}
if isSignal && m.Signal != nil && m.Signal.GroupID != "" {
channelTooltip = fmt.Sprintf(`Group ID: %s`, m.Signal.GroupID)
}
itemHTML := fmt.Sprintf(`-
%s
%s%s`, m.AppName, m.Channel, channelBadge, channelTooltip)
if m.WebPush != nil && isWebPush {
if u, err := url.Parse(m.WebPush.Endpoint); err == nil {
itemHTML += fmt.Sprintf(`%s`, u.Hostname())
}
}
itemHTML += `
`
if m.WebPush != nil {
webpushLabel := "WebPush"
if !m.WebPush.HasEncryption() {
webpushLabel = "Webhook"
}
itemHTML += fmt.Sprintf(``, m.AppName, map[bool]string{true: " selected", false: ""}[isSignal], map[bool]string{true: " selected", false: ""}[isWebPush], webpushLabel)
}
itemHTML += fmt.Sprintf(`
`, m.AppName)
html += itemHTML
}
html += `
`
return html
}