Browse Source

Replying to already cross-posted toot

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
6 6
 	class Mastodon
7 7
 		def initialize(config)
8 8
 			settings = config[:settings]
9
+			@posts   = config[:posts]
9 10
 
10 11
 			url   = settings['mastodon.url']
11 12
 			token = settings['mastodon.token']
@@ -34,7 +35,7 @@ class CrossPost
34 35
 					end
35 36
 				rescue => e
36 37
 					LOGGER.error e
37
-					#raise
38
+					raise
38 39
 				end
39 40
 			end
40 41
 		end
@@ -42,9 +43,11 @@ class CrossPost
42 43
 		private
43 44
 
44 45
 		def reject?(status)
45
-			status.account.url != @user_url or
46
-					status.visibility != 'public' or
47
-					status.in_reply_to_id
46
+			return true if status.account.url != @user_url or
47
+					status.visibility != 'public'
48
+			reply = status.in_reply_to_id
49
+			return true if reply and !@posts[reply]
50
+			false
48 51
 		end
49 52
 	end
50 53
 end

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

@@ -2,11 +2,15 @@ require 'twitter'
2 2
 require 'twitter-text'
3 3
 require 'sanitize'
4 4
 require 'cgi'
5
+require 'ostruct'
6
+
7
+::Twitter::Validation::MAX_LENGTH = 280
5 8
 
6 9
 class CrossPost
7 10
 	class Twitter
8 11
 		def initialize(config)
9 12
 			settings = config[:settings]
13
+			@posts   = config[:posts]
10 14
 
11 15
 			config  = {
12 16
 					consumer_key:        settings['twitter.consumer.key'],
@@ -18,17 +22,19 @@ class CrossPost
18 22
 			@stream = ::Twitter::Streaming::Client.new config
19 23
 		end
20 24
 
21
-		def post(content, media = [])
22
-			media = media.collect { |f| @client.upload f }
25
+		def post(content, media = [], id:, reply_to:)
26
+			reply_to = OpenStruct.new id: reply_to unless reply_to.respond_to? :id
23 27
 
28
+			media = media.collect { |f| @client.upload f }
24 29
 			parts = split content
25
-			last  = nil
26
-
27 30
 			unless media.empty?
28 31
 				first, *parts = parts
29
-				last          = @client.update first, media_ids: media.join(',')
32
+				reply_to      = @client.update first, media_ids: media.join(','), in_reply_to_status: reply_to
30 33
 			end
31
-			parts.each { |p| last = @client.update p, in_reply_to_status: last }
34
+			parts.each { |p| reply_to = @client.update p, in_reply_to_status: reply_to }
35
+
36
+			reply_to = reply_to.id if reply_to.respond_to? :id
37
+			@posts.put id, reply_to, save: true
32 38
 		end
33 39
 
34 40
 		WHITESPACE_TAGS = {
@@ -47,7 +53,9 @@ class CrossPost
47 53
 			LOGGER.debug { "  Content: #{content}" }
48 54
 			LOGGER.debug { "  Attachments: #{media.size}" }
49 55
 
50
-			self.post content, media
56
+			reply   = status.in_reply_to_id
57
+			reply_to = reply ? @posts[reply] : nil
58
+			self.post content, media, id: status.id, reply_to: reply_to
51 59
 
52 60
 			media.each do |f|
53 61
 				f.close

Loading…
Cancel
Save