Fix issue when updating event and introduce background jobs
Signed-off-by: Thomas Citharel <tcit@tcit.fr>
This commit is contained in:
@@ -4,6 +4,7 @@ defmodule Mobilizon.Service.ActivityPub.Converter.Utils do
|
||||
"""
|
||||
|
||||
alias Mobilizon.Actors.Actor
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Tag
|
||||
alias Mobilizon.Mention
|
||||
alias Mobilizon.Service.ActivityPub
|
||||
@@ -66,7 +67,7 @@ defmodule Mobilizon.Service.ActivityPub.Converter.Utils do
|
||||
defp fetch_tag(tag, acc) when is_map(tag) do
|
||||
case tag["type"] do
|
||||
"Hashtag" ->
|
||||
acc ++ [%{title: tag}]
|
||||
acc ++ [existing_tag_or_data(tag["name"])]
|
||||
|
||||
_err ->
|
||||
acc
|
||||
@@ -74,7 +75,18 @@ defmodule Mobilizon.Service.ActivityPub.Converter.Utils do
|
||||
end
|
||||
|
||||
defp fetch_tag(tag, acc) when is_bitstring(tag) do
|
||||
acc ++ [%{title: tag}]
|
||||
acc ++ [existing_tag_or_data(tag)]
|
||||
end
|
||||
|
||||
defp existing_tag_or_data("#" <> tag_title) do
|
||||
existing_tag_or_data(tag_title)
|
||||
end
|
||||
|
||||
defp existing_tag_or_data(tag_title) do
|
||||
case Events.get_tag_by_title(tag_title) do
|
||||
%Tag{} = tag -> %{title: tag.title, id: tag.id}
|
||||
nil -> %{title: tag_title}
|
||||
end
|
||||
end
|
||||
|
||||
@spec create_mention(map(), list()) :: list()
|
||||
|
||||
@@ -1,12 +1,28 @@
|
||||
defmodule Mobilizon.Service.Search do
|
||||
defmodule Mobilizon.Service.Workers.BuildSearchWorker do
|
||||
@moduledoc """
|
||||
Module to handle search service
|
||||
Worker to build search results
|
||||
"""
|
||||
|
||||
alias Mobilizon.Events
|
||||
alias Mobilizon.Events.Event
|
||||
alias Mobilizon.Storage.Repo
|
||||
alias Ecto.Adapters.SQL
|
||||
|
||||
use Mobilizon.Service.Workers.WorkerHelper, queue: "search"
|
||||
|
||||
@impl Oban.Worker
|
||||
def perform(%{"op" => "insert_search_event", "event_id" => event_id}, _job) do
|
||||
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id) do
|
||||
insert_search_event(event)
|
||||
end
|
||||
end
|
||||
|
||||
def perform(%{"op" => "update_search_event", "event_id" => event_id}, _job) do
|
||||
with {:ok, %Event{} = event} <- Events.get_event_with_preload(event_id) do
|
||||
update_search_event(event)
|
||||
end
|
||||
end
|
||||
|
||||
def insert_search_event(%Event{} = event) do
|
||||
SQL.query(
|
||||
Repo,
|
||||
50
lib/service/workers/worker_helper.ex
Normal file
50
lib/service/workers/worker_helper.ex
Normal file
@@ -0,0 +1,50 @@
|
||||
# Portions of this file are derived from Pleroma:
|
||||
# Copyright © 2017-2019 Pleroma Authors <https://pleroma.social>
|
||||
# SPDX-License-Identifier: AGPL-3.0-only
|
||||
# Upstream: https://git.pleroma.social/pleroma/pleroma/blob/develop/lib/pleroma/workers/worker_helper.ex
|
||||
|
||||
defmodule Mobilizon.Service.Workers.WorkerHelper do
|
||||
@moduledoc """
|
||||
Tools to ease dealing with workers
|
||||
"""
|
||||
alias Mobilizon.Config
|
||||
alias Mobilizon.Service.Workers.WorkerHelper
|
||||
|
||||
def worker_args(queue) do
|
||||
case Config.get([:workers, :retries, queue]) do
|
||||
nil -> []
|
||||
max_attempts -> [max_attempts: max_attempts]
|
||||
end
|
||||
end
|
||||
|
||||
def sidekiq_backoff(attempt, pow \\ 4, base_backoff \\ 15) do
|
||||
backoff =
|
||||
:math.pow(attempt, pow) +
|
||||
base_backoff +
|
||||
:rand.uniform(2 * base_backoff) * attempt
|
||||
|
||||
trunc(backoff)
|
||||
end
|
||||
|
||||
defmacro __using__(opts) do
|
||||
caller_module = __CALLER__.module
|
||||
queue = Keyword.fetch!(opts, :queue)
|
||||
|
||||
quote do
|
||||
# Note: `max_attempts` is intended to be overridden in `new/2` call
|
||||
use Oban.Worker,
|
||||
queue: unquote(queue),
|
||||
max_attempts: 1
|
||||
|
||||
def enqueue(operation, params, worker_args \\ []) do
|
||||
params = Map.merge(%{"op" => operation}, params)
|
||||
queue_atom = String.to_existing_atom(unquote(queue))
|
||||
worker_args = worker_args ++ WorkerHelper.worker_args(queue_atom)
|
||||
|
||||
unquote(caller_module)
|
||||
|> apply(:new, [params, worker_args])
|
||||
|> Mobilizon.Storage.Repo.insert()
|
||||
end
|
||||
end
|
||||
end
|
||||
end
|
||||
Reference in New Issue
Block a user