Skip to content

Commit 9c3af47

Browse files
committed
quote update_many
1 parent 0f8680d commit 9c3af47

File tree

1 file changed

+39
-0
lines changed

1 file changed

+39
-0
lines changed

lib/quotes.ex

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,45 @@ defmodule Bonfire.Social.Quotes do
100100
end
101101
end
102102

103+
@doc """
104+
Batch version of check_quote_permission for multiple objects.
105+
Returns a map of object_id => permission_tuple.
106+
"""
107+
def check_quote_permissions_many(_user, []), do: %{}
108+
109+
def check_quote_permissions_many(user, quoted_objects) when is_list(quoted_objects) do
110+
user_id = id(user)
111+
112+
# Preload associations with follow_pointers to handle Needle.Pointer objects
113+
quoted_objects =
114+
repo().maybe_preload(quoted_objects, [:created, controlled: :acl], follow_pointers: true)
115+
116+
# Map each object to its permission status
117+
Map.new(quoted_objects, fn obj ->
118+
obj_id = id(obj)
119+
120+
permission =
121+
cond do
122+
# User's own content
123+
e(obj, :created, :creator_id, nil) == user_id || obj_id == user_id ->
124+
{:auto_approve, obj}
125+
126+
# Can quote directly
127+
Bonfire.Boundaries.can?(user, quote_verb(), obj) ->
128+
{:auto_approve, obj}
129+
130+
# Can request to quote
131+
Bonfire.Boundaries.can?(user, :request, obj) ->
132+
{:request_needed, obj}
133+
134+
true ->
135+
{:not_permitted, obj}
136+
end
137+
138+
{obj_id, permission}
139+
end)
140+
end
141+
103142
@doc """
104143
Creates quote requests for pending quotes after the post has been created.
105144
"""

0 commit comments

Comments
 (0)