1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User}
3 alias Pleroma.Web.OStatus.UserRepresenter
6 defp get_in_reply_to(%{"object" => %{"inReplyTo" => in_reply_to}}) do
7 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to)], []}]
10 defp get_in_reply_to(_), do: []
12 defp get_mentions(to) do
13 Enum.map(to, fn (id) ->
15 # Special handling for the AP/Ostatus public collections
16 "https://www.w3.org/ns/activitystreams#Public" == id ->
17 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
18 # Ostatus doesn't handle follower collections, ignore these.
19 Regex.match?(~r/^#{Pleroma.Web.base_url}.+followers$/, id) ->
22 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
27 def to_simple_form(activity, user, with_author \\ false)
28 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
29 h = fn(str) -> [to_charlist(str)] end
31 updated_at = activity.data["published"]
32 inserted_at = activity.data["published"]
34 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
35 url = hd(attachment["url"])
36 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
39 in_reply_to = get_in_reply_to(activity.data)
40 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
41 mentions = activity.data["to"] |> get_mentions
43 categories = (activity.data["object"]["tag"] || [])
44 |> Enum.map(fn (tag) -> {:category, [term: to_charlist(tag)], []} end)
47 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
48 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
49 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
50 {:title, ['New note by #{user.nickname}']},
51 {:content, [type: 'html'], h.(activity.data["object"]["content"])},
52 {:published, h.(inserted_at)},
53 {:updated, h.(updated_at)},
54 {:"ostatus:conversation", [], h.(activity.data["context"])},
55 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
56 {:link, [type: ['application/atom+xml'], href: h.(activity.data["object"]["id"]), rel: 'self'], []}
57 ] ++ categories ++ attachments ++ in_reply_to ++ author ++ mentions
60 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
61 h = fn(str) -> [to_charlist(str)] end
63 updated_at = activity.data["published"]
64 inserted_at = activity.data["published"]
66 in_reply_to = get_in_reply_to(activity.data)
67 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
68 mentions = activity.data["to"] |> get_mentions
71 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
72 {:id, h.(activity.data["id"])},
73 {:title, ['New favorite by #{user.nickname}']},
74 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
75 {:published, h.(inserted_at)},
76 {:updated, h.(updated_at)},
77 {:"activity:object", [
78 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
79 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
81 {:"ostatus:conversation", [], h.(activity.data["context"])},
82 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
83 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
84 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
85 ] ++ author ++ mentions
88 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
89 h = fn(str) -> [to_charlist(str)] end
91 updated_at = activity.data["published"]
92 inserted_at = activity.data["published"]
94 in_reply_to = get_in_reply_to(activity.data)
95 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
97 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
98 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
100 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
102 mentions = activity.data["to"] |> get_mentions
104 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
105 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
106 {:id, h.(activity.data["id"])},
107 {:title, ['#{user.nickname} repeated a notice']},
108 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
109 {:published, h.(inserted_at)},
110 {:updated, h.(updated_at)},
111 {:"ostatus:conversation", [], h.(activity.data["context"])},
112 {:link, [ref: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
113 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
114 {:"activity:object", retweeted_xml}
115 ] ++ mentions ++ author
118 def to_simple_form(%{data: %{"type" => "Follow"}} = activity, user, with_author) do
119 h = fn(str) -> [to_charlist(str)] end
121 updated_at = activity.data["published"]
122 inserted_at = activity.data["published"]
124 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
126 mentions = (activity.data["to"] || []) |> get_mentions
128 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
129 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/follow']},
130 {:id, h.(activity.data["id"])},
131 {:title, ['#{user.nickname} started following #{activity.data["object"]}']},
132 {:content, [type: 'html'], ['#{user.nickname} started following #{activity.data["object"]}']},
133 {:published, h.(inserted_at)},
134 {:updated, h.(updated_at)},
135 {:"activity:object", [
136 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
137 {:id, h.(activity.data["object"])},
138 {:uri, h.(activity.data["object"])},
140 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
141 ] ++ mentions ++ author
144 # Only undos of follow for now. Will need to get redone once there are more
145 def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do
146 h = fn(str) -> [to_charlist(str)] end
148 updated_at = activity.data["published"]
149 inserted_at = activity.data["published"]
151 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
152 follow_activity = Activity.get_by_ap_id(activity.data["object"])
154 mentions = (activity.data["to"] || []) |> get_mentions
156 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
157 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
158 {:id, h.(activity.data["id"])},
159 {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
160 {:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
161 {:published, h.(inserted_at)},
162 {:updated, h.(updated_at)},
163 {:"activity:object", [
164 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
165 {:id, h.(follow_activity.data["object"])},
166 {:uri, h.(follow_activity.data["object"])},
168 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
169 ] ++ mentions ++ author
172 def wrap_with_entry(simple_form) do
175 xmlns: 'http://www.w3.org/2005/Atom',
176 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
177 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
178 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
179 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
184 def to_simple_form(_, _, _), do: nil