diff options
Diffstat (limited to 'lib/plugins/link/twitter.ex')
-rw-r--r-- | lib/plugins/link/twitter.ex | 127 |
1 files changed, 76 insertions, 51 deletions
diff --git a/lib/plugins/link/twitter.ex b/lib/plugins/link/twitter.ex index 48e6bae..ac2efe7 100644 --- a/lib/plugins/link/twitter.ex +++ b/lib/plugins/link/twitter.ex @@ -22,12 +22,15 @@ defmodule Nola.Plugins.Link.Twitter do * `expand_quoted`: Add the quoted tweet instead of its URL. Default: true. """ - def match(uri = %URI{host: twitter, path: path}, _opts) when twitter in ["twitter.com", "m.twitter.com", "mobile.twitter.com"] do + def match(uri = %URI{host: twitter, path: path}, _opts) + when twitter in ["twitter.com", "m.twitter.com", "mobile.twitter.com"] do case String.split(path, "/", parts: 4) do ["", _username, "status", status_id] -> {status_id, _} = Integer.parse(status_id) {true, %{status_id: status_id}} - _ -> false + + _ -> + false end end @@ -62,56 +65,75 @@ defmodule Nola.Plugins.Link.Twitter do # Format tweet text text = expand_twitter_text(tweet, opts) - text = if tweet.quoted_status do - quote_url = link_tweet(tweet.quoted_status, opts, true) - String.replace(text, quote_url, "") - else - text - end + + text = + if tweet.quoted_status do + quote_url = link_tweet(tweet.quoted_status, opts, true) + String.replace(text, quote_url, "") + else + text + end + text = Nola.Irc.Message.splitlong(text) - reply_to = if tweet.in_reply_to_status_id do - reply_url = link_tweet({tweet.in_reply_to_screen_name, tweet.in_reply_to_status_id}, opts) - text = if tweet.in_reply_to_screen_name == tweet.user.screen_name, do: "continued from", else: "replying to" - <<3, 15, " ↪ ", text::binary, " ", reply_url::binary, 3>> - end + reply_to = + if tweet.in_reply_to_status_id do + reply_url = link_tweet({tweet.in_reply_to_screen_name, tweet.in_reply_to_status_id}, opts) - quoted = if tweet.quoted_status do - full_text = tweet.quoted_status - |> expand_twitter_text(opts) - |> Nola.Irc.Message.splitlong_with_prefix(">") + text = + if tweet.in_reply_to_screen_name == tweet.user.screen_name, + do: "continued from", + else: "replying to" - head = format_tweet_header(tweet.quoted_status, opts, details: false, prefix: "↓ quoting") + <<3, 15, " ↪ ", text::binary, " ", reply_url::binary, 3>> + end - [head | full_text] - else - [] - end + quoted = + if tweet.quoted_status do + full_text = + tweet.quoted_status + |> expand_twitter_text(opts) + |> Nola.Irc.Message.splitlong_with_prefix(">") + + head = format_tweet_header(tweet.quoted_status, opts, details: false, prefix: "↓ quoting") + + [head | full_text] + else + [] + end - #<<2, "#{tweet.user.name} (@#{tweet.user.screen_name})", 2, " ", 3, 61, "#{foot} #{nitter_link}", 3>>, reply_to] ++ text ++ quoted + # <<2, "#{tweet.user.name} (@#{tweet.user.screen_name})", 2, " ", 3, 61, "#{foot} #{nitter_link}", 3>>, reply_to] ++ text ++ quoted + + text = + ([head, reply_to | text] ++ quoted) + |> Enum.filter(& &1) - text = [head, reply_to | text] ++ quoted - |> Enum.filter(& &1) {:ok, text} end defp expand_twitter_text(tweet, _opts) do - text = Enum.reduce(tweet.entities.urls, tweet.full_text, fn(entity, text) -> - String.replace(text, entity.url, entity.expanded_url) - end) + text = + Enum.reduce(tweet.entities.urls, tweet.full_text, fn entity, text -> + String.replace(text, entity.url, entity.expanded_url) + end) + extended = tweet.extended_entities || %{media: []} - text = Enum.reduce(extended.media, text, fn(entity, text) -> - url = Enum.filter(extended.media, fn(e) -> entity.url == e.url end) - |> Enum.map(fn(e) -> - cond do - e.type == "video" -> e.expanded_url - true -> e.media_url_https - end + + text = + Enum.reduce(extended.media, text, fn entity, text -> + url = + Enum.filter(extended.media, fn e -> entity.url == e.url end) + |> Enum.map(fn e -> + cond do + e.type == "video" -> e.expanded_url + true -> e.media_url_https + end + end) + |> Enum.join(" ") + + String.replace(text, entity.url, url) end) - |> Enum.join(" ") - String.replace(text, entity.url, url) - end) - |> HtmlEntities.decode() + |> HtmlEntities.decode() end defp format_tweet_header(tweet, opts, format_opts \\ []) do @@ -134,25 +156,28 @@ defmodule Nola.Plugins.Link.Twitter do replies = if tweet.reply_count && tweet.reply_count > 0, do: "#{tweet.reply_count} Reps" dmcad = if tweet.withheld_copyright, do: <<3, 52, "DMCA", 3>> - withheld_local = if tweet.withheld_in_countries && length(tweet.withheld_in_countries) > 0 do - "Withheld in #{length(tweet.withheld_in_countries)} countries" - end + + withheld_local = + if tweet.withheld_in_countries && length(tweet.withheld_in_countries) > 0 do + "Withheld in #{length(tweet.withheld_in_countries)} countries" + end verified = if tweet.user.verified, do: <<3, 51, "✔", 3>> - meta = if details do - [verified, nsfw, formatted_time, dmcad, withheld_local, rts, qrts, likes, replies] - else - [verified, nsfw, formatted_time, dmcad, withheld_local] - end + meta = + if details do + [verified, nsfw, formatted_time, dmcad, withheld_local, rts, qrts, likes, replies] + else + [verified, nsfw, formatted_time, dmcad, withheld_local] + end - meta = meta - |> Enum.filter(& &1) - |> Enum.join(" - ") + meta = + meta + |> Enum.filter(& &1) + |> Enum.join(" - ") meta = <<3, 15, meta::binary, " → #{link}", 3>> <<author::binary, " — ", meta::binary>> end - end |