Browse Source

Replying to already cross-posted toot

tags/v0.2.0
aeris 1 year ago
parent
commit
72ce72fa2c
2 changed files with 22 additions and 11 deletions
  1. 7
    4
      lib/cross-post/mastodon.rb
  2. 15
    7
      lib/cross-post/twitter.rb

+ 7
- 4
lib/cross-post/mastodon.rb View File

@@ -6,6 +6,7 @@ class CrossPost
class Mastodon
def initialize(config)
settings = config[:settings]
@posts = config[:posts]

url = settings['mastodon.url']
token = settings['mastodon.token']
@@ -34,7 +35,7 @@ class CrossPost
end
rescue => e
LOGGER.error e
#raise
raise
end
end
end
@@ -42,9 +43,11 @@ class CrossPost
private

def reject?(status)
status.account.url != @user_url or
status.visibility != 'public' or
status.in_reply_to_id
return true if status.account.url != @user_url or
status.visibility != 'public'
reply = status.in_reply_to_id
return true if reply and !@posts[reply]
false
end
end
end

+ 15
- 7
lib/cross-post/twitter.rb View File

@@ -2,11 +2,15 @@ require 'twitter'
require 'twitter-text'
require 'sanitize'
require 'cgi'
require 'ostruct'

::Twitter::Validation::MAX_LENGTH = 280

class CrossPost
class Twitter
def initialize(config)
settings = config[:settings]
@posts = config[:posts]

config = {
consumer_key: settings['twitter.consumer.key'],
@@ -18,17 +22,19 @@ class CrossPost
@stream = ::Twitter::Streaming::Client.new config
end

def post(content, media = [])
media = media.collect { |f| @client.upload f }
def post(content, media = [], id:, reply_to:)
reply_to = OpenStruct.new id: reply_to unless reply_to.respond_to? :id

media = media.collect { |f| @client.upload f }
parts = split content
last = nil

unless media.empty?
first, *parts = parts
last = @client.update first, media_ids: media.join(',')
reply_to = @client.update first, media_ids: media.join(','), in_reply_to_status: reply_to
end
parts.each { |p| last = @client.update p, in_reply_to_status: last }
parts.each { |p| reply_to = @client.update p, in_reply_to_status: reply_to }

reply_to = reply_to.id if reply_to.respond_to? :id
@posts.put id, reply_to, save: true
end

WHITESPACE_TAGS = {
@@ -47,7 +53,9 @@ class CrossPost
LOGGER.debug { " Content: #{content}" }
LOGGER.debug { " Attachments: #{media.size}" }

self.post content, media
reply = status.in_reply_to_id
reply_to = reply ? @posts[reply] : nil
self.post content, media, id: status.id, reply_to: reply_to

media.each do |f|
f.close

Loading…
Cancel
Save