mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-30 14:57:59 +01:00
4dcb2958d3
commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] <byteslice@airmail.cc> Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] <byteslice@airmail.cc> Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable
148 lines
3.7 KiB
Elixir
148 lines
3.7 KiB
Elixir
defmodule Philomena.Interactions do
|
|
import Ecto.Query
|
|
|
|
alias Philomena.ImageHides.ImageHide
|
|
alias Philomena.ImageFaves.ImageFave
|
|
alias Philomena.ImageVotes.ImageVote
|
|
alias Philomena.Images.Image
|
|
alias Philomena.Repo
|
|
alias Ecto.Multi
|
|
|
|
def user_interactions(_images, nil),
|
|
do: []
|
|
|
|
def user_interactions(images, user) do
|
|
ids =
|
|
images
|
|
|> flatten_images()
|
|
|> Enum.uniq()
|
|
|
|
hide_interactions =
|
|
ImageHide
|
|
|> select([h], %{
|
|
image_id: h.image_id,
|
|
user_id: h.user_id,
|
|
interaction_type: ^"hidden",
|
|
value: ^""
|
|
})
|
|
|> where([h], h.image_id in ^ids)
|
|
|> where(user_id: ^user.id)
|
|
|
|
fave_interactions =
|
|
ImageFave
|
|
|> select([f], %{
|
|
image_id: f.image_id,
|
|
user_id: f.user_id,
|
|
interaction_type: ^"faved",
|
|
value: ^""
|
|
})
|
|
|> where([f], f.image_id in ^ids)
|
|
|> where(user_id: ^user.id)
|
|
|
|
upvote_interactions =
|
|
ImageVote
|
|
|> select([v], %{
|
|
image_id: v.image_id,
|
|
user_id: v.user_id,
|
|
interaction_type: ^"voted",
|
|
value: ^"up"
|
|
})
|
|
|> where([v], v.image_id in ^ids)
|
|
|> where(user_id: ^user.id, up: true)
|
|
|
|
downvote_interactions =
|
|
ImageVote
|
|
|> select([v], %{
|
|
image_id: v.image_id,
|
|
user_id: v.user_id,
|
|
interaction_type: ^"voted",
|
|
value: ^"down"
|
|
})
|
|
|> 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
|
|
|
|
def migrate_interactions(source, target) do
|
|
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
|
|
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})
|
|
|
|
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()
|
|
|> 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)
|
|
|> Multi.run(:image, fn repo,
|
|
%{hides: hides, faves: faves, upvotes: upvotes, downvotes: downvotes} ->
|
|
image_query = where(Image, id: ^target.id)
|
|
|
|
repo.update_all(
|
|
image_query,
|
|
inc: [
|
|
hides_count: hides,
|
|
faves_count: faves,
|
|
upvotes_count: upvotes,
|
|
downvotes_count: downvotes,
|
|
score: upvotes - downvotes
|
|
]
|
|
)
|
|
|
|
{:ok, nil}
|
|
end)
|
|
|> Repo.transaction()
|
|
end
|
|
|
|
defp union_all_queries([query]),
|
|
do: query
|
|
|
|
defp union_all_queries([query | rest]),
|
|
do: query |> union_all(^union_all_queries(rest))
|
|
|
|
defp flatten_images(images) do
|
|
Enum.flat_map(images, fn
|
|
nil -> []
|
|
%{id: id} -> [id]
|
|
{%{id: id}, _hit} -> [id]
|
|
enum -> flatten_images(enum)
|
|
end)
|
|
end
|
|
end
|