Merge branch 'fixes' into 'main'

Make sure every relation of actor is loaded when operating on it

Closes #1049, #1016 et #996

See merge request framasoft/mobilizon!1196
This commit is contained in:
Thomas Citharel 2022-04-01 13:42:33 +00:00
commit 2e1c284565
11 changed files with 74 additions and 24 deletions

View File

@ -259,6 +259,7 @@ export default class GroupSettings extends mixins(GroupMixin) {
@Watch("group") @Watch("group")
async watchUpdateGroup(oldGroup: IGroup, newGroup: IGroup): Promise<void> { async watchUpdateGroup(oldGroup: IGroup, newGroup: IGroup): Promise<void> {
try {
if ( if (
oldGroup?.avatar !== undefined && oldGroup?.avatar !== undefined &&
oldGroup?.avatar !== newGroup?.avatar oldGroup?.avatar !== newGroup?.avatar
@ -271,6 +272,10 @@ export default class GroupSettings extends mixins(GroupMixin) {
) { ) {
this.bannerFile = await buildFileFromIMedia(this.group.banner); this.bannerFile = await buildFileFromIMedia(this.group.banner);
} }
} catch (e) {
// Catch errors while building media
console.error(e);
}
this.editableGroup = { ...this.group }; this.editableGroup = { ...this.group };
} }

View File

@ -93,6 +93,7 @@
</b-select> </b-select>
</b-field> </b-field>
<b-field <b-field
v-if="config"
expanded expanded
:label="$t('Category')" :label="$t('Category')"
label-for="category" label-for="category"

View File

@ -58,6 +58,8 @@ defmodule Mobilizon.Actors do
@moderator_roles [:moderator] ++ @administrator_roles @moderator_roles [:moderator] ++ @administrator_roles
@member_roles [:member] ++ @moderator_roles @member_roles [:member] ++ @moderator_roles
@associations_to_preload [:organized_events, :followers, :followings, :user, :physical_address]
@doc """ @doc """
Gets a single actor. Gets a single actor.
""" """
@ -169,7 +171,7 @@ defmodule Mobilizon.Actors do
def get_local_actor_by_name_with_preload(name) do def get_local_actor_by_name_with_preload(name) do
name name
|> get_local_actor_by_name() |> get_local_actor_by_name()
|> Repo.preload([:organized_events, :followers, :followings]) |> Repo.preload(@associations_to_preload)
end end
@doc """ @doc """
@ -179,7 +181,7 @@ defmodule Mobilizon.Actors do
def get_actor_by_name_with_preload(name, type \\ nil) do def get_actor_by_name_with_preload(name, type \\ nil) do
name name
|> get_actor_by_name(type) |> get_actor_by_name(type)
|> Repo.preload([:organized_events, :user, :physical_address]) |> Repo.preload(@associations_to_preload)
end end
@doc """ @doc """
@ -246,7 +248,7 @@ defmodule Mobilizon.Actors do
@spec update_actor(Actor.t(), map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()} @spec update_actor(Actor.t(), map) :: {:ok, Actor.t()} | {:error, Ecto.Changeset.t()}
def update_actor(%Actor{} = actor, attrs) do def update_actor(%Actor{} = actor, attrs) do
actor actor
|> Repo.preload([:physical_address]) |> Repo.preload(@associations_to_preload)
|> Actor.update_changeset(attrs) |> Actor.update_changeset(attrs)
|> delete_files_if_media_changed() |> delete_files_if_media_changed()
|> Repo.update() |> Repo.update()
@ -271,7 +273,7 @@ defmodule Mobilizon.Actors do
case insert do case insert do
{:ok, actor} -> {:ok, actor} ->
actor = if preload, do: Repo.preload(actor, [:followers]), else: actor actor = if preload, do: Repo.preload(actor, @associations_to_preload), else: actor
{:ok, actor} {:ok, actor}
@ -1304,7 +1306,7 @@ defmodule Mobilizon.Actors do
defp actor_with_preload_query(actor_id, true) do defp actor_with_preload_query(actor_id, true) do
Actor Actor
|> where([a], a.id == ^actor_id) |> where([a], a.id == ^actor_id)
|> preload([a], [:organized_events, :followers, :followings]) |> preload([a], ^@associations_to_preload)
end end
@spec actor_by_username_query(String.t()) :: Ecto.Query.t() @spec actor_by_username_query(String.t()) :: Ecto.Query.t()

View File

@ -190,6 +190,29 @@ defmodule Mobilizon.Service.ActorSuspension do
defp delete_discussions(%Multi{} = multi, %Actor{type: :Group, id: actor_id}) do defp delete_discussions(%Multi{} = multi, %Actor{type: :Group, id: actor_id}) do
Logger.debug("Delete group's discussions") Logger.debug("Delete group's discussions")
multi =
Multi.run(multi, :group_discussion_comments, fn _, _ ->
group_comments_ids =
Comment
|> join(:inner, [c], d in Discussion, on: c.discussion_id == d.id)
|> where([_c, d], d.actor_id == ^actor_id)
|> select([c], [c.id])
|> Repo.all()
|> Enum.concat()
{:ok, group_comments_ids}
end)
multi =
Multi.delete_all(
multi,
:delete_discussions_comments,
fn %{group_discussion_comments: group_discussion_comments} ->
where(Comment, [c], c.id in ^group_discussion_comments)
end
)
Multi.delete_all(multi, :delete_discussions, where(Discussion, [e], e.actor_id == ^actor_id)) Multi.delete_all(multi, :delete_discussions, where(Discussion, [e], e.actor_id == ^actor_id))
end end

View File

@ -7,7 +7,7 @@ defmodule Mobilizon.Service.Address do
@type address :: %{name: String.t(), alternative_name: String.t()} @type address :: %{name: String.t(), alternative_name: String.t()}
@spec render_address(AddressModel.t()) :: String.t() | no_return @spec render_address(AddressModel.t()) :: String.t() | nil
def render_address(%AddressModel{} = address) do def render_address(%AddressModel{} = address) do
%{name: name, alternative_name: alternative_name} = render_names(address) %{name: name, alternative_name: alternative_name} = render_names(address)
@ -22,7 +22,18 @@ defmodule Mobilizon.Service.Address do
alternative_name alternative_name
true -> true ->
nil
end
end
@spec render_address!(AddressModel.t()) :: String.t() | no_return
def render_address!(%AddressModel{} = address) do
case render_address(address) do
nil ->
raise ArgumentError, message: "Invalid address" raise ArgumentError, message: "Invalid address"
address ->
address
end end
end end

View File

@ -9,7 +9,7 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
alias Mobilizon.Web.Router.Helpers, as: Routes alias Mobilizon.Web.Router.Helpers, as: Routes
import Mobilizon.Service.Metadata.Utils, import Mobilizon.Service.Metadata.Utils,
only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address: 1] only: [process_description: 2, strip_tags: 1, datetime_to_string: 2, render_address!: 1]
def build_tags(%Event{} = event, locale \\ "en") do def build_tags(%Event{} = event, locale \\ "en") do
formatted_description = description(event, locale) formatted_description = description(event, locale)
@ -150,7 +150,13 @@ defimpl Mobilizon.Service.Metadata, for: Mobilizon.Events.Event do
@spec maybe_build_address(list(String.t()), Address.t() | nil) :: list(String.t()) @spec maybe_build_address(list(String.t()), Address.t() | nil) :: list(String.t())
defp maybe_build_address(elements, %Address{} = address) do defp maybe_build_address(elements, %Address{} = address) do
elements ++ [render_address(address)] elements ++ [render_address!(address)]
rescue
# If the address is not renderable
e in ArgumentError ->
require Logger
Logger.error(Exception.format(:error, e, __STACKTRACE__))
elements
end end
defp maybe_build_address(elements, _address), do: elements defp maybe_build_address(elements, _address), do: elements

View File

@ -54,7 +54,7 @@ defmodule Mobilizon.Service.Metadata.Utils do
end end
defdelegate datetime_to_string(datetime, locale \\ "en", format \\ :medium), to: DateTime defdelegate datetime_to_string(datetime, locale \\ "en", format \\ :medium), to: DateTime
defdelegate render_address(address), to: Address defdelegate render_address!(address), to: Address
defp maybe_slice(description, nil), do: description defp maybe_slice(description, nil), do: description

View File

@ -26,7 +26,7 @@
</td> </td>
</tr> </tr>
<!-- row for datetime end --> <!-- row for datetime end -->
<%= if @event.physical_address do %> <%= if not is_nil(@event.physical_address) and not is_nil(render_address(@event.physical_address)) do %>
<!-- venue block --> <!-- venue block -->
<tr style="vertical-align: baseline; margin: 0; padding: 0; border: 0;" align="left"> <tr style="vertical-align: baseline; margin: 0; padding: 0; border: 0;" align="left">
<td style="word-wrap: break-word; border-collapse: collapse; color: rgb(10,10,10); font-family: Helvetica,Arial,sans-serif; font-weight: normal; line-height: 1.3; font-size: 16px; margin: 0; padding: 0 12px 0 0; border: 0;" valign="baseline" align="left"> <td style="word-wrap: break-word; border-collapse: collapse; color: rgb(10,10,10); font-family: Helvetica,Arial,sans-serif; font-weight: normal; line-height: 1.3; font-size: 16px; margin: 0; padding: 0 12px 0 0; border: 0;" valign="baseline" align="left">

View File

@ -1,2 +1,2 @@
<%= gettext("Date:") %> <%= render("date/event_tz_date_range.text", event: @event, start_date: datetime_tz_convert(@event.begins_on, @event.options.timezone), end_date: datetime_tz_convert(@event.ends_on, @event.options.timezone), timezone: @timezone, locale: @locale) %> <%= gettext("Date:") %> <%= render("date/event_tz_date_range.text", event: @event, start_date: datetime_tz_convert(@event.begins_on, @event.options.timezone), end_date: datetime_tz_convert(@event.ends_on, @event.options.timezone), timezone: @timezone, locale: @locale) %>
<%= gettext("Address:") %> <%= if @event.physical_address do %><%= render_address(@event.physical_address) %><% end %><%= if @event.options.is_online do %><%= gettext "Online event" %><% end %> <%= gettext("Address:") %> <%= if not is_nil(@event.physical_address) and not is_nil(render_address(@event.physical_address)) do %><%= render_address(@event.physical_address) %><% end %><%= if @event.options.is_online do %><%= gettext "Online event" %><% end %>

View File

@ -125,7 +125,7 @@ th {
<dt><%= gettext("Ends on") %></dt> <dt><%= gettext("Ends on") %></dt>
<dd><%= datetime_to_string(@event.ends_on, @locale, :long) %></dd> <dd><%= datetime_to_string(@event.ends_on, @locale, :long) %></dd>
<% end %> <% end %>
<%= if @event.physical_address do %> <%= if not is_nil(@event.physical_address) and not is_nil(render_address(@event.physical_address)) do %>
<dt><%= gettext("Location") %></dt> <dt><%= gettext("Location") %></dt>
<dd><%= render_address(@event.physical_address) %></dd> <dd><%= render_address(@event.physical_address) %></dd>
<% end %> <% end %>

View File

@ -50,8 +50,10 @@ defmodule Mobilizon.Service.AddressTest do
end end
test "with no data" do test "with no data" do
assert AddressRenderer.render_address(%Address{}) == nil
assert_raise ArgumentError, "Invalid address", fn -> assert_raise ArgumentError, "Invalid address", fn ->
AddressRenderer.render_address(%Address{}) AddressRenderer.render_address!(%Address{})
end end
end end
end end