revert linux wait and update payload on win

This commit is contained in:
popaprozac 2025-03-24 21:26:02 -07:00
commit 7d0f7f4652
2 changed files with 33 additions and 107 deletions

View file

@ -9,7 +9,6 @@ import (
"os"
"path/filepath"
"sync"
"time"
"github.com/godbus/dbus/v5"
"github.com/wailsapp/wails/v3/pkg/application"
@ -49,29 +48,6 @@ func New() *Service {
notifications: make(map[uint32]*notificationData),
}
ctx, cancel := context.WithTimeout(context.Background(), 2*time.Second)
defer cancel()
conn, err := dbus.ConnectSessionBus()
if err != nil {
fmt.Printf("Warning: Failed to connect to D-Bus session bus: %v\n", err)
fmt.Printf("Notifications will be unavailable\n")
} else {
impl.conn = conn
obj := conn.Object(dbusNotificationInterface, dbusNotificationPath)
call := obj.CallWithContext(ctx, dbusNotificationInterface+".GetCapabilities", 0)
var capabilities []string
err := call.Store(&capabilities)
if err != nil {
fmt.Printf("Warning: D-Bus notification service not ready: %v\n", err)
} else {
fmt.Printf("D-Bus notification service is ready with capabilities: %v\n", capabilities)
}
}
NotificationService = &Service{
impl: impl,
}
@ -80,29 +56,15 @@ func New() *Service {
return NotificationService
}
// Helper method to check if D-Bus connection is available
func (ln *linuxNotifier) checkConnection() error {
if ln.conn == nil {
return fmt.Errorf("D-Bus connection is not initialized, notifications are unavailable")
}
return nil
}
// Startup is called when the service is loaded.
func (ln *linuxNotifier) Startup(ctx context.Context, options application.ServiceOptions) error {
ln.appName = application.Get().Config().Name
if ln.conn == nil {
conn, err := dbus.ConnectSessionBus()
if err != nil {
fmt.Printf("Warning: Failed to connect to D-Bus session bus: %v\n", err)
fmt.Printf("Notifications will be unavailable\n")
return nil
}
ln.conn = conn
conn, err := dbus.ConnectSessionBus()
if err != nil {
return fmt.Errorf("failed to connect to session bus: %w", err)
}
ln.conn = conn
if err := ln.loadCategories(); err != nil {
fmt.Printf("Failed to load notification categories: %v\n", err)
@ -112,7 +74,7 @@ func (ln *linuxNotifier) Startup(ctx context.Context, options application.Servic
signalCtx, ln.cancel = context.WithCancel(context.Background())
if err := ln.setupSignalHandling(signalCtx); err != nil {
fmt.Printf("Warning: Failed to set up notification signal handling: %v\n", err)
return fmt.Errorf("failed to set up notification signal handling: %w", err)
}
return nil
@ -148,10 +110,6 @@ func (ln *linuxNotifier) CheckNotificationAuthorization() (bool, error) {
// SendNotification sends a basic notification with a unique identifier, title, subtitle, and body.
func (ln *linuxNotifier) SendNotification(options NotificationOptions) error {
if err := ln.checkConnection(); err != nil {
return err
}
hints := map[string]dbus.Variant{}
body := options.Body
@ -218,10 +176,6 @@ func (ln *linuxNotifier) SendNotification(options NotificationOptions) error {
// SendNotificationWithActions sends a notification with additional actions.
func (ln *linuxNotifier) SendNotificationWithActions(options NotificationOptions) error {
if err := ln.checkConnection(); err != nil {
return err
}
ln.categoriesLock.RLock()
category, exists := ln.categories[options.CategoryID]
ln.categoriesLock.RUnlock()
@ -330,10 +284,6 @@ func (ln *linuxNotifier) RemoveNotificationCategory(categoryId string) error {
// RemoveAllPendingNotifications attempts to remove all active notifications.
func (ln *linuxNotifier) RemoveAllPendingNotifications() error {
if err := ln.checkConnection(); err != nil {
return err
}
ln.notificationsLock.Lock()
dbusIDs := make([]uint32, 0, len(ln.notifications))
for id := range ln.notifications {
@ -350,10 +300,6 @@ func (ln *linuxNotifier) RemoveAllPendingNotifications() error {
// RemovePendingNotification removes a pending notification.
func (ln *linuxNotifier) RemovePendingNotification(identifier string) error {
if err := ln.checkConnection(); err != nil {
return err
}
var dbusID uint32
found := false
@ -391,10 +337,6 @@ func (ln *linuxNotifier) RemoveNotification(identifier string) error {
// Helper method to close a notification.
func (ln *linuxNotifier) closeNotification(id uint32) error {
if err := ln.checkConnection(); err != nil {
return err
}
obj := ln.conn.Object(dbusNotificationInterface, dbusNotificationPath)
call := obj.Call(dbusNotificationInterface+".CloseNotification", 0, id)
@ -475,10 +417,6 @@ func (ln *linuxNotifier) loadCategories() error {
// Setup signal handling for notification actions.
func (ln *linuxNotifier) setupSignalHandling(ctx context.Context) error {
if err := ln.checkConnection(); err != nil {
return err
}
if err := ln.conn.AddMatchSignal(
dbus.WithMatchInterface(dbusNotificationInterface),
dbus.WithMatchMember("ActionInvoked"),

View file

@ -36,8 +36,8 @@ const (
// NotificationPayload combines the action ID and user data into a single structure
type NotificationPayload struct {
Action string `json:"action"`
Data map[string]interface{} `json:"data,omitempty"`
Action string `json:"action"`
Options NotificationOptions `json:"payload,omitempty"`
}
// Creates a new Notifications Service.
@ -79,7 +79,8 @@ func (wn *windowsNotifier) Startup(ctx context.Context, options application.Serv
toast.SetActivationCallback(func(args string, data []toast.UserData) {
result := NotificationResult{}
actionIdentifier, userInfo, err := parseNotificationResponse(args)
actionIdentifier, options, err := parseNotificationResponse(args)
if err != nil {
result.Error = err
@ -90,21 +91,15 @@ func (wn *windowsNotifier) Startup(ctx context.Context, options application.Serv
return
}
// Subtitle is retained but was not shown with the notification
response := NotificationResponse{
ID: options.ID,
ActionIdentifier: actionIdentifier,
}
if userInfo != "" {
var userInfoMap map[string]interface{}
if err := json.Unmarshal([]byte(userInfo), &userInfoMap); err != nil {
result.Error = fmt.Errorf("failed to unmarshal notification response: %w", err)
if ns := getNotificationService(); ns != nil {
ns.handleNotificationResult(result)
}
return
}
response.UserInfo = userInfoMap
Title: options.Title,
Subtitle: options.Subtitle,
Body: options.Body,
CategoryID: options.CategoryID,
UserInfo: options.Data,
}
if userText, found := wn.getUserText(data); found {
@ -154,9 +149,9 @@ func (wn *windowsNotifier) SendNotification(options NotificationOptions) error {
}
if options.Data != nil {
encodedPayload, err := wn.encodePayload(DefaultActionIdentifier, options.Data)
encodedPayload, err := wn.encodePayload(DefaultActionIdentifier, options)
if err != nil {
return fmt.Errorf("failed to encode notification data: %w", err)
return fmt.Errorf("failed to encode notification payload: %w", err)
}
n.ActivationArguments = encodedPayload
}
@ -208,16 +203,16 @@ func (wn *windowsNotifier) SendNotificationWithActions(options NotificationOptio
}
if options.Data != nil {
encodedPayload, err := wn.encodePayload(n.ActivationArguments, options.Data)
encodedPayload, err := wn.encodePayload(n.ActivationArguments, options)
if err != nil {
return fmt.Errorf("failed to encode notification data: %w", err)
return fmt.Errorf("failed to encode notification payload: %w", err)
}
n.ActivationArguments = encodedPayload
for index := range n.Actions {
encodedPayload, err := wn.encodePayload(n.Actions[index].Arguments, options.Data)
encodedPayload, err := wn.encodePayload(n.Actions[index].Arguments, options)
if err != nil {
return fmt.Errorf("failed to encode notification data: %w", err)
return fmt.Errorf("failed to encode notification payload: %w", err)
}
n.Actions[index].Arguments = encodedPayload
}
@ -284,10 +279,10 @@ func (wn *windowsNotifier) RemoveNotification(identifier string) error {
}
// encodePayload combines an action ID and user data into a single encoded string
func (wn *windowsNotifier) encodePayload(actionID string, data map[string]interface{}) (string, error) {
func (wn *windowsNotifier) encodePayload(actionID string, options NotificationOptions) (string, error) {
payload := NotificationPayload{
Action: actionID,
Data: data,
Action: actionID,
Options: options,
}
jsonData, err := json.Marshal(payload)
@ -300,37 +295,30 @@ func (wn *windowsNotifier) encodePayload(actionID string, data map[string]interf
}
// decodePayload extracts the action ID and user data from an encoded payload
func decodePayload(encodedString string) (string, map[string]interface{}, error) {
func decodePayload(encodedString string) (string, NotificationOptions, error) {
jsonData, err := base64.StdEncoding.DecodeString(encodedString)
if err != nil {
return encodedString, nil, fmt.Errorf("failed to decode base64 payload: %w", err)
return encodedString, NotificationOptions{}, fmt.Errorf("failed to decode base64 payload: %w", err)
}
var payload NotificationPayload
if err := json.Unmarshal(jsonData, &payload); err != nil {
return encodedString, nil, fmt.Errorf("failed to unmarshal notification payload: %w", err)
return encodedString, NotificationOptions{}, fmt.Errorf("failed to unmarshal notification payload: %w", err)
}
return payload.Action, payload.Data, nil
return payload.Action, payload.Options, nil
}
// parseNotificationResponse updated to use structured payload decoding
func parseNotificationResponse(response string) (action string, data string, err error) {
actionID, userData, err := decodePayload(response)
func parseNotificationResponse(response string) (action string, options NotificationOptions, err error) {
actionID, options, err := decodePayload(response)
if err != nil {
fmt.Printf("Warning: Failed to decode notification response: %v\n", err)
return response, "", err
return response, NotificationOptions{}, err
}
if userData != nil {
userDataJSON, err := json.Marshal(userData)
if err == nil {
return actionID, string(userDataJSON), nil
}
}
return actionID, "", nil
return actionID, options, nil
}
func (wn *windowsNotifier) saveIconToDir() error {