This repository has been archived by the owner on May 26, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 58
/
mastodon_ext.rb
77 lines (75 loc) · 2.1 KB
/
mastodon_ext.rb
1
2
3
4
5
6
7
8
9
10
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
45
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
75
76
77
module Mastodon
class Status
MASTODON_REGEX = /(?<domain>[^\/]+)\/@(?<username>.+)/
PLEROMA_REGEX = /(?<domain>[^\/]+)\/users\/(?<username>.+)/
OTHER_REGEX = /(?<domain>[^\/]+)\/(?<username>.+)/
MENTION_REGEX = /(<a href="https:\/\/(?<mention>[^"]+)" [^>]*class=\"u-url mention\">@<span>[^>]+<\/span><\/a>)/
def is_reblog?
begin
_ = reblog
true
rescue NoMethodError
false
end
end
def is_reply?
in_reply_to_id.nil? == false
end
def in_reply_to_account_id
self.attributes["in_reply_to_account_id"]
end
def visibility
self.attributes["visibility"]
end
def application
self.attributes["application"]
end
def is_mention?
text_content[0] == "@"
end
def is_direct?
visibility == "direct"
end
def is_unlisted?
visibility == "unlisted"
end
def is_private?
visibility == "private"
end
def is_public?
!(is_private? || is_unlisted? || is_direct?)
end
def sensitive?
self.attributes["sensitive"]
end
def spoiler_text
self.attributes["spoiler_text"]
end
def self.scrubber
@@scrubber ||= Rails::Html::PermitScrubber.new
@@scrubber.tags = ["br", "p"]
@@scrubber
end
def self.html_entities
@@html_entities ||= HTMLEntities.new
end
def text_content
return @text_content if @text_content
temp_content = content.dup
while mention_m = temp_content.match(MENTION_REGEX)
username_m = mention_m[:mention].match(MASTODON_REGEX) ||
mention_m[:mention].match(PLEROMA_REGEX) ||
mention_m[:mention].match(OTHER_REGEX)
temp_content.gsub!(mention_m[0], "@#{username_m[:username]}@#{username_m[:domain]}")
end
@text_content = Loofah.fragment(temp_content).scrub!(Status.scrubber).to_s
@text_content.gsub!("<br>", "\n")
@text_content.gsub!("</p><p>", "\n\n")
@text_content.gsub!(/(^<p>|<\/p>$)/, "")
@text_content = Status.html_entities.decode(@text_content)
end
end
class Client
attr_writer :user_agent
end
end