Toot toooooooot (Bluesky-Mastodon cross-poster)

added support for copying videos

Changed files
+48 -5
app
+14 -1
app/mastodon_api.rb
··· 22 22 end 23 23 end 24 24 25 + MEDIA_CHECK_INTERVAL = 5.0 26 + 25 27 attr_accessor :access_token 26 28 27 29 def initialize(host, access_token = nil) ··· 82 84 http.request(request) 83 85 end 84 86 85 - if response.code.to_i / 100 == 2 87 + json = if response.code.to_i / 100 == 2 86 88 JSON.parse(response.body) 87 89 else 88 90 raise APIError.new(response) 89 91 end 92 + 93 + while json['url'].nil? 94 + sleep MEDIA_CHECK_INTERVAL 95 + json = get_media(json['id']) 96 + end 97 + 98 + json 99 + end 100 + 101 + def get_media(media_id) 102 + get_json("/media/#{media_id}") 90 103 end 91 104 92 105 def get_json(path, params = {})
+34 -4
app/tootify.rb
··· 128 128 if images = attached_images(record) 129 129 media_ids = [] 130 130 131 - images.each do |image| 132 - alt = image['alt'] 133 - cid = image['image']['ref']['$link'] 134 - mime = image['image']['mimeType'] 131 + images.each do |embed| 132 + alt = embed['alt'] 133 + cid = embed['image']['ref']['$link'] 134 + mime = embed['image']['mimeType'] 135 135 136 136 if alt.length > @mastodon.max_alt_length 137 137 alt = alt[0...@mastodon.max_alt_length - 3] + "(…)" ··· 142 142 uploaded_media = @mastodon.upload_media(data, cid, mime, alt) 143 143 media_ids << uploaded_media['id'] 144 144 end 145 + elsif embed = attached_video(record) 146 + alt = embed['alt'] 147 + cid = embed['video']['ref']['$link'] 148 + mime = embed['video']['mimeType'] 149 + 150 + if alt.length > @mastodon.max_alt_length 151 + alt = alt[0...@mastodon.max_alt_length - 3] + "(…)" 152 + end 153 + 154 + data = @bluesky.fetch_blob(cid) 155 + 156 + uploaded_media = @mastodon.upload_media(data, cid, mime, alt) 157 + media_ids = [uploaded_media['id']] 145 158 end 146 159 147 160 if tags = record['tags'] ··· 205 218 when 'app.bsky.embed.recordWithMedia' 206 219 if embed['media']['$type'] == 'app.bsky.embed.images' 207 220 embed['media']['images'] 221 + else 222 + nil 223 + end 224 + else 225 + nil 226 + end 227 + end 228 + end 229 + 230 + def attached_video(record) 231 + if embed = record['embed'] 232 + case embed['$type'] 233 + when 'app.bsky.embed.video' 234 + embed 235 + when 'app.bsky.embed.recordWithMedia' 236 + if embed['media']['$type'] == 'app.bsky.embed.video' 237 + embed['media'] 208 238 else 209 239 nil 210 240 end