Cotton code
834 lines · 18.8 KB · JSONRaw
{  "format_version": 1,  "id": "email_thread",  "name": "Email",  "plural": "Email",  "picture": {    "photo": "photo:b5cf213d649bc342c2915d314e56b4f4fbc12dabd589527e171f51fa674aa2ea",    "object": "tool-email"  },  "builtin": true,  "engine": "email",  "record_route": "/email/[id]",  "columns": [    {      "key": "subject",      "type": "text",      "readonly": true    },    {      "key": "sender",      "type": "text",      "readonly": true,      "fact": true,      "reading": "{value}"    },    {      "key": "sender_address",      "type": "text",      "readonly": true    },    {      "key": "preview",      "type": "text",      "readonly": true    },    {      "key": "first_at",      "type": "date",      "readonly": true,      "name": "First email"    },    {      "key": "last_at",      "type": "date",      "readonly": true,      "name": "Latest email",      "fact": false,      "reading": "{datetime}"    },    {      "key": "email_count",      "type": "number",      "readonly": true,      "name": "Emails"    },    {      "key": "summary",      "type": "longtext",      "readonly": false    },    {      "key": "summary_by",      "type": "text",      "readonly": true,      "name": "Summary by"    },    {      "key": "read_state",      "type": "choice",      "readonly": true,      "name": "Read state",      "choices": [        "read",        "unread"      ],      "red_when": [        "unread"      ]    },    {      "key": "starred",      "type": "bool",      "readonly": true    },    {      "key": "in_inbox",      "type": "bool",      "readonly": true,      "name": "In inbox"    },    {      "key": "trashed_at",      "type": "date",      "readonly": true,      "name": "Binned"    },    {      "key": "binned",      "name": "In bin",      "type": "bool",      "readonly": true    },    {      "key": "marks",      "type": "text",      "readonly": true,      "name": "Marked"    },    {      "key": "sender_kind",      "name": "Sender is",      "type": "choice",      "readonly": true,      "choices": [        "person",        "service",        "junk"      ]    },    {      "key": "record_kind",      "name": "Record",      "type": "choice",      "readonly": true,      "choices": [        "receipt",        "statement",        "ticket",        "booking",        "code",        "delivery",        "notice"      ]    },    {      "key": "amount_minor",      "name": "Amount",      "type": "amount",      "currency": "currency",      "readonly": true    },    {      "key": "currency",      "type": "text",      "readonly": true    },    {      "key": "merchant",      "name": "Merchant",      "type": "text",      "readonly": true    },    {      "key": "record_on",      "name": "On",      "type": "date",      "readonly": true    },    {      "key": "reference",      "name": "Reference",      "type": "text",      "readonly": true    },    {      "key": "people",      "name": "People",      "type": "link",      "link_to": "person",      "multiple": true,      "readonly": true    },    {      "key": "transaction_id",      "name": "Recorded in Money",      "type": "link",      "link_to": "transaction",      "readonly": true    },    {      "key": "event_id",      "name": "In the calendar",      "type": "link",      "link_to": "event",      "readonly": true    }  ],  "title": "subject",  "voice_words": [    "merchant"  ],  "date": "last_at",  "project": null,  "order": null,  "slip": {    "title": "sender",    "subtitle": "subject",    "trailing": "last_at",    "photo": null,    "toggle": null,    "unread": "read_state",    "badge": {      "column": "starred",      "symbol": "star.fill",      "spoken": "Starred"    }  },  "tile": {    "face": "slip-list",    "current": {      "column": "last_at",      "days": 2    }  },  "page": [    {      "heading": null,      "columns": [        "subject",        "sender",        "marks",        "email_count",        "first_at",        "last_at",        "transaction_id",        "event_id"      ]    },    {      "heading": "Summary",      "columns": [        "summary",        "summary_by"      ]    },    {      "heading": null,      "columns": [        "trashed_at"      ]    }  ],  "slip_by": {    "column": "record_kind",    "slips": {      "receipt": {        "title": "merchant",        "subtitle": "amount_minor",        "trailing": "record_on"      },      "statement": {        "title": "merchant",        "subtitle": "record_on"      },      "ticket": {        "title": "merchant",        "subtitle": "record_on",        "trailing": "reference"      },      "booking": {        "title": "merchant",        "subtitle": "record_on",        "trailing": "reference"      },      "delivery": {        "title": "merchant",        "subtitle": "subject"      },      "code": {        "title": "merchant",        "subtitle": "record_kind"      }    }  },  "instructions": "Imported email archive. Ingest and Gmail sync run in Rust. Cotton has no send or reply verb. Never create imported mail as a substitute for delivery.",  "sort": {    "column": "last_at",    "descending": true  },  "groups": {    "stack": "slips",    "widget": {      "face": "slips",      "sizes": [        "medium",        "large"      ]    }  },  "page_parts": [    "title",    "facts",    "messages",    "grid"  ],  "conversation": {    "table": "email",    "links": [      "thread_id"    ],    "body": "body",    "at": "sent_at",    "mine": {      "column": "direction",      "values": [        "out"      ]    },    "can_send": false,    "folds": true,    "media": "media",    "header_columns": [      "from_name",      "from_email",      "to_line",      "cc_line",      "subject"    ]  },  "commands": [    {      "id": "record_as_transaction",      "label": "Record transaction",      "kind": "create",      "method": "records.recordAsTransaction",      "bindings": {        "threadId": "$id"      },      "when": {        "record_kind": [          "receipt",          "statement"        ],        "transaction_id": null      },      "receipt": "Recorded in Money",      "does": "moves"    },    {      "id": "add_to_calendar",      "label": "Add event",      "kind": "create",      "method": "records.addToCalendar",      "bindings": {        "threadId": "$id"      },      "when": {        "record_kind": [          "ticket",          "booking"        ],        "event_id": null      },      "receipt": "Added to the calendar",      "does": "moves"    },    {      "id": "star",      "label": "Star",      "object": "star",      "kind": "update",      "verb": "update_email",      "data": {        "starred": true      },      "bindings": {        "threadId": "$id"      },      "when": {        "starred": false,        "binned": false      },      "receipt": "Starred",      "undo": "unstar",      "does": "completes"    },    {      "id": "unstar",      "label": "Unstar",      "object": "star",      "kind": "update",      "verb": "update_email",      "data": {        "starred": false      },      "bindings": {        "threadId": "$id"      },      "when": {        "starred": true,        "binned": false      },      "receipt": "Unstarred",      "undo": "star"    },    {      "id": "archive",      "label": "Archive",      "kind": "update",      "verb": "update_email",      "data": {        "inbox": false      },      "bindings": {        "threadId": "$id"      },      "when": {        "in_inbox": true,        "binned": false      },      "receipt": "Archived",      "undo": "inbox",      "does": "puts-away"    },    {      "id": "inbox",      "label": "Move to inbox",      "object": "restore",      "kind": "update",      "verb": "update_email",      "data": {        "inbox": true      },      "bindings": {        "threadId": "$id"      },      "when": {        "in_inbox": false,        "binned": false      },      "receipt": "Moved {name} to the inbox",      "undo": "archive",      "does": "moves"    },    {      "id": "junk",      "label": "Junk",      "object": "junk",      "kind": "update",      "verb": "block_sender",      "bindings": {        "address": "sender_address"      },      "when": {        "sender_kind": {          "not": "junk"        },        "binned": false      },      "destructive": true,      "confirm": "Mail from this sender goes to Junk from now on. Not junk brings it back.",      "receipt": "Moved {name} to Junk",      "does": "destroys"    },    {      "id": "not_junk",      "label": "Unblock",      "kind": "update",      "verb": "unblock_sender",      "bindings": {        "address": "sender_address"      },      "when": {        "sender_kind": "junk",        "binned": false      },      "receipt": "Moved {name} out of Junk",      "does": "completes"    },    {      "id": "transaction",      "label": "Open transaction",      "kind": "open",      "opens": "transaction_id"    },    {      "id": "event",      "label": "Open event",      "kind": "open",      "opens": "event_id"    }  ],  "delete": false,  "empty": {    "title": "No email in this view",    "message": "Email from connected accounts appears here when it matches this view."  },  "add": false,  "links": [    {      "label": "Senders",      "table": "email_sender"    },    {      "label": "Junk senders",      "table": "email_sender",      "where": {        "blocked": true      }    }  ],  "link_tiles": {    "after": 4  },  "parts": {    "shape": "desk",    "head": {      "facts": [        "sender"      ]    },    "links": [],    "lists": [],    "acts": {      "more": [        "star",        "unstar",        "archive",        "inbox",        "junk",        "not_junk",        "record_as_transaction",        "add_to_calendar",        "transaction",        "event"      ]    },    "strip": [      {        "column": "marks"      },      {        "column": "record_on"      },      {        "column": "amount_minor"      },      {        "column": "merchant"      },      {        "column": "reference"      }    ]  },  "tool": {    "name": "Email",    "key": "email",    "icon": "mail",    "card_stock": "rose",    "route": "/email"  ,"purpose":"Your email conversations and attachments."},  "tables": [    {      "name": "cotton_email_threads",      "columns": [        {          "name": "id",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": true,          "declaration": "\n    id TEXT PRIMARY KEY",          "position": 0        },        {          "name": "workspace_id",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    workspace_id TEXT NOT NULL",          "position": 1        },        {          "name": "external_key",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    external_key TEXT NOT NULL",          "position": 2        },        {          "name": "subject",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    subject TEXT",          "position": 3        },        {          "name": "first_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    first_at TEXT",          "position": 4        },        {          "name": "last_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    last_at TEXT",          "position": 5        },        {          "name": "email_count",          "type": "INTEGER",          "default": "0",          "nullable": false,          "primary_key": false,          "declaration": "\n    email_count INTEGER NOT NULL DEFAULT 0",          "position": 6        },        {          "name": "summary",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    summary TEXT",          "position": 7        },        {          "name": "summary_updated_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    summary_updated_at TEXT",          "position": 8        },        {          "name": "summary_by",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    summary_by TEXT",          "position": 9        },        {          "name": "created_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    created_at TEXT NOT NULL",          "position": 10        },        {          "name": "updated_at",          "type": "TEXT",          "default": null,          "nullable": false,          "primary_key": false,          "declaration": "\n    updated_at TEXT NOT NULL",          "position": 11        },        {          "name": "_hlc",          "type": "TEXT",          "default": "''",          "nullable": false,          "primary_key": false,          "declaration": "\n    _hlc TEXT NOT NULL DEFAULT ''",          "position": 12        },        {          "name": "trashed_at",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": " trashed_at TEXT",          "position": 13        },        {          "name": "_born",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    _born TEXT",          "position": 14        },        {          "name": "sender_address",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    sender_address TEXT",          "position": 15        },        {          "name": "sender_kind",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    sender_kind TEXT",          "position": 16        },        {          "name": "record_kind",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    record_kind TEXT",          "position": 17        },        {          "name": "amount_minor",          "type": "INTEGER",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    amount_minor INTEGER",          "position": 18        },        {          "name": "currency",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    currency TEXT",          "position": 19        },        {          "name": "merchant",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    merchant TEXT",          "position": 20        },        {          "name": "record_on",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    record_on TEXT",          "position": 21        },        {          "name": "reference",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    reference TEXT",          "position": 22        },        {          "name": "record_reason",          "type": "TEXT",          "default": null,          "nullable": true,          "primary_key": false,          "declaration": "\n    record_reason TEXT",          "position": 23        }      ],      "constraints": [        {          "declaration": "\n    UNIQUE (workspace_id, external_key)",          "position": 24        },        {          "declaration": "\n    CHECK ((record_kind IS NULL) OR (record_kind IN ('receipt', 'statement', 'ticket', 'booking', 'code', 'delivery', 'notice')))",          "position": 25        },        {          "declaration": "\n    CHECK ((currency IS NULL) OR (length(currency) = 3 AND currency = upper(currency)))",          "position": 26        },        {          "declaration": "\n    CHECK ((record_on IS NULL) OR (record_on GLOB '[0-9][0-9][0-9][0-9]-[0-9][0-9]-[0-9][0-9]'))",          "position": 27        },        {          "declaration": "\n    CHECK ((sender_address IS NULL) OR (sender_address = lower(sender_address) AND length(sender_address) > 0))",          "position": 28        },        {          "declaration": "\n    CHECK ((sender_kind IS NULL) OR (sender_kind IN ('person', 'service', 'junk')))",          "position": 29        },        {          "declaration": "\n    CHECK (length(external_key) > 0)",          "position": 30        },        {          "declaration": "\n    CHECK (email_count >= 0)",          "position": 31        },        {          "declaration": "\n    CHECK ((summary IS NULL) OR (length(summary) <= 4000))",          "position": 32        },        {          "declaration": "\n    CHECK ((summary_by IS NULL) OR (length(trim(summary_by)) > 0))",          "position": 33        },        {          "declaration": "\n    FOREIGN KEY (workspace_id) REFERENCES cotton_workspaces(id) ON DELETE CASCADE\n  ",          "position": 34        }      ],      "prefix": "CREATE TABLE cotton_email_threads (",      "suffix": ")",      "indexes": [        "CREATE INDEX idx_cotton_email_threads_workspace_recent ON cotton_email_threads (workspace_id, last_at DESC)",        "CREATE INDEX idx_cotton_email_threads_sender_kind ON cotton_email_threads (workspace_id, sender_kind, last_at DESC)",        "CREATE INDEX idx_cotton_email_threads_sender ON cotton_email_threads (workspace_id, sender_address) WHERE sender_address IS NOT NULL",        "CREATE INDEX idx_cotton_email_threads_record_kind ON cotton_email_threads (workspace_id, record_kind, last_at DESC) WHERE record_kind IS NOT NULL",        "CREATE INDEX idx_cotton_email_threads_merchant ON cotton_email_threads (workspace_id, id, merchant, trashed_at) WHERE merchant IS NOT NULL AND trashed_at IS NULL",        "CREATE INDEX IF NOT EXISTS idx_cotton_email_threads_author ON cotton_email_threads (substr(_hlc, 20), _hlc)"      ],      "full_text": [],      "triggers": []    }  ]}