Add following using screen_name parameter
[akkoma] / lib / pleroma / web / twitter_api / twitter_api.ex
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
5
6 import Ecto.Query
7
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
11 end)
12
13 context = ActivityPub.generate_context_id
14
15 content = HtmlSanitizeEx.strip_tags(data["status"])
16
17 mentions = parse_mentions(content)
18
19 default_to = [
20 User.ap_followers(user),
21 "https://www.w3.org/ns/activitystreams#Public"
22 ]
23
24 to = default_to ++ Enum.map(mentions, fn ({_, %{ap_id: ap_id}}) -> ap_id end)
25
26 content_html = add_user_links(content, mentions)
27
28 date = make_date()
29
30 activity = %{
31 "type" => "Create",
32 "to" => to,
33 "actor" => user.ap_id,
34 "object" => %{
35 "type" => "Note",
36 "to" => to,
37 "content" => content_html,
38 "published" => date,
39 "context" => context,
40 "attachment" => attachments
41 },
42 "published" => date,
43 "context" => context
44 }
45
46 # Wire up reply info.
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"]
50 do
51
52 to = activity["to"] ++ [inReplyTo.data["actor"]]
53
54 activity
55 |> put_in(["to"], to)
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"])
62 else _e ->
63 activity
64 end
65
66 with {:ok, activity} <- ActivityPub.insert(activity) do
67 add_conversation_id(activity)
68 end
69 end
70
71 def fetch_friend_statuses(user, opts \\ %{}) do
72 ActivityPub.fetch_activities(user.following, opts)
73 |> activities_to_statuses(%{for: user})
74 end
75
76 def fetch_public_statuses(user, opts \\ %{}) do
77 ActivityPub.fetch_public_activities(opts)
78 |> activities_to_statuses(%{for: user})
79 end
80
81 def fetch_conversation(user, id) do
82 query = from activity in Activity,
83 where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
84 limit: 1
85
86 with %Activity{} = activity <- Repo.one(query),
87 context <- activity.data["context"],
88 activities <- ActivityPub.fetch_activities_for_context(context),
89 statuses <- activities |> activities_to_statuses(%{for: user})
90 do
91 statuses
92 else e ->
93 IO.inspect(e)
94 []
95 end
96 end
97
98 def fetch_status(user, id) do
99 with %Activity{} = activity <- Repo.get(Activity, id) do
100 activity_to_status(activity, %{for: user})
101 end
102 end
103
104 def follow(%User{} = follower, %{ "user_id" => followed_id }) do
105 with %User{} = followed <- Repo.get(User, followed_id),
106 { :ok, follower } <- User.follow(follower, followed),
107 { :ok, activity } <- ActivityPub.insert(%{
108 "type" => "Follow",
109 "actor" => follower.ap_id,
110 "object" => followed.ap_id,
111 "published" => make_date()
112 })
113 do
114 { :ok, follower, followed, activity }
115 end
116 end
117
118 def follow(%User{} = follower, %{ "screen_name" => followed_name }) do
119 with %User{} = followed <- Repo.get_by(User, nickname: followed_name),
120 { :ok, follower } <- User.follow(follower, followed),
121 { :ok, activity } <- ActivityPub.insert(%{
122 "type" => "Follow",
123 "actor" => follower.ap_id,
124 "object" => followed.ap_id,
125 "published" => make_date()
126 })
127 do
128 { :ok, follower, followed, activity }
129 end
130 end
131
132 def unfollow(%User{} = follower, followed_id) do
133 with %User{} = followed <- Repo.get(User, followed_id),
134 { :ok, follower } <- User.unfollow(follower, followed)
135 do
136 { :ok, follower, followed }
137 end
138 end
139
140 def upload(%Plug.Upload{} = file) do
141 {:ok, object} = ActivityPub.upload(file)
142
143 url = List.first(object.data["url"])
144 href = url["href"]
145 type = url["mediaType"]
146
147 # Fake this as good as possible...
148 """
149 <?xml version="1.0" encoding="UTF-8"?>
150 <rsp stat="ok" xmlns:atom="http://www.w3.org/2005/Atom">
151 <mediaid>#{object.id}</mediaid>
152 <media_id>#{object.id}</media_id>
153 <media_id_string>#{object.id}</media_id_string>
154 <media_url>#{href}</media_url>
155 <mediaurl>#{href}</mediaurl>
156 <atom:link rel="enclosure" href="#{href}" type="#{type}"></atom:link>
157 </rsp>
158 """
159 end
160
161 def parse_mentions(text) do
162 # Modified from https://www.w3.org/TR/html5/forms.html#valid-e-mail-address
163 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])?)*/
164
165 Regex.scan(regex, text)
166 |> List.flatten
167 |> Enum.uniq
168 |> Enum.map(fn ("@" <> match = full_match) -> {full_match, Repo.get_by(User, nickname: match)} end)
169 |> Enum.filter(fn ({_match, user}) -> user end)
170 end
171
172 def add_user_links(text, mentions) do
173 Enum.reduce(mentions, text, fn ({match, %User{ap_id: ap_id}}, text) -> String.replace(text, match, "<a href='#{ap_id}'>#{match}</a>") end)
174 end
175
176 defp add_conversation_id(activity) do
177 if is_integer(activity.data["statusnetConversationId"]) do
178 {:ok, activity}
179 else
180 data = activity.data
181 |> put_in(["object", "statusnetConversationId"], activity.id)
182 |> put_in(["statusnetConversationId"], activity.id)
183
184 changeset = Ecto.Changeset.change(activity, data: data)
185 Repo.update(changeset)
186 end
187 end
188
189 defp activities_to_statuses(activities, opts) do
190 Enum.map(activities, fn(activity) ->
191 activity_to_status(activity, opts)
192 end)
193 end
194
195 defp activity_to_status(activity, opts) do
196 actor = get_in(activity.data, ["actor"])
197 user = Repo.get_by!(User, ap_id: actor)
198 mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
199 ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user, mentioned: mentioned_users}))
200 end
201
202 defp make_date do
203 DateTime.utc_now() |> DateTime.to_iso8601
204 end
205 end