@startuml
title Stream — Direct search (type "known"/"partner") : Peer A → Peer B

participant "App UI A" as UIA
participant "App Pair A" as AppA
participant "NATS A" as NATSA
participant "Node A" as NodeA
participant "PubSubService A" as PubSubA
participant "StreamService A" as StreamA
participant "DB Pair A (oc-lib)" as DBA
participant "Node B" as NodeB
participant "StreamService B" as StreamB
participant "DB Pair B (oc-lib)" as DBB

UIA -> AppA: websocket subscription, sending {type:"all", search:"search"} in query

AppA -> NATSA: Publish(PROPALGATION_EVENT, {PB_SEARCH, type:"partner", search:"gpu"})
NATSA -> NodeA: ListenNATS → PB_SEARCH (type "partner")
NodeA -> PubSubA: SearchPublishEvent(ctx, dt, "partner", user, "gpu")

PubSubA -> StreamA: SearchPartnersPublishEvent(dt, user, "gpu")
StreamA -> DBA: Search(PEER, PARTNER) + PeerIDS config
DBA --> StreamA: [Peer B, ...]

loop Pour chaque pair partenaire (Pair B)
    StreamA -> StreamA: write(PeerID_B, addr_B, dt, user, payload, ProtocolSearchResource)
    StreamA -> NodeB: TempStream /opencloud/resource/search/1.0
    StreamA -> NodeB: stream.Encode(Event{Type:search, From:DID_A, DataType, Payload:{search:"gpu"}})

    NodeB -> StreamB: HandleResponse(stream) → readLoop
    StreamB -> StreamB: handleEvent(ProtocolSearchResource, evt)
    StreamB -> StreamB: handleEventFromPartner(evt, ProtocolSearchResource)

    alt evt.DataType == -1 (toutes ressources)
        StreamB -> DBA: Search(PEER, evt.From=DID_A)
        note over StreamB: Local Resolving (DB) or GetPeerRecord (Indexer Way)
        StreamB -> StreamB: SendResponse(Peer A, evt)
        StreamB -> DBB: Search(ALL_RESOURCES, filter{creator=B + public OR partner A + search:"gpu"})
        DBB --> StreamB: [Resource1, Resource2, ...]
    else evt.DataType specified
        StreamB -> DBB: Search(DataType, filter{creator=B + access + search:"gpu"})
        DBB --> StreamB: [Resource1, ...]
    end

    loop Pour chaque ressource
        StreamB -> StreamA: write(PeerID_A, addr_A, dt, resource JSON, ProtocolSearchResource)
        StreamA -> StreamA: readLoop → handleEvent(ProtocolSearchResource, evt)
        StreamA -> StreamA: retrieveResponse(evt)
        StreamA -> NATSA: SetNATSPub(SEARCH_EVENT, {DataType, resource JSON})
        NATSA -> AppA: Peer B results
        AppA -> UIA: emit on websocket
    end
end
@enduml
