package mainimport ("context""errors""fmt""strings""time""go.mau.fi/whatsmeow/appstate""go.mau.fi/whatsmeow/proto/waCommon""go.mau.fi/whatsmeow/types""go.mau.fi/whatsmeow/types/events""google.golang.org/protobuf/proto")// Source acts (owner, 24 Nov 2026; chat CONTRACT.md "Blocking a WhatsApp sender, deleting a WhatsApp chat"): two// acts on his WhatsApp itself, each by his own tap in Comms, through the outbox like a send. This file alone makes// them (readonly_test.go)://// - block and unblock a person: WhatsApp's own block list, as his phone's Block does.// - delete a one-to-one chat for him: the app-state patch his phone's Delete chat sends, which removes the chat// from his own WhatsApp on every device of his. Never for everyone: the other person keeps every message.// It ERASES on his WhatsApp: the phone counts the messages and he confirms that count first.//// Nothing else changes WhatsApp's state: no archive, pin, mute, mark read, privacy setting or group act.// sourceActor is what the acts need of a linked client: the fake in source_acts_test.go stands in for whatsmeow's.type sourceActor interface {UpdateBlocklist(ctx context.Context, jid types.JID, action events.BlocklistChangeAction) (*types.Blocklist, error)SendAppState(ctx context.Context, patch appstate.PatchInfo) error}// sourceActs are the send kinds this file answers.var sourceActs = map[string]bool{"block": true, "unblock": true, "delete_chat": true}// SourceAct blocks, unblocks or deletes a chat for the owner, on the linked number the request names.func (h *Hub) SourceAct(ctx context.Context, req *Request) (map[string]any, error) {if !h.mu.TryLock() {returnnil, errors.New("busy") } held := h.held[req.Account] h.mu.Unlock()if held {returnnil, errors.New("busy") } cli, err := h.client(req.Account)if err != nil {returnnil, err }return sourceAct(ctx, cli, req, time.Now())}// sourceAct is the act itself, on any client.func sourceAct(ctx context.Context, cli sourceActor, req *Request, now time.Time) (map[string]any, error) { jid, err := types.ParseJID(req.Chat)if err != nil || jid.User == "" {returnnil, fmt.Errorf("%s needs a chat jid, got %q", req.Kind, req.Chat) }// A person's chat only: a group is left on his phone, as WhatsApp itself asks him to leave one before deleting it.if jid.Server != types.DefaultUserServer && jid.Server != types.HiddenUserServer {returnnil, fmt.Errorf("%s is for a person's chat, not %q", req.Kind, req.Chat) }switch req.Kind {case"block", "unblock": action := events.BlocklistChangeActionBlockif req.Kind == "unblock" { action = events.BlocklistChangeActionUnblock }if _, err := cli.UpdateBlocklist(ctx, jid, action); err != nil {returnnil, fmt.Errorf("WhatsApp did not take the %s: %w", req.Kind, err) }returnmap[string]any{"id": req.MessageID, "ts": now.Unix()}, nilcase"delete_chat":// WhatsApp's patch names the chat's newest message: his other devices delete up to it, and keep what came after.if strings.TrimSpace(req.Target) == "" || req.TargetTS <= 0 {returnnil, errors.New("delete_chat needs the chat's newest message and its time") } key := &waCommon.MessageKey{RemoteJID: proto.String(jid.String()), FromMe: proto.Bool(req.TargetFromMe), ID: proto.String(req.Target)} patch := appstate.BuildDeleteChat(jid, time.Unix(req.TargetTS, 0), key, false)if err := cli.SendAppState(ctx, patch); err != nil {returnnil, fmt.Errorf("WhatsApp did not delete the chat: %w", err) }returnmap[string]any{"id": req.MessageID, "ts": now.Unix()}, nil }returnnil, fmt.Errorf("unknown source act %q", req.Kind)}