- def to_for_user_and_mentions(user, mentions) do
- default_to = [
- User.ap_followers(user),
- "https://www.w3.org/ns/activitystreams#Public"
- ]
-
- default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
- end
-
- def format_input(text, mentions) do
- HtmlSanitizeEx.strip_tags(text)
- |> String.replace("\n", "<br>")
- |> add_user_links(mentions)
- end
-
- def attachments_from_ids(ids) do
- Enum.map(ids || [], fn (media_id) ->
- Repo.get(Object, media_id).data
- end)
- end
-
- def get_replied_to_activity(id) when not is_nil(id) do
- Repo.get(Activity, id)
- end
-
- def get_replied_to_activity(_), do: nil
-
- def add_attachments(text, attachments) do
- attachment_text = Enum.map(attachments, fn
- (%{"url" => [%{"href" => href} | _]}) ->
- "<a href='#{href}'>#{href}</a>"
- _ -> ""
- end)
- Enum.join([text | attachment_text], "<br>")
- end
-
- def create_status(%User{} = user, %{"status" => status} = data) do
- attachments = attachments_from_ids(data["media_ids"])
- context = ActivityPub.generate_context_id
- mentions = parse_mentions(status)
- content_html = status
- |> format_input(mentions)
- |> add_attachments(attachments)
-
- to = to_for_user_and_mentions(user, mentions)
- date = make_date()
-
- inReplyTo = get_replied_to_activity(data["in_reply_to_status_id"])
-
- # Wire up reply info.
- [to, context, object, additional] =
- if inReplyTo do
- context = inReplyTo.data["context"]
- to = to ++ [inReplyTo.data["actor"]]
-
- object = %{
- "type" => "Note",
- "to" => to,
- "content" => content_html,
- "published" => date,
- "context" => context,
- "attachment" => attachments,
- "actor" => user.ap_id,
- "inReplyTo" => inReplyTo.data["object"]["id"],
- "inReplyToStatusId" => inReplyTo.id,
- }
- additional = %{}
-
- [to, context, object, additional]
- else
- object = %{
- "type" => "Note",
- "to" => to,
- "content" => content_html,
- "published" => date,
- "context" => context,
- "attachment" => attachments,
- "actor" => user.ap_id
- }
- [to, context, object, %{}]
- end