2019-11-17 03:20:33 +01:00
|
|
|
defmodule Philomena.Interactions do
|
|
|
|
import Ecto.Query
|
|
|
|
|
|
|
|
alias Philomena.ImageHides.ImageHide
|
|
|
|
alias Philomena.ImageFaves.ImageFave
|
|
|
|
alias Philomena.ImageVotes.ImageVote
|
2019-12-08 05:53:18 +01:00
|
|
|
alias Philomena.Images.Image
|
2019-11-17 03:20:33 +01:00
|
|
|
alias Philomena.Repo
|
2019-12-08 05:53:18 +01:00
|
|
|
alias Ecto.Multi
|
2019-11-17 03:20:33 +01:00
|
|
|
|
|
|
|
def user_interactions(_images, nil),
|
|
|
|
do: []
|
|
|
|
|
|
|
|
def user_interactions(images, user) do
|
|
|
|
ids =
|
2020-01-11 05:20:19 +01:00
|
|
|
images
|
2020-08-13 17:32:35 +02:00
|
|
|
|> flatten_images()
|
2019-11-17 03:20:33 +01:00
|
|
|
|> Enum.uniq()
|
|
|
|
|
|
|
|
hide_interactions =
|
|
|
|
ImageHide
|
2020-01-11 05:20:19 +01:00
|
|
|
|> select([h], %{
|
|
|
|
image_id: h.image_id,
|
|
|
|
user_id: h.user_id,
|
|
|
|
interaction_type: ^"hidden",
|
|
|
|
value: ^""
|
|
|
|
})
|
2019-11-17 03:20:33 +01:00
|
|
|
|> where([h], h.image_id in ^ids)
|
|
|
|
|> where(user_id: ^user.id)
|
|
|
|
|
|
|
|
fave_interactions =
|
|
|
|
ImageFave
|
2020-01-11 05:20:19 +01:00
|
|
|
|> select([f], %{
|
|
|
|
image_id: f.image_id,
|
|
|
|
user_id: f.user_id,
|
|
|
|
interaction_type: ^"faved",
|
|
|
|
value: ^""
|
|
|
|
})
|
2019-11-17 03:20:33 +01:00
|
|
|
|> where([f], f.image_id in ^ids)
|
|
|
|
|> where(user_id: ^user.id)
|
|
|
|
|
|
|
|
upvote_interactions =
|
|
|
|
ImageVote
|
2020-01-11 05:20:19 +01:00
|
|
|
|> select([v], %{
|
|
|
|
image_id: v.image_id,
|
|
|
|
user_id: v.user_id,
|
|
|
|
interaction_type: ^"voted",
|
|
|
|
value: ^"up"
|
|
|
|
})
|
2019-11-17 03:20:33 +01:00
|
|
|
|> where([v], v.image_id in ^ids)
|
|
|
|
|> where(user_id: ^user.id, up: true)
|
|
|
|
|
|
|
|
downvote_interactions =
|
|
|
|
ImageVote
|
2020-01-11 05:20:19 +01:00
|
|
|
|> select([v], %{
|
|
|
|
image_id: v.image_id,
|
|
|
|
user_id: v.user_id,
|
|
|
|
interaction_type: ^"voted",
|
|
|
|
value: ^"down"
|
|
|
|
})
|
2019-11-17 03:20:33 +01:00
|
|
|
|> where([v], v.image_id in ^ids)
|
|
|
|
|> where(user_id: ^user.id, up: false)
|
|
|
|
|
|
|
|
[
|
|
|
|
hide_interactions,
|
|
|
|
fave_interactions,
|
|
|
|
upvote_interactions,
|
|
|
|
downvote_interactions
|
|
|
|
]
|
|
|
|
|> union_all_queries()
|
|
|
|
|> Repo.all()
|
|
|
|
end
|
|
|
|
|
2019-12-08 05:53:18 +01:00
|
|
|
def migrate_interactions(source, target) do
|
2024-07-10 03:20:00 +02:00
|
|
|
now = DateTime.utc_now(:second)
|
2019-12-08 05:53:18 +01:00
|
|
|
source = Repo.preload(source, [:hiders, :favers, :upvoters, :downvoters])
|
|
|
|
|
|
|
|
new_hides = Enum.map(source.hiders, &%{image_id: target.id, user_id: &1.id, created_at: now})
|
|
|
|
new_faves = Enum.map(source.favers, &%{image_id: target.id, user_id: &1.id, created_at: now})
|
|
|
|
|
2020-01-11 05:20:19 +01:00
|
|
|
new_upvotes =
|
|
|
|
Enum.map(
|
|
|
|
source.upvoters,
|
|
|
|
&%{image_id: target.id, user_id: &1.id, created_at: now, up: true}
|
|
|
|
)
|
|
|
|
|
|
|
|
new_downvotes =
|
|
|
|
Enum.map(
|
|
|
|
source.downvoters,
|
|
|
|
&%{image_id: target.id, user_id: &1.id, created_at: now, up: false}
|
|
|
|
)
|
|
|
|
|
|
|
|
Multi.new()
|
2019-12-08 05:53:18 +01:00
|
|
|
|> Multi.run(:hides, fn repo, %{} ->
|
|
|
|
{count, nil} = repo.insert_all(ImageHide, new_hides, on_conflict: :nothing)
|
|
|
|
|
|
|
|
{:ok, count}
|
|
|
|
end)
|
|
|
|
|> Multi.run(:faves, fn repo, %{} ->
|
|
|
|
{count, nil} = repo.insert_all(ImageFave, new_faves, on_conflict: :nothing)
|
|
|
|
|
|
|
|
{:ok, count}
|
|
|
|
end)
|
|
|
|
|> Multi.run(:upvotes, fn repo, %{} ->
|
|
|
|
{count, nil} = repo.insert_all(ImageVote, new_upvotes, on_conflict: :nothing)
|
|
|
|
|
|
|
|
{:ok, count}
|
|
|
|
end)
|
|
|
|
|> Multi.run(:downvotes, fn repo, %{} ->
|
|
|
|
{count, nil} = repo.insert_all(ImageVote, new_downvotes, on_conflict: :nothing)
|
|
|
|
|
|
|
|
{:ok, count}
|
|
|
|
end)
|
2020-01-11 05:20:19 +01:00
|
|
|
|> Multi.run(:image, fn repo,
|
|
|
|
%{hides: hides, faves: faves, upvotes: upvotes, downvotes: downvotes} ->
|
2019-12-08 05:53:18 +01:00
|
|
|
image_query = where(Image, id: ^target.id)
|
|
|
|
|
2019-12-09 05:41:35 +01:00
|
|
|
repo.update_all(
|
|
|
|
image_query,
|
|
|
|
inc: [
|
|
|
|
hides_count: hides,
|
|
|
|
faves_count: faves,
|
|
|
|
upvotes_count: upvotes,
|
|
|
|
downvotes_count: downvotes,
|
|
|
|
score: upvotes - downvotes
|
|
|
|
]
|
|
|
|
)
|
2019-12-08 05:53:18 +01:00
|
|
|
|
|
|
|
{:ok, nil}
|
|
|
|
end)
|
2020-09-06 07:30:53 +02:00
|
|
|
|> Repo.transaction()
|
2019-12-08 05:53:18 +01:00
|
|
|
end
|
|
|
|
|
2019-11-17 03:20:33 +01:00
|
|
|
defp union_all_queries([query]),
|
|
|
|
do: query
|
2020-01-11 05:20:19 +01:00
|
|
|
|
2019-11-17 03:20:33 +01:00
|
|
|
defp union_all_queries([query | rest]),
|
|
|
|
do: query |> union_all(^union_all_queries(rest))
|
2020-08-13 17:32:35 +02:00
|
|
|
|
|
|
|
defp flatten_images(images) do
|
|
|
|
Enum.flat_map(images, fn
|
|
|
|
nil -> []
|
|
|
|
%{id: id} -> [id]
|
|
|
|
{%{id: id}, _hit} -> [id]
|
|
|
|
enum -> flatten_images(enum)
|
|
|
|
end)
|
|
|
|
end
|
2020-01-11 05:20:19 +01:00
|
|
|
end
|