Skip to content

Commit 256a115

Browse files
committed
load target association with Activities.get
1 parent ee763e3 commit 256a115

File tree

4 files changed

+56
-18
lines changed

4 files changed

+56
-18
lines changed

config/config.exs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,9 @@ config :algora, Oban,
3131
event_consumers: 1,
3232
comment_consumers: 1,
3333
github_og_image: 5,
34-
notify_bounty: 1
34+
notify_bounty: 1,
35+
activity_notifier: 1,
36+
email_notfier: 1
3537
]
3638

3739
# Configures the mailer

lib/algora/activities/activities.ex

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -52,9 +52,26 @@ defmodule Algora.Activities do
5252
transactions: "transaction_activities"
5353
}
5454

55+
@table_from_schema Map.new(@schema_from_table, &{elem(&1, 1), elem(&1, 0)})
5556
@tables Map.keys(@schema_from_table)
5657
@user_attributes Map.keys(@table_from_user_relation)
5758

59+
def schema_from_table(name) when is_binary(name), do: name |> String.to_atom() |> schema_from_table()
60+
61+
def schema_from_table(name) when is_atom(name) do
62+
Map.fetch!(@schema_from_table, name)
63+
end
64+
65+
def table_from_schema(name) when is_binary(name), do: name |> String.to_atom() |> table_from_schema()
66+
67+
def table_from_schema(name) when is_atom(name) do
68+
Map.fetch!(@table_from_schema, name)
69+
end
70+
71+
def table_from_user_relation(table) do
72+
Map.fetch!(@table_from_user_relation, table)
73+
end
74+
5875
def tables, do: @tables
5976
def user_attributes, do: @user_attributes
6077

@@ -167,14 +184,21 @@ defmodule Algora.Activities do
167184
end
168185

169186
def get(table, id) do
187+
assoc_query =
188+
from t in schema_from_table(table),
189+
where: parent_as(:activity).assoc_id == t.id
190+
170191
query =
171192
from a in table,
193+
as: :activity,
172194
where: a.id == ^id,
195+
inner_lateral_join: t in subquery(assoc_query),
173196
select: %{
174197
id: a.id,
175198
type: a.type,
176199
assoc_id: a.assoc_id,
177200
assoc_name: ^table,
201+
assoc: t,
178202
inserted_at: a.inserted_at
179203
}
180204

@@ -212,16 +236,6 @@ defmodule Algora.Activities do
212236
build_url(activity)
213237
end
214238

215-
def schema_from_table(name) when is_binary(name), do: name |> String.to_atom() |> schema_from_table()
216-
217-
def schema_from_table(name) when is_atom(name) do
218-
Map.fetch!(@schema_from_table, name)
219-
end
220-
221-
def table_from_user_relation(table) do
222-
Map.fetch!(@table_from_user_relation, table)
223-
end
224-
225239
def build_url(%{assoc: %Bounty{owner: user}}), do: {:ok, "/org/#{user.handle}/bounties"}
226240
def build_url(%{assoc: %Identity{user: %{type: :individual} = user}}), do: {:ok, "/@/#{user.handle}"}
227241
def build_url(%{assoc: %Identity{user: %{type: :organization} = user}}), do: {:ok, "/org/#{user.handle}"}
Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,38 @@
11
defmodule Algora.Activities.Notifier do
22
@moduledoc false
33
use Oban.Worker,
4-
queue: :activities,
5-
max_attempts: 1,
6-
tags: ["notify", "activities"]
4+
queue: :activity_notifier,
5+
max_attempts: 1
6+
7+
alias Algora.Activities
78

89
# unique: [period: 30]
910

1011
def changeset(activity, target) do
11-
new(%{activity_id: activity.id, target_id: target.id})
12+
case Activities.table_from_schema(target.__meta__.schema) do
13+
nil ->
14+
:error
15+
16+
table when is_atom(table) ->
17+
new(%{activity_id: activity.id, target_id: target.id, table_name: table})
18+
end
1219
end
1320

1421
@impl Oban.Worker
15-
def perform(%Oban.Job{args: %{}} = job) do
16-
IO.inspect(job)
17-
:ok
22+
def perform(%Oban.Job{args: args} = job) do
23+
case args do
24+
%{
25+
"activity_id" => activity_id,
26+
"target_id" => target_id,
27+
"table_name" => table
28+
} = args
29+
when is_binary(table) ->
30+
_activity = Activities.get(table, activity_id)
31+
32+
:ok
33+
34+
_args ->
35+
:error
36+
end
1837
end
1938
end

test/algora/contracts_test.exs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ defmodule Algora.ContractsTest do
77

88
alias Algora.Activities
99
alias Algora.Contracts
10+
alias Algora.Contracts.Contract
1011
alias Algora.Payments
1112
alias Algora.Payments.Transaction
1213

@@ -253,6 +254,8 @@ defmodule Algora.ContractsTest do
253254

254255
assert [contract_activity | _rest] = Enum.reverse(Activities.all())
255256
assert contract_activity.assoc.id == contract_a_0.id
257+
258+
assert Activities.get(contract_activity.assoc_name, contract_activity.id).assoc.__meta__.schema == Contract
256259
end
257260

258261
test "prepayment fails when payment method is invalid" do

0 commit comments

Comments
 (0)