+ def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
+ object = Object.get_by_ap_id(object["id"])
+
+ {:ok, _like_activity, object} = ActivityPub.like(user, object)
+ new_data = activity.data
+ |> Map.put("object", object.data)
+
+ status = %{activity | data: new_data}
+ |> activity_to_status(%{for: user})
+
+ {:ok, status}
+ end
+
+ def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
+ object = Object.get_by_ap_id(object["id"])
+
+ {:ok, object} = ActivityPub.unlike(user, object)
+ new_data = activity.data
+ |> Map.put("object", object.data)
+
+ status = %{activity | data: new_data}
+ |> activity_to_status(%{for: user})
+
+ {:ok, status}
+ end
+
+ def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
+ object = Object.get_by_ap_id(object["id"])
+
+ {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
+ new_data = activity.data
+ |> Map.put("object", object.data)
+
+ status = %{activity | data: new_data}
+ |> activity_to_status(%{for: user})
+
+ {:ok, status}
+ end
+
+ def upload(%Plug.Upload{} = file, format \\ "xml") do
+ {:ok, object} = ActivityPub.upload(file)
+
+ url = List.first(object.data["url"])
+ href = url["href"]
+ type = url["mediaType"]
+
+ case format do
+ "xml" ->
+ # Fake this as good as possible...
+ """
+ <?xml version="1.0" encoding="UTF-8"?>
+ <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
+ <mediaid>#{object.id}</mediaid>
+ <media_id>#{object.id}</media_id>
+ <media_id_string>#{object.id}</media_id_string>
+ <media_url>#{href}</media_url>
+ <mediaurl>#{href}</mediaurl>
+ <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
+ </rsp>
+ """
+ "json" ->
+ %{
+ media_id: object.id,
+ media_id_string: "#{object.id}}",
+ media_url: href,
+ size: 0
+ } |> Poison.encode!
+ end
+ end
+
+ def parse_mentions(text) do
+ # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
+ regex = ~r/@[a-zA-Z0-9.!#$%&'*+\/=?^_`{|}~-]+@?[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?(?:\.[a-zA-Z0-9](?:[a-zA-Z0-9-]{0,61}[a-zA-Z0-9])?)*/
+
+ Regex.scan(regex, text)
+ |> List.flatten
+ |> Enum.uniq
+ |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
+ |> Enum.filter(fn ({_match, user}) -> user end)
+ end
+
+ def add_user_links(text, mentions) do
+ mentions = mentions
+ |> Enum.sort_by(fn ({name, _}) -> -String.length(name) end)
+ |> Enum.map(fn({name, user}) -> {name, user, Ecto.UUID.generate} end)
+
+ # This replaces the mention with a unique reference first so it doesn't
+ # contain parts of other replaced mentions. There probably is a better
+ # solution for this...
+ step_one = mentions
+ |> Enum.reduce(text, fn ({match, _user, uuid}, text) ->
+ String.replace(text, match, uuid)
+ end)
+
+ Enum.reduce(mentions, step_one, fn ({match, %User{ap_id: ap_id}, uuid}, text) ->
+ String.replace(text, uuid, "<a href='#{ap_id}'>#{match}</a>")
+ end)
+ end
+
+ def register_user(params) do
+ params = %{
+ nickname: params["nickname"],
+ name: params["fullname"],
+ bio: params["bio"],
+ email: params["email"],
+ password: params["password"],
+ password_confirmation: params["confirm"]
+ }
+
+ changeset = User.register_changeset(%User{}, params)
+
+ with {:ok, user} <- Repo.insert(changeset) do
+ {:ok, UserRepresenter.to_map(user)}
+ else
+ {:error, changeset} ->
+ errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
+ |> Poison.encode!
+ {:error, %{error: errors}}
+ end
+ end
+
+ def get_user(user \\ nil, params) do
+ case params do
+ %{"user_id" => user_id} ->
+ case target = Repo.get(User, user_id) do
+ nil ->
+ {:error, "No user with such user_id"}
+ _ ->
+ {:ok, target}
+ end
+ %{"screen_name" => nickname} ->
+ case target = Repo.get_by(User, nickname: nickname) do
+ nil ->
+ {:error, "No user with such screen_name"}
+ _ ->
+ {:ok, target}
+ end
+ _ ->
+ if user do
+ {:ok, user}
+ else
+ {:error, "You need to specify screen_name or user_id"}
+ end
+ end
+ end
+
+ defp activities_to_statuses(activities, opts) do