Cotton code
87 lines · 3.6 KB · GoRaw
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() {		return nil, errors.New("busy")	}	held := h.held[req.Account]	h.mu.Unlock()	if held {		return nil, errors.New("busy")	}	cli, err := h.client(req.Account)	if err != nil {		return nil, 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 == "" {		return nil, 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 {		return nil, fmt.Errorf("%s is for a person's chat, not %q", req.Kind, req.Chat)	}	switch req.Kind {	case "block", "unblock":		action := events.BlocklistChangeActionBlock		if req.Kind == "unblock" {			action = events.BlocklistChangeActionUnblock		}		if _, err := cli.UpdateBlocklist(ctx, jid, action); err != nil {			return nil, fmt.Errorf("WhatsApp did not take the %s: %w", req.Kind, err)		}		return map[string]any{"id": req.MessageID, "ts": now.Unix()}, nil	case "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 {			return nil, 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 {			return nil, fmt.Errorf("WhatsApp did not delete the chat: %w", err)		}		return map[string]any{"id": req.MessageID, "ts": now.Unix()}, nil	}	return nil, fmt.Errorf("unknown source act %q", req.Kind)}