| 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
 | # Pleroma: A lightweight social networking server
# Copyright © 2017-2020 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Web.ActivityPub.Transmogrifier.QuestionHandlingTest do
  use Pleroma.DataCase
  alias Pleroma.Activity
  alias Pleroma.Object
  alias Pleroma.Web.ActivityPub.Transmogrifier
  alias Pleroma.Web.CommonAPI
  import Pleroma.Factory
  setup_all do
    Tesla.Mock.mock_global(fn env -> apply(HttpRequestMock, :request, [env]) end)
    :ok
  end
  test "Mastodon Question activity" do
    data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
    object = Object.normalize(activity, false)
    assert object.data["url"] == "https://mastodon.sdf.org/@rinpatch/102070944809637304"
    assert object.data["closed"] == "2019-05-11T09:03:36Z"
    assert object.data["context"] == activity.data["context"]
    assert object.data["context"] ==
             "tag:mastodon.sdf.org,2019-05-10:objectId=15095122:objectType=Conversation"
    assert object.data["context_id"]
    assert object.data["anyOf"] == []
    assert Enum.sort(object.data["oneOf"]) ==
             Enum.sort([
               %{
                 "name" => "25 char limit is dumb",
                 "replies" => %{"totalItems" => 0, "type" => "Collection"},
                 "type" => "Note"
               },
               %{
                 "name" => "Dunno",
                 "replies" => %{"totalItems" => 0, "type" => "Collection"},
                 "type" => "Note"
               },
               %{
                 "name" => "Everyone knows that!",
                 "replies" => %{"totalItems" => 1, "type" => "Collection"},
                 "type" => "Note"
               },
               %{
                 "name" => "I can't even fit a funny",
                 "replies" => %{"totalItems" => 1, "type" => "Collection"},
                 "type" => "Note"
               }
             ])
    user = insert(:user)
    {:ok, reply_activity} = CommonAPI.post(user, %{status: "hewwo", in_reply_to_id: activity.id})
    reply_object = Object.normalize(reply_activity, false)
    assert reply_object.data["context"] == object.data["context"]
    assert reply_object.data["context_id"] == object.data["context_id"]
  end
  test "Mastodon Question activity with HTML tags in plaintext" do
    options = [
      %{
        "type" => "Note",
        "name" => "<input type=\"date\">",
        "replies" => %{"totalItems" => 0, "type" => "Collection"}
      },
      %{
        "type" => "Note",
        "name" => "<input type=\"date\"/>",
        "replies" => %{"totalItems" => 0, "type" => "Collection"}
      },
      %{
        "type" => "Note",
        "name" => "<input type=\"date\" />",
        "replies" => %{"totalItems" => 1, "type" => "Collection"}
      },
      %{
        "type" => "Note",
        "name" => "<input type=\"date\"></input>",
        "replies" => %{"totalItems" => 1, "type" => "Collection"}
      }
    ]
    data =
      File.read!("test/fixtures/mastodon-question-activity.json")
      |> Poison.decode!()
      |> Kernel.put_in(["object", "oneOf"], options)
    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
    object = Object.normalize(activity, false)
    assert Enum.sort(object.data["oneOf"]) == Enum.sort(options)
  end
  test "Mastodon Question activity with custom emojis" do
    options = [
      %{
        "type" => "Note",
        "name" => ":blobcat:",
        "replies" => %{"totalItems" => 0, "type" => "Collection"}
      },
      %{
        "type" => "Note",
        "name" => ":blobfox:",
        "replies" => %{"totalItems" => 0, "type" => "Collection"}
      }
    ]
    tag = [
      %{
        "icon" => %{
          "type" => "Image",
          "url" => "https://blob.cat/emoji/custom/blobcats/blobcat.png"
        },
        "id" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
        "name" => ":blobcat:",
        "type" => "Emoji",
        "updated" => "1970-01-01T00:00:00Z"
      },
      %{
        "icon" => %{"type" => "Image", "url" => "https://blob.cat/emoji/blobfox/blobfox.png"},
        "id" => "https://blob.cat/emoji/blobfox/blobfox.png",
        "name" => ":blobfox:",
        "type" => "Emoji",
        "updated" => "1970-01-01T00:00:00Z"
      }
    ]
    data =
      File.read!("test/fixtures/mastodon-question-activity.json")
      |> Poison.decode!()
      |> Kernel.put_in(["object", "oneOf"], options)
      |> Kernel.put_in(["object", "tag"], tag)
    {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
    object = Object.normalize(activity, false)
    assert object.data["oneOf"] == options
    assert object.data["emoji"] == %{
             "blobcat" => "https://blob.cat/emoji/custom/blobcats/blobcat.png",
             "blobfox" => "https://blob.cat/emoji/blobfox/blobfox.png"
           }
  end
  test "returns an error if received a second time" do
    data = File.read!("test/fixtures/mastodon-question-activity.json") |> Poison.decode!()
    assert {:ok, %Activity{local: false} = activity} = Transmogrifier.handle_incoming(data)
    assert {:error, {:validate_object, {:error, _}}} = Transmogrifier.handle_incoming(data)
  end
  test "accepts a Question with no content" do
    data =
      File.read!("test/fixtures/mastodon-question-activity.json")
      |> Poison.decode!()
      |> Kernel.put_in(["object", "content"], "")
    assert {:ok, %Activity{local: false}} = Transmogrifier.handle_incoming(data)
  end
end
 |