Files
Warp/apps/desktop/internal/shell/bridge.go
T
tajniak81andClaude Opus 5.5 829b236efe
CI / Test (ubuntu-latest) (push) Canceled after 0s
CI / Test (windows-latest) (push) Canceled after 0s
CI / Storage tests on PostgreSQL (push) Canceled after 0s
CI / Complement (federation off) (push) Canceled after 0s
CI / golangci-lint (push) Canceled after 0s
CI / Smoke test the binary (push) Canceled after 0s
CI / Admin panel (push) Canceled after 0s
CI / Web client (push) Canceled after 0s
CI / Desktop client (ubuntu-24.04) (push) Canceled after 0s
CI / Desktop client (windows-latest) (push) Canceled after 0s
Desktop: notifications, tray, auto-update, installer; web: sliding sync, voice messages, notifications
Web client (also what the desktop app runs):

- Simplified Sliding Sync (MSC4186) when the homeserver advertises
  org.matrix.simplified_msc3575. src/matrix/sliding.ts answers the SDK's
  GET /sync with a sliding sync request and rewrites the response into
  the /sync shape, as the Android app does; the SDK's own sliding sync
  mode doesn't sort thread replies into threads. 20 rooms first, then
  every room (up to 1000); to_device, e2ee, account data, receipts and
  typing extensions; M_UNKNOWN_POS starts a new connection. The SDK's
  /members?at=<sync token> drops the token, which here is ours. Settings
  can switch back to /sync.
- Notifications for new messages and incoming calls while Warp isn't in
  front, by the account's push rules, evaluated after decryption. The
  browser's Notification API once turned on in Settings, or the desktop
  app's bridge (window.warpDesktop). One per room; a click opens it.
- Voice messages: record from the composer, sent as m.audio with
  MSC3245's voice marker and MSC1767's duration and waveform (encrypted
  in encrypted rooms); a waveform player in the timeline.
- @warp/ui: a slot for extra composer buttons; bell-off, play and pause
  icons.

Desktop app:

- System notifications through Wails' notifications service, fed by
  /_desktop/bridge.js (/_desktop/notify, JSON only); clicking one shows
  the window and opens the room.
- Tray icon with Open and Quit; closing the window hides it (a one-time
  notification says so), File > Quit ends it. The page title goes to
  the window and the tray tooltip, the unread count to the taskbar or
  Dock badge.
- Auto-update from a Wails update manifest at DESKTOP_UPDATE_URL: a quiet
  check a minute after start and every six hours, a notification when a
  newer version exists, Help > Check for updates opens the update window.
  Optional signature key (DESKTOP_UPDATE_KEY). Linux package installs
  leave updates to the package manager. build/manifest writes the
  manifest.
- Per-user NSIS installer (make desktop-windows-installer), the macOS
  bundle zipped for the updater, a desktop CI job, and a Desktop release
  workflow that builds all three platforms and the manifest.

Tested on Windows against a local homeserver: sliding sync with E2EE,
threads and a server restart; notifications (toast history), close to
tray, relaunch restoring the window; voice messages recorded, sent and
played, encrypted too; the update check finding a newer version. Not
tested: installing an update, the NSIS installer, CI, macOS and Linux.
Code signing needs certificates and isn't done.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-19 14:01:02 +02:00

136 lines
4.0 KiB
Go

package shell
import (
"encoding/json"
"mime"
"net/http"
"strings"
"unicode/utf8"
)
// bridgeScriptPath serves bridgeScript, which withDesktopScripts adds to
// the client's index.html next to the link script.
const bridgeScriptPath = "/_desktop/bridge.js"
const (
// notifyPath shows a system notification: the client's notifications
// store calls window.warpDesktop.notify for new messages and calls.
notifyPath = "/_desktop/notify"
// titlePath takes the page's title, which counts unread rooms, for the
// window title and the tray icon's tooltip.
titlePath = "/_desktop/title"
)
// bridgeScript is what the client finds of the desktop app: window.warpDesktop
// (see apps/web/src/stores/notifications.ts), and the page title sent along
// whenever it changes. A click on a notification comes back as a
// "warp-desktop:open-room" event, dispatched by the app.
const bridgeScript = `(() => {
if (window.warpDesktop) return
const post = (path, body) => fetch(path, {
method: 'POST', credentials: 'omit',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(body),
}).catch(() => {})
window.warpDesktop = {
notify: (n) => post('/_desktop/notify', { id: n.id, title: n.title, body: n.body, roomId: n.roomId }),
}
let last = null
const sendTitle = () => {
if (document.title === last) return
last = document.title
post('/_desktop/title', { title: last })
}
new MutationObserver(sendTitle).observe(document.head, { subtree: true, childList: true, characterData: true })
document.addEventListener('DOMContentLoaded', sendTitle)
})()
`
// Notification is a system notification the client asks for.
type Notification struct {
// ID replaces an earlier notification with the same ID: one per room.
ID string `json:"id"`
Title string `json:"title"`
Body string `json:"body"`
RoomID string `json:"roomId"`
}
// Longest title and body shown; the system cuts them shorter anyway.
const (
maxTitle = 200
maxBody = 1000
)
// readJSON decodes a small JSON body. It insists on the JSON content type,
// which a page of another origin can't send without a CORS preflight that
// the shell never answers.
func readJSON(w http.ResponseWriter, r *http.Request, v any) bool {
if r.Method != http.MethodPost {
w.Header().Set("Allow", "POST")
http.Error(w, "method not allowed", http.StatusMethodNotAllowed)
return false
}
if ct, _, _ := mime.ParseMediaType(r.Header.Get("Content-Type")); ct != "application/json" {
http.Error(w, "expected application/json", http.StatusUnsupportedMediaType)
return false
}
if err := json.NewDecoder(http.MaxBytesReader(w, r.Body, 16<<10)).Decode(v); err != nil {
http.Error(w, "bad request", http.StatusBadRequest)
return false
}
return true
}
func (s *Shell) serveNotify(w http.ResponseWriter, r *http.Request) {
var n Notification
if !readJSON(w, r, &n) {
return
}
n.Title = clip(n.Title, maxTitle)
n.Body = clip(n.Body, maxBody)
if n.ID == "" || n.Title == "" {
http.Error(w, "id and title are required", http.StatusBadRequest)
return
}
if s.opts.Notify == nil {
w.WriteHeader(http.StatusNoContent)
return
}
if err := s.opts.Notify(n); err != nil {
s.opts.Logger.Warn("can't show a notification", "err", err)
http.Error(w, "can't show the notification", http.StatusInternalServerError)
return
}
w.WriteHeader(http.StatusNoContent)
}
func (s *Shell) serveTitle(w http.ResponseWriter, r *http.Request) {
var body struct {
Title string `json:"title"`
}
if !readJSON(w, r, &body) {
return
}
if s.opts.SetTitle != nil {
s.opts.SetTitle(clip(body.Title, maxTitle))
}
w.WriteHeader(http.StatusNoContent)
}
// clip trims s and cuts it to at most n runes, without control characters.
func clip(s string, n int) string {
s = strings.Map(func(r rune) rune {
if r == '\n' || r == '\t' {
return ' '
}
if r < 0x20 || r == 0x7f {
return -1
}
return r
}, strings.TrimSpace(s))
if utf8.RuneCountInString(s) <= n {
return s
}
return string([]rune(s)[:n-1]) + "…"
}