causeway 0.1.0 → 0.2.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +14 -0
- data/README.md +105 -6
- data/lib/causeway/configuration.rb +18 -1
- data/lib/causeway/publishers/base.rb +59 -3
- data/lib/causeway/publishers/bluesky.rb +28 -6
- data/lib/causeway/publishers/linkedin.rb +9 -3
- data/lib/causeway/publishers/mastodon.rb +39 -6
- data/lib/causeway/publishers/threads.rb +81 -0
- data/lib/causeway/version.rb +1 -1
- data/lib/causeway.rb +37 -0
- metadata +2 -1
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: 8ae5916d54eb563619d1edbf1192a1865a88f354cab789af774f4580ead487cc
|
|
4
|
+
data.tar.gz: b231fa433015156253c153a154c85a14e65f5b179eed55d1c9b3b8888a589ddf
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 59189955fefacd9e8c7bc7e2817c6b177122ff8b181dbca7f93db78577655c633a52fe3efcd247b4da855eb925e8a9eaae4b3866069118dc6c26bd02f2bed578
|
|
7
|
+
data.tar.gz: ed29b0a700bb23cc70c800cb501e5bd7fad613d9b91e8c4b420545ea247a349702d56150fe0d65a9960d915227e48c89d842785233f63292e703cfbc736b6302
|
data/CHANGELOG.md
CHANGED
|
@@ -1,5 +1,19 @@
|
|
|
1
1
|
## [Unreleased]
|
|
2
2
|
|
|
3
|
+
## [0.2.0] - 2026-12-02
|
|
4
|
+
|
|
5
|
+
- Add the Threads publisher, configured with `threads_token` and `threads_user_id`. Container status checks are
|
|
6
|
+
configured with `threads_status_interval` (default 5 seconds) and `threads_status_attempts` (default 6).
|
|
7
|
+
- Add `Causeway.post`, which posts to each publisher in `publishers` (default `%i[mastodon bluesky]`), with
|
|
8
|
+
`reply_to:` and `skip:`, and returns the results by publisher key.
|
|
9
|
+
- Add `Causeway.compose`, which builds post text from `template`, with tags and `extra_tags` as hashtags.
|
|
10
|
+
- Add replies: `post` takes `reply_to:` with the id of an earlier post. Supported on Mastodon, Bluesky, and Threads.
|
|
11
|
+
- Add `max_length`, `text_length`, and `truncate` to every publisher. A publisher's `post` returns an error,
|
|
12
|
+
without calling the API, for text that is too long; `Causeway.post` truncates first unless `truncate` is false.
|
|
13
|
+
Mastodon's limits are read from the instance (`/api/v2/instance`), falling back to 500 characters with links
|
|
14
|
+
counting 23; `mastodon_max_length` overrides the limit.
|
|
15
|
+
- Breaking: publishers implement a private `publish(text, reply_to)` instead of overriding `post`.
|
|
16
|
+
|
|
3
17
|
## [0.1.0] - 2026-12-02
|
|
4
18
|
|
|
5
19
|
- Add the LinkedIn publisher, posting to an organization feed via `linkedin_token`,
|
data/README.md
CHANGED
|
@@ -28,6 +28,9 @@ Causeway.configure do |config|
|
|
|
28
28
|
|
|
29
29
|
config.linkedin_token = ENV["LINKEDIN_TOKEN"]
|
|
30
30
|
config.linkedin_person_id = ENV["LINKEDIN_PERSON_ID"] # or linkedin_organization_id
|
|
31
|
+
|
|
32
|
+
config.threads_token = ENV["THREADS_TOKEN"]
|
|
33
|
+
config.threads_user_id = ENV["THREADS_USER_ID"]
|
|
31
34
|
end
|
|
32
35
|
```
|
|
33
36
|
|
|
@@ -38,16 +41,81 @@ expiring tokens:
|
|
|
38
41
|
config.linkedin_token = -> { MySecretStore.fetch("linkedin_token") }
|
|
39
42
|
```
|
|
40
43
|
|
|
41
|
-
|
|
44
|
+
Choose which services `Causeway.post` uses, in order (the default is Mastodon and Bluesky):
|
|
45
|
+
|
|
46
|
+
```ruby
|
|
47
|
+
config.publishers = %i[mastodon bluesky linkedin threads]
|
|
48
|
+
```
|
|
49
|
+
|
|
50
|
+
### Posting
|
|
51
|
+
|
|
52
|
+
```ruby
|
|
53
|
+
results = Causeway.post("Hello, world!")
|
|
54
|
+
# => { mastodon: { id: "123", url: "https://mastodon.social/@you/123" },
|
|
55
|
+
# bluesky: { error: "..." } }
|
|
56
|
+
```
|
|
57
|
+
|
|
58
|
+
`Causeway.post` posts to each configured publisher and returns each one's result by key: `{ id:, url: }` on
|
|
59
|
+
success, or `{ error: }` on failure. Publishers never raise on API errors. Pass a block to handle each result as it
|
|
60
|
+
arrives, and `skip:` to leave some publishers out:
|
|
61
|
+
|
|
62
|
+
```ruby
|
|
63
|
+
Causeway.post(text, skip: [:linkedin]) do |publisher, result|
|
|
64
|
+
puts "#{publisher.name}: #{result[:url] || result[:error]}"
|
|
65
|
+
end
|
|
66
|
+
```
|
|
67
|
+
|
|
68
|
+
To post to one service, use its publisher directly: `Causeway::Publishers::Mastodon.new.post(text)`. The publishers
|
|
69
|
+
are `Bluesky`, `LinkedIn`, `Mastodon`, and `Threads`, keyed `:bluesky`, `:linkedin`, `:mastodon`, and `:threads`.
|
|
70
|
+
|
|
71
|
+
### Composing posts
|
|
72
|
+
|
|
73
|
+
`Causeway.compose` builds post text from `config.template`, replacing `{description}`, `{url}`, and `{tags}`. Tags
|
|
74
|
+
can be an array or a space-separated string, and become hashtags along with `config.extra_tags`:
|
|
75
|
+
|
|
76
|
+
```ruby
|
|
77
|
+
config.template = "{description}\n\n{url}\n\n{tags}" # the default
|
|
78
|
+
config.extra_tags = ["blog"]
|
|
79
|
+
|
|
80
|
+
Causeway.compose(description: "Understanding Rack", url: "https://example.com/rack", tags: "ruby rack")
|
|
81
|
+
# => "Understanding Rack\n\nhttps://example.com/rack\n\n#ruby #rack #blog"
|
|
82
|
+
|
|
83
|
+
Causeway.compose(url: "https://example.com/rack", template: "New post: {url}")
|
|
84
|
+
# => "New post: https://example.com/rack"
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
### Replies
|
|
88
|
+
|
|
89
|
+
Pass the `id` of an earlier post to reply to it, building a thread:
|
|
42
90
|
|
|
43
91
|
```ruby
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
# or { error: "..." } on failure
|
|
92
|
+
first = Causeway.post("We're meeting on Thursday!")
|
|
93
|
+
Causeway.post("Starting in an hour!", reply_to: first.transform_values { |result| result[:id] })
|
|
47
94
|
```
|
|
48
95
|
|
|
49
|
-
|
|
50
|
-
|
|
96
|
+
Mastodon, Bluesky, and Threads support replies. LinkedIn returns an error.
|
|
97
|
+
|
|
98
|
+
### Post length
|
|
99
|
+
|
|
100
|
+
Each publisher has a `max_length`, and `text_length(text)` measures text the way that service counts it:
|
|
101
|
+
|
|
102
|
+
| Publisher | `max_length` | Counting |
|
|
103
|
+
|-----------|-----------------------------------------|--------------------------------|
|
|
104
|
+
| Bluesky | 300 | graphemes |
|
|
105
|
+
| Mastodon | the instance's limit* | graphemes, links count 23* |
|
|
106
|
+
| LinkedIn | 3000 | graphemes |
|
|
107
|
+
| Threads | 500 | graphemes |
|
|
108
|
+
|
|
109
|
+
\* Causeway asks the Mastodon instance for its limits (`/api/v2/instance`) the first time it needs them. If the
|
|
110
|
+
instance doesn't say, it uses Mastodon's defaults: 500 characters, with each link counting 23. Set
|
|
111
|
+
`config.mastodon_max_length` to override the limit.
|
|
112
|
+
|
|
113
|
+
`Causeway.post` shortens text to fit each publisher with `publisher.truncate(text)`. It keeps the footer (the
|
|
114
|
+
trailing lines that are blank or contain links or hashtags) and cuts the text above it, ending it with `…`. If
|
|
115
|
+
there's no footer, or the footer alone is too long, it cuts the whole text. Set `config.truncate = false` to turn
|
|
116
|
+
this off.
|
|
117
|
+
|
|
118
|
+
A publisher's own `post` never truncates: it returns an error, without calling the API, if the text is too long.
|
|
51
119
|
|
|
52
120
|
### Getting credentials
|
|
53
121
|
|
|
@@ -105,6 +173,37 @@ to generate a new one.
|
|
|
105
173
|
`linkedin_api_version` sets the `LinkedIn-Version` header and defaults to `202608`. LinkedIn retires old versions
|
|
106
174
|
periodically, so you may need to update it.
|
|
107
175
|
|
|
176
|
+
#### Threads
|
|
177
|
+
|
|
178
|
+
1. Create an app at <https://developers.facebook.com/apps> with the **Access the Threads API** use case, and add the
|
|
179
|
+
`threads_basic` and `threads_content_publish` permissions.
|
|
180
|
+
2. Until Meta grants the app advanced access, only Threads testers can post. Add your Threads account as a tester in
|
|
181
|
+
the app's roles, then accept the invite in the Threads app.
|
|
182
|
+
3. Generate a user access token for your account. It's short-lived (one hour), so exchange it for a long-lived (60
|
|
183
|
+
day) token:
|
|
184
|
+
|
|
185
|
+
```bash
|
|
186
|
+
curl -s "https://graph.threads.com/access_token?grant_type=th_exchange_token&client_secret=$THREADS_APP_SECRET&access_token=$SHORT_LIVED_TOKEN" | jq -r .access_token
|
|
187
|
+
```
|
|
188
|
+
|
|
189
|
+
Long-lived tokens can be refreshed once they're at least a day old, before they expire:
|
|
190
|
+
|
|
191
|
+
```bash
|
|
192
|
+
curl -s "https://graph.threads.com/refresh_access_token?grant_type=th_refresh_token&access_token=$THREADS_TOKEN" | jq -r .access_token
|
|
193
|
+
```
|
|
194
|
+
|
|
195
|
+
4. Get your Threads user ID:
|
|
196
|
+
|
|
197
|
+
```bash
|
|
198
|
+
curl -s "https://graph.threads.com/v1.0/me?fields=id,username&access_token=$THREADS_TOKEN" | jq
|
|
199
|
+
```
|
|
200
|
+
|
|
201
|
+
5. Set `threads_token` to the long-lived token and `threads_user_id` to the ID.
|
|
202
|
+
|
|
203
|
+
Threads posts in two steps: it creates a container, then publishes it once Threads has processed it. Causeway checks
|
|
204
|
+
the container's status every `threads_status_interval` seconds (default 5), up to `threads_status_attempts` times
|
|
205
|
+
(default 6), before giving up.
|
|
206
|
+
|
|
108
207
|
## Development
|
|
109
208
|
|
|
110
209
|
After checking out the repo, run `bin/setup` to install dependencies. Then, run `rake spec` to run the tests. You can also run `bin/console` for an interactive prompt that will allow you to experiment.
|
|
@@ -5,21 +5,38 @@ module Causeway
|
|
|
5
5
|
# Credentials and settings for each publisher. Values may be callables, resolved on read.
|
|
6
6
|
class Configuration
|
|
7
7
|
attr_writer :bluesky_handle, :bluesky_app_password, :mastodon_token, :mastodon_instance,
|
|
8
|
-
:linkedin_token, :linkedin_organization_id, :linkedin_person_id, :linkedin_api_version
|
|
8
|
+
:linkedin_token, :linkedin_organization_id, :linkedin_person_id, :linkedin_api_version,
|
|
9
|
+
:mastodon_max_length, :threads_token, :threads_user_id,
|
|
10
|
+
:threads_status_interval, :threads_status_attempts, :publishers, :template, :extra_tags, :truncate
|
|
9
11
|
|
|
10
12
|
def initialize
|
|
11
13
|
@mastodon_instance = "https://mastodon.social"
|
|
12
14
|
@linkedin_api_version = "202608"
|
|
15
|
+
@threads_status_interval = 5
|
|
16
|
+
@threads_status_attempts = 6
|
|
17
|
+
@publishers = %i[mastodon bluesky]
|
|
18
|
+
@template = "{description}\n\n{url}\n\n{tags}"
|
|
19
|
+
@extra_tags = []
|
|
20
|
+
@truncate = true
|
|
13
21
|
end
|
|
14
22
|
|
|
15
23
|
def bluesky_handle = resolve(@bluesky_handle)
|
|
16
24
|
def bluesky_app_password = resolve(@bluesky_app_password)
|
|
17
25
|
def mastodon_token = resolve(@mastodon_token)
|
|
18
26
|
def mastodon_instance = resolve(@mastodon_instance)
|
|
27
|
+
def mastodon_max_length = resolve(@mastodon_max_length)
|
|
19
28
|
def linkedin_token = resolve(@linkedin_token)
|
|
20
29
|
def linkedin_organization_id = resolve(@linkedin_organization_id)
|
|
21
30
|
def linkedin_person_id = resolve(@linkedin_person_id)
|
|
22
31
|
def linkedin_api_version = resolve(@linkedin_api_version)
|
|
32
|
+
def threads_token = resolve(@threads_token)
|
|
33
|
+
def threads_user_id = resolve(@threads_user_id)
|
|
34
|
+
def threads_status_interval = resolve(@threads_status_interval)
|
|
35
|
+
def threads_status_attempts = resolve(@threads_status_attempts)
|
|
36
|
+
def publishers = resolve(@publishers)
|
|
37
|
+
def template = resolve(@template)
|
|
38
|
+
def extra_tags = resolve(@extra_tags)
|
|
39
|
+
def truncate = resolve(@truncate)
|
|
23
40
|
|
|
24
41
|
private
|
|
25
42
|
|
|
@@ -6,19 +6,69 @@ require "uri"
|
|
|
6
6
|
|
|
7
7
|
module Causeway
|
|
8
8
|
module Publishers
|
|
9
|
-
# Interface and shared
|
|
9
|
+
# Interface and shared helpers for all publishers.
|
|
10
10
|
class Base
|
|
11
|
+
# A footer line is blank, or contains a link or a hashtag.
|
|
12
|
+
FOOTER_LINE = %r{\A\s*\z|https?://|(?<=\s|^)#\w+}
|
|
13
|
+
ELLIPSIS = "…"
|
|
14
|
+
|
|
11
15
|
def name
|
|
12
16
|
raise NotImplementedError
|
|
13
17
|
end
|
|
14
18
|
|
|
15
|
-
#
|
|
16
|
-
def
|
|
19
|
+
# The publisher's key in Causeway.configuration.publishers, e.g. :mastodon.
|
|
20
|
+
def key
|
|
21
|
+
self.class.name.split("::").last.downcase.to_sym
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
# Maximum post length, as measured by #text_length.
|
|
25
|
+
def max_length
|
|
17
26
|
raise NotImplementedError
|
|
18
27
|
end
|
|
19
28
|
|
|
29
|
+
# The length of text as the service counts it.
|
|
30
|
+
def text_length(text)
|
|
31
|
+
text.grapheme_clusters.size
|
|
32
|
+
end
|
|
33
|
+
|
|
34
|
+
# Shortens text to fit #max_length. The footer (the trailing lines that are blank or contain links or hashtags)
|
|
35
|
+
# is kept, and the text above it is cut. Without a footer that fits, the whole text is cut.
|
|
36
|
+
def truncate(text)
|
|
37
|
+
return text if text_length(text) <= max_length
|
|
38
|
+
|
|
39
|
+
body, footer = split_footer(text)
|
|
40
|
+
allowed = max_length - text_length(footer) - "#{ELLIPSIS}\n\n".size
|
|
41
|
+
return "#{cut(text, max_length - ELLIPSIS.size)}#{ELLIPSIS}" if footer.empty? || allowed <= 0
|
|
42
|
+
|
|
43
|
+
"#{cut(body, allowed)}#{ELLIPSIS}\n\n#{footer}"
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
# Posts text, as a reply to the post with the reply_to id if given.
|
|
47
|
+
# Returns { id:, url: } on success, { error: } on failure.
|
|
48
|
+
def post(text, reply_to: nil)
|
|
49
|
+
length = text_length(text)
|
|
50
|
+
return { error: "#{name} post is too long (#{length}/#{max_length})." } if length > max_length
|
|
51
|
+
|
|
52
|
+
publish(text, reply_to)
|
|
53
|
+
end
|
|
54
|
+
|
|
20
55
|
private
|
|
21
56
|
|
|
57
|
+
def publish(text, reply_to)
|
|
58
|
+
raise NotImplementedError
|
|
59
|
+
end
|
|
60
|
+
|
|
61
|
+
def split_footer(text)
|
|
62
|
+
lines = text.lines
|
|
63
|
+
last_body_line = lines.rindex { |line| !line.match?(FOOTER_LINE) }
|
|
64
|
+
footer_start = last_body_line ? last_body_line + 1 : lines.size - 1
|
|
65
|
+
[lines[0...footer_start].join, lines[footer_start..].join.strip]
|
|
66
|
+
end
|
|
67
|
+
|
|
68
|
+
def cut(text, length)
|
|
69
|
+
text.grapheme_clusters.take(length).join.strip
|
|
70
|
+
end
|
|
71
|
+
|
|
22
72
|
def config
|
|
23
73
|
Causeway.configuration
|
|
24
74
|
end
|
|
@@ -35,6 +85,12 @@ module Causeway
|
|
|
35
85
|
{ error: "#{name} API error: #{response.code} #{response.body}" }
|
|
36
86
|
end
|
|
37
87
|
|
|
88
|
+
def get(uri, headers = {})
|
|
89
|
+
request = Net::HTTP::Get.new(uri)
|
|
90
|
+
headers.each { |key, value| request[key] = value }
|
|
91
|
+
perform(uri, request)
|
|
92
|
+
end
|
|
93
|
+
|
|
38
94
|
def post_json(uri, body, headers = {})
|
|
39
95
|
request = Net::HTTP::Post.new(uri)
|
|
40
96
|
request["Content-Type"] = "application/json"
|
|
@@ -10,7 +10,13 @@ module Causeway
|
|
|
10
10
|
"Bluesky"
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def
|
|
13
|
+
def max_length
|
|
14
|
+
300
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def publish(text, reply_to)
|
|
14
20
|
handle = config.bluesky_handle
|
|
15
21
|
app_password = config.bluesky_app_password
|
|
16
22
|
|
|
@@ -20,10 +26,25 @@ module Causeway
|
|
|
20
26
|
session = create_session(handle, app_password)
|
|
21
27
|
return session if session[:error]
|
|
22
28
|
|
|
23
|
-
|
|
29
|
+
reply = reply_to && reply_ref(session, reply_to)
|
|
30
|
+
return reply if reply&.key?(:error)
|
|
31
|
+
|
|
32
|
+
create_post(text, handle, session, reply)
|
|
24
33
|
end
|
|
25
34
|
|
|
26
|
-
|
|
35
|
+
# The reply refs for a reply to the post at uri: its parent is that post, and its root is that post's root (or
|
|
36
|
+
# the post itself, if it isn't a reply).
|
|
37
|
+
def reply_ref(session, uri)
|
|
38
|
+
repo, collection, rkey = uri.delete_prefix("at://").split("/")
|
|
39
|
+
query = URI.encode_www_form(repo: repo, collection: collection, rkey: rkey)
|
|
40
|
+
response = get(URI("#{API}/xrpc/com.atproto.repo.getRecord?#{query}"),
|
|
41
|
+
"Authorization" => "Bearer #{session[:access_jwt]}")
|
|
42
|
+
return { error: "Bluesky reply target not found: #{response.code} #{response.body}" } unless success?(response)
|
|
43
|
+
|
|
44
|
+
data = JSON.parse(response.body)
|
|
45
|
+
parent = { "uri" => data["uri"], "cid" => data["cid"] }
|
|
46
|
+
{ "root" => data.dig("value", "reply", "root") || parent, "parent" => parent }
|
|
47
|
+
end
|
|
27
48
|
|
|
28
49
|
def create_session(handle, app_password)
|
|
29
50
|
uri = URI("#{API}/xrpc/com.atproto.server.createSession")
|
|
@@ -35,9 +56,9 @@ module Causeway
|
|
|
35
56
|
{ did: data["did"], access_jwt: data["accessJwt"] }
|
|
36
57
|
end
|
|
37
58
|
|
|
38
|
-
def create_post(text, handle, session)
|
|
59
|
+
def create_post(text, handle, session, reply)
|
|
39
60
|
uri = URI("#{API}/xrpc/com.atproto.repo.createRecord")
|
|
40
|
-
body = { "repo" => session[:did], "collection" => "app.bsky.feed.post", "record" => build_record(text) }
|
|
61
|
+
body = { "repo" => session[:did], "collection" => "app.bsky.feed.post", "record" => build_record(text, reply) }
|
|
41
62
|
response = post_json(uri, body, "Authorization" => "Bearer #{session[:access_jwt]}")
|
|
42
63
|
|
|
43
64
|
return api_error(response) unless success?(response)
|
|
@@ -47,7 +68,7 @@ module Causeway
|
|
|
47
68
|
{ id: data["uri"], url: "https://bsky.app/profile/#{handle}/post/#{rkey}" }
|
|
48
69
|
end
|
|
49
70
|
|
|
50
|
-
def build_record(text)
|
|
71
|
+
def build_record(text, reply)
|
|
51
72
|
record = {
|
|
52
73
|
"$type" => "app.bsky.feed.post",
|
|
53
74
|
"text" => text,
|
|
@@ -55,6 +76,7 @@ module Causeway
|
|
|
55
76
|
}
|
|
56
77
|
facets = detect_facets(text)
|
|
57
78
|
record["facets"] = facets unless facets.empty?
|
|
79
|
+
record["reply"] = reply if reply
|
|
58
80
|
record
|
|
59
81
|
end
|
|
60
82
|
|
|
@@ -10,7 +10,15 @@ module Causeway
|
|
|
10
10
|
"LinkedIn"
|
|
11
11
|
end
|
|
12
12
|
|
|
13
|
-
def
|
|
13
|
+
def max_length
|
|
14
|
+
3000
|
|
15
|
+
end
|
|
16
|
+
|
|
17
|
+
private
|
|
18
|
+
|
|
19
|
+
def publish(text, reply_to)
|
|
20
|
+
return { error: "LinkedIn does not support replies." } if reply_to
|
|
21
|
+
|
|
14
22
|
token = config.linkedin_token
|
|
15
23
|
author = author_urn
|
|
16
24
|
|
|
@@ -20,8 +28,6 @@ module Causeway
|
|
|
20
28
|
result(create_post(text, author, token))
|
|
21
29
|
end
|
|
22
30
|
|
|
23
|
-
private
|
|
24
|
-
|
|
25
31
|
def author_urn
|
|
26
32
|
return "urn:li:person:#{config.linkedin_person_id}" if present?(config.linkedin_person_id)
|
|
27
33
|
|
|
@@ -4,29 +4,62 @@ module Causeway
|
|
|
4
4
|
module Publishers
|
|
5
5
|
# Posts a status to a Mastodon instance.
|
|
6
6
|
class Mastodon < Base
|
|
7
|
+
# Mastodon's own defaults, used when the instance doesn't report its limits.
|
|
8
|
+
DEFAULT_LIMITS = { max_characters: 500, characters_reserved_per_url: 23 }.freeze
|
|
9
|
+
|
|
7
10
|
def name
|
|
8
11
|
"Mastodon"
|
|
9
12
|
end
|
|
10
13
|
|
|
11
|
-
|
|
14
|
+
# mastodon_max_length if set, otherwise the instance's own limit.
|
|
15
|
+
def max_length
|
|
16
|
+
config.mastodon_max_length || limits[:max_characters]
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
# Mastodon counts every link as the same number of characters (23 by default), whatever its real length.
|
|
20
|
+
def text_length(text)
|
|
21
|
+
super(text.gsub(%r{https?://\S+}, "x" * limits[:characters_reserved_per_url]))
|
|
22
|
+
end
|
|
23
|
+
|
|
24
|
+
private
|
|
25
|
+
|
|
26
|
+
def publish(text, reply_to)
|
|
12
27
|
token = config.mastodon_token
|
|
13
28
|
return { error: "Mastodon token not configured." } unless present?(token)
|
|
14
29
|
|
|
15
|
-
response = create_status(text, token)
|
|
30
|
+
response = create_status(text, token, reply_to)
|
|
16
31
|
return api_error(response) unless success?(response)
|
|
17
32
|
|
|
18
33
|
data = JSON.parse(response.body)
|
|
19
34
|
{ id: data["id"], url: data["url"] }
|
|
20
35
|
end
|
|
21
36
|
|
|
22
|
-
|
|
37
|
+
def instance
|
|
38
|
+
config.mastodon_instance || "https://mastodon.social"
|
|
39
|
+
end
|
|
40
|
+
|
|
41
|
+
# The instance's status limits, fetched once from its public instance API, falling back to DEFAULT_LIMITS.
|
|
42
|
+
def limits
|
|
43
|
+
@limits ||= DEFAULT_LIMITS.merge(fetch_limits)
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def fetch_limits
|
|
47
|
+
response = get(URI("#{instance}/api/v2/instance"))
|
|
48
|
+
return {} unless success?(response)
|
|
49
|
+
|
|
50
|
+
statuses = JSON.parse(response.body).dig("configuration", "statuses") || {}
|
|
51
|
+
statuses.slice("max_characters", "characters_reserved_per_url").transform_keys(&:to_sym)
|
|
52
|
+
rescue StandardError
|
|
53
|
+
{}
|
|
54
|
+
end
|
|
23
55
|
|
|
24
|
-
def create_status(text, token)
|
|
25
|
-
instance = config.mastodon_instance || "https://mastodon.social"
|
|
56
|
+
def create_status(text, token, reply_to)
|
|
26
57
|
uri = URI("#{instance}/api/v1/statuses")
|
|
27
58
|
request = Net::HTTP::Post.new(uri)
|
|
28
59
|
request["Authorization"] = "Bearer #{token}"
|
|
29
|
-
|
|
60
|
+
form = { "status" => text }
|
|
61
|
+
form["in_reply_to_id"] = reply_to if reply_to
|
|
62
|
+
request.set_form_data(form)
|
|
30
63
|
perform(uri, request)
|
|
31
64
|
end
|
|
32
65
|
end
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module Causeway
|
|
4
|
+
module Publishers
|
|
5
|
+
# Posts to Threads: creates a text container, waits for it to be ready, publishes it, then looks up its link.
|
|
6
|
+
class Threads < Base
|
|
7
|
+
API = "https://graph.threads.com/v1.0"
|
|
8
|
+
|
|
9
|
+
class Failure < StandardError; end
|
|
10
|
+
|
|
11
|
+
def name
|
|
12
|
+
"Threads"
|
|
13
|
+
end
|
|
14
|
+
|
|
15
|
+
def max_length
|
|
16
|
+
500
|
|
17
|
+
end
|
|
18
|
+
|
|
19
|
+
private
|
|
20
|
+
|
|
21
|
+
def publish(text, reply_to)
|
|
22
|
+
return { error: "Threads token not configured." } unless present?(config.threads_token)
|
|
23
|
+
return { error: "Threads user ID not configured." } unless present?(config.threads_user_id)
|
|
24
|
+
|
|
25
|
+
container_id = create_container(text, reply_to)
|
|
26
|
+
wait_until_ready(container_id)
|
|
27
|
+
media_id = publish_container(container_id)
|
|
28
|
+
{ id: media_id, url: permalink(media_id) }
|
|
29
|
+
rescue Failure => e
|
|
30
|
+
{ error: e.message }
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
def create_container(text, reply_to)
|
|
34
|
+
params = { media_type: "TEXT", text: text }
|
|
35
|
+
params[:reply_to_id] = reply_to if reply_to
|
|
36
|
+
request(:post, "#{config.threads_user_id}/threads", params)["id"]
|
|
37
|
+
end
|
|
38
|
+
|
|
39
|
+
def publish_container(container_id)
|
|
40
|
+
request(:post, "#{config.threads_user_id}/threads_publish", creation_id: container_id)["id"]
|
|
41
|
+
end
|
|
42
|
+
|
|
43
|
+
def permalink(media_id)
|
|
44
|
+
request(:get, media_id, fields: "permalink")["permalink"]
|
|
45
|
+
end
|
|
46
|
+
|
|
47
|
+
def wait_until_ready(container_id)
|
|
48
|
+
data = container_status(container_id)
|
|
49
|
+
(config.threads_status_attempts - 1).times do
|
|
50
|
+
break unless data["status"] == "IN_PROGRESS"
|
|
51
|
+
|
|
52
|
+
sleep(config.threads_status_interval)
|
|
53
|
+
data = container_status(container_id)
|
|
54
|
+
end
|
|
55
|
+
raise Failure, container_error(data) unless data["status"] == "FINISHED"
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def container_status(container_id)
|
|
59
|
+
request(:get, container_id, fields: "status,error_message")
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
def container_error(data)
|
|
63
|
+
if data["status"] == "IN_PROGRESS"
|
|
64
|
+
"Threads container not ready after #{config.threads_status_attempts} checks."
|
|
65
|
+
else
|
|
66
|
+
"Threads container #{data["status"]}: #{data["error_message"]}"
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def request(method, path, params)
|
|
71
|
+
uri = URI("#{API}/#{path}")
|
|
72
|
+
uri.query = URI.encode_www_form(params.merge(access_token: config.threads_token))
|
|
73
|
+
request = method == :get ? Net::HTTP::Get.new(uri) : Net::HTTP::Post.new(uri)
|
|
74
|
+
response = perform(uri, request)
|
|
75
|
+
raise Failure, api_error(response)[:error] unless success?(response)
|
|
76
|
+
|
|
77
|
+
JSON.parse(response.body)
|
|
78
|
+
end
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
data/lib/causeway/version.rb
CHANGED
data/lib/causeway.rb
CHANGED
|
@@ -6,7 +6,44 @@ require_relative "causeway/publishers/base"
|
|
|
6
6
|
require_relative "causeway/publishers/bluesky"
|
|
7
7
|
require_relative "causeway/publishers/linkedin"
|
|
8
8
|
require_relative "causeway/publishers/mastodon"
|
|
9
|
+
require_relative "causeway/publishers/threads"
|
|
9
10
|
|
|
11
|
+
# Cross-posts text to social networks.
|
|
10
12
|
module Causeway
|
|
11
13
|
class Error < StandardError; end
|
|
14
|
+
|
|
15
|
+
PUBLISHERS = {
|
|
16
|
+
bluesky: Publishers::Bluesky,
|
|
17
|
+
linkedin: Publishers::LinkedIn,
|
|
18
|
+
mastodon: Publishers::Mastodon,
|
|
19
|
+
threads: Publishers::Threads
|
|
20
|
+
}.freeze
|
|
21
|
+
|
|
22
|
+
class << self
|
|
23
|
+
# Builds post text from the template (configuration.template unless given), replacing {description}, {url}, and
|
|
24
|
+
# {tags}. Tags may be an array or a space-separated string; they and configuration.extra_tags become hashtags.
|
|
25
|
+
def compose(description: "", url: "", tags: [], template: configuration.template)
|
|
26
|
+
tags = tags.split if tags.is_a?(String)
|
|
27
|
+
hashtags = (tags + configuration.extra_tags).map { |tag| "##{tag.delete_prefix("#")}" }.uniq.join(" ")
|
|
28
|
+
template.gsub("{description}", description.to_s).gsub("{url}", url.to_s).gsub("{tags}", hashtags)
|
|
29
|
+
.gsub(/\n{3,}/, "\n\n").strip
|
|
30
|
+
end
|
|
31
|
+
|
|
32
|
+
# The configured publishers, in order.
|
|
33
|
+
def publishers
|
|
34
|
+
configuration.publishers.map { |key| PUBLISHERS.fetch(key).new }
|
|
35
|
+
end
|
|
36
|
+
|
|
37
|
+
# Posts text to each configured publisher not in skip, truncating it to fit when configuration.truncate is set.
|
|
38
|
+
# reply_to maps a publisher key to the id of the post to reply to there. Yields each publisher and its result as
|
|
39
|
+
# it goes, and returns the results by publisher key.
|
|
40
|
+
def post(text, reply_to: {}, skip: [])
|
|
41
|
+
publishers.reject { |publisher| skip.include?(publisher.key) }.to_h do |publisher|
|
|
42
|
+
body = configuration.truncate ? publisher.truncate(text) : text
|
|
43
|
+
result = publisher.post(body, reply_to: reply_to[publisher.key])
|
|
44
|
+
yield publisher, result if block_given?
|
|
45
|
+
[publisher.key, result]
|
|
46
|
+
end
|
|
47
|
+
end
|
|
48
|
+
end
|
|
12
49
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: causeway
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.2.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- Marc Heiligers
|
|
@@ -27,6 +27,7 @@ files:
|
|
|
27
27
|
- lib/causeway/publishers/bluesky.rb
|
|
28
28
|
- lib/causeway/publishers/linkedin.rb
|
|
29
29
|
- lib/causeway/publishers/mastodon.rb
|
|
30
|
+
- lib/causeway/publishers/threads.rb
|
|
30
31
|
- lib/causeway/version.rb
|
|
31
32
|
- sig/causeway.rbs
|
|
32
33
|
homepage: https://github.com/FASCINATION-works/causeway.
|