Fix bookmarks depending on embeded object and move checking if the

status is bookmarked to SQL
This commit is contained in:
rinpatch 2019-04-27 23:06:46 +03:00
parent 002ea343f8
commit c3e9fcf098
5 changed files with 54 additions and 10 deletions

View File

@ -41,6 +41,13 @@ def for_user_query(user_id) do
|> preload([b, a], activity: a) |> preload([b, a], activity: a)
end end
def get(user_id, activity_id) do
Bookmark
|> where(user_id: ^user_id)
|> where(activity_id: ^activity_id)
|> Repo.one()
end
@spec destroy(FlakeId.t(), FlakeId.t()) :: {:ok, Bookmark.t()} | {:error, Changeset.t()} @spec destroy(FlakeId.t(), FlakeId.t()) :: {:ok, Bookmark.t()} | {:error, Changeset.t()}
def destroy(user_id, activity_id) do def destroy(user_id, activity_id) do
from(b in Bookmark, from(b in Bookmark,

View File

@ -4,6 +4,7 @@
defmodule Pleroma.Web.CommonAPI do defmodule Pleroma.Web.CommonAPI do
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Bookmark
alias Pleroma.Formatter alias Pleroma.Formatter
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.ThreadMute alias Pleroma.ThreadMute
@ -282,6 +283,15 @@ def thread_muted?(user, activity) do
end end
end end
def bookmarked?(user, activity) do
with %Bookmark{} <- Bookmark.get(user.id, activity.id) do
true
else
_ ->
false
end
end
def report(user, data) do def report(user, data) do
with {:account_id, %{"account_id" => account_id}} <- {:account_id, data}, with {:account_id, %{"account_id" => account_id}} <- {:account_id, data},
{:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)}, {:account, %User{} = account} <- {:account, User.get_cached_by_id(account_id)},

View File

@ -86,11 +86,7 @@ def render(
activity_object = Object.normalize(activity) activity_object = Object.normalize(activity)
favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || []) favorited = opts[:for] && opts[:for].ap_id in (activity_object.data["likes"] || [])
bookmarked = bookmarked = opts[:for] && CommonAPI.bookmarked?(opts[:for], reblogged_activity)
opts[:for] && Ecto.assoc_loaded?(opts[:for].bookmarks) &&
Enum.any?(opts[:for].bookmarks, fn b ->
b.activity_id == activity.id or b.activity.data["object"]["id"] == object
end)
mentions = mentions =
activity.recipients activity.recipients
@ -153,11 +149,7 @@ def render("status.json", %{activity: %{data: %{"object" => _object}} = activity
favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || []) favorited = opts[:for] && opts[:for].ap_id in (object.data["likes"] || [])
bookmarked = bookmarked = opts[:for] && CommonAPI.bookmarked?(opts[:for], activity)
opts[:for] && Ecto.assoc_loaded?(opts[:for].bookmarks) &&
Enum.any?(opts[:for].bookmarks, fn b ->
b.activity_id == activity.id
end)
attachment_data = object.data["attachment"] || [] attachment_data = object.data["attachment"] || []
attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment) attachments = render_many(attachment_data, StatusView, "attachment.json", as: :attachment)

View File

@ -34,4 +34,19 @@ test "with valid params" do
{:ok, _deleted_bookmark} = Bookmark.destroy(user.id, activity.id) {:ok, _deleted_bookmark} = Bookmark.destroy(user.id, activity.id)
end end
end end
describe "get/2" do
test "gets a bookmark" do
user = insert(:user)
{:ok, activity} =
CommonAPI.post(user, %{
"status" =>
"Scientists Discover The Secret Behind Tenshi Eating A Corndog Being So Cute Science Daily"
})
{:ok, bookmark} = Bookmark.create(user.id, activity.id)
assert bookmark == Bookmark.get(user.id, activity.id)
end
end
end end

View File

@ -6,6 +6,7 @@ defmodule Pleroma.Web.MastodonAPI.StatusViewTest do
use Pleroma.DataCase use Pleroma.DataCase
alias Pleroma.Activity alias Pleroma.Activity
alias Pleroma.Bookmark
alias Pleroma.Object alias Pleroma.Object
alias Pleroma.Repo alias Pleroma.Repo
alias Pleroma.User alias Pleroma.User
@ -153,6 +154,25 @@ test "tells if the message is muted for some reason" do
assert status.muted == true assert status.muted == true
end end
test "tells if the status is bookmarked" do
user = insert(:user)
{:ok, activity} = CommonAPI.post(user, %{"status" => "Cute girls doing cute things"})
status = StatusView.render("status.json", %{activity: activity})
assert status.bookmarked == false
status = StatusView.render("status.json", %{activity: activity, for: user})
assert status.bookmarked == false
{:ok, _bookmark} = Bookmark.create(user.id, activity.id)
status = StatusView.render("status.json", %{activity: activity, for: user})
assert status.bookmarked == true
end
test "a reply" do test "a reply" do
note = insert(:note_activity) note = insert(:note_activity)
user = insert(:user) user = insert(:user)