syntax = "proto3";

package ory.keto.relation_tuples.v1alpha2;

import "ory/keto/relation_tuples/v1alpha2/relation_tuples.proto";

option csharp_namespace = "Ory.Keto.RelationTuples.v1alpha2";
option go_package = "github.com/ory/keto/proto/ory/keto/relation_tuples/v1alpha2;rts";
option java_multiple_files = true;
option java_outer_classname = "WriteServiceProto";
option java_package = "sh.ory.keto.relation_tuples.v1alpha2";
option php_namespace = "Ory\\Keto\\RelationTuples\\v1alpha2";

// The write service to create and delete Access Control Lists.
//
// This service is part of the [write-APIs](../concepts/25_api-overview.mdx#write-apis).
service WriteService {
  // Writes one or more relationships in a single transaction.
  rpc TransactRelationTuples(TransactRelationTuplesRequest) returns (TransactRelationTuplesResponse);
  // Deletes relationships based on relation query
  rpc DeleteRelationTuples(DeleteRelationTuplesRequest) returns (DeleteRelationTuplesResponse);
}

// The request of a WriteService.TransactRelationTuples RPC.
message TransactRelationTuplesRequest {
  // The write delta for the relationships operated in one single transaction.
  // Either all actions succeed or no change takes effect on error.
  repeated RelationTupleDelta relation_tuple_deltas = 1;
}

// Write-delta for a TransactRelationTuplesRequest.
message RelationTupleDelta {
  // The action to perform on the relation tuple within a transaction.
  enum Action {
    // Unspecified.
    // The `TransactRelationTuples` RPC ignores this
    // RelationTupleDelta if an action was unspecified.
    ACTION_UNSPECIFIED = 0;

    // Insertion of a new RelationTuple.
    // It is ignored if already existing.
    ACTION_INSERT = 1;

    // Deletion of the RelationTuple.
    // It is ignored if it does not exist.
    ACTION_DELETE = 2;
  }
  // The action to do on the RelationTuple.
  Action action = 1;
  // The target RelationTuple.
  RelationTuple relation_tuple = 2;
}

// The response of a WriteService.TransactRelationTuples rpc.
message TransactRelationTuplesResponse {
  // This field is not implemented yet and has no effect.
  // <!--
  // The list of the new latest snapshot tokens of the affected RelationTuple,
  // with the same index as specified in the `relation_tuple_deltas` field of
  // the TransactRelationTuplesRequest request.
  //
  // If the RelationTupleDelta_Action was DELETE
  // the snaptoken is empty at the same index.
  // -->
  repeated string snaptokens = 1;
}

// The request for a WriteService.DeleteRelationTuples RPC.
// Deletes relationships matching the provided query.
message DeleteRelationTuplesRequest {
  // The query for deleting relationships
  message Query {
    // Optional. The namespace to query.
    string namespace = 1;
    // Optional. The object to query for.
    string object = 2;
    // Optional. The relation to query for.
    string relation = 3;
    // Optional. The subject to query for.
    Subject subject = 4;
  }

  // Deprecated query for deleting relationships. Use relation_query instead.
  Query query = 1 [deprecated = true];

  // The query describing which relation tuples to delete.
  RelationQuery relation_query = 2;
}

// The response of a WriteService.DeleteRelationTuples RPC.
message DeleteRelationTuplesResponse {}
