I’m working on a Phoenix LiveView app that uses boundary, and we’ve bumped into warnings writing out a way to do notifications. There are a couple of tricks that would remove these warnings, but I care about the right way to model data and to organize the code.
Here’s a rough example of the code I’m working with:
defmodule MyApp.Notifications do
@moduledoc """
The Notifications module is responsible for managing user notifications - both in-app and email.
"""
use Boundary, top_level?: true
# importing verified routes
use MyAppWeb, :verified_routes
def send_invitation(invitation) do
to =
if invitation.user_id do
url(~p"/app/users/invitations")
else
url(~p"/auth/register"))
end
MyApp.Mailer.deliver_invitation_email(invitation, to)
send_invitation_notification(invitation)
# ...
:ok
end
defp send_notification(invitation) do
# ...
end
end
The real code is a bit more complicated, but hopefully this illustrates the issue.
We’ve configured Boundary
to reject any calls to from the MyApp
context to the MyAppWeb
context, which makes this module not pass the boundary checks:
warning: forbidden reference to MyAppWeb
(references from MyApp.Notifications to MyAppWeb are not allowed)
lib/my_app/notifications.ex:17
It makes sense to prevent access to the Web context from the non-Web, but in this case we’re, were just trying to use verified routes so that the links for the invitations are verified to exist. Previously we could use Phoenix.Router.Helpers
, but from the docs, it looks like the routes module helper is deprecated, so no help there.
Moving the logic that switches between the possible URLs inside the Mailer doesn’t seem helpful, since it looks like we’re leaking the logic around Invitations when Mailer should just care about sending email.
We’ve also toyed with a few ideas that basically revolve around replacing the Route Helpers module, but quickly ran into the thought of “there’s got to be a better way to do this”.
Can someone help us organize our code a bit better?
Thanks!