CommonAPI.post(user, data)
end
- def fetch_friend_statuses(user, opts \\ %{}) do
- opts =
- opts
- |> Map.put("blocking_user", user)
- |> Map.put("user", user)
- |> Map.put("type", ["Create", "Announce", "Follow", "Like"])
-
- ActivityPub.fetch_activities([user.ap_id | user.following], opts)
- |> activities_to_statuses(%{for: user})
- end
-
- def fetch_public_statuses(user, opts \\ %{}) do
- opts =
- opts
- |> Map.put("local_only", true)
- |> Map.put("blocking_user", user)
- |> Map.put("type", ["Create", "Announce", "Follow"])
-
- ActivityPub.fetch_public_activities(opts)
- |> activities_to_statuses(%{for: user})
- end
-
- def fetch_public_and_external_statuses(user, opts \\ %{}) do
- opts =
- opts
- |> Map.put("blocking_user", user)
- |> Map.put("type", ["Create", "Announce", "Follow"])
-
- ActivityPub.fetch_public_activities(opts)
- |> activities_to_statuses(%{for: user})
- end
-
- def fetch_user_statuses(user, opts \\ %{}) do
- opts =
- opts
- |> Map.put("type", ["Create"])
-
- ActivityPub.fetch_public_activities(opts)
- |> activities_to_statuses(%{for: user})
- end
-
- def fetch_mentions(user, opts \\ %{}) do
- ActivityPub.fetch_activities([user.ap_id], opts)
- |> activities_to_statuses(%{for: user})
- end
-
- def fetch_conversation(user, id) do
- with context when is_binary(context) <- conversation_id_to_context(id),
- activities <-
- ActivityPub.fetch_activities_for_context(context, %{
- "blocking_user" => user,
- "user" => user
- }),
- statuses <- activities |> activities_to_statuses(%{for: user}) do
- statuses
- else
- _e ->
- []
- end
- end
-
- def fetch_status(user, id) do
- with %Activity{} = activity <- Repo.get(Activity, id),
- true <- ActivityPub.visible_for_user?(activity, user) do
- activity_to_status(activity, %{for: user})
- end
- end
-
def follow(%User{} = follower, params) do
with {:ok, %User{} = followed} <- get_user(params),
{:ok, follower} <- User.follow(follower, followed),
def repeat(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.repeat(ap_id_or_id, user),
- %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
- status <- activity_to_status(activity, %{for: user}) do
- {:ok, status}
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
+ {:ok, activity}
end
end
def fav(%User{} = user, ap_id_or_id) do
with {:ok, _announce, %{data: %{"id" => id}}} = CommonAPI.favorite(ap_id_or_id, user),
- %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
- status <- activity_to_status(activity, %{for: user}) do
- {:ok, status}
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
+ {:ok, activity}
end
end
def unfav(%User{} = user, ap_id_or_id) do
with {:ok, %{data: %{"id" => id}}} = CommonAPI.unfavorite(ap_id_or_id, user),
- %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id),
- status <- activity_to_status(activity, %{for: user}) do
- {:ok, status}
+ %Activity{} = activity <- Activity.get_create_activity_by_object_ap_id(id) do
+ {:ok, activity}
end
end
activities = Repo.all(q)
end
- defp activities_to_statuses(activities, opts) do
- Enum.map(activities, fn activity ->
- activity_to_status(activity, opts)
- end)
- end
-
- # For likes, fetch the liked activity, too.
- defp activity_to_status(%Activity{data: %{"type" => "Like"}} = activity, opts) do
- actor = get_in(activity.data, ["actor"])
- user = User.get_cached_by_ap_id(actor)
- [liked_activity] = Activity.all_by_object_ap_id(activity.data["object"])
-
- ActivityRepresenter.to_map(
- activity,
- Map.merge(opts, %{user: user, liked_activity: liked_activity})
- )
- end
-
- # For announces, fetch the announced activity and the user.
- defp activity_to_status(%Activity{data: %{"type" => "Announce"}} = activity, opts) do
- actor = get_in(activity.data, ["actor"])
- user = User.get_cached_by_ap_id(actor)
- [announced_activity] = Activity.all_by_object_ap_id(activity.data["object"])
- announced_actor = User.get_cached_by_ap_id(announced_activity.data["actor"])
-
- ActivityRepresenter.to_map(
- activity,
- Map.merge(opts, %{users: [user, announced_actor], announced_activity: announced_activity})
- )
- end
-
- defp activity_to_status(%Activity{data: %{"type" => "Delete"}} = activity, opts) do
- actor = get_in(activity.data, ["actor"])
- user = User.get_cached_by_ap_id(actor)
- ActivityRepresenter.to_map(activity, Map.merge(opts, %{user: user}))
- end
-
- defp activity_to_status(activity, opts) do
- actor = get_in(activity.data, ["actor"])
- user = User.get_cached_by_ap_id(actor)
- # mentioned_users = Repo.all(from user in User, where: user.ap_id in ^activity.data["to"])
- mentioned_users =
- Enum.map(activity.recipients || [], fn ap_id ->
- if ap_id do
- User.get_cached_by_ap_id(ap_id)
- else
- nil
- end
- end)
- |> Enum.filter(& &1)
-
- ActivityRepresenter.to_map(
- activity,
- Map.merge(opts, %{user: user, mentioned: mentioned_users})
- )
- end
-
defp make_date do
DateTime.utc_now() |> DateTime.to_iso8601()
end