CLEMA protocol

Contact Management

A contact list belongs to one person and is account state: every change to it is an event of the owner's account stream, contact.added, contact.renamed, contact.deleted or contact.requested. The device that makes a change is answered with the entry as it now stands, at the place of its event; the owner's other devices, and a device that was away, receive the event. Contact changes are operations: each carries an operation_id, and a retry answers as the first request did. A change that changes nothing, such as a rename to the name the entry has, makes no event and its answer carries no seq.

Every command and event about an entry carries the same object:

Field Type Description
user_id number The person's numeric ID
nick string The entry's name: the owner's for the person, or a snapshot of the name the person went by when nobody named them
avatar_hash string The person's avatar hash, absent without an avatar and across a block either way
requested_at string When a request was made, for a request
phone string The number in the owner's address book that found the person, on the change an address book made
removed boolean true for an entry that is gone from the list

List Contacts

Request:

{"id": "30", "type": "contact.list"}

Success:

{
  "id": "30",
  "type": "contact.list",
  "ok": true,
  "data": {
    "contacts": [
      {"user_id": 67890, "nick": "Friend", "avatar_hash": "a1b2c3d4"},
      {"user_id": 67891, "nick": "Work Buddy"}
    ],
    "requests": [
      {"user_id": 67892, "nick": "Stranger", "requested_at": "2024-01-15T14:00:00Z"}
    ]
  }
}

requests lists the contact requests waiting for an answer, newest first, and is absent when there are none. A device that follows nothing yet loads the list after asking sync.state where its account stream stands, as with block.list (see Following Streams).

Add Contact

Request:

{
  "id": "31",
  "type": "contact.add",
  "data": {
    "operation_id": "0195d1a0-add",
    "user_id": 67890,
    "nick": "New Friend"
  }
}

Adding somebody who asked takes them in.

Field Type Required Description
operation_id string Yes Idempotency key generated once for this logical operation
user_id number Yes User's numeric ID
nick string No Name for the entry; without one it takes the name the person goes by (see below)
request boolean No true answers the person's request: it takes them in only while they still wait

An answer (request: true) holds only while the request waits. Once it was answered elsewhere — by another device that kept the question on screen — or dropped by a block, the answer changes nothing and fails with contact_not_found, which does not say why. Adding the same person twice in two operations fails the second time with contact_exists.

Success:

{
  "id": "31",
  "type": "contact.add",
  "seq": "42",
  "ok": true,
  "data": {
    "contact": {"user_id": 67890, "nick": "New Friend", "avatar_hash": "a1b2c3d4"},
    "presence": {"user_id": 67890, "online": true, "active": true, "time": "2024-01-15T14:00:00Z"}
  }
}

The answer, and the contact.added event of the owner's other devices, carry the entry and the contact's presence as presence.get would report it to the owner.

An entry nobody named — added without a nick, or a request — takes the name the person goes by: their profile nickname, else their first and last name, else their numeric ID, cut to 64 characters. It is a snapshot, as a name in an address book is: the person renaming themselves later does not rename them in anybody's list, and only the owner renames the entry.

Failure:

{
  "id": "31",
  "type": "contact.add",
  "ok": false,
  "error": {
    "code": "user_not_found",
    "message": "User does not exist"
  }
}

Rename Contact

Request:

{
  "id": "32",
  "type": "contact.rename",
  "data": {
    "operation_id": "0195d1a0-rename",
    "user_id": 67890,
    "nick": "Best Friend"
  }
}

Success:

{"id": "32", "type": "contact.rename", "seq": "43", "ok": true, "data": {"contact": {"user_id": 67890, "nick": "Best Friend", "avatar_hash": "a1b2c3d4"}}}

Delete Contact

Request:

{
  "id": "33",
  "type": "contact.delete",
  "data": {
    "operation_id": "0195d1a0-delete",
    "user_id": 67890
  }
}

Success:

{"id": "33", "type": "contact.delete", "seq": "44", "ok": true, "data": {"contact": {"user_id": 67890, "removed": true}}}

The same command turns a contact request away. Either way the person is out of the list for good: no request and no address book brings them back, only adding them by hand does. contact_not_found answers somebody who is neither a contact nor a request.

With "request": true the command only answers the person's request, as contact.add does: it turns them away while they still wait, and fails with contact_not_found once the request was answered elsewhere or dropped — a contact taken in meanwhile stays one.

Everybody has a link of their own to pass to friends: whoever opens it and the person become each other's contacts. It is made the first time it is asked for and stays the same until its owner replaces it, and it goes with the account when the account is deleted.

{"id":"l1","type":"link.get"}
{"id":"l1","type":"link.get","ok":true,"data":{"token":"k7mq2xw9ra4d","link":"https://mandarin.im/add/k7mq2xw9ra4d"}}

A link passed further than meant is replaced: link.reset answers as link.get does, with a new token, and the old one admits nobody from then on.

{"id":"l4","type":"link.reset"}

The token is a random code as an invitation's is: 12 lower-case letters and digits, read in either case. link is <link_base>/add/<token> on a server with a link base and absent otherwise; it opens as an invitation's link does — a mobile client opens the links of its own service itself, anywhere else the link opens the web client — which shows whose link it is and, once the person agrees, signed in first when needed, makes them contacts. A client that takes a pasted link accepts the link as well as the bare token.

{"id":"l2","type":"link.open","data":{"token":"k7mq2xw9ra4d"}}
{"id":"l2","type":"link.open","ok":true,"data":{"user":{"user_id":12345,"nick":"Max","first_name":"Max","last_name":"Orlov","about":"","gender":""}}}
Field Type Description
user object Whose link it is: their public card, the fields of a user.search result
own boolean true when the link is the caller's own
contact boolean true when its owner is in the caller's contact list already

link.open answers before sign-in too, so that a link opened on a device that is not signed in says whose it is; own and contact are for one signed in. An unknown or replaced token, and the link of an account deleted or banned since, return not_found; a block between the caller and the owner, either way, not_authorized — signed out, a link shows its card to anybody who holds it. The picture comes from the link too:

GET /links/{token}/avatar

It is served like a user's avatar, without Authorization; an unknown link and a person without a picture return 404 not_found.

{"id":"l3","type":"link.accept","data":{"operation_id":"add-by-link-1","token":"k7mq2xw9ra4d"}}
{"id":"l3","type":"link.accept","seq":"45","ok":true,"data":{"contact":{"user_id":12345,"nick":"Max"},"presence":{"user_id":12345,"online":true,"active":true,"time":"2024-01-15T14:00:00Z"}}}

Taking up a link is an operation of the person who opened it. It puts the owner into their list and them into the owner's, each under the name the other goes by (see Add Contact), and answers a request either had made of the other. Each side's devices hear of it as of any addition, in a contact.added of their account stream; the caller is answered with the owner's entry and presence, as contact.add answers. The owner is also told by Mandarin, in their language, who came, the name quoted as the newcomer wrote it. Whoever lists the other already stays as they are, and an owner who once took the caller out of their list is not given them back by the link — they still enter the caller's list; a link both already used changes nothing, makes no event and answers without a seq. One's own link returns invalid_state, a block either way not_authorized, and a token that is nobody's link now not_found.

A link opened before signing in is kept by the client through the sign-in: a person who signs up by it is made the owner's contact once their profile is set, so that both lists name them; one who had an account is shown the link again to agree to it.

Contact Events

{"type": "contact.renamed", "seq": "43", "data": {"contact": {"user_id": 67890, "nick": "Best Friend", "avatar_hash": "a1b2c3d4"}}}
{"type": "contact.deleted", "seq": "44", "data": {"contact": {"user_id": 67890, "removed": true}}}

contact.added carries the entry with the contact's presence, as the answer to contact.add does; contact.renamed, contact.deleted and contact.requested carry the entry alone. An event is read as its recipient sees the person when it is delivered: the name the change gave the entry, and the avatar and presence the person has then. A block takes the blocked person out of the blocker's list without a contact.deleted of its own: clients apply block.added to the list.

The other party hears nothing: a contact list is one person's, and being in somebody's list is not something the protocol reports.