← Public packages
@danj/hey-email
HEY.com email for Kody: search threads, read messages, download PDF attachments, and write drafts. Sending is barred until you allow it.
src/paper-trail.js
58 lines · 1.9 KB · JavaScriptimport { heyFetch, heyRequest, BASE } from './client.js'
import listBoxes from './boxes.js'
const JSON_HEADERS = { 'Content-Type': 'application/json' }
function topicIdFromMessage(message) {
return message?.topic_id ?? message?.topicId ?? message?.topic?.id
}
async function resolveTopicId(input) {
if (input?.topicId != null) return input.topicId
const entryId = input?.entryId
if (entryId == null) throw new Error('topicId or entryId is required')
const message = await heyFetch(`/messages/${entryId}.json`)
const topicId = topicIdFromMessage(message)
if (topicId == null) throw new Error('message names no topic')
return topicId
}
async function paperTrailBoxId() {
const boxes = await listBoxes()
const trail = (Array.isArray(boxes) ? boxes : []).find((box) => box?.kind === 'trailbox')
if (trail?.id == null) throw new Error('HEY lists no Paper Trail box')
return trail.id
}
/**
* Move one HEY topic (thread) to the Paper Trail. Per topic only: it does
* not change the sender's screening or where their other mail lands.
*
* Resolves the Paper Trail box from GET /boxes.json (kind `trailbox`), then
* POSTs the official SDK MoveTopic body to /topics/{topicId}/moves.json.
*
* @param {{ topicId?: number|string, entryId?: number|string }} input
* @returns {Promise<{ topicId: number|string, boxId: number, status: number, url: string }>}
*
* @example
* import moveToPaperTrail from 'kody:@danj/hey-email/paper-trail'
* const moved = await moveToPaperTrail({ topicId: 123456789 })
*/
export async function moveToPaperTrail(input = {}) {
const topicId = await resolveTopicId(input)
const boxId = await paperTrailBoxId()
const path = `/topics/${topicId}/moves.json`
const { status } = await heyRequest(path, {
method: 'POST',
headers: JSON_HEADERS,
body: JSON.stringify({ box_id: boxId }),
})
return {
topicId,
boxId,
status,
url: `${BASE}/topics/${topicId}`,
}
}
export default moveToPaperTrail