Make outgoing salmons work.
[akkoma] / lib / pleroma / web / ostatus / activity_representer.ex
1 defmodule Pleroma.Web.OStatus.ActivityRepresenter do
2 alias Pleroma.Activity
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
14 ("https://www.w3.org/ns/activitystreams#Public") ->
15 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/collection", href: "http://activityschema.org/collection/public"], []}
16 (id) ->
17 {:link, [rel: "mentioned", "ostatus:object-type": "http://activitystrea.ms/schema/1.0/person", href: id], []}
18 end)
19 end
20
21 def to_simple_form(activity, user, with_author \\ false)
22 def to_simple_form(%{data: %{"object" => %{"type" => "Note"}}} = activity, user, with_author) do
23 h = fn(str) -> [to_charlist(str)] end
24
25 updated_at = activity.updated_at
26 |> NaiveDateTime.to_iso8601
27 inserted_at = activity.inserted_at
28 |> NaiveDateTime.to_iso8601
29
30 attachments = Enum.map(activity.data["object"]["attachment"] || [], fn(attachment) ->
31 url = hd(attachment["url"])
32 {:link, [rel: 'enclosure', href: to_charlist(url["href"]), type: to_charlist(url["mediaType"])], []}
33 end)
34
35 in_reply_to = get_in_reply_to(activity.data)
36 author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
37 mentions = activity.data["to"] |> get_mentions
38
39 [
40 {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/note']},
41 {:"activity:verb", ['http://activitystrea.ms/schema/1.0/post']},
42 {:id, h.(activity.data["object"]["id"])}, # For notes, federate the object id.
43 {:title, ['New note by #{user.nickname}']},
44 {:content, [type: 'html'], h.(activity.data["object"]["content"])},
45 {:published, h.(inserted_at)},
46 {:updated, h.(updated_at)},
47 {:"ostatus:conversation", [], h.(activity.data["context"])},
48 {:link, [href: h.(activity.data["context"]), rel: 'ostatus:conversation'], []}
49 ] ++ attachments ++ in_reply_to ++ author ++ mentions
50 end
51
52 def wrap_with_entry(simple_form) do
53 [{
54 :entry, [
55 xmlns: 'http://www.w3.org/2005/Atom',
56 "xmlns:thr": 'http://purl.org/syndication/thread/1.0',
57 "xmlns:activity": 'http://activitystrea.ms/spec/1.0/',
58 "xmlns:poco": 'http://portablecontacts.net/spec/1.0',
59 "xmlns:ostatus": 'http://ostatus.org/schema/1.0'
60 ], simple_form
61 }]
62 end
63
64 def to_simple_form(_,_,_), do: nil
65 end