forked from YokaiRick/akkoma
Add favoriting to Mastodon API.
This commit is contained in:
parent
66e4c710d4
commit
454dc18570
8 changed files with 47 additions and 21 deletions
|
@ -22,6 +22,17 @@ def repeat(id_or_ap_id, user) do
|
|||
end
|
||||
end
|
||||
|
||||
def favorite(id_or_ap_id, user) do
|
||||
with %Activity{} = activity <- get_by_id_or_ap_id(id_or_ap_id),
|
||||
false <- activity.data["actor"] == user.ap_id,
|
||||
object <- Object.get_by_ap_id(activity.data["object"]["id"]) do
|
||||
ActivityPub.like(user, object)
|
||||
else
|
||||
_ ->
|
||||
{:error, "Could not favorite"}
|
||||
end
|
||||
end
|
||||
|
||||
# This is a hack for twidere.
|
||||
def get_by_id_or_ap_id(id) do
|
||||
activity = Repo.get(Activity, id) || Activity.get_create_activity_by_object_ap_id(id)
|
||||
|
|
|
@ -87,4 +87,11 @@ def reblog_status(%{assigns: %{user: user}} = conn, %{"id" => ap_id_or_id}) do
|
|||
render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
|
||||
end
|
||||
end
|
||||
|
||||
def fav_status(%{assigns: %{user: user}} = conn, %{"id" => 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) do
|
||||
render conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity}
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -22,6 +22,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
|
|||
|> Enum.map(fn (user) -> AccountView.render("mention.json", %{user: user}) end)
|
||||
|
||||
repeated = opts[:for] && opts[:for].ap_id in (object["announcements"] || [])
|
||||
favorited = opts[:for] && opts[:for].ap_id in (object["likes"] || [])
|
||||
|
||||
%{
|
||||
id: activity.id,
|
||||
|
@ -36,7 +37,7 @@ def render("status.json", %{activity: %{data: %{"object" => object}} = activity}
|
|||
reblogs_count: announcement_count,
|
||||
favourites_count: like_count,
|
||||
reblogged: !!repeated,
|
||||
favourited: false, # fix
|
||||
favourited: !!favorited,
|
||||
muted: false,
|
||||
sensitive: sensitive,
|
||||
spoiler_text: "",
|
||||
|
|
|
@ -59,6 +59,7 @@ def user_fetcher(username) do
|
|||
delete "/statuses/:id", MastodonAPIController, :delete_status
|
||||
|
||||
post "/statuses/:id/reblog", MastodonAPIController, :reblog_status
|
||||
post "/statuses/:id/favourite", MastodonAPIController, :fav_status
|
||||
end
|
||||
|
||||
scope "/api", Pleroma.Web do
|
||||
|
|
|
@ -115,19 +115,6 @@ def unfollow(%User{} = follower, params) do
|
|||
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"])
|
||||
|
||||
|
@ -149,6 +136,14 @@ def repeat(%User{} = user, ap_id_or_id) do
|
|||
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}
|
||||
end
|
||||
end
|
||||
|
||||
def upload(%Plug.Upload{} = file, format \\ "xml") do
|
||||
{:ok, object} = ActivityPub.upload(file)
|
||||
|
||||
|
|
|
@ -149,12 +149,9 @@ def get_by_id_or_ap_id(id) do
|
|||
end
|
||||
|
||||
def favorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
activity = get_by_id_or_ap_id(id)
|
||||
{:ok, status} = TwitterAPI.favorite(user, activity)
|
||||
response = Poison.encode!(status)
|
||||
|
||||
conn
|
||||
|> json_reply(200, response)
|
||||
with {:ok, status} <- TwitterAPI.fav(user, id) do
|
||||
json(conn, status)
|
||||
end
|
||||
end
|
||||
|
||||
def unfavorite(%{assigns: %{user: user}} = conn, %{"id" => id}) do
|
||||
|
|
|
@ -135,4 +135,18 @@ test "reblogs and returns the reblogged status", %{conn: conn} do
|
|||
assert activity.id == id
|
||||
end
|
||||
end
|
||||
|
||||
describe "favoriting" do
|
||||
test "favs a status and returns it", %{conn: conn} do
|
||||
activity = insert(:note_activity)
|
||||
user = insert(:user)
|
||||
|
||||
conn = conn
|
||||
|> assign(:user, user)
|
||||
|> post("/api/v1/statuses/#{activity.id}/favourite")
|
||||
|
||||
assert %{"id" => id, "favourites_count" => 1, "favourited" => true} = json_response(conn, 200)
|
||||
assert activity.id == id
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
|
@ -264,7 +264,7 @@ test "it favorites a status, returns the updated status" do
|
|||
note_activity = insert(:note_activity)
|
||||
activity_user = Repo.get_by!(User, ap_id: note_activity.data["actor"])
|
||||
|
||||
{:ok, status} = TwitterAPI.favorite(user, note_activity)
|
||||
{:ok, status} = TwitterAPI.fav(user, note_activity.id)
|
||||
updated_activity = Activity.get_by_ap_id(note_activity.data["id"])
|
||||
|
||||
assert status == ActivityRepresenter.to_map(updated_activity, %{user: activity_user, for: user})
|
||||
|
|
Loading…
Reference in a new issue