Cotton code
751 lines · 19.8 KB · JSONRaw
{  "format_version": 1,  "id": "message",  "name": "Message",  "plural": "Messages",  "picture": null,  "builtin": true,  "delete_when": {    "column": "can_delete",    "present": true  },  "deletion": {    "bin": true  },  "links": [    {      "label": "Deleted",      "table": "bin",      "where": {        "table": "message"      }    }  ],  "engine": "messages",  "columns": [    {      "key": "thread_id",      "name": "Chat",      "type": "link",      "link_to": "thread",      "readonly": true,      "contributes": false    },    {      "key": "body",      "type": "longtext",      "readonly": true    },    {  "key": "direction",  "type": "choice",  "choices": [    "in",    "out"  ],  "readonly": true,  "labels": {    "in": "Incoming",    "out": "Outgoing"  }},    {      "key": "channel",      "type": "choice",      "choices": [        "cotton",        "whatsapp",        "imessage",        "sms",        "instagram"      ],      "labels": {        "cotton": "Cotton",        "whatsapp": "WhatsApp",        "imessage": "iMessage",        "sms": "Text message",        "instagram": "Instagram"      },      "readonly": true    },    {  "key": "origin",  "type": "choice",  "choices": [    "live",    "imported"  ],  "readonly": true,  "labels": {    "live": "Live",    "imported": "Imported"  }},    {  "key": "sent_at",  "name": "Sent",  "type": "date",  "readonly": true,  "fact": true,  "reading": "{value}",  "format": "calendar-datetime"},    {      "key": "reply_to_id",      "name": "Reply to",      "type": "link",      "link_to": "message",      "readonly": true,      "contributes": false    },    {      "key": "edited_at",      "name": "Edited",      "type": "date",      "readonly": true    },    {      "key": "deleted_at",      "name": "Deleted",      "type": "date",      "readonly": true    },    {      "key": "starred_at",      "name": "Kept",      "type": "date",      "readonly": true    },    {      "key": "display_body",      "type": "longtext",      "readonly": true,      "markdown": true    },    {      "key": "preview",      "name": "Line",      "type": "text",      "readonly": true    },    {      "key": "sender_name",      "type": "text",      "readonly": true,      "fact": true,      "reading": "{value}"    },    {      "key": "reply_preview",      "type": "longtext",      "readonly": true    },    {      "key": "reactions",      "type": "longtext",      "readonly": true    },    {      "key": "media",      "type": "file",      "readonly": true,      "presentation": "attachments"    },    {      "key": "provenance",      "type": "text",      "readonly": true    },    {      "key": "code_for",      "name": "Sign-in code for",      "type": "text",      "readonly": true    },    {      "key": "code",      "name": "Sign-in code",      "type": "text",      "readonly": true,      "secret": true    },    {      "key": "is_system",      "type": "bool",      "readonly": true    },    {      "key": "edited_label",      "type": "text",      "readonly": true    },    {      "key": "starred_label",      "type": "text",      "readonly": true    },    {      "key": "kept",      "type": "bool",      "readonly": true    },    {      "key": "paper_sender",      "type": "text",      "readonly": true    },    {      "key": "sender_label",      "type": "text",      "readonly": true    },    {      "key": "sender_photo",      "type": "photo",      "readonly": true    },    {  "key": "send_state",  "name": "Sending",  "type": "text",  "readonly": true,  "labels": {    "queued": "Queued",    "sending": "Sending",    "sent": "Sent",    "delivered": "Delivered",    "read": "Read",    "failed": "Failed"  }},    {      "key": "send_error",      "name": "Why it was not sent",      "type": "text",      "readonly": true    },    {      "key": "external_id",      "name": "Site id",      "type": "text",      "readonly": true    },    {      "key": "can_delete",      "type": "bool",      "readonly": true    },    {      "key": "filed_copy",      "type": "bool",      "readonly": true    }  ],  "title": "preview",  "date": "sent_at",  "project": null,  "order": null,  "slip": {    "title": "preview",    "subtitle": "sender_name",    "trailing": "sent_at",    "photo": null,    "toggle": null  },  "tile": {    "face": "slip-list",    "current": {      "column": "sent_at",      "days": 2    }  },  "page": [    {      "heading": null,      "columns": [        "display_body",        "sender_name",        "reply_preview",        "media",        "reactions"      ]    },    {      "heading": "Details",      "columns": [        "thread_id",        "provenance",        "direction",        "channel",        "origin",        "sent_at",        "edited_at",        "deleted_at",        "starred_at"      ]    }  ],  "instructions": "A message is one line of a chat, incoming or outgoing, as it was written; a message is authored, never made up. Imported messages keep where they came from. The only changes the owner makes are keeping one and, from their own app, replying into a WhatsApp or Instagram chat that can send through its explicitly bound account; never write a message into a chat. send_state follows a text sent from Cotton (queued, sending, sent, delivered, read, failed; delivered and read require recipient evidence, and send_error explains a failure or an unconfirmed send) and is empty on every message that arrived. display_body is what the conversation says: a deleted message says only that, an importer's marker is said in words. media is the entry list; a deleted message answers none. Reactions are observed site facts, never a request to send a reaction.",  "sort": {    "column": "sent_at",    "descending": true  },  "commands": [    {      "id": "keep",      "label": "Star",      "object": "star",      "kind": "update",      "verb": "star_message",      "data": {        "starred": true      },      "bindings": {        "messageId": "$id"      },      "when": {        "kept": false,        "filed_copy": false      },      "receipt": "Starred",      "undo": "unkeep",      "does": "completes"    },    {      "id": "unkeep",      "label": "Unstar",      "object": "star",      "kind": "update",      "verb": "star_message",      "data": {        "starred": false      },      "bindings": {        "messageId": "$id"      },      "when": {        "kept": true,        "filed_copy": false      },      "receipt": "No longer starred",      "undo": "keep"    },    {      "id": "hide",      "label": "Hide",      "object": "hidden",      "kind": "update",      "verb": "hide_chat_messages",      "data": {},      "destructive": true,      "bindings": {        "messageIds": "$id_list"      },      "when": {        "filed_copy": false      },      "receipt": "Deleted from your copy",      "does": "destroys"    },    {      "id": "thread",      "label": "Open chat",      "kind": "open",      "opens": "thread_id"    }  ],  "tables": [    {      "name": "cotton_msg_messages",      "columns": [        {          "name": "id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": true,          "declaration": "\n    id TEXT PRIMARY KEY",          "position": 0        },        {          "name": "thread_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    thread_id TEXT NOT NULL",          "position": 1        },        {          "name": "workspace_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    workspace_id TEXT NOT NULL",          "position": 2        },        {          "name": "direction",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    direction TEXT NOT NULL",          "position": 3        },        {          "name": "channel",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    channel TEXT NOT NULL",          "position": 4        },        {          "name": "origin",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    origin TEXT NOT NULL",          "position": 5        },        {          "name": "body",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    body TEXT",          "position": 6        },        {          "name": "media",          "type": "TEXT",          "default": "'[]'",          "nullable": false,          "primary_key": false,          "declaration": "\n    media TEXT NOT NULL DEFAULT '[]'",          "position": 7        },        {          "name": "external_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    external_id TEXT",          "position": 8        },        {          "name": "sent_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    sent_at TEXT NOT NULL",          "position": 9        },        {          "name": "created_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    created_at TEXT NOT NULL",          "position": 10        },        {          "name": "read_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    read_at TEXT",          "position": 11        },        {          "name": "reply_to_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    reply_to_id TEXT",          "position": 12        },        {          "name": "reply_to_external_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    reply_to_external_id TEXT",          "position": 13        },        {          "name": "reactions",          "type": "TEXT",          "default": "'[]'",          "nullable": false,          "primary_key": false,          "declaration": "\n    reactions TEXT NOT NULL DEFAULT '[]'",          "position": 14        },        {          "name": "meta",          "type": "TEXT",          "default": "'{}'",          "nullable": false,          "primary_key": false,          "declaration": "\n    meta TEXT NOT NULL DEFAULT '{}'",          "position": 15        },        {          "name": "sender_contact_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    sender_contact_id TEXT",          "position": 16        },        {          "name": "sender_participant_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    sender_participant_id TEXT",          "position": 17        },        {          "name": "seq",          "type": "INTEGER",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    seq INTEGER",          "position": 18        },        {          "name": "edited_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    edited_at TEXT",          "position": 19        },        {          "name": "deleted_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    deleted_at TEXT",          "position": 20        },        {          "name": "forward_of_id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    forward_of_id TEXT",          "position": 21        },        {          "name": "mentions",          "type": "TEXT",          "default": "'[]'",          "nullable": false,          "primary_key": false,          "declaration": "\n    mentions TEXT NOT NULL DEFAULT '[]'",          "position": 22        },        {          "name": "starred_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    starred_at TEXT",          "position": 23        },        {          "name": "hidden_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    hidden_at TEXT",          "position": 24        },        {          "name": "send_state",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    send_state TEXT",          "position": 25        },        {          "name": "send_error",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    send_error TEXT",          "position": 26        },        {          "name": "send_attempts",          "type": "INTEGER",          "default": "0",          "nullable": false,          "primary_key": false,          "declaration": "\n    send_attempts INTEGER NOT NULL DEFAULT 0",          "position": 27        },        {          "name": "trashed_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    trashed_at TEXT",          "position": 28        },        {          "name": "_hlc",          "type": "TEXT",          "default": "''",          "nullable": false,          "primary_key": false,          "declaration": "\n    _hlc TEXT NOT NULL DEFAULT ''",          "position": 29        },        {          "name": "_born",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    _born TEXT",          "position": 30        }      ],      "constraints": [        {          "declaration": "\n    CHECK (channel IN ('cotton', 'whatsapp', 'imessage', 'sms', 'instagram'))",          "position": 31        },        {          "declaration": "\n    CHECK (send_state IS NULL OR send_state IN ('queued', 'sending', 'sent', 'delivered', 'read', 'failed'))",          "position": 32        },        {          "declaration": "\n    CHECK (direction IN ('in', 'out'))",          "position": 33        },        {          "declaration": "\n    CHECK ((deleted_at IS NOT NULL) OR (trim(COALESCE(body, '')) <> '') OR (json_array_length(media) > 0))",          "position": 34        },        {          "declaration": "\n    CHECK (json_type(media) = 'array')",          "position": 35        },        {          "declaration": "\n    CHECK (json_type(meta) = 'object')",          "position": 36        },        {          "declaration": "\n    CHECK (origin IN ('live', 'imported'))",          "position": 37        },        {          "declaration": "\n    CHECK (json_type(reactions) = 'array')",          "position": 38        },        {          "declaration": "\n    CHECK (json_type(mentions) = 'array')",          "position": 39        },        {          "declaration": "\n    FOREIGN KEY (thread_id) REFERENCES cotton_msg_threads(id)",          "position": 40        },        {          "declaration": "\n    FOREIGN KEY (workspace_id) REFERENCES cotton_workspaces(id) ON DELETE CASCADE\n  ",          "position": 41        }      ],      "prefix": "CREATE TABLE \"cotton_msg_messages\" (",      "suffix": ")",      "indexes": [        "CREATE INDEX idx_cotton_msg_messages_thread_sent ON cotton_msg_messages (thread_id, sent_at DESC)",        "CREATE INDEX idx_cotton_msg_messages_unread ON cotton_msg_messages (workspace_id, thread_id) WHERE read_at IS NULL",        "CREATE INDEX idx_cotton_msg_messages_unresolved_reply ON cotton_msg_messages (thread_id, reply_to_external_id) WHERE (reply_to_external_id IS NOT NULL) AND (reply_to_id IS NULL)",        "CREATE UNIQUE INDEX uq_cotton_msg_messages_external ON cotton_msg_messages (thread_id, channel, external_id) WHERE external_id IS NOT NULL",        "CREATE INDEX idx_cotton_msg_messages_channel_external ON cotton_msg_messages (workspace_id, channel, external_id) WHERE external_id IS NOT NULL",        "CREATE INDEX idx_cotton_msg_messages_sender_participant ON cotton_msg_messages (sender_participant_id)",        "CREATE INDEX idx_cotton_msg_messages_thread_seq ON cotton_msg_messages (thread_id, seq)",        "CREATE INDEX idx_cotton_msg_messages_thread_channel ON cotton_msg_messages (thread_id, channel)",        "CREATE INDEX idx_cotton_msg_messages_source_account ON cotton_msg_messages (workspace_id, channel, json_extract(meta, '$.source.account')) WHERE deleted_at IS NULL AND hidden_at IS NULL AND json_extract(meta, '$.source.account') IS NOT NULL",        "CREATE INDEX idx_cotton_msg_messages_starred ON cotton_msg_messages (workspace_id, starred_at) WHERE starred_at IS NOT NULL",        "CREATE INDEX idx_cotton_msg_messages_thread_created ON cotton_msg_messages (thread_id, created_at)",        "CREATE INDEX IF NOT EXISTS idx_cotton_msg_messages_outbox ON cotton_msg_messages (channel, created_at, id) WHERE send_state IN ('queued', 'sending')",        "CREATE INDEX IF NOT EXISTS idx_cotton_msg_messages_author ON cotton_msg_messages (substr(_hlc, 20), _hlc)"      ],      "full_text": [        "CREATE VIRTUAL TABLE fts_messages USING fts5(\n       body, content = 'cotton_msg_messages', content_rowid = 'rowid',\n       tokenize = 'unicode61 remove_diacritics 2'\n     )"      ],      "triggers": [        "CREATE TRIGGER fts_messages_ai AFTER INSERT ON cotton_msg_messages WHEN new.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_messages'), 0) BEGIN\n       INSERT INTO fts_messages (rowid, body) VALUES (new.rowid, new.body);\n     END",        "CREATE TRIGGER fts_messages_ad AFTER DELETE ON cotton_msg_messages WHEN old.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_messages'), 0) BEGIN\n       INSERT INTO fts_messages (fts_messages, rowid, body) VALUES ('delete', old.rowid, old.body);\n     END",        "CREATE TRIGGER fts_messages_au AFTER UPDATE OF body ON cotton_msg_messages\n       FOR EACH ROW WHEN old.rowid >= coalesce((SELECT floor FROM _fts_fill WHERE fts = 'fts_messages'), 0) AND (new.body IS NOT old.body) BEGIN\n       INSERT INTO fts_messages (fts_messages, rowid, body) VALUES ('delete', old.rowid, old.body);\n       INSERT INTO fts_messages (rowid, body) VALUES (new.rowid, new.body);\n     END"      ]    }  ],  "parts": {    "shape": "desk",    "head": {      "facts": [        "sender_name",        "sent_at"      ]    },    "links": [],    "body": "display_body",    "lists": [],    "acts": {      "more": [        "keep",        "unkeep",        "hide",        "thread"      ]    },    "strip": [      {        "column": "media"      },      {        "column": "reply_preview"      },      {        "column": "reactions"      },      {        "column": "send_state"      },      {        "column": "send_error"      }    ]  }}