@@ -17,7 +17,9 @@ defmodule Plausible.Annotations do
1717
1818 @ max_annotations 500
1919
20- def get_all_for_site ( % Plausible.Site { } = site , site_role ) do
20+ @ spec get_all_for_site ( Plausible.Site . t ( ) , atom ( ) , pos_integer ( ) ) ::
21+ { :error , :not_enough_permissions } | { :ok , list ( Annotation . t ( ) ) }
22+ def get_all_for_site ( % Plausible.Site { } = site , site_role , user_id ) do
2123 fields = [ :id , :note , :type , :datetime , :granularity , :inserted_at , :updated_at ]
2224
2325 cond do
@@ -27,21 +29,25 @@ defmodule Plausible.Annotations do
2729 from ( annotation in Annotation ,
2830 select: ^ fields ,
2931 where: annotation . site_id == ^ site . id ,
32+ where: annotation . type == :site ,
3033 order_by: [ desc: annotation . updated_at , desc: annotation . id ]
3134 )
3235 )
3336
3437 { :ok , Enum . map ( annotations , & localize_annotation ( & 1 , site . timezone ) ) }
3538
36- site_role in @ roles_with_personal_annotations or
37- site_role in @ roles_with_maybe_site_annotations ->
39+ site_role in roles_with_personal_annotations ( ) or
40+ site_role in roles_with_maybe_site_annotations ( ) ->
3841 fields = fields ++ [ :owner_id ]
3942
4043 annotations =
4144 Repo . all (
4245 from ( annotation in Annotation ,
4346 select: ^ fields ,
4447 where: annotation . site_id == ^ site . id ,
48+ where:
49+ annotation . type == :site or
50+ ( annotation . type == :personal and annotation . owner_id == ^ user_id ) ,
4551 order_by: [ desc: annotation . updated_at , desc: annotation . id ] ,
4652 preload: [ :owner ]
4753 )
@@ -90,7 +96,8 @@ defmodule Plausible.Annotations do
9096 % Annotation { } ,
9197 Map . merge ( params , % { "site_id" => site . id , "owner_id" => user_id } )
9298 ) do
93- { :ok , changeset |> Repo . insert! ( ) |> Repo . preload ( :owner ) |> localize_annotation ( site . timezone ) }
99+ { :ok ,
100+ changeset |> Repo . insert! ( ) |> Repo . preload ( :owner ) |> localize_annotation ( site . timezone ) }
94101 else
95102 % { valid?: false , errors: errors } ->
96103 { :error , { :invalid_annotation , errors } }
@@ -124,7 +131,8 @@ defmodule Plausible.Annotations do
124131 ) do
125132 Repo . update! ( changeset )
126133
127- { :ok , Repo . reload! ( annotation ) |> Repo . preload ( :owner ) |> localize_annotation ( site . timezone ) }
134+ { :ok ,
135+ Repo . reload! ( annotation ) |> Repo . preload ( :owner ) |> localize_annotation ( site . timezone ) }
128136 else
129137 % { valid?: false , errors: errors } ->
130138 { :error , { :invalid_annotation , errors } }
@@ -223,7 +231,9 @@ defmodule Plausible.Annotations do
223231 from ( annotation in Annotation ,
224232 where: annotation . site_id == ^ site_id ,
225233 where: annotation . id == ^ annotation_id ,
226- where: annotation . type == :site or annotation . owner_id == ^ user_id ,
234+ where:
235+ annotation . type == :site or
236+ ( annotation . type == :personal and annotation . owner_id == ^ user_id ) ,
227237 preload: [ :owner ]
228238 )
229239
@@ -282,7 +292,7 @@ defmodule Plausible.Annotations do
282292 def roles_with_maybe_site_annotations ( ) , do: @ roles_with_maybe_site_annotations
283293
284294 def site_annotations_available? ( % Plausible.Site { } = site ) ,
285- do: Plausible.Billing.Feature.SiteAnnotations . check_availability ( site . team ) == :ok
295+ do: Plausible.Billing.Feature.SiteSegments . check_availability ( site . team ) == :ok
286296
287297 @ doc """
288298 iex> serialize_first_error([{"name", {"should be at most %{count} byte(s)", [count: 255]}}])
0 commit comments