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