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,
64 [to, context, object, additional]
69 "content" => content_html,
72 "attachment" => attachments,
75 [to, context, object, %{}]
78 ActivityPub.create(to, user, context, object, additional, data)
81 def fetch_friend_statuses(user, opts \\ %{}) do
82 ActivityPub.fetch_activities([user.ap_id | user.following], opts)
83 |> activities_to_statuses(%{for: user})
86 def fetch_public_statuses(user, opts \\ %{}) do
87 opts = Map.put(opts, "local_only", true)
88 ActivityPub.fetch_public_activities(opts)
89 |> activities_to_statuses(%{for: user})
92 def fetch_public_and_external_statuses(user, opts \\ %{}) do
93 ActivityPub.fetch_public_activities(opts)
94 |> activities_to_statuses(%{for: user})
97 def fetch_user_statuses(user, opts \\ %{}) do
98 ActivityPub.fetch_activities([], opts)
99 |> activities_to_statuses(%{for: user})
102 def fetch_mentions(user, opts \\ %{}) do
103 ActivityPub.fetch_activities([user.ap_id], opts)
104 |> activities_to_statuses(%{for: user})
107 def fetch_conversation(user, id) do
108 with context when is_binary(context) <- conversation_id_to_context(id),
109 activities <- ActivityPub.fetch_activities_for_context(context),
110 statuses <- activities |> activities_to_statuses(%{for: user})
119 def fetch_status(user, id) do
120 with %Activity{} = activity <- Repo.get(Activity, id) do
121 activity_to_status(activity, %{for: user})
125 def follow(%User{} = follower, params) do
126 with { :ok, %User{} = followed } <- get_user(params),
127 { :ok, follower } <- User.follow(follower, followed),
128 { :ok, activity } <- ActivityPub.insert(%{
130 "actor" => follower.ap_id,
131 "object" => followed.ap_id,
132 "published" => make_date()
135 { :ok, follower, followed, activity }
141 def unfollow(%User{} = follower, params) do
142 with { :ok, %User{} = unfollowed } <- get_user(params),
143 { :ok, follower } <- User.unfollow(follower, unfollowed)
145 { :ok, follower, unfollowed}
151 def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
152 object = Object.get_by_ap_id(object["id"])
154 {:ok, _like_activity, object} = ActivityPub.like(user, object)
155 new_data = activity.data
156 |> Map.put("object", object.data)
158 status = %{activity | data: new_data}
159 |> activity_to_status(%{for: user})
164 def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
165 object = Object.get_by_ap_id(object["id"])
167 {:ok, object} = ActivityPub.unlike(user, object)
168 new_data = activity.data
169 |> Map.put("object", object.data)
171 status = %{activity | data: new_data}
172 |> activity_to_status(%{for: user})
177 def retweet(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
178 object = Object.get_by_ap_id(object["id"])
180 {:ok, _announce_activity, object} = ActivityPub.announce(user, object)
181 new_data = activity.data
182 |> Map.put("object", object.data)
184 status = %{activity | data: new_data}
185 |> activity_to_status(%{for: user})
190 def upload(%Plug.Upload{} = file, format \\ "xml") do
191 {:ok, object} = ActivityPub.upload(file)
193 url = List.first(object.data["url"])
195 type = url["mediaType"]
199 # Fake this as good as possible...
201 <?xml version="1.0" encoding="UTF-8"?>
202 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
203 <mediaid>#{object.id}</mediaid>
204 <media_id>#{object.id}</media_id>
205 <media_id_string>#{object.id}</media_id_string>
206 <media_url>#{href}</media_url>
207 <mediaurl>#{href}</mediaurl>
208 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
214 media_id_string: "#{object.id}}",
221 def parse_mentions(text) do
222 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
223 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])?)*/
225 Regex.scan(regex, text)
228 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, User.get_cached_by_nickname(match)} end)
229 |> Enum.filter(fn ({_match, user}) -> user end)
232 def add_user_links(text, mentions) do
233 Enum.reduce(mentions, text, fn ({match, %User{ap_id: ap_id}}, text) -> String.replace(text, match, "<a href='#{ap_id}'>#{match}</a>") end)
236 def register_user(params) do
238 nickname: params["nickname"],
239 name: params["fullname"],
241 email: params["email"],
242 password: params["password"],
243 password_confirmation: params["confirm"]
246 changeset = User.register_changeset(%User{}, params)
248 with {:ok, user} <- Repo.insert(changeset) do
249 {:ok, UserRepresenter.to_map(user)}
251 {:error, changeset} ->
252 errors = Ecto.Changeset.traverse_errors(changeset, fn {msg, _opts} -> msg end)
254 {:error, %{error: errors}}
258 def get_user(user \\ nil, params) do
260 %{ "user_id" => user_id } ->
261 case target = Repo.get(User, user_id) do
263 {:error, "No user with such user_id"}
267 %{ "screen_name" => nickname } ->
268 case target = Repo.get_by(User, nickname: nickname) do
270 {:error, "No user with such screen_name"}
278 {:error, "You need to specify screen_name or user_id"}
283 defp activities_to_statuses(activities, opts) do
284 Enum.map(activities, fn(activity) ->
285 activity_to_status(activity, opts)
289 # For likes, fetch the liked activity, too.
290 defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do
291 actor = get_in(activity.data, ["actor"])
292 user = User.get_cached_by_ap_id(actor)
293 [liked_activity] = Activity.all_by_object_ap_id(activity.data["object"])
295 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, liked_activity: liked_activity}))
298 # For announces, fetch the announced activity and the user.
299 defp activity_to_status(%Activity{data: %{"type" => "Announce"}} = activity, opts) do
300 actor = get_in(activity.data, ["actor"])
301 user = User.get_cached_by_ap_id(actor)
302 [announced_activity] = Activity.all_by_object_ap_id(activity.data["object"])
303 announced_actor = User.get_cached_by_ap_id(announced_activity.data["actor"])
305 ActivityRepresenter.to_map(activity, Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity}))
308 defp activity_to_status(activity, opts) do
309 actor = get_in(activity.data, ["actor"])
310 user = User.get_cached_by_ap_id(actor)
311 # mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
312 mentioned_users = Enum.map(activity.data["to"] || [], fn (ap_id) ->
313 User.get_cached_by_ap_id(ap_id)
315 |> Enum.filter(&(&1))
317 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
321 DateTime.utc_now() |> DateTime.to_iso8601
324 def context_to_conversation_id(context) do
325 with %Object{id: id} <- Object.get_cached_by_ap_id(context) do
328 changeset = Object.context_mapping(context)
329 {:ok, %{id: id}} = Repo.insert(changeset)
334 def conversation_id_to_context(id) do
335 with %Object{data: %{"id" => context}} <- Repo.get(Object, id) do
338 {:error, "No such conversation"}