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 add_attachments(text, attachments) do
36 attachment_text = Enum.map(attachments, fn
37 (%{"url" => [%{"href" => href} | _]}) ->
38 "<a href='#{href}'>#{href}</a>"
41 Enum.join([text | attachment_text], "<br>")
44 def create_status(%User{} = user, %{"status" => status} = data) do
45 attachments = attachments_from_ids(data["media_ids"])
46 context = ActivityPub.generate_context_id
47 mentions = parse_mentions(status)
49 |> format_input(mentions)
50 |> add_attachments(attachments)
52 to = to_for_user_and_mentions(user, mentions)
55 inReplyTo = get_replied_to_activity(data["in_reply_to_status_id"])
58 [to, context, object, additional] =
60 context = inReplyTo.data["context"]
61 to = to ++ [inReplyTo.data["actor"]]
66 "content" => content_html,
69 "attachment" => attachments,
70 "actor" => user.ap_id,
71 "inReplyTo" => inReplyTo.data["object"]["id"],
72 "inReplyToStatusId" => inReplyTo.id,
76 [to, context, object, additional]
81 "content" => content_html,
84 "attachment" => attachments,
87 [to, context, object, %{}]
90 ActivityPub.create(to, user, context, object, additional, data)
93 def fetch_friend_statuses(user, opts \\ %{}) do
94 ActivityPub.fetch_activities([user.ap_id | user.following], opts)
95 |> activities_to_statuses(%{for: user})
98 def fetch_public_statuses(user, opts \\ %{}) do
99 opts = Map.put(opts, "local_only", true)
100 ActivityPub.fetch_public_activities(opts)
101 |> activities_to_statuses(%{for: user})
104 def fetch_public_and_external_statuses(user, opts \\ %{}) do
105 ActivityPub.fetch_public_activities(opts)
106 |> activities_to_statuses(%{for: user})
109 def fetch_user_statuses(user, opts \\ %{}) do
110 ActivityPub.fetch_activities([], opts)
111 |> activities_to_statuses(%{for: user})
114 def fetch_mentions(user, opts \\ %{}) do
115 ActivityPub.fetch_activities([user.ap_id], opts)
116 |> activities_to_statuses(%{for: user})
119 def fetch_conversation(user, id) do
120 with context when is_binary(context) <- conversation_id_to_context(id),
121 activities <- ActivityPub.fetch_activities_for_context(context),
122 statuses <- activities |> activities_to_statuses(%{for: user})
130 def fetch_status(user, id) do
131 with %Activity{} = activity <- Repo.get(Activity, id) do
132 activity_to_status(activity, %{for: user})
136 def follow(%User{} = follower, params) do
137 with {:ok, %User{} = followed} <- get_user(params),
138 {:ok, follower} <- User.follow(follower, followed),
139 {:ok, activity} <- ActivityPub.follow(follower, followed)
141 {:ok, follower, followed, activity}
147 def unfollow(%User{} = follower, params) do
148 with { :ok, %User{} = unfollowed } <- get_user(params),
149 { :ok, follower, follow_activity } <- User.unfollow(follower, unfollowed),
150 { :ok, _activity } <- ActivityPub.insert(%{
152 "actor" => follower.ap_id,
153 "object" => follow_activity.data["id"], # get latest Follow for these users
154 "published" => make_date()
157 { :ok, follower, unfollowed }
163 def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
164 object = Object.get_by_ap_id(object["id"])
166 {:ok, _like_activity, object} = ActivityPub.like(user, object)
167 new_data = activity.data
168 |> Map.put("object", object.data)
170 status = %{activity | data: new_data}
171 |> activity_to_status(%{for: user})
176 def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
177 object = Object.get_by_ap_id(object["id"])
179 {:ok, object} = ActivityPub.unlike(user, object)
180 new_data = activity.data
181 |> Map.put("object", object.data)
183 status = %{activity | data: new_data}
184 |> activity_to_status(%{for: user})
189 def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
190 object = Object.get_by_ap_id(object["id"])
192 {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
193 new_data = activity.data
194 |> Map.put("object", object.data)
196 status = %{activity | data: new_data}
197 |> activity_to_status(%{for: user})
202 def upload(%Plug.Upload{} = file, format \\ "xml") do
203 {:ok, object} = ActivityPub.upload(file)
205 url = List.first(object.data["url"])
207 type = url["mediaType"]
211 # Fake this as good as possible...
213 <?xml version="1.0" encoding="UTF-8"?>
214 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
215 <mediaid>#{object.id}</mediaid>
216 <media_id>#{object.id}</media_id>
217 <media_id_string>#{object.id}</media_id_string>
218 <media_url>#{href}</media_url>
219 <mediaurl>#{href}</mediaurl>
220 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
226 media_id_string: "#{object.id}}",
233 def parse_mentions(text) do
234 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
235 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])?)*/
237 Regex.scan(regex, text)
240 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
241 |> Enum.filter(fn ({_match, user}) -> user end)
244 def add_user_links(text, mentions) do
245 Enum.reduce(mentions, text, fn ({match, %User{ap_id: ap_id}}, text) -> String.replace(text, match, "<a href='#{ap_id}'>#{match}</a>") end)
248 def register_user(params) do
250 nickname: params["nickname"],
251 name: params["fullname"],
253 email: params["email"],
254 password: params["password"],
255 password_confirmation: params["confirm"]
258 changeset = User.register_changeset(%User{}, params)
260 with {:ok, user} <- Repo.insert(changeset) do
261 {:ok, UserRepresenter.to_map(user)}
263 {:error, changeset} ->
264 errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
266 {:error, %{error: errors}}
270 def get_user(user \\ nil, params) do
272 %{"user_id" => user_id} ->
273 case target = Repo.get(User, user_id) do
275 {:error, "No user with such user_id"}
279 %{"screen_name" => nickname} ->
280 case target = Repo.get_by(User, nickname: nickname) do
282 {:error, "No user with such screen_name"}
290 {:error, "You need to specify screen_name or user_id"}
295 defp activities_to_statuses(activities, opts) do
296 Enum.map(activities, fn(activity) ->
297 activity_to_status(activity, opts)
301 # For likes, fetch the liked activity, too.
302 defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do
303 actor = get_in(activity.data, ["actor"])
304 user = User.get_cached_by_ap_id(actor)
305 [liked_activity] = Activity.all_by_object_ap_id(activity.data["object"])
307 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity}))
310 # For announces, fetch the announced activity and the user.
311 defp activity_to_status(%Activity{data: %{"type" => "Announce"}} = activity, opts) do
312 actor = get_in(activity.data, ["actor"])
313 user = User.get_cached_by_ap_id(actor)
314 [announced_activity] = Activity.all_by_object_ap_id(activity.data["object"])
315 announced_actor = User.get_cached_by_ap_id(announced_activity.data["actor"])
317 ActivityRepresenter.to_map(activity, Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity}))
320 defp activity_to_status(activity, opts) do
321 actor = get_in(activity.data, ["actor"])
322 user = User.get_cached_by_ap_id(actor)
323 # mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
324 mentioned_users = Enum.map(activity.data["to"] || [], fn (ap_id) ->
325 User.get_cached_by_ap_id(ap_id)
327 |> Enum.filter(&(&1))
329 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
333 DateTime.utc_now() |> DateTime.to_iso8601
336 def context_to_conversation_id(context) do
337 with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
340 changeset = Object.context_mapping(context)
341 {:ok, %{id: id}} = Repo.insert(changeset)
346 def conversation_id_to_context(id) do
347 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
350 {:error, "No such conversation"}