Browse files

Split.split/2 duplicates leading mentions across splits

When posting a longer update in response to an update on Twitter or
Mastodon, the mention at the front of the update wasn't duplicated
across the splits.

On Twitter, this caused the first tweet to mention the author of the
tweet it's replying to, but the rest of the updates were replies to each
other, and dropped the mention.

This patch duplicates any mentions from the start of the update across
all splits, so each one mentions the user the first one is replying to.
  • Loading branch information...
jeffkreeftmeijer committed Jan 13, 2019
1 parent c5d1234 commit 95a8273c64e5051d0fdab528fe8b6aed7cc30787
Showing with 27 additions and 8 deletions.
  1. +17 −8 lib/dolphin/update/split.ex
  2. +10 −0 test/dolphin/update/split_test.exs
@@ -1,36 +1,45 @@
defmodule Dolphin.Update.Split do
def split(text, max) do
{mentions, content} =
case^(\@[\w_]+\s)*/, text) do
[match, _] ->
{match, String.replace_leading(text, match, "")}

_ ->
{"", text}

splits =
|> String.split(~r/(\r?\n){3}/)
|> Enum.flat_map(fn break ->
|> String.split(~r/(\r?\n){2}/)
|> join!(max, [])
|> join!(max, [], mentions)
|> Enum.reverse()


defp join!([head | tail], max, [previous | rest] = acc) do
defp join!([head | tail], max, [previous | rest] = acc, prefix) do
joined = previous <> "\n\n" <> head

if joined
|> shorten_urls
|> remove_local_images
|> String.length() <= max do
join!(tail, max, [joined | rest])
join!(tail, max, [joined | rest], prefix)
join!(tail, max, [head | acc])
join!(tail, max, [prefix <> head | acc], prefix)

defp join!([head | tail], max, acc) do
join!(tail, max, [head | acc])
defp join!([head | tail], max, acc, prefix) do
join!(tail, max, [prefix <> head | acc], prefix)

defp join!([], _max, acc), do: acc
defp join!([], _max, acc, _prefix), do: acc

defp shorten_urls(text) do
Regex.replace(~r/https?:\/\/[\w\.\/-]+/, text, &String.slice(&1, 0..22))
@@ -61,4 +61,14 @@ defmodule Dolphin.Update.SplitTest do

assert [text] = Split.split(text, 14)

test "duplicates mentions across splits" do
text = """
@jvanbaarsen Contexts provide the API to interact with your schemas to keep knowledge about your repository out of the rest of your app.
While they can group functions for multiple schemas together (like in the Accounts example), you’re encouraged to start with one context per schema if it’s not immediately clear what the groups could be.

assert ["@jvanbaarsen Contexts" <> _, "@jvanbaarsen While" <> _] = Split.split(text, 280)

0 comments on commit 95a8273

Please sign in to comment.