Handle unfollows from OStatus

This commit is contained in:
Francis Dinh 2018-05-21 04:36:20 -04:00
parent 2573332214
commit f2d6c3a512
3 changed files with 45 additions and 29 deletions

View file

@ -232,7 +232,12 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
end end
# Only undos of follow for now. Will need to get redone once there are more # Only undos of follow for now. Will need to get redone once there are more
def to_simple_form(%{data: %{"type" => "Undo"}} = activity, user, with_author) do def to_simple_form(
%{data: %{"type" => "Undo", "object" => %{"type" => "Follow"} = follow_activity}} =
activity,
user,
with_author
) do
h = fn str -> [to_charlist(str)] end h = fn str -> [to_charlist(str)] end
updated_at = activity.data["published"] updated_at = activity.data["published"]
@ -240,34 +245,25 @@ defmodule Pleroma.Web.OStatus.ActivityRepresenter do
author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: [] author = if with_author, do: [{:author, UserRepresenter.to_simple_form(user)}], else: []
follow_activity =
if is_map(activity.data["object"]) do
Activity.get_by_ap_id(activity.data["object"]["id"])
else
Activity.get_by_ap_id(activity.data["object"])
end
mentions = (activity.recipients || []) |> get_mentions mentions = (activity.recipients || []) |> get_mentions
follow_activity = Activity.get_by_ap_id(follow_activity["id"])
if follow_activity do [
[ {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']},
{:"activity:object-type", ['http://activitystrea.ms/schema/1.0/activity']}, {:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']},
{:"activity:verb", ['http://activitystrea.ms/schema/1.0/unfollow']}, {:id, h.(activity.data["id"])},
{:id, h.(activity.data["id"])}, {:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
{:title, ['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, {:content, [type: 'html'],
{:content, [type: 'html'], ['#{user.nickname} stopped following #{follow_activity.data["object"]}']},
['#{user.nickname} stopped following #{follow_activity.data["object"]}']}, {:published, h.(inserted_at)},
{:published, h.(inserted_at)}, {:updated, h.(updated_at)},
{:updated, h.(updated_at)}, {:"activity:object",
{:"activity:object", [
[ {:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']},
{:"activity:object-type", ['http://activitystrea.ms/schema/1.0/person']}, {:id, h.(follow_activity.data["object"])},
{:id, h.(follow_activity.data["object"])}, {:uri, h.(follow_activity.data["object"])}
{:uri, h.(follow_activity.data["object"])} ]},
]}, {:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []}
{:link, [rel: 'self', type: ['application/atom+xml'], href: h.(activity.data["id"])], []} ] ++ mentions ++ author
] ++ mentions ++ author
end
end end
def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do def to_simple_form(%{data: %{"type" => "Delete"}} = activity, user, with_author) do

View file

@ -0,0 +1,17 @@
defmodule Pleroma.Web.OStatus.UnfollowHandler do
alias Pleroma.Web.{XML, OStatus}
alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.User
def handle(entry, doc) do
with {:ok, actor} <- OStatus.find_make_or_update_user(doc),
id when not is_nil(id) <- XML.string_from_xpath("/entry/id", entry),
followed_uri when not is_nil(followed_uri) <-
XML.string_from_xpath("/entry/activity:object/id", entry),
{:ok, followed} <- OStatus.find_or_make_user(followed_uri),
{:ok, activity} <- ActivityPub.unfollow(actor, followed, id, false) do
User.unfollow(actor, followed)
{:ok, activity}
end
end
end

View file

@ -8,7 +8,7 @@ defmodule Pleroma.Web.OStatus do
alias Pleroma.{Repo, User, Web, Object, Activity} alias Pleroma.{Repo, User, Web, Object, Activity}
alias Pleroma.Web.ActivityPub.ActivityPub alias Pleroma.Web.ActivityPub.ActivityPub
alias Pleroma.Web.{WebFinger, Websub} alias Pleroma.Web.{WebFinger, Websub}
alias Pleroma.Web.OStatus.{FollowHandler, NoteHandler, DeleteHandler} alias Pleroma.Web.OStatus.{FollowHandler, UnfollowHandler, NoteHandler, DeleteHandler}
alias Pleroma.Web.ActivityPub.Transmogrifier alias Pleroma.Web.ActivityPub.Transmogrifier
def feed_path(user) do def feed_path(user) do
@ -47,6 +47,9 @@ defmodule Pleroma.Web.OStatus do
'http://activitystrea.ms/schema/1.0/follow' -> 'http://activitystrea.ms/schema/1.0/follow' ->
with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity with {:ok, activity} <- FollowHandler.handle(entry, doc), do: activity
'http://activitystrea.ms/schema/1.0/unfollow' ->
with {:ok, activity} <- UnfollowHandler.handle(entry, doc), do: activity
'http://activitystrea.ms/schema/1.0/share' -> 'http://activitystrea.ms/schema/1.0/share' ->
with {:ok, activity, retweeted_activity} <- handle_share(entry, doc), with {:ok, activity, retweeted_activity} <- handle_share(entry, doc),
do: [activity, retweeted_activity] do: [activity, retweeted_activity]