syntax = "proto3";

import "teletype-crdt.proto";

message PortalSubscriptionResponse {
  map<string, uint32> site_ids_by_peer_id = 1;
  reserved 2; // active_editor_proxy field is no longer supported
  reserved 3; // active_buffer_proxy field is no longer supported
  repeated Tether tethers = 4;
  repeated BufferProxy active_buffer_proxies = 5;
  repeated EditorProxy active_editor_proxies = 6;
  map<uint32, uint32> active_editor_proxy_ids_by_site_id = 7;
  repeated EditorProxyMetadata editor_proxies_metadata = 8;
}

message PortalUpdate {
  oneof variant {
    EditorProxySwitch editor_proxy_switch = 1;
    SiteAssignment site_assignment = 2;
    // EditorProxyRemoval editor_proxy_removal = 3; // No longer supported
    Tether tether_update = 4;
    EditorProxyCreation editor_proxy_creation = 5;
  }

  message EditorProxySwitch {
    uint32 editor_proxy_id = 1;
    reserved 2; // buffer_proxy_id field is no longer supported
  }

  message SiteAssignment {
    string peer_id = 1;
    uint32 site_id = 2;
  }

  message EditorProxyRemoval {
    uint32 editor_proxy_id = 1;
  }

  message EditorProxyCreation {
    EditorProxyMetadata editor_proxy_metadata = 1;
  }
}

message Tether {
  uint32 follower_site_id = 1;
  uint32 leader_site_id = 2;
  uint32 state = 3;
}

message EditorProxy {
  uint32 id = 1;
  uint32 buffer_proxy_id = 2;
  map<uint32, uint32> selection_layer_ids_by_site_id = 3;
  reserved 4; // tethers_by_follower_id field is no longer supported
}

message EditorProxyMetadata {
  uint32 id = 1;
  uint32 buffer_proxy_id = 2;
  string buffer_proxy_uri = 3;
}

message EditorProxyUpdate {
  oneof variant {
    SelectionsUpdate selections_update = 1;
    // TetherUpdate tether_update = 2; // No longer supported
  }

  message SelectionsUpdate {
    map<uint32, uint32> selection_layer_ids_by_site_id = 1;
  }
}

message BufferProxy {
  uint32 id = 1;
  string uri = 2;
  repeated Operation operations = 3;
}

message BufferProxyUpdate {
  reserved 1; // operations field is no longer supported; use OperationsUpdate instead

  oneof variant {
    OperationsUpdate operations_update = 2;
    URIUpdate uri_update = 3;
  }

  message OperationsUpdate {
    repeated Operation operations = 1;
  }

  message URIUpdate {
    string uri = 1;
  }
}

message RouterMessage {
  oneof variant {
    Notification notification = 2;
    Request request = 3;
    Response response = 4;
  }

  message Notification {
    string channel_id = 1;
    bytes body = 2;
  }

  message Request {
    string channel_id = 1;
    uint32 request_id = 2;
    bytes body = 3;
  }

  message Response {
    uint32 request_id = 1;
    bytes body = 2;
    bool ok = 3;
  }
}

message NetworkMessage {
  string network_id = 1;

  oneof variant {
    StarJoinRequest star_join_request = 2;
    StarJoinResponse star_join_response = 3;
    StarJoinNotification star_join_notification = 4;
    StarLeaveNotification star_leave_notification = 5;
    StarUnicast star_unicast = 6;
    StarBroadcast star_broadcast = 7;
  }

  message StarJoinRequest {
    string sender_id = 1;
  }

  message StarJoinResponse {
    map<string, PeerIdentity> member_identities_by_id = 1;
  }

  message StarJoinNotification {
    string member_id = 1;
    PeerIdentity member_identity = 2;
  }

  message StarLeaveNotification {
    string member_id = 1;
    bool connection_lost = 2;
  }

  message StarUnicast {
    string sender_id = 1;
    string recipient_id = 2;
    bytes body = 3;
  }

  message StarBroadcast {
    string sender_id = 1;
    bytes body = 2;
  }
}

message PeerIdentity {
  string login = 1;
}
