syntax = "proto2";

package push_server.messages2;
option csharp_namespace = "LeanCloud.Realtime.Internal.Protocol";

// note that this line will be removed by out build script until we
// finally upgraded to protobuffer 3
option objc_class_prefix = "AVIM";

enum CommandType {
  session = 0;
  conv = 1;
  direct = 2;
  ack = 3;
  rcp = 4;
  unread = 5;
  logs = 6;
  error = 7;
  login = 8;
  data = 9;
  room = 10;
  read = 11;
  presence = 12;
  report = 13;
  echo = 14;
  loggedin = 15;
  logout = 16;
  loggedout = 17;
  patch = 18;
  pubsub = 19;
  blacklist = 20;
  goaway = 21;
}

enum OpType {
  // session
  open = 1;
  add = 2;
  remove = 3;
  close = 4;
  opened = 5;
  closed = 6;
  query = 7;
  query_result = 8;
  conflict = 9;
  added = 10;
  removed = 11;
  refresh = 12;
  refreshed = 13;

  // conv
  start = 30;
  started = 31;
  joined = 32;
  members_joined = 33;
  // add = 34; reuse session.add
  // added = 35; reuse session.added
  // remove = 37; reuse session.remove
  // removed = 38; reuse session.removed
  left = 39;
  members_left = 40;
  //  query = 41; reuse session.query
  results = 42;
  count = 43;
  result = 44;
  update = 45;
  updated = 46;
  mute = 47;
  unmute = 48;
  status = 49;
  members = 50;
  max_read = 51;
  is_member = 52;
  member_info_update = 53;
  member_info_updated = 54;
  member_info_changed = 55;

  // room
  join = 80;
  invite = 81;
  leave = 82;
  kick = 83;
  reject = 84;
  invited = 85;
  //  joined = 32; reuse the value in conv section
  //  left = 39; reuse the value in conv section
  kicked = 86;
  //  members-joined = 33; reuse the value in conv section
  //  members-left = 40; reuse the value in conv section

  // report
  upload = 100;
  uploaded = 101;

  // pubsub
  subscribe = 120;
  subscribed = 121;
  unsubscribe = 122;
  unsubscribed = 123;
  is_subscribed = 124;

  // patch
  modify = 150;
  modified = 151;

  // blacklist, query, query_result defined with 7, 8
  block = 170;
  unblock = 171;
  blocked = 172;
  unblocked = 173;
  members_blocked = 174;
  members_unblocked = 175;
  check_block = 176;
  check_result = 177;

  add_shutup = 180;
  remove_shutup = 181;
  query_shutup = 182;
  shutup_added = 183;
  shutup_removed = 184;
  shutup_result = 185;
  shutuped = 186;
  unshutuped = 187;
  members_shutuped = 188;
  members_unshutuped = 189;
  check_shutup = 190;  // check_result define in 177
}

enum StatusType {
  on = 1;
  off = 2;
}

enum DeviceType {
  unknown = 0;
  android = 1;
  ios = 2;
}

message SemanticVersion {
  optional int32 major = 1;
  optional int32 minor = 2;
  optional int32 patch = 3;
  optional string preRelease = 4;
  optional string build = 5;
}

message AndroidVersion {
  optional string codename = 1;
  optional string apiLevel = 2;
}

message SystemInfo {
  optional DeviceType deviceType = 1;
  optional SemanticVersion osVersion = 2;
  optional AndroidVersion androidVersion = 3;
  optional bool isEmulator = 4;
}

message JsonObjectMessage {
  required string data = 1;
}

message UnreadTuple {
  required string cid = 1;
  required int32 unread = 2;
  optional string mid = 3;
  optional int64 timestamp = 4;
  optional string from = 5;
  optional string data = 6;
  optional int64 patchTimestamp = 7;
  optional bool mentioned = 8;
  optional bytes binaryMsg = 9;
  optional int32 convType = 10;
}

message LogItem {
  optional string from = 1;
  optional string data = 2;
  optional int64 timestamp = 3;
  optional string msgId = 4;
  optional int64 ackAt = 5;
  optional int64 readAt = 6;
  optional int64 patchTimestamp = 7;
  optional bool mentionAll = 8;
  repeated string mentionPids = 9;
  optional bool bin = 10;
  optional int32 convType = 11;
}

message ConvMemberInfo {
  optional string pid = 1;
  optional string role = 2;
  optional string infoId = 3;
}

message LoginCommand {
  optional SystemInfo systemInfo = 1;
}

message LoggedinCommand {
  optional bool pushDisabled = 1;
}

message DataCommand {
  repeated string ids = 1;
  repeated JsonObjectMessage msg = 2;
  optional bool offline = 3;
}

message SessionCommand {
  optional int64 t = 1;
  optional string n = 2;
  optional string s = 3;
  optional string ua = 4;
  optional bool r = 5;
  optional string tag = 6;
  optional string deviceId = 7;
  repeated string sessionPeerIds = 8;
  repeated string onlineSessionPeerIds = 9;
  optional string st = 10;
  optional int32 stTtl = 11;
  optional int32 code = 12;
  optional string reason = 13;
  optional string deviceToken = 14;
  optional bool sp = 15;
  optional string detail = 16;
  optional int64 lastUnreadNotifTime = 17;
  optional int64 lastPatchTime = 18;
  optional int64 configBitmap = 19;
  optional SystemInfo systemInfo = 20;
}

message ErrorCommand {
  required int32 code = 1;
  required string reason = 2;
  optional int32 appCode = 3;
  optional string detail = 4;
  repeated string pids = 5;
  optional string appMsg = 6;
}

message DirectCommand {
  optional string msg = 1;
  optional string uid = 2;
  optional string fromPeerId = 3;
  optional int64 timestamp = 4;
  optional bool offline = 5;
  optional bool hasMore = 6;
  repeated string toPeerIds = 7;
  optional bool r = 10;
  optional string cid = 11;
  optional string id = 12;
  optional bool transient = 13;
  optional string dt = 14;
  optional string roomId = 15;
  optional string pushData = 16;
  optional bool will = 17;
  optional int64 patchTimestamp = 18;
  optional bytes binaryMsg = 19;
  repeated string mentionPids = 20;
  optional bool mentionAll = 21;
  optional int32 convType = 22;
}

message AckCommand {
  optional int32 code = 1;
  optional string reason = 2;
  optional string mid = 3;
  optional string cid = 4;
  optional int64 t = 5;
  optional string uid = 6;
  optional int64 fromts = 7;
  optional int64 tots = 8;
  optional string type = 9;
  repeated string ids = 10;
  optional int32 appCode = 11;
  optional string appMsg = 12;
}

message UnreadCommand {
  repeated UnreadTuple convs = 1;
  optional int64 notifTime = 2;
}

message ConvCommand {
  repeated string m = 1;
  optional bool transient = 2;
  optional bool unique = 3;
  optional string cid = 4;
  optional string cdate = 5;
  optional string initBy = 6;
  optional string sort = 7;
  optional int32 limit = 8;
  optional int32 skip = 9;
  optional int32 flag = 10;
  optional int32 count = 11;
  optional string udate = 12;
  optional int64 t = 13;
  optional string n = 14;
  optional string s = 15;

  optional bool statusSub = 16;
  optional bool statusPub = 17;
  optional int32 statusTTL = 18;
  optional string uniqueId = 19;

  optional string targetClientId = 20;
  optional int64 maxReadTimestamp = 21;
  optional int64 maxAckTimestamp = 22;
  optional bool queryAllMembers = 23;
  repeated MaxReadTuple maxReadTuples = 24;
  repeated string cids = 25;

  optional ConvMemberInfo info = 26;

  optional bool tempConv = 27;
  optional int32 tempConvTTL = 28;
  repeated string tempConvIds = 29;

  repeated string allowedPids = 30;
  repeated ErrorCommand failedPids = 31;

  // used in shutup query
  optional string next = 40;

  optional JsonObjectMessage results = 100;
  optional JsonObjectMessage where = 101;
  optional JsonObjectMessage attr = 103;
  optional JsonObjectMessage attrModified = 104;
}

message RoomCommand {
  optional string roomId = 1;
  optional string s = 2;
  optional int64 t = 3;
  optional string n = 4;
  optional bool transient = 5;
  repeated string roomPeerIds = 6;
  optional string byPeerId = 7;
}

message LogsCommand {
  optional string cid = 1;
  optional int32 l = 2;
  optional int32 limit = 3;
  optional int64 t = 4;
  optional int64 tt = 5;
  optional string tmid = 6;
  optional string mid = 7;
  optional string checksum = 8;
  optional bool stored = 9;
  enum QueryDirection {
     OLD = 1;
     NEW = 2;
  }
  optional QueryDirection direction = 10 [default = OLD];
  optional bool tIncluded = 11;
  optional bool ttIncluded = 12;
  optional int32 lctype = 13;

  repeated LogItem logs = 105;
}

message RcpCommand {
  optional string id = 1;
  optional string cid = 2;
  optional int64 t = 3;
  optional bool read = 4;
  optional string from = 5;
}

message ReadTuple {
  required string cid = 1;
  optional int64 timestamp = 2;
  optional string mid = 3;
}

message MaxReadTuple {
  optional string pid = 1;
  optional int64 maxAckTimestamp = 2;
  optional int64 maxReadTimestamp = 3;
}

message ReadCommand {
  optional string cid = 1;
  repeated string cids = 2;
  repeated ReadTuple convs = 3;
}

message PresenceCommand {
  optional StatusType status = 1;
  repeated string sessionPeerIds = 2;
  optional string cid = 3;
}

message ReportCommand {
  optional bool initiative = 1;
  optional string type = 2;
  optional string data = 3;
}

message PatchItem {
  optional string cid = 1;
  optional string mid = 2;
  optional int64 timestamp = 3;
  optional bool recall = 4;
  optional string data = 5;
  optional int64 patchTimestamp = 6;
  optional string from = 7;
  optional bytes binaryMsg = 8;
  optional bool mentionAll = 9;
  repeated string mentionPids = 10;
  optional int64 patchCode = 11;
  optional string patchReason = 12;
}

message PatchCommand {
  repeated PatchItem patches = 1;
  optional int64 lastPatchTime = 2;
}

message PubsubCommand {
  optional string cid = 1;
  repeated string cids = 2;
  optional string topic = 3;
  optional string subtopic = 4;
  repeated string topics = 5;
  repeated string subtopics = 6;
  optional JsonObjectMessage results = 7;
}

message BlacklistCommand {
  optional string srcCid      = 1;
  repeated string toPids      = 2;
  optional string srcPid      = 3;
  repeated string toCids      = 4;
  optional int32 limit        = 5;
  optional string next        = 6;

  repeated string blockedPids = 8;
  repeated string blockedCids = 9;

  repeated string allowedPids = 10;
  repeated ErrorCommand failedPids = 11;

  optional int64 t = 12;
  optional string n = 13;
  optional string s = 14;
}

message GenericCommand {
  optional CommandType cmd = 1;
  optional OpType op = 2;

  optional string appId = 3;
  optional string peerId = 4;
  optional int32 i = 5;
  optional string installationId = 6;
  optional int32 priority = 7;
  optional int32 service = 8;
  optional int64 serverTs = 9;
  optional int64 clientTs = 10;
  optional int32 notificationType = 11;

  optional LoginCommand loginMessage = 100;
  optional DataCommand dataMessage = 101;
  optional SessionCommand sessionMessage = 102;
  optional ErrorCommand errorMessage = 103;
  optional DirectCommand directMessage = 104;
  optional AckCommand ackMessage = 105;
  optional UnreadCommand unreadMessage = 106;
  optional ReadCommand readMessage = 107;
  optional RcpCommand rcpMessage = 108;
  optional LogsCommand logsMessage = 109;
  optional ConvCommand convMessage = 110;
  optional RoomCommand roomMessage = 111;
  optional PresenceCommand presenceMessage = 112;
  optional ReportCommand reportMessage = 113;
  optional PatchCommand patchMessage = 114;
  optional PubsubCommand pubsubMessage = 115;
  optional BlacklistCommand blacklistMessage = 116;
  optional LoggedinCommand loggedinMessage = 117;
}
