Rename MobilizonWeb to Mobilizon.Web
This commit is contained in:
80
lib/web/cache/activity_pub.ex
vendored
Normal file
80
lib/web/cache/activity_pub.ex
vendored
Normal file
@@ -0,0 +1,80 @@
|
||||
defmodule Mobilizon.Web.Cache.ActivityPub do
|
||||
@moduledoc """
|
||||
ActivityPub related cache.
|
||||
"""
|
||||
|
||||
alias Mobilizon.{Actors, Events, Tombstone}
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events.{Comment, Event}
|
||||
|
||||
alias Mobilizon.Federation.ActivityPub.Relay
|
||||
|
||||
alias Mobilizon.Web.Endpoint
|
||||
alias Mobilizon.Web.Router.Helpers, as: Routes
|
||||
|
||||
@cache :activity_pub
|
||||
|
||||
@doc """
|
||||
Gets a local actor by username.
|
||||
"""
|
||||
@spec get_local_actor_by_name(String.t()) ::
|
||||
{:commit, Actor.t()} | {:ignore, nil}
|
||||
def get_local_actor_by_name(name) do
|
||||
Cachex.fetch(@cache, "actor_" <> name, fn "actor_" <> name ->
|
||||
case Actors.get_local_actor_by_name(name) do
|
||||
%Actor{} = actor ->
|
||||
{:commit, actor}
|
||||
|
||||
nil ->
|
||||
{:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a public event by its UUID, with all associations loaded.
|
||||
"""
|
||||
@spec get_public_event_by_uuid_with_preload(String.t()) ::
|
||||
{:commit, Event.t()} | {:ignore, nil}
|
||||
def get_public_event_by_uuid_with_preload(uuid) do
|
||||
Cachex.fetch(@cache, "event_" <> uuid, fn "event_" <> uuid ->
|
||||
case Events.get_public_event_by_uuid_with_preload(uuid) do
|
||||
%Event{} = event ->
|
||||
{:commit, event}
|
||||
|
||||
nil ->
|
||||
with url <- Routes.page_url(Endpoint, :event, uuid),
|
||||
%Tombstone{} = tomstone <- Tombstone.find_tombstone(url) do
|
||||
tomstone
|
||||
else
|
||||
_ -> {:ignore, nil}
|
||||
end
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a comment by its UUID, with all associations loaded.
|
||||
"""
|
||||
@spec get_comment_by_uuid_with_preload(String.t()) ::
|
||||
{:commit, Comment.t()} | {:ignore, nil}
|
||||
def get_comment_by_uuid_with_preload(uuid) do
|
||||
Cachex.fetch(@cache, "comment_" <> uuid, fn "comment_" <> uuid ->
|
||||
case Events.get_comment_from_uuid_with_preload(uuid) do
|
||||
%Comment{} = comment ->
|
||||
{:commit, comment}
|
||||
|
||||
nil ->
|
||||
{:ignore, nil}
|
||||
end
|
||||
end)
|
||||
end
|
||||
|
||||
@doc """
|
||||
Gets a relay.
|
||||
"""
|
||||
@spec get_relay :: {:commit, Actor.t()} | {:ignore, nil}
|
||||
def get_relay do
|
||||
Cachex.fetch(@cache, "relay_actor", &Relay.get_actor/0)
|
||||
end
|
||||
end
|
||||
24
lib/web/cache/cache.ex
vendored
Normal file
24
lib/web/cache/cache.ex
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
defmodule Mobilizon.Web.Cache do
|
||||
@moduledoc """
|
||||
Facade module which provides access to all cached data.
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors.Actor
|
||||
|
||||
alias Mobilizon.Web.Cache.ActivityPub
|
||||
|
||||
@caches [:activity_pub, :feed, :ics]
|
||||
|
||||
@doc """
|
||||
Clears all caches for an actor.
|
||||
"""
|
||||
@spec clear_cache(Actor.t()) :: {:ok, true}
|
||||
def clear_cache(%Actor{preferred_username: preferred_username, domain: nil}) do
|
||||
Enum.each(@caches, &Cachex.del(&1, "actor_" <> preferred_username))
|
||||
end
|
||||
|
||||
defdelegate get_local_actor_by_name(name), to: ActivityPub
|
||||
defdelegate get_public_event_by_uuid_with_preload(uuid), to: ActivityPub
|
||||
defdelegate get_comment_by_uuid_with_preload(uuid), to: ActivityPub
|
||||
defdelegate get_relay, to: ActivityPub
|
||||
end
|
||||
Reference in New Issue
Block a user