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
8 def create_status(user = %User{}, data = %{}) do
9 attachments = Enum.map(data["media_ids"] || [], fn (media_id) ->
10 Repo.get(Object, media_id).data
13 context = ActivityPub.generate_context_id
15 content = HtmlSanitizeEx.strip_tags(data["status"])
17 mentions = parse_mentions(content)
20 User.ap_followers(user),
21 "https://www.w3.org/ns/activitystreams#Public"
24 to = default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
26 content_html = add_user_links(content, mentions)
33 "actor" => user.ap_id,
37 "content" => content_html,
40 "attachment" => attachments
47 activity = with inReplyToId when not is_nil(inReplyToId) <- data["in_reply_to_status_id"],
48 inReplyTo <- Repo.get(Activity, inReplyToId),
49 context <- inReplyTo.data["context"]
52 to = activity["to"] ++ [inReplyTo.data["actor"]]
56 |> put_in(["context"], context)
57 |> put_in(["object", "context"], context)
58 |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
59 |> put_in(["object", "inReplyToStatusId"], inReplyToId)
60 |> put_in(["statusnetConversationId"], inReplyTo.data["statusnetConversationId"])
61 |> put_in(["object", "statusnetConversationId"], inReplyTo.data["statusnetConversationId"])
66 with {:ok, activity} <- ActivityPub.insert(activity) do
67 add_conversation_id(activity)
71 def fetch_friend_statuses(user, opts \\ %{}) do
72 ActivityPub.fetch_activities([user.ap_id | user.following], opts)
73 |> activities_to_statuses(%{for: user})
76 def fetch_public_statuses(user, opts \\ %{}) do
77 ActivityPub.fetch_public_activities(opts)
78 |> activities_to_statuses(%{for: user})
81 def fetch_user_statuses(user, opts \\ %{}) do
82 target = get_user(user, opts)
83 ActivityPub.fetch_activities([], Map.merge(opts, %{"actor_id" => target.ap_id}))
84 |> activities_to_statuses(%{for: user})
87 def fetch_conversation(user, id) do
88 query = from activity in Activity,
89 where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
92 with %Activity{} = activity <- Repo.one(query),
93 context <- activity.data["context"],
94 activities <- ActivityPub.fetch_activities_for_context(context),
95 statuses <- activities |> activities_to_statuses(%{for: user})
104 def fetch_status(user, id) do
105 with %Activity{} = activity <- Repo.get(Activity, id) do
106 activity_to_status(activity, %{for: user})
110 def follow(%User{} = follower, followed_id) do
111 with %User{} = followed <- Repo.get(User, followed_id),
112 { :ok, follower } <- User.follow(follower, followed),
113 { :ok, activity } <- ActivityPub.insert(%{
115 "actor" => follower.ap_id,
116 "object" => followed.ap_id,
117 "published" => make_date()
120 { :ok, follower, followed, activity }
124 def unfollow(%User{} = follower, followed_id) do
125 with %User{} = followed <- Repo.get(User, followed_id),
126 { :ok, follower } <- User.unfollow(follower, followed)
128 { :ok, follower, followed }
132 def upload(%Plug.Upload{} = file) do
133 {:ok, object} = ActivityPub.upload(file)
135 url = List.first(object.data["url"])
137 type = url["mediaType"]
139 # Fake this as good as possible...
141 <?xml version="1.0" encoding="UTF-8"?>
142 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
143 <mediaid>#{object.id}</mediaid>
144 <media_id>#{object.id}</media_id>
145 <media_id_string>#{object.id}</media_id_string>
146 <media_url>#{href}</media_url>
147 <mediaurl>#{href}</mediaurl>
148 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
153 def parse_mentions(text) do
154 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
155 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])?)*/
157 Regex.scan(regex, text)
160 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, Repo.get_by(User, nickname: match)} end)
161 |> Enum.filter(fn ({_match, user}) -> user end)
164 def add_user_links(text, mentions) do
165 Enum.reduce(mentions, text, fn ({match, %User{ap_id: ap_id}}, text) -> String.replace(text, match, "<a href='#{ap_id}'>#{match}</a>") end)
168 defp add_conversation_id(activity) do
169 if is_integer(activity.data["statusnetConversationId"]) do
173 |> put_in(["object", "statusnetConversationId"], activity.id)
174 |> put_in(["statusnetConversationId"], activity.id)
176 changeset = Ecto.Changeset.change(activity, data: data)
177 Repo.update(changeset)
181 defp activities_to_statuses(activities, opts) do
182 Enum.map(activities, fn(activity) ->
183 activity_to_status(activity, opts)
187 defp activity_to_status(activity, opts) do
188 actor = get_in(activity.data, ["actor"])
189 user = Repo.get_by!(User, ap_id: actor)
190 mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
191 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
195 DateTime.utc_now() |> DateTime.to_iso8601
198 defp get_user(user, params) do
200 %{ "user_id" => user_id } ->
201 Repo.get(User, user_id)
202 %{ "screen_name" => nickname } ->
203 Repo.get_by!(User, nickname: nickname)