Module: Tempest::Commands::Post

Defined in:
lib/tempest/commands/post.rb

Constant Summary collapse

MAX_GRAPHEMES =
300

Class Method Summary collapse

Class Method Details

.build_reply(uri, client:) ⇒ Object



84
85
86
87
# File 'lib/tempest/commands/post.rb', line 84

def build_reply(uri, client:)
  return nil if uri.nil? || uri.empty?
  Tempest::Post.fetch_reply_refs(client, uri)
end

.call(argv:, session:, client:, stdout:, stderr:, stdin:) ⇒ Object



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
# File 'lib/tempest/commands/post.rb', line 11

def call(argv:, session:, client:, stdout:, stderr:, stdin:)
  opts, positional = parse(argv)
  return 64 if opts[:invalid]

  text = read_text(positional, stdin: stdin)
  if text.nil? || text.strip.empty?
    stderr.puts "error: empty post text"
    return 64
  end
  if text.grapheme_clusters.length > MAX_GRAPHEMES
    stderr.puts "error: post exceeds #{MAX_GRAPHEMES} graphemes"
    return 64
  end

  reply = build_reply(opts[:reply_to], client: client)

  response = Tempest::Post.create(
    client, did: session.did, text: text, reply: reply,
    langs: opts[:langs],
  )

  url = Tempest::Post.bsky_url(at_uri: response["uri"], handle: session.handle)

  if opts[:json]
    require "json"
    payload = { "uri" => response["uri"], "cid" => response["cid"] }
    payload["url"] = url if url
    stdout.puts JSON.generate(payload)
  else
    stdout.puts "posted: #{response["uri"]}"
    stdout.puts "url: #{url}" if url
  end
  0
end

.parse(argv) ⇒ Object



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
# File 'lib/tempest/commands/post.rb', line 46

def parse(argv)
  opts = { langs: ["ja"], json: false, reply_to: nil, invalid: false }
  positional = []
  i = 0
  while i < argv.length
    a = argv[i]
    case a
    when "--lang"
      opts[:langs] = argv[i + 1].to_s.split(",")
      i += 2
    when /\A--lang=(.+)\z/
      opts[:langs] = $1.split(",")
      i += 1
    when "--reply-to"
      opts[:reply_to] = argv[i + 1]
      i += 2
    when /\A--reply-to=(.+)\z/
      opts[:reply_to] = $1
      i += 1
    when "--json"
      opts[:json] = true
      i += 1
    else
      positional << a
      i += 1
    end
  end
  [opts, positional]
end

.read_text(positional, stdin:) ⇒ Object



76
77
78
79
80
81
82
# File 'lib/tempest/commands/post.rb', line 76

def read_text(positional, stdin:)
  if positional == ["-"]
    stdin.read.to_s.chomp
  else
    positional.join(" ")
  end
end