1 defmodule Pleroma.Web.TwitterAPI.TwitterAPI do
2 alias Pleroma.{User, Activity, Repo, Object}
3 alias Pleroma.Web.ActivityPub.ActivityPub
4 alias Pleroma.Web.TwitterAPI.Representers.{ActivityRepresenter, UserRepresenter}
8 def to_for_user_and_mentions(user, mentions) do
10 User.ap_followers(user),
11 "https://www.w3.org/ns/activitystreams#Public"
14 default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
17 def format_input(text, mentions) do
18 HtmlSanitizeEx.strip_tags(text)
19 |> String.replace("\n", "<br>")
20 |> add_user_links(mentions)
23 def attachments_from_ids(ids) do
24 Enum.map(ids || [], fn (media_id) ->
25 Repo.get(Object, media_id).data
29 def get_replied_to_activity(id) when not is_nil(id) do
30 Repo.get(Activity, id)
33 def get_replied_to_activity(_), do: nil
35 def create_status(user = %User{}, data = %{"status" => status}) do
36 attachments = attachments_from_ids(data["media_ids"])
37 context = ActivityPub.generate_context_id
38 mentions = parse_mentions(status)
39 content_html = format_input(status, mentions)
40 to = to_for_user_and_mentions(user, mentions)
43 inReplyTo = get_replied_to_activity(data["in_reply_to_status_id"])
46 [to, context, object, additional] =
48 context = inReplyTo.data["context"]
49 to = to ++ [inReplyTo.data["actor"]]
54 "content" => content_html,
57 "attachment" => attachments,
58 "actor" => user.ap_id,
59 "inReplyTo" => inReplyTo.data["object"]["id"],
60 "inReplyToStatusId" => inReplyTo.id,
61 "statusnetConversationId" => inReplyTo.data["statusnetConversationId"]
64 "statusnetConversationId" => inReplyTo.data["statusnetConversationId"]
67 [to, context, object, additional]
72 "content" => content_html,
75 "attachment" => attachments,
78 [to, context, object, %{}]
81 ActivityPub.create(to, user, context, object, additional, data)
84 def fetch_friend_statuses(user, opts \\ %{}) do
85 ActivityPub.fetch_activities([user.ap_id | user.following], opts)
86 |> activities_to_statuses(%{for: user})
89 def fetch_public_statuses(user, opts \\ %{}) do
90 ActivityPub.fetch_public_activities(opts)
91 |> activities_to_statuses(%{for: user})
94 def fetch_user_statuses(user, opts \\ %{}) do
95 ActivityPub.fetch_activities([], opts)
96 |> activities_to_statuses(%{for: user})
99 def fetch_mentions(user, opts \\ %{}) do
100 ActivityPub.fetch_activities([user.ap_id], opts)
101 |> activities_to_statuses(%{for: user})
104 def fetch_conversation(user, id) do
105 with context when is_binary(context) <- conversation_id_to_context(id),
106 activities <- ActivityPub.fetch_activities_for_context(context),
107 statuses <- activities |> activities_to_statuses(%{for: user})
116 def fetch_status(user, id) do
117 with %Activity{} = activity <- Repo.get(Activity, id) do
118 activity_to_status(activity, %{for: user})
122 def follow(%User{} = follower, params) do
123 with { :ok, %User{} = followed } <- get_user(params),
124 { :ok, follower } <- User.follow(follower, followed),
125 { :ok, activity } <- ActivityPub.insert(%{
127 "actor" => follower.ap_id,
128 "object" => followed.ap_id,
129 "published" => make_date()
132 { :ok, follower, followed, activity }
138 def unfollow(%User{} = follower, params) do
139 with { :ok, %User{} = unfollowed } <- get_user(params),
140 { :ok, follower } <- User.unfollow(follower, unfollowed)
142 { :ok, follower, unfollowed}
148 def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
149 object = Object.get_by_ap_id(object["id"])
151 {:ok, _like_activity, object} = ActivityPub.like(user, object)
152 new_data = activity.data
153 |> Map.put("object", object.data)
155 status = %{activity | data: new_data}
156 |> activity_to_status(%{for: user})
161 def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
162 object = Object.get_by_ap_id(object["id"])
164 {:ok, object} = ActivityPub.unlike(user, object)
165 new_data = activity.data
166 |> Map.put("object", object.data)
168 status = %{activity | data: new_data}
169 |> activity_to_status(%{for: user})
174 def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
175 object = Object.get_by_ap_id(object["id"])
177 {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
178 new_data = activity.data
179 |> Map.put("object", object.data)
181 status = %{activity | data: new_data}
182 |> activity_to_status(%{for: user})
187 def upload(%Plug.Upload{} = file, format \\ "xml") do
188 {:ok, object} = ActivityPub.upload(file)
190 url = List.first(object.data["url"])
192 type = url["mediaType"]
196 # Fake this as good as possible...
198 <?xml version="1.0" encoding="UTF-8"?>
199 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
200 <mediaid>#{object.id}</mediaid>
201 <media_id>#{object.id}</media_id>
202 <media_id_string>#{object.id}</media_id_string>
203 <media_url>#{href}</media_url>
204 <mediaurl>#{href}</mediaurl>
205 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
211 media_id_string: "#{object.id}}",
218 def parse_mentions(text) do
219 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
220 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])?)*/
222 Regex.scan(regex, text)
225 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
226 |> Enum.filter(fn ({_match, user}) -> user end)
229 def add_user_links(text, mentions) do
230 Enum.reduce(mentions, text, fn ({match, %User{ap_id: ap_id}}, text) -> String.replace(text, match, "<a href='#{ap_id}'>#{match}</a>") end)
233 def register_user(params) do
235 nickname: params["nickname"],
236 name: params["fullname"],
238 email: params["email"],
239 password: params["password"],
240 password_confirmation: params["confirm"]
243 changeset = User.register_changeset(%User{}, params)
245 with {:ok, user} <- Repo.insert(changeset) do
246 {:ok, UserRepresenter.to_map(user)}
248 {:error, changeset} ->
249 errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
251 {:error, %{error: errors}}
255 def get_user(user \\ nil, params) do
257 %{ "user_id" => user_id } ->
258 case target = Repo.get(User, user_id) do
260 {:error, "No user with such user_id"}
264 %{ "screen_name" => nickname } ->
265 case target = Repo.get_by(User, nickname: nickname) do
267 {:error, "No user with such screen_name"}
275 {:error, "You need to specify screen_name or user_id"}
280 defp activities_to_statuses(activities, opts) do
281 Enum.map(activities, fn(activity) ->
282 activity_to_status(activity, opts)
286 # For likes, fetch the liked activity, too.
287 defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do
288 actor = get_in(activity.data, ["actor"])
289 user = User.get_cached_by_ap_id(actor)
290 [liked_activity] = Activity.all_by_object_ap_id(activity.data["object"])
292 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity}))
295 # For announces, fetch the announced activity and the user.
296 defp activity_to_status(%Activity{data: %{"type" => "Announce"}} = activity, opts) do
297 actor = get_in(activity.data, ["actor"])
298 user = User.get_cached_by_ap_id(actor)
299 [announced_activity] = Activity.all_by_object_ap_id(activity.data["object"])
300 announced_actor = User.get_cached_by_ap_id(announced_activity.data["actor"])
302 ActivityRepresenter.to_map(activity, Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity}))
305 defp activity_to_status(activity, opts) do
306 actor = get_in(activity.data, ["actor"])
307 user = User.get_cached_by_ap_id(actor)
308 # mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
309 mentioned_users = Enum.map(activity.data["to"] || [], fn (ap_id) ->
310 User.get_cached_by_ap_id(ap_id)
312 |> Enum.filter(&(&1))
314 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
318 DateTime.utc_now() |> DateTime.to_iso8601
321 def context_to_conversation_id(context) do
322 with %Object{id: id} <- Object.get_by_ap_id(context) do
325 changeset = Object.context_mapping(context)
326 {:ok, %{id: id}} = Repo.insert(changeset)
331 def conversation_id_to_context(id) do
332 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
335 {:error, "No such conversation"}