logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma

activity_representer_test.exs (5415B)


      1 # Pleroma: A lightweight social networking server
      2 # Copyright © 2017-2019 Pleroma Authors <https://pleroma.social/>
      3 # SPDX-License-Identifier: AGPL-3.0-only
      4 
      5 defmodule Pleroma.Web.TwitterAPI.Representers.ActivityRepresenterTest do
      6   use Pleroma.DataCase
      7   alias Pleroma.Activity
      8   alias Pleroma.Object
      9   alias Pleroma.User
     10   alias Pleroma.Web.ActivityPub.ActivityPub
     11   alias Pleroma.Web.TwitterAPI.Representers.ActivityRepresenter
     12   alias Pleroma.Web.TwitterAPI.Representers.ObjectRepresenter
     13   alias Pleroma.Web.TwitterAPI.UserView
     14   import Pleroma.Factory
     15 
     16   test "a like activity" do
     17     user = insert(:user)
     18     note_activity = insert(:note_activity)
     19     object = Object.get_by_ap_id(note_activity.data["object"]["id"])
     20 
     21     {:ok, like_activity, _object} = ActivityPub.like(user, object)
     22 
     23     status =
     24       ActivityRepresenter.to_map(like_activity, %{user: user, liked_activity: note_activity})
     25 
     26     assert status["id"] == like_activity.id
     27     assert status["in_reply_to_status_id"] == note_activity.id
     28 
     29     note_activity = Activity.get_by_ap_id(note_activity.data["id"])
     30     activity_actor = Repo.get_by(User, ap_id: note_activity.data["actor"])
     31     liked_status = ActivityRepresenter.to_map(note_activity, %{user: activity_actor, for: user})
     32     assert liked_status["favorited"] == true
     33     assert status["activity_type"] == "like"
     34   end
     35 
     36   test "an activity" do
     37     user = insert(:user)
     38     #   {:ok, mentioned_user } = UserBuilder.insert(%{nickname: "shp", ap_id: "shp"})
     39     mentioned_user = insert(:user, %{nickname: "shp"})
     40 
     41     # {:ok, follower} = UserBuilder.insert(%{following: [User.ap_followers(user)]})
     42     follower = insert(:user, %{following: [User.ap_followers(user)]})
     43 
     44     object = %Object{
     45       data: %{
     46         "type" => "Image",
     47         "url" => [
     48           %{
     49             "type" => "Link",
     50             "mediaType" => "image/jpg",
     51             "href" => "http://example.org/image.jpg"
     52           }
     53         ],
     54         "uuid" => 1
     55       }
     56     }
     57 
     58     content_html =
     59       "<script>alert('YAY')</script>Some :2hu: content mentioning <a href='#{mentioned_user.ap_id}'>@shp</shp>"
     60 
     61     content = HtmlSanitizeEx.strip_tags(content_html)
     62     date = DateTime.from_naive!(~N[2016-05-24 13:26:08.003], "Etc/UTC") |> DateTime.to_iso8601()
     63 
     64     {:ok, convo_object} = Object.context_mapping("2hu") |> Repo.insert()
     65 
     66     to = [
     67       User.ap_followers(user),
     68       "https://www.w3.org/ns/activitystreams#Public",
     69       mentioned_user.ap_id
     70     ]
     71 
     72     activity = %Activity{
     73       id: 1,
     74       data: %{
     75         "type" => "Create",
     76         "id" => "id",
     77         "to" => to,
     78         "actor" => User.ap_id(user),
     79         "object" => %{
     80           "published" => date,
     81           "type" => "Note",
     82           "content" => content_html,
     83           "summary" => "2hu :2hu:",
     84           "inReplyToStatusId" => 213_123,
     85           "attachment" => [
     86             object
     87           ],
     88           "external_url" => "some url",
     89           "like_count" => 5,
     90           "announcement_count" => 3,
     91           "context" => "2hu",
     92           "tag" => ["content", "mentioning", "nsfw"],
     93           "emoji" => %{
     94             "2hu" => "corndog.png"
     95           }
     96         },
     97         "published" => date,
     98         "context" => "2hu"
     99       },
    100       local: false,
    101       recipients: to
    102     }
    103 
    104     corndog_emojo = ~s(<img class="emoji" alt="2hu" title="2hu" src="corndog.png" />)
    105 
    106     expected_html =
    107       ~s(<p>2hu ) <>
    108         corndog_emojo <>
    109         ~s(</p>alert\('YAY'\)Some ) <>
    110         corndog_emojo <>
    111         ~s( content mentioning <a href=") <> mentioned_user.ap_id <> ~s(">@shp</a>)
    112 
    113     expected_status = %{
    114       "id" => activity.id,
    115       "user" => UserView.render("show.json", %{user: user, for: follower}),
    116       "is_local" => false,
    117       "statusnet_html" => expected_html,
    118       "text" => "2hu :2hu:" <> content,
    119       "is_post_verb" => true,
    120       "created_at" => "Tue May 24 13:26:08 +0000 2016",
    121       "in_reply_to_status_id" => 213_123,
    122       "in_reply_to_screen_name" => nil,
    123       "in_reply_to_user_id" => nil,
    124       "in_reply_to_profileurl" => nil,
    125       "in_reply_to_ostatus_uri" => nil,
    126       "statusnet_conversation_id" => convo_object.id,
    127       "attachments" => [
    128         ObjectRepresenter.to_map(object)
    129       ],
    130       "attentions" => [
    131         UserView.render("show.json", %{user: mentioned_user, for: follower})
    132       ],
    133       "fave_num" => 5,
    134       "repeat_num" => 3,
    135       "favorited" => false,
    136       "repeated" => false,
    137       "pinned" => false,
    138       "external_url" => "some url",
    139       "tags" => ["nsfw", "content", "mentioning"],
    140       "activity_type" => "post",
    141       "possibly_sensitive" => true,
    142       "uri" => activity.data["object"]["id"],
    143       "visibility" => "direct",
    144       "card" => nil,
    145       "muted" => false,
    146       "summary" => "2hu :2hu:",
    147       "summary_html" =>
    148         "2hu <img class=\"emoji\" alt=\"2hu\" title=\"2hu\" src=\"corndog.png\" />"
    149     }
    150 
    151     assert ActivityRepresenter.to_map(activity, %{
    152              user: user,
    153              for: follower,
    154              mentioned: [mentioned_user]
    155            }) == expected_status
    156   end
    157 
    158   test "a delete activity" do
    159     object = insert(:note)
    160     user = User.get_by_ap_id(object.data["actor"])
    161 
    162     {:ok, delete} = ActivityPub.delete(object)
    163 
    164     map = ActivityRepresenter.to_map(delete, %{user: user})
    165 
    166     assert map["is_post_verb"] == false
    167     assert map["activity_type"] == "delete"
    168     assert map["uri"] == object.data["id"]
    169   end
    170 end