Parse incoming retweets.
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.{Activity, User}
3 alias Pleroma.Web.OStatus.UserRepresenter
4 require Logger
5
6 defp get_in_reply_to(%{"object" => %{ "inReplyTo" => in_reply_to}}) do
7 [{:"thr:in-reply-to", [ref: to_charlist(in_reply_to)], []}]
8 end
9
10 defp get_in_reply_to(_), do: []
11
12 defp get_mentions(to) do
13 Enum.map(to, fn (id) ->
14 cond do
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) ->
20 []
21 true ->
22 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
23 end
24 end)
25 end
26
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
30
31 updated_at = activity.updated_at
32 |> NaiveDateTime.to_iso8601
33 inserted_at = activity.inserted_at
34 |> NaiveDateTime.to_iso8601
35
36 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
37 url = hd(attachment["url"])
38 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
39 end)
40
41 in_reply_to = get_in_reply_to(activity.data)
42 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
43 mentions = activity.data["to"] |> get_mentions
44
45 [
46 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
47 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
48 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
49 {:title, ['New note by #{user.nickname}']},
50 {:content, [type: 'html'], h.(activity.data["object"]["content"])},
51 {:published, h.(inserted_at)},
52 {:updated, h.(updated_at)},
53 {:"ostatus:conversation", [], h.(activity.data["context"])},
54 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
55 {:link, [type: ['application/atom+xml'], href: h.(activity.data["object"]["id"]), rel: 'self'], []}
56 ] ++ attachments ++ in_reply_to ++ author ++ mentions
57 end
58
59 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
60 h = fn(str) -> [to_charlist(str)] end
61
62 updated_at = activity.updated_at
63 |> NaiveDateTime.to_iso8601
64 inserted_at = activity.inserted_at
65 |> NaiveDateTime.to_iso8601
66
67 in_reply_to = get_in_reply_to(activity.data)
68 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
69 mentions = activity.data["to"] |> get_mentions
70
71 [
72 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/favorite']},
73 {:id, h.(activity.data["id"])},
74 {:title, ['New favorite by #{user.nickname}']},
75 {:content, [type: 'html'], ['#{user.nickname} favorited something']},
76 {:published, h.(inserted_at)},
77 {:updated, h.(updated_at)},
78 {:"activity:object", [
79 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
80 {:id, h.(activity.data["object"])}, # For notes, federate the object id.
81 ]},
82 {:"ostatus:conversation", [], h.(activity.data["context"])},
83 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
84 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
85 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
86 ] ++ author ++ mentions
87 end
88
89 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
90 h = fn(str) -> [to_charlist(str)] end
91
92 updated_at = activity.updated_at
93 |> NaiveDateTime.to_iso8601
94 inserted_at = activity.inserted_at
95 |> NaiveDateTime.to_iso8601
96
97 in_reply_to = get_in_reply_to(activity.data)
98 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
99
100 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
101 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
102
103 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
104
105 mentions = activity.data["to"] |> get_mentions
106 [
107 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
108 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/share']},
109 {:id, h.(activity.data["id"])},
110 {:title, ['#{user.nickname} repeated a notice']},
111 {:content, [type: 'html'], ['RT #{retweeted_activity.data["object"]["content"]}']},
112 {:published, h.(inserted_at)},
113 {:updated, h.(updated_at)},
114 {:"ostatus:conversation", [], h.(activity.data["context"])},
115 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
116 {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []},
117 {:"activity:object", retweeted_xml}
118 ] ++ mentions ++ author
119 end
120
121 def wrap_with_entry(simple_form) do
122 [{
123 :entry, [
124 xmlns: 'http://www.w3.org/2005/Atom',
125 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
126 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
127 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
128 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
129 ], simple_form
130 }]
131 end
132
133 def to_simple_form(_,_,_), do: nil
134 end