Add user to announced status.
[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 ] ++ attachments ++ in_reply_to ++ author ++ mentions
56 end
57
58 def to_simple_form(%{data: %{"type" => "Like"}} = activity, user, with_author) do
59 h = fn(str) -> [to_charlist(str)] end
60
61 updated_at = activity.updated_at
62 |> NaiveDateTime.to_iso8601
63 inserted_at = activity.inserted_at
64 |> NaiveDateTime.to_iso8601
65
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
69
70 [
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.
80 ]},
81 {:"ostatus:conversation", [], h.(activity.data["context"])},
82 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
83 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []}
84 ] ++ author ++ mentions
85 end
86
87 def to_simple_form(%{data: %{"type" => "Announce"}} = activity, user, with_author) do
88 h = fn(str) -> [to_charlist(str)] end
89
90 updated_at = activity.updated_at
91 |> NaiveDateTime.to_iso8601
92 inserted_at = activity.inserted_at
93 |> NaiveDateTime.to_iso8601
94
95 in_reply_to = get_in_reply_to(activity.data)
96 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
97
98 retweeted_activity = Activity.get_create_activity_by_object_ap_id(activity.data["object"])
99 retweeted_user = User.get_cached_by_ap_id(retweeted_activity.data["actor"])
100
101 retweeted_xml = to_simple_form(retweeted_activity, retweeted_user, true)
102
103 mentions = activity.data["to"] |> get_mentions
104 [
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, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []},
113 {:"thr:in-reply-to", [ref: to_charlist(activity.data["object"])], []},
114 {:"activity:object", retweeted_xml}
115 ] ++ mentions ++ author
116 end
117
118 def wrap_with_entry(simple_form) do
119 [{
120 :entry, [
121 xmlns: 'http://www.w3.org/2005/Atom',
122 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
123 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
124 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
125 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
126 ], simple_form
127 }]
128 end
129
130 def to_simple_form(_,_,_), do: nil
131 end