Commit e55c6f31 authored by Thog's avatar Thog

Migrate to comeonin 4 and Cachex 3

Also fix some warning in the code and add a missing alias
parent 413de8e4
......@@ -21,7 +21,7 @@ config :pleroma, Pleroma.Repo,
pool: Ecto.Adapters.SQL.Sandbox
# Reduce hash rounds for testing
config :comeonin, :pbkdf2_rounds, 1
config :pbkdf2_elixir, rounds: 1
config :pleroma, :websub, Pleroma.Web.WebsubMock
config :pleroma, :ostatus, Pleroma.Web.OStatusMock
......
......@@ -5,6 +5,7 @@ defmodule Pleroma.Application do
# for more information on OTP Applications
def start(_type, _args) do
import Supervisor.Spec
import Cachex.Spec
# Define workers and child supervisors to be supervised
children =
......@@ -28,8 +29,11 @@ defmodule Pleroma.Application do
[
:idempotency_cache,
[
default_ttl: :timer.seconds(6 * 60 * 60),
ttl_interval: :timer.seconds(60),
expiration:
expiration(
default: :timer.seconds(6 * 60 * 60),
interval: :timer.seconds(60)
),
limit: 2500
]
],
......
......@@ -33,19 +33,15 @@ defmodule Pleroma.Object do
else
key = "object:#{ap_id}"
Cachex.get!(
:user_cache,
key,
fallback: fn _ ->
object = get_by_ap_id(ap_id)
if object do
{:commit, object}
else
{:ignore, object}
end
Cachex.fetch!(:user_cache, key, fn _ ->
object = get_by_ap_id(ap_id)
if object do
{:commit, object}
else
{:ignore, object}
end
)
end)
end
end
......
......@@ -223,9 +223,9 @@ defmodule Pleroma.User do
def update_and_set_cache(changeset) do
with {:ok, user} <- Repo.update(changeset) do
Cachex.set(:user_cache, "ap_id:#{user.ap_id}", user)
Cachex.set(:user_cache, "nickname:#{user.nickname}", user)
Cachex.set(:user_cache, "user_info:#{user.id}", user_info(user))
Cachex.put(:user_cache, "ap_id:#{user.ap_id}", user)
Cachex.put(:user_cache, "nickname:#{user.nickname}", user)
Cachex.put(:user_cache, "user_info:#{user.id}", user_info(user))
{:ok, user}
else
e -> e
......@@ -239,12 +239,12 @@ defmodule Pleroma.User do
def get_cached_by_ap_id(ap_id) do
key = "ap_id:#{ap_id}"
Cachex.get!(:user_cache, key, fallback: fn _ -> get_by_ap_id(ap_id) end)
Cachex.fetch!(:user_cache, key, fn _ -> get_by_ap_id(ap_id) end)
end
def get_cached_by_nickname(nickname) do
key = "nickname:#{nickname}"
Cachex.get!(:user_cache, key, fallback: fn _ -> get_or_fetch_by_nickname(nickname) end)
Cachex.fetch!(:user_cache, key, fn _ -> get_or_fetch_by_nickname(nickname) end)
end
def get_by_nickname(nickname) do
......@@ -260,7 +260,7 @@ defmodule Pleroma.User do
def get_cached_user_info(user) do
key = "user_info:#{user.id}"
Cachex.get!(:user_cache, key, fallback: fn _ -> user_info(user) end)
Cachex.fetch!(:user_cache, key, fn _ -> user_info(user) end)
end
def fetch_by_nickname(nickname) do
......
......@@ -229,7 +229,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
"object" => %{"type" => "Announce", "object" => object_id},
"actor" => actor,
"id" => id
} = data
} = _data
) do
with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
{:ok, object} <-
......@@ -237,7 +237,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
{:ok, activity, _, _} <- ActivityPub.unannounce(actor, object, id, false) do
{:ok, activity}
else
e -> :error
_e -> :error
end
end
......@@ -247,7 +247,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
"object" => %{"type" => "Like", "object" => object_id},
"actor" => actor,
"id" => id
} = data
} = _data
) do
with %User{} = actor <- User.get_or_fetch_by_ap_id(actor),
{:ok, object} <-
......@@ -255,7 +255,7 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
{:ok, activity, _, _} <- ActivityPub.unlike(actor, object, id, false) do
{:ok, activity}
else
e -> :error
_e -> :error
end
end
......@@ -516,10 +516,10 @@ defmodule Pleroma.Web.ActivityPub.Transmogrifier do
def maybe_fix_user_url(data) do
if is_map(data["url"]) do
data = Map.put(data, "url", data["url"]["href"])
Map.put(data, "url", data["url"]["href"])
else
data
end
data
end
def maybe_fix_user_object(data) do
......
......@@ -2,6 +2,7 @@
defmodule Pleroma.Web.HTTPSignatures do
alias Pleroma.User
alias Pleroma.Web.ActivityPub.Utils
alias Pleroma.Web.ActivityPub.ActivityPub
require Logger
def split_signature(sig) do
......
......@@ -275,11 +275,7 @@ defmodule Pleroma.Web.MastodonAPI.MastodonAPIController do
end
{:ok, activity} =
Cachex.get!(
:idempotency_cache,
idempotency_key,
fallback: fn _ -> CommonAPI.post(user, params) end
)
Cachex.fetch!(:idempotency_cache, idempotency_key, fn _ -> CommonAPI.post(user, params) end)
render(conn, StatusView, "status.json", %{activity: activity, for: user, as: :activity})
end
......
......@@ -36,12 +36,13 @@ defmodule Pleroma.Mixfile do
{:postgrex, ">= 0.0.0"},
{:gettext, "~> 0.11"},
{:cowboy, "~> 1.0", override: true},
{:comeonin, "~> 3.0"},
{:comeonin, "~> 4.0"},
{:pbkdf2_elixir, "~> 0.12"},
{:trailing_format_plug, "~> 0.0.5"},
{:html_sanitize_ex, "~> 1.3.0-rc1"},
{:phoenix_html, "~> 2.10"},
{:calendar, "~> 0.16.1"},
{:cachex, "~> 2.1"},
{:cachex, "~> 3.0"},
{:httpoison, "~> 1.1.0"},
{:jason, "~> 1.0"},
{:ex_machina, "~> 2.0", only: :test},
......
This diff is collapsed.
......@@ -24,7 +24,7 @@ defmodule Pleroma.Web.TwitterAPI.NotificationViewTest do
{:ok, follower} = User.follow(follower, user)
{:ok, activity} = ActivityPub.follow(follower, user)
Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
[follow_notif] = Notification.for_user(user)
represented = %{
......
......@@ -31,7 +31,7 @@ defmodule Pleroma.Web.TwitterAPI.UserViewTest do
User.follow(second_follower, user)
User.follow(user, follower)
{:ok, user} = User.update_follower_count(user)
Cachex.set(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
Cachex.put(:user_cache, "user_info:#{user.id}", User.user_info(Repo.get!(User, user.id)))
image = "http://localhost:4001/images/avi.png"
banner = "http://localhost:4001/images/banner.png"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment