]> git.giacomolli.uk - onechat/blobdiff - whatsapp_import.go
Bind external events to permanent agent IDs
[onechat] / whatsapp_import.go
index 0021bc8be5904d820375a70e5635eec349ee3143..cb77563c9f60747e8f69f2f9dd29a23e62bd799e 100644 (file)
@@ -357,7 +357,7 @@ func whatsappImportIdle(root string, agent *Agent) error {
        }
 
        for _, message := range queue.Pending {
-               if message.Target == agent.config.Name {
+               if (message.Notification && message.TargetID != "" && message.TargetID == agent.config.ID) || (message.TargetID == "" && message.Target == agent.config.Name) {
                        return fmt.Errorf("import refused: agent %q has pending deliveries", agent.config.Name)
                }
        }
@@ -452,16 +452,6 @@ func whatsappImportNames(ctx context.Context, dir string) (names map[string]What
        return names, owner, nil
 }
 
-// whatsappImportName adds an exact match unless distinct IDs share its name.
-func whatsappImportName(names map[string]WhatsAppChat, chat WhatsAppChat) {
-       old, found := names[chat.Name]
-       if found && old.ID != chat.ID {
-               chat.ID = ""
-       }
-
-       names[chat.Name] = chat
-}
-
 // whatsappImportPlan filters each chat at its earliest live message across the
 // target and root logs. Only imported days in the target are skipped. Each
 // remaining chat-day becomes a work message dated at its last exported message.
@@ -570,6 +560,16 @@ func whatsappImportPlan(ctx context.Context, paths []string, old, rootLog []Date
        return plan, nil
 }
 
+// whatsappImportName adds an exact match unless distinct IDs share its name.
+func whatsappImportName(names map[string]WhatsAppChat, chat WhatsAppChat) {
+       old, found := names[chat.Name]
+       if found && old.ID != chat.ID {
+               chat.ID = ""
+       }
+
+       names[chat.Name] = chat
+}
+
 // whatsappImportHistory reads batch names and live cutoffs from both logs.
 // Imported chat-days belong only to the target log, which is first.
 func whatsappImportHistory(target, root []DatedMessage, names map[string]WhatsAppChat, cutoffs map[string]time.Time, location *time.Location) (map[string]bool, error) {