ctucx.git: travelynx2fedi

Automaticly post travelynx checkins on the fediverse

1 
2 
3 
4 
5 
6 
7 
8 
9 
10 
11 
12 
13 
14 
15 
16 
17 
18 
19 
20 
21 
22 
23 
24 
25 
26 
27 
28 
29 
30 
31 
32 
33 
34 
35 
36 
37 
38 
39 
40 
41 
42 
43 
44 
45 
import std/[asyncdispatch, httpclient]
import std/[json, options]

import types

proc fediPost* (config: Config, text: string, visibility: PostVisibility): Future[JsonNode] {.async.} =
  let client = newAsyncHttpClient(userAgent = "travelynx2fedi", headers = newHttpHeaders({
    "Authorization": "Bearer " &  config.FediAccessToken
  }))

  var postData = newMultipartData({
    "status":     text,
    "language":   "de",
    "visibility": $visibility
  })

  if config.FediSpoilerText.isSome:
    postData["spoiler_text"] = config.FediSpoilerText.get

  let request = await client.request(
    httpmethod = HttpPost,
    url        = config.FediURL & "/api/v1/statuses",
    multipart  = postData
  )

  result                 = parseJson(await request.body)
  result["responseCode"] = newJString($request.code)
  
  client.close()


proc fediDelete* (config: Config, id: string): Future[JsonNode] {.async.} =
  let client = newAsyncHttpClient(userAgent = "travelynx2fedi", headers = newHttpHeaders({
    "Authorization": "Bearer " &  config.FediAccessToken
  }))

  let request = await client.request(
    httpmethod = HttpDelete,
    url        = config.FediURL & "/api/v1/statuses/" & id,
  )

  result                 = parseJson(await request.body)
  result["responseCode"] = newJString($request.code)
  
  client.close()