+ # Wire up reply info.
+ activity = with inReplyToId when not is_nil(inReplyToId) <- data["in_reply_to_status_id"],
+ inReplyTo <- Repo.get(Activity, inReplyToId),
+ context <- inReplyTo.data["context"]
+ do
+
+ to = activity["to"] ++ [inReplyTo.data["actor"]]
+
+ activity
+ |> put_in(["to"], to)
+ |> put_in(["context"], context)
+ |> put_in(["object", "context"], context)
+ |> put_in(["object", "inReplyTo"], inReplyTo.data["object"]["id"])
+ |> put_in(["object", "inReplyToStatusId"], inReplyToId)
+ |> put_in(["statusnetConversationId"], inReplyTo.data["statusnetConversationId"])
+ |> put_in(["object", "statusnetConversationId"], inReplyTo.data["statusnetConversationId"])
+ else _e ->
+ activity
+ end
+
+ with {:ok, activity} <- ActivityPub.insert(activity) do
+ add_conversation_id(activity)
+ end
+ end
+
+ def fetch_friend_statuses(user, opts \\ %{}) do
+ ActivityPub.fetch_activities([user.ap_id | user.following], opts)
+ |> activities_to_statuses(%{for: user})
+ end
+
+ def fetch_public_statuses(user, opts \\ %{}) do
+ ActivityPub.fetch_public_activities(opts)
+ |> activities_to_statuses(%{for: user})
+ end
+
+ def fetch_user_statuses(user, opts \\ %{}) do
+ ActivityPub.fetch_activities([], opts)
+ |> activities_to_statuses(%{for: user})
+ end
+
+ def fetch_conversation(user, id) do
+ query = from activity in Activity,
+ where: fragment("? @> ?", activity.data, ^%{ statusnetConversationId: id}),
+ limit: 1
+
+ with %Activity{} = activity <- Repo.one(query),
+ context <- activity.data["context"],
+ activities <- ActivityPub.fetch_activities_for_context(context),
+ statuses <- activities |> activities_to_statuses(%{for: user})
+ do
+ statuses
+ else e ->
+ IO.inspect(e)
+ []
+ end
+ end
+
+ def fetch_status(user, id) do
+ with %Activity{} = activity <- Repo.get(Activity, id) 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),
+ { :ok, activity } <- ActivityPub.insert(%{
+ "type" => "Follow",
+ "actor" => follower.ap_id,
+ "object" => followed.ap_id,
+ "published" => make_date()
+ })
+ do
+ { :ok, follower, followed, activity }
+ else
+ err -> err
+ end
+ end
+
+ def unfollow(%User{} = follower, followed_id) do
+ with %User{} = followed <- Repo.get(User, followed_id),
+ { :ok, follower } <- User.unfollow(follower, followed)
+ do
+ { :ok, follower, followed }
+ else
+ err -> err
+ end
+ end
+
+ def favorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
+ object = Object.get_by_ap_id(object["id"])
+
+ {:ok, _like_activity, object} = ActivityPub.like(user, object)
+ new_data = activity.data
+ |> Map.put("object", object.data)
+
+ status = %{activity | data: new_data}
+ |> activity_to_status(%{for: user})
+
+ {:ok, status}
+ end
+
+ def unfavorite(%User{} = user, %Activity{data: %{"object" => object}} = activity) do
+ object = Object.get_by_ap_id(object["id"])
+
+ {:ok, object} = ActivityPub.unlike(user, object)
+ new_data = activity.data
+ |> Map.put("object", object.data)
+
+ status = %{activity | data: new_data}
+ |> activity_to_status(%{for: user})
+
+ {:ok, status}