@@ -2,17 +2,19 @@ defmodule Algora.Activities do
2
2
@ moduledoc false
3
3
import Ecto.Query
4
4
5
+ alias Algora.Accounts.Identity
5
6
alias Algora.Accounts.User
6
7
alias Algora.Activities.Activity
8
+ alias Algora.Bounties.Bounty
7
9
alias Algora.Repo
8
10
alias Ecto.Multi
9
11
10
12
@ schema_from_table % {
11
- identity_activities: Algora.Accounts. Identity,
13
+ identity_activities: Identity ,
12
14
user_activities: Algora.Accounts.User ,
13
15
attempt_activities: Algora.Bounties.Attempt ,
14
16
bonus_activities: Algora.Bounties.Bonus ,
15
- bounty_activities: Algora.Bounties. Bounty,
17
+ bounty_activities: Bounty ,
16
18
claim_activities: Algora.Bounties.Claim ,
17
19
tip_activities: Algora.Bounties.Tip ,
18
20
message_activities: Algora.Chat.Message ,
@@ -40,9 +42,9 @@ defmodule Algora.Activities do
40
42
connected_installations: "installation_activities" ,
41
43
contractor_contracts: "contract_activities" ,
42
44
created_bounties: "bounty_activities" ,
45
+ # owned_bounties: "bounty_activities",
43
46
owned_tips: "tip_activities" ,
44
47
created_tips: "tip_activities" ,
45
- owned_bounties: "bounty_activities" ,
46
48
identities: "identity_activities" ,
47
49
owned_installations: "installation_activities" ,
48
50
# projects: "project_activities",
@@ -147,20 +149,14 @@ defmodule Algora.Activities do
147
149
|> Algora.Repo . insert ( )
148
150
end
149
151
150
- def dataloader do
151
- Dataloader . add_source (
152
- Dataloader . new ( ) ,
153
- :db ,
154
- Dataloader.Ecto . new ( Algora.Repo )
155
- )
156
- end
157
-
158
152
def all_with_assoc ( query ) do
159
153
activities = Repo . all ( query )
154
+ source = Dataloader.Ecto . new ( Algora.Repo )
155
+ dataloader = Dataloader . add_source ( Dataloader . new ( ) , :db , source )
160
156
161
157
loader =
162
158
activities
163
- |> Enum . reduce ( dataloader ( ) , fn activity , loader ->
159
+ |> Enum . reduce ( dataloader , fn activity , loader ->
164
160
schema = schema_from_table ( activity . assoc_name )
165
161
Dataloader . load ( loader , :db , schema , activity . assoc_id )
166
162
end )
@@ -173,11 +169,75 @@ defmodule Algora.Activities do
173
169
end )
174
170
end
175
171
176
- def schema_from_table ( name ) do
177
- Map . fetch! ( @ schema_from_table , String . to_atom ( name ) )
172
+ def get ( table , id ) do
173
+ query =
174
+ from a in table ,
175
+ where: a . id == ^ id ,
176
+ select: % {
177
+ id: a . id ,
178
+ type: a . type ,
179
+ assoc_id: a . assoc_id ,
180
+ assoc_name: ^ table ,
181
+ inserted_at: a . inserted_at
182
+ }
183
+
184
+ Algora.Repo . one ( query )
185
+ end
186
+
187
+ def get_assoc ( prefix , assoc_id ) when prefix in [ "bounty_activities" ] do
188
+ get_assoc ( prefix , assoc_id , [ :owner ] )
189
+ end
190
+
191
+ def get_assoc ( prefix , assoc_id ) when prefix in [ "identity_activities" ] do
192
+ get_assoc ( prefix , assoc_id , [ :user ] )
193
+ end
194
+
195
+ def get_assoc ( prefix , assoc_id , preload ) do
196
+ assoc_table = schema_from_table ( prefix )
197
+
198
+ query =
199
+ from a in assoc_table ,
200
+ preload: ^ preload ,
201
+ where: a . id == ^ assoc_id
202
+
203
+ Algora.Repo . one ( query )
204
+ end
205
+
206
+ def get_with_assoc ( table , id ) do
207
+ with % { assoc_id: assoc_id } = activity <- get ( table , id ) ,
208
+ assoc when is_map ( assoc ) <- get_assoc ( table , assoc_id ) do
209
+ Map . put ( activity , :assoc , assoc )
210
+ end
211
+ end
212
+
213
+ def assoc_url ( table , id ) do
214
+ activity = get_with_assoc ( table , id )
215
+ build_url ( activity )
216
+ end
217
+
218
+ def schema_from_table ( name ) when is_binary ( name ) , do: name |> String . to_atom ( ) |> schema_from_table ( )
219
+
220
+ def schema_from_table ( name ) when is_atom ( name ) do
221
+ Map . fetch! ( @ schema_from_table , name )
178
222
end
179
223
180
224
def table_from_user_relation ( table ) do
181
225
Map . fetch! ( @ table_from_user_relation , table )
182
226
end
227
+
228
+ def build_url ( % { assoc: % Bounty { id: id , owner: owner } } ) do
229
+ { :ok , "/org/#{ owner . handle } /bounties" }
230
+ end
231
+
232
+ def build_url ( % { assoc: % Identity { id: id , user: % { type: :individual } = user } } ) do
233
+ { :ok , "/@/#{ user . handle } " }
234
+ end
235
+
236
+ def build_url ( % { assoc: % Identity { id: id , user: % { type: :organization } = user } } ) do
237
+ { :ok , "/org/#{ user . handle } " }
238
+ end
239
+
240
+ def build_url ( a ) do
241
+ { :error , :not_found }
242
+ end
183
243
end
0 commit comments