Skip to content

PasteFerry (隔空贴): clipboard and file sync across macOS, Windows and Linux. Direct-first, end-to-end encrypted. Optional official access: no server to host. Independent self-hosting remains available.

fix(core): mark new links as links in live updates

baiyuzee3e5364238f7
diff --git a/client-core/internal/store/link.go b/client-core/internal/store/link.go
index 0802e67..e93efbd 100644
--- a/client-core/internal/store/link.go
+++ b/client-core/internal/store/link.go
@@ -23,3 +23,9 @@ func IsLink(text string) bool {
scheme := strings.ToLower(u.Scheme)
return (scheme == "http" || scheme == "https") && u.Host != ""
}
+
+// DetectLink 按内容算出 IsLink。PutClip 写入时也按这个算,但改的是它自己的副本:
+// 写入后马上推给界面的记录要先调用它,否则界面要等重新加载才知道这是链接。
+func (c *Clip) DetectLink() {
+ c.IsLink = c.Kind == KindText && IsLink(c.TextContent)
+}
diff --git a/client-core/internal/store/store.go b/client-core/internal/store/store.go
index 120c280..d80e275 100644
--- a/client-core/internal/store/store.go
+++ b/client-core/internal/store/store.go
@@ -313,6 +313,7 @@ func (s *Store) addColumnIfMissing(table, column, decl string) error {
// ─────────────────────────── clips ───────────────────────────
func (s *Store) PutClip(c Clip) error {
+ c.DetectLink()
tx, err := s.db.Begin()
if err != nil {
return err
@@ -345,7 +346,7 @@ ON CONFLICT(id) DO UPDATE SET
c.ID, c.Kind, c.Status, c.OriginDeviceID, c.OriginDeviceName, c.Outgoing,
c.TotalSize, c.TextPreview, c.TextContent, c.TextPlain, c.CachePath,
string(srcJSON), c.Error, c.CreatedAt.Unix(), c.ExpiresAt.Unix(),
- c.Favorite, favoritedAt, c.SourceClipID, c.Kind == KindText && IsLink(c.TextContent))
+ c.Favorite, favoritedAt, c.SourceClipID, c.IsLink)
if err != nil {
return fmt.Errorf("写入 clip: %w", err)
}
diff --git a/client-core/internal/sync/engine.go b/client-core/internal/sync/engine.go
index 0a061f8..aa0fa19 100644
--- a/client-core/internal/sync/engine.go
+++ b/client-core/internal/sync/engine.go
@@ -276,6 +276,7 @@ func (e *Engine) recordOutgoing(c clipboard.Content, cfg config.Config) (store.C
return clip, fmt.Errorf("不支持的剪贴板类型: %v", c.Kind)
}
+ clip.DetectLink() // 返回的记录随即推给界面
if err := e.store.PutClip(clip); err != nil {
return clip, err
}
@@ -407,6 +408,7 @@ func (e *Engine) handleOffer(ctx context.Context, from string, offer *pb.ClipOff
clip.TextPreview = describeItems(clip.Items)
}
+ clip.DetectLink() // 写入后随即推给界面
if err := e.store.PutClip(clip); err != nil {
e.log.Warn("保存对端记录失败", "err", err)
return
diff --git a/client-core/internal/sync/remote_placeholder_test.go b/client-core/internal/sync/remote_placeholder_test.go
index 0007d34..2cb7f18 100644
--- a/client-core/internal/sync/remote_placeholder_test.go
+++ b/client-core/internal/sync/remote_placeholder_test.go
@@ -79,3 +79,27 @@ func TestRemotePlaceholderFlood(t *testing.T) {
}
}
}
+
+// 推给界面的记录要带着「是链接」:本机复制的与对端发来的都一样,不能等界面重新加载
+func TestRecordsAreMarkedAsLinkWhenEmitted(t *testing.T) {
+ e := newBacklogEngine(t, &fakePeers{})
+ e.deviceName = func() string { return "test" }
+ var emitted []*pb.ClipRecord
+ e.onRecord = func(r *pb.ClipRecord) { emitted = append(emitted, r) }
+
+ c, err := e.recordOutgoing(clipboard.Content{Kind: clipboard.KindText, Text: "https://github.com/baiyuze/pasteferry"}, config.Default())
+ if err != nil || !c.IsLink {
+ t.Fatalf("outgoing link: IsLink=%v err=%v", c.IsLink, err)
+ }
+
+ cfg := config.Default()
+ cfg.AutoApplyToClipboard = false
+ e.loadConfig = func() config.Config { return cfg }
+ e.handleOffer(context.Background(), "office", &pb.ClipOffer{
+ ClipId: "remote-link", Kind: pb.ClipKind(store.KindText), TextContent: "https://pasteferry.cc/download/",
+ CreatedAtUnix: time.Now().Unix(),
+ })
+ if len(emitted) != 1 || !emitted[0].GetIsLink() {
+ t.Fatalf("received link emitted as %v", emitted)
+ }
+}