mobilizon.chapril.org-mobil.../lib/web/cache/activity_pub.ex

81 lines
2.1 KiB
Elixir
Raw Normal View History

2020-01-26 21:36:50 +01:00
defmodule Mobilizon.Web.Cache.ActivityPub do
2019-09-17 23:39:26 +02:00
@moduledoc """
2020-01-23 21:59:50 +01:00
ActivityPub related cache.
2019-09-17 23:39:26 +02:00
"""
2020-01-22 02:14:42 +01:00
alias Mobilizon.{Actors, Events, Tombstone}
2019-09-17 23:39:26 +02:00
alias Mobilizon.Actors.Actor
alias Mobilizon.Events.{Comment, Event}
2020-01-22 02:14:42 +01:00
alias Mobilizon.Federation.ActivityPub.Relay
2020-01-26 21:36:50 +01:00
alias Mobilizon.Web.Endpoint
alias Mobilizon.Web.Router.Helpers, as: Routes
2019-09-17 23:39:26 +02:00
@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
2019-09-17 23:39:26 +02:00
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
2020-01-22 02:14:42 +01:00
Cachex.fetch(@cache, "relay_actor", &Relay.get_actor/0)
2019-09-17 23:39:26 +02:00
end
end