Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 21 additions & 8 deletions internal/daemon/dedup.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,15 +44,25 @@ func (d *Deduper) AllowAt(env NotifyEnvelope, now time.Time) (bool, *NotifyEnvel
return true, nil
}
msg := env.GenericMessage
if msg == nil {
return true, nil
}

key := DedupKey{
Source: env.Source,
Type: dedupType(env),
Title: msg.Title,
BodyHash: md5.Sum([]byte(msg.Body)),
var key DedupKey
switch {
case env.ImageTransfer != nil:
key = DedupKey{
Source: env.Source,
Type: string(KindImageTransfer),
Title: env.ImageTransfer.SessionID,
BodyHash: md5.Sum([]byte(env.ImageTransfer.Fingerprint)),
}
case msg != nil:
key = DedupKey{
Source: env.Source,
Type: dedupType(env),
Title: msg.Title,
BodyHash: md5.Sum([]byte(msg.Body)),
}
default:
return true, nil
}

d.mu.Lock()
Expand All @@ -79,6 +89,9 @@ func (d *Deduper) AllowAt(env NotifyEnvelope, now time.Time) (bool, *NotifyEnvel
entry.SeenAt = now
d.items[key] = entry

if msg == nil {
return false, nil
}
merged := env
clone := *msg
clone.DedupCount = entry.Count
Expand Down
23 changes: 15 additions & 8 deletions internal/daemon/dedup_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,29 +153,36 @@ func TestDeduperDifferentMessagesNotMerged(t *testing.T) {
}
}

func TestDeduperNilGenericMessagePassesThrough(t *testing.T) {
func TestDeduperImageTransferDedup(t *testing.T) {
d := NewDeduper(15 * time.Second)
now := time.Unix(1000, 0)

env := NotifyEnvelope{
Kind: KindImageTransfer,
Source: "clipboard",
ImageTransfer: &ImageTransferPayload{
SessionID: "abc",
Seq: 1,
Format: "png",
SessionID: "abc",
Seq: 1,
Fingerprint: "deadbeef",
Format: "png",
},
}

allowed, merged := d.AllowAt(env, now)
if !allowed || merged != nil {
t.Fatal("envelope without GenericMessage should always pass")
t.Fatal("first image transfer should pass")
}

// Second identical should also pass (no GenericMessage to dedup on)
allowed, merged = d.AllowAt(env, now.Add(1*time.Second))
// Second identical fingerprint within window should be suppressed
allowed, _ = d.AllowAt(env, now.Add(1*time.Second))
if allowed {
t.Fatal("repeated image transfer with same fingerprint should be suppressed within dedup window")
}

// After the window expires, same fingerprint should pass again
allowed, merged = d.AllowAt(env, now.Add(17*time.Second))
if !allowed || merged != nil {
t.Fatal("envelope without GenericMessage should always pass, even repeated")
t.Fatal("image transfer should pass after dedup window expires")
}
}

Expand Down
16 changes: 8 additions & 8 deletions internal/daemon/notify_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -431,20 +431,20 @@ func TestDuplicateDetectionViaNotification(t *testing.T) {
srv.mux.ServeHTTP(w, req)
}

// Only the first fetch should produce a notification; the second
// has the same fingerprint within the dedup window and is suppressed.
deadline := time.Now().Add(2 * time.Second)
for notifier.count.Load() < 2 && time.Now().Before(deadline) {
for notifier.count.Load() < 1 && time.Now().Before(deadline) {
time.Sleep(10 * time.Millisecond)
}
time.Sleep(100 * time.Millisecond)

if notifier.count.Load() != 2 {
t.Fatalf("expected 2 notifications, got %d", notifier.count.Load())
if notifier.count.Load() != 1 {
t.Fatalf("expected 1 notification (duplicate suppressed), got %d", notifier.count.Load())
}

evt := notifier.last.Load().(NotifyEvent)
if evt.Seq != 2 {
t.Errorf("expected seq 2, got %d", evt.Seq)
}
if evt.DuplicateOf != 1 {
t.Errorf("expected duplicate of seq 1, got %d", evt.DuplicateOf)
if evt.Seq != 1 {
t.Errorf("expected seq 1, got %d", evt.Seq)
}
}