From 4dcb2958d31960fbb554b101f776e5f382ef544a Mon Sep 17 00:00:00 2001 From: "byte[]" Date: Sun, 6 Sep 2020 01:30:53 -0400 Subject: [PATCH] Squashed commit of the following: commit 8ea9cff4af46e24c38020652cedeff72957354fb Author: byte[] Date: Sun Sep 6 01:29:24 2020 -0400 remove remaining serializable aside hiding related commit 99ccf06264db6319ece2a896a104031447447a5f Author: byte[] Date: Sun Sep 6 01:20:40 2020 -0400 interactions: remove serializable commit a63bef06a6962368f69cf83afbc3c44f2467618c Author: byte[] Date: Sun Sep 6 01:16:27 2020 -0400 users: remove serializable commit 8053229f6fab507c29a40f0e22dd9cab7971e34f Author: byte[] Date: Sun Sep 6 01:11:14 2020 -0400 user_links: remove serializable commit 9b058add825b0a876a91a1cf2b1b22dc34066e42 Author: byte[] Date: Sun Sep 6 01:09:33 2020 -0400 topics: remove serializable commit cd9ea908c34f72c0120fca1c4d581540db60db98 Author: byte[] Date: Sun Sep 6 01:05:23 2020 -0400 tags: remove serializable commit c7563fef8fc905c32a0727a4b104222227a6bafa Author: byte[] Date: Sun Sep 6 01:02:22 2020 -0400 static_pages: remove serializable commit 3da661bdd1aec74e4ac5b69ec21124bc1ebc6fb4 Author: byte[] Date: Sun Sep 6 01:00:15 2020 -0400 posts: remove serializable commit 18a50a4e5bed1ab6e4e6c13c3051a21ae7e8fbb0 Author: byte[] Date: Sun Sep 6 00:55:55 2020 -0400 poll_votes: remove serializable commit 7d946ef23d7b27877d4bf0fb6a4db4ae64a9ffab Author: byte[] Date: Sun Sep 6 00:51:49 2020 -0400 galleries: remove serializable commit d8c35a0934e5394b092b050e071abdada4bdb640 Author: byte[] Date: Sun Sep 6 00:42:43 2020 -0400 conversations: remove serializable commit 079e6dca6c8064867f2c0f90f351ea83c0f12b75 Author: byte[] Date: Sun Sep 6 00:38:28 2020 -0400 comments: remove serializable commit 00ae38bad566fb6badeccceac2e394e65ec9428e Author: byte[] Date: Sun Sep 6 00:37:15 2020 -0400 commissions: remove serializable commit b3c4a4b13671ca73c58080b090dd6165552c87d6 Author: byte[] Date: Sun Sep 6 00:17:12 2020 -0400 bans: remove serializable commit 8be9fe913ff1f6264b899e96ee38fa52032b8bda Author: byte[] Date: Sun Sep 6 00:02:44 2020 -0400 badges: remove serializable commit 162adda185f705b9749774c4af8c7d8db0d89790 Author: byte[] Date: Sat Sep 5 23:56:51 2020 -0400 adverts: remove serializable --- lib/philomena/adverts.ex | 55 ++++++++-------- lib/philomena/badges.ex | 49 +++++++------- lib/philomena/bans.ex | 48 ++++++-------- lib/philomena/comments.ex | 4 +- lib/philomena/commissions.ex | 30 +-------- lib/philomena/conversations.ex | 2 +- lib/philomena/galleries.ex | 35 +++++----- lib/philomena/images.ex | 64 ++++++++++--------- lib/philomena/interactions.ex | 2 +- lib/philomena/poll_votes.ex | 11 +++- lib/philomena/posts.ex | 4 +- lib/philomena/static_pages.ex | 4 +- lib/philomena/tags.ex | 39 +++++------ lib/philomena/topics.ex | 4 +- lib/philomena/user_ips.ex | 38 +++++++---- lib/philomena/user_links.ex | 2 +- lib/philomena/users.ex | 42 ++++++------ .../controllers/admin/badge_controller.ex | 2 +- .../controllers/image/destroy_controller.ex | 2 +- .../controllers/image/fave_controller.ex | 4 +- .../controllers/image/file_controller.ex | 5 +- .../controllers/image/tamper_controller.ex | 2 +- .../controllers/image/vote_controller.ex | 4 +- .../controllers/tag/image_controller.ex | 2 +- 24 files changed, 223 insertions(+), 231 deletions(-) diff --git a/lib/philomena/adverts.ex b/lib/philomena/adverts.ex index 2752c112..c0dd178c 100644 --- a/lib/philomena/adverts.ex +++ b/lib/philomena/adverts.ex @@ -4,7 +4,6 @@ defmodule Philomena.Adverts do """ import Ecto.Query, warn: false - alias Ecto.Multi alias Philomena.Repo alias Philomena.Adverts.Advert @@ -74,7 +73,7 @@ defmodule Philomena.Adverts do def get_advert!(id), do: Repo.get!(Advert, id) @doc """ - Creates a advert. + Creates an advert. ## Examples @@ -86,24 +85,24 @@ defmodule Philomena.Adverts do """ def create_advert(attrs \\ %{}) do - advert = - %Advert{} - |> Advert.save_changeset(attrs) - |> Uploader.analyze_upload(attrs) + %Advert{} + |> Advert.save_changeset(attrs) + |> Uploader.analyze_upload(attrs) + |> Repo.insert() + |> case do + {:ok, advert} -> + Uploader.persist_upload(advert) + Uploader.unpersist_old_upload(advert) - Multi.new() - |> Multi.insert(:advert, advert) - |> Multi.run(:after, fn _repo, %{advert: advert} -> - Uploader.persist_upload(advert) - Uploader.unpersist_old_upload(advert) + {:ok, advert} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end @doc """ - Updates a advert. + Updates an advert. ## Examples @@ -121,24 +120,24 @@ defmodule Philomena.Adverts do end def update_advert_image(%Advert{} = advert, attrs) do - advert = - advert - |> Advert.changeset(attrs) - |> Uploader.analyze_upload(attrs) + advert + |> Advert.changeset(attrs) + |> Uploader.analyze_upload(attrs) + |> Repo.update() + |> case do + {:ok, advert} -> + Uploader.persist_upload(advert) + Uploader.unpersist_old_upload(advert) - Multi.new() - |> Multi.update(:advert, advert) - |> Multi.run(:after, fn _repo, %{advert: advert} -> - Uploader.persist_upload(advert) - Uploader.unpersist_old_upload(advert) + {:ok, advert} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end @doc """ - Deletes a Advert. + Deletes an Advert. ## Examples diff --git a/lib/philomena/badges.ex b/lib/philomena/badges.ex index 7b06847b..d917f147 100644 --- a/lib/philomena/badges.ex +++ b/lib/philomena/badges.ex @@ -4,7 +4,6 @@ defmodule Philomena.Badges do """ import Ecto.Query, warn: false - alias Ecto.Multi alias Philomena.Repo alias Philomena.Badges.Badge @@ -52,20 +51,20 @@ defmodule Philomena.Badges do """ def create_badge(attrs \\ %{}) do - badge = - %Badge{} - |> Badge.changeset(attrs) - |> Uploader.analyze_upload(attrs) + %Badge{} + |> Badge.changeset(attrs) + |> Uploader.analyze_upload(attrs) + |> Repo.insert() + |> case do + {:ok, badge} -> + Uploader.persist_upload(badge) + Uploader.unpersist_old_upload(badge) - Multi.new() - |> Multi.insert(:badge, badge) - |> Multi.run(:after, fn _repo, %{badge: badge} -> - Uploader.persist_upload(badge) - Uploader.unpersist_old_upload(badge) + {:ok, badge} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end @doc """ @@ -87,20 +86,20 @@ defmodule Philomena.Badges do end def update_badge_image(%Badge{} = badge, attrs) do - badge = - badge - |> Badge.changeset(attrs) - |> Uploader.analyze_upload(attrs) + badge + |> Badge.changeset(attrs) + |> Uploader.analyze_upload(attrs) + |> Repo.update() + |> case do + {:ok, badge} -> + Uploader.persist_upload(badge) + Uploader.unpersist_old_upload(badge) - Multi.new() - |> Multi.update(:badge, badge) - |> Multi.run(:after, fn _repo, %{badge: badge} -> - Uploader.persist_upload(badge) - Uploader.unpersist_old_upload(badge) + {:ok, badge} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end @doc """ diff --git a/lib/philomena/bans.ex b/lib/philomena/bans.ex index 767658e3..c0660339 100644 --- a/lib/philomena/bans.ex +++ b/lib/philomena/bans.ex @@ -4,10 +4,9 @@ defmodule Philomena.Bans do """ import Ecto.Query, warn: false - alias Ecto.Multi alias Philomena.Repo - alias Philomena.UserIps.UserIp + alias Philomena.UserIps alias Philomena.Bans.Fingerprint @doc """ @@ -244,40 +243,29 @@ defmodule Philomena.Bans do """ def create_user(creator, attrs \\ %{}) do - user_ban = - %User{banning_user_id: creator.id} - |> User.save_changeset(attrs) + %User{banning_user_id: creator.id} + |> User.save_changeset(attrs) + |> Repo.insert() + |> case do + {:ok, user_ban} -> + ip = UserIps.get_ip_for_user(user_ban.user_id) - Multi.new() - |> Multi.insert(:user_ban, user_ban) - |> Multi.run(:auto_ip_ban, fn repo, %{user_ban: user_ban} -> - UserIp - |> where(user_id: ^user_ban.user_id) - |> order_by(desc: :updated_at) - |> limit(1) - |> select([u], u.ip) - |> Repo.one() - |> case do - nil -> - {:ok, nil} - - ip -> - ip = masked_ip(ip) + if ip do + # Automatically create associated IP ban. + ip = UserIps.masked_ip(ip) %Subnet{banning_user_id: creator.id, specification: ip} |> Subnet.save_changeset(attrs) - |> repo.insert() - end - end) - |> Repo.isolated_transaction(:serializable) + |> Repo.insert() + end + + {:ok, user_ban} + + error -> + error + end end - defp masked_ip(%Postgrex.INET{address: {_1, _2, _3, _4}} = ip), - do: ip - - defp masked_ip(%Postgrex.INET{address: {h1, h2, h3, h4, _5, _6, _7, _8}} = ip), - do: %{ip | address: {h1, h2, h3, h4, 0, 0, 0, 0}, netmask: 64} - @doc """ Updates a user. diff --git a/lib/philomena/comments.ex b/lib/philomena/comments.ex index 3deb5948..5c1258cd 100644 --- a/lib/philomena/comments.ex +++ b/lib/philomena/comments.ex @@ -59,7 +59,7 @@ defmodule Philomena.Comments do |> Multi.run(:subscribe, fn _repo, _changes -> Images.create_subscription(image, attribution[:user]) end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def notify_comment(comment) do @@ -117,7 +117,7 @@ defmodule Philomena.Comments do "edit_reason" => current_reason }) end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ diff --git a/lib/philomena/commissions.ex b/lib/philomena/commissions.ex index b5e1370b..cea6d6f6 100644 --- a/lib/philomena/commissions.ex +++ b/lib/philomena/commissions.ex @@ -9,19 +9,6 @@ defmodule Philomena.Commissions do alias Philomena.Commissions.Commission - @doc """ - Returns the list of commissions. - - ## Examples - - iex> list_commissions() - [%Commission{}, ...] - - """ - def list_commissions do - Repo.all(Commission) - end - @doc """ Gets a single commission. @@ -105,19 +92,6 @@ defmodule Philomena.Commissions do alias Philomena.Commissions.Item - @doc """ - Returns the list of commission_items. - - ## Examples - - iex> list_commission_items() - [%Item{}, ...] - - """ - def list_commission_items do - Repo.all(Item) - end - @doc """ Gets a single item. @@ -159,7 +133,7 @@ defmodule Philomena.Commissions do Multi.new() |> Multi.insert(:item, changeset) |> Multi.update_all(:commission, update, []) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ @@ -201,7 +175,7 @@ defmodule Philomena.Commissions do Multi.new() |> Multi.delete(:item, item) |> Multi.update_all(:commission, update, []) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ diff --git a/lib/philomena/conversations.ex b/lib/philomena/conversations.ex index 846ae947..5542c021 100644 --- a/lib/philomena/conversations.ex +++ b/lib/philomena/conversations.ex @@ -198,7 +198,7 @@ defmodule Philomena.Conversations do |> Multi.update_all(:conversation, conversation_query, set: [from_read: false, to_read: false, last_message_at: now] ) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ diff --git a/lib/philomena/galleries.ex b/lib/philomena/galleries.ex index c05a1190..70fd3a46 100644 --- a/lib/philomena/galleries.ex +++ b/lib/philomena/galleries.ex @@ -14,19 +14,6 @@ defmodule Philomena.Galleries do alias Philomena.Notifications alias Philomena.Images - @doc """ - Returns the list of galleries. - - ## Examples - - iex> list_galleries() - [%Gallery{}, ...] - - """ - def list_galleries do - Repo.all(Gallery) - end - @doc """ Gets a single gallery. @@ -155,6 +142,15 @@ defmodule Philomena.Galleries do def add_image_to_gallery(gallery, image) do Multi.new() + |> Multi.run(:lock, fn repo, %{} -> + gallery = + Gallery + |> where(id: ^gallery.id) + |> lock("FOR UPDATE") + |> repo.one() + + {:ok, gallery} + end) |> Multi.run(:interaction, fn repo, %{} -> position = (last_position(gallery.id) || -1) + 1 @@ -172,11 +168,20 @@ defmodule Philomena.Galleries do {:ok, count} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def remove_image_from_gallery(gallery, image) do Multi.new() + |> Multi.run(:lock, fn repo, %{} -> + gallery = + Gallery + |> where(id: ^gallery.id) + |> lock("FOR UPDATE") + |> repo.one() + + {:ok, gallery} + end) |> Multi.run(:interaction, fn repo, %{} -> {count, nil} = Interaction @@ -195,7 +200,7 @@ defmodule Philomena.Galleries do {:ok, count} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end defp last_position(gallery_id) do diff --git a/lib/philomena/images.ex b/lib/philomena/images.ex index 18584dfa..92a3a89b 100644 --- a/lib/philomena/images.ex +++ b/lib/philomena/images.ex @@ -90,15 +90,12 @@ defmodule Philomena.Images do |> Multi.run(:subscribe, fn _repo, %{image: image} -> create_subscription(image, attribution[:user]) end) - |> Multi.run(:after, fn _repo, %{image: image} -> - Uploader.persist_upload(image) - Uploader.unpersist_old_upload(image) - - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() |> case do {:ok, %{image: image}} = result -> + Uploader.persist_upload(image) + Uploader.unpersist_old_upload(image) + repair_image(image) reindex_image(image) Tags.reindex_tags(image.added_tags) @@ -135,21 +132,23 @@ defmodule Philomena.Images do {:ok, count} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def destroy_image(%Image{} = image) do - changeset = Image.remove_image_changeset(image) + image + |> Image.remove_image_changeset() + |> Repo.update() + |> case do + {:ok, image} -> + Uploader.unpersist_old_upload(image) + Hider.destroy_thumbnails(image) - Multi.new() - |> Multi.update(:image, changeset) - |> Multi.run(:remove_file, fn _repo, %{image: image} -> - Uploader.unpersist_old_upload(image) - Hider.destroy_thumbnails(image) + {:ok, image} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end def lock_comments(%Image{} = image, locked) do @@ -201,20 +200,23 @@ defmodule Philomena.Images do defp queue(_mime_type), do: "images" def update_file(%Image{} = image, attrs) do - image = - image - |> Image.changeset(attrs) - |> Uploader.analyze_upload(attrs) + image + |> Image.changeset(attrs) + |> Uploader.analyze_upload(attrs) + |> Repo.update() + |> case do + {:ok, image} -> + Uploader.persist_upload(image) + Uploader.unpersist_old_upload(image) - Multi.new() - |> Multi.update(:image, image) - |> Multi.run(:after, fn _repo, %{image: image} -> - Uploader.persist_upload(image) - Uploader.unpersist_old_upload(image) + repair_image(image) + reindex_image(image) - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + {:ok, image} + + error -> + error + end end @doc """ @@ -261,7 +263,7 @@ defmodule Philomena.Images do {:ok, nil} end end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def update_tags(%Image{} = image, attribution, attrs) do @@ -324,7 +326,7 @@ defmodule Philomena.Images do {:ok, count} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end defp tag_change_attributes(attribution, image, tag, added, user) do diff --git a/lib/philomena/interactions.ex b/lib/philomena/interactions.ex index 45b4022a..c4dc0f0d 100644 --- a/lib/philomena/interactions.ex +++ b/lib/philomena/interactions.ex @@ -128,7 +128,7 @@ defmodule Philomena.Interactions do {:ok, nil} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end defp union_all_queries([query]), diff --git a/lib/philomena/poll_votes.ex b/lib/philomena/poll_votes.ex index 10829031..318b1fdd 100644 --- a/lib/philomena/poll_votes.ex +++ b/lib/philomena/poll_votes.ex @@ -44,6 +44,15 @@ defmodule Philomena.PollVotes do poll_votes = filter_options(user, poll, now, attrs) Multi.new() + |> Multi.run(:lock, fn repo, _ -> + poll = + Poll + |> where(id: ^poll.id) + |> lock("FOR UPDATE") + |> repo.one() + + {:ok, poll} + end) |> Multi.run(:existing_votes, fn _repo, _changes -> # Don't proceed if any votes exist case voted?(poll, user) do @@ -76,7 +85,7 @@ defmodule Philomena.PollVotes do {:ok, count} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end defp filter_options(user, poll, now, %{"option_ids" => options}) when is_list(options) do diff --git a/lib/philomena/posts.ex b/lib/philomena/posts.ex index 971f1754..13c8678c 100644 --- a/lib/philomena/posts.ex +++ b/lib/philomena/posts.ex @@ -88,7 +88,7 @@ defmodule Philomena.Posts do |> Multi.run(:subscribe, fn _repo, _changes -> Topics.create_subscription(topic, attributes[:user]) end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def notify_post(post) do @@ -146,7 +146,7 @@ defmodule Philomena.Posts do "edit_reason" => current_reason }) end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ diff --git a/lib/philomena/static_pages.ex b/lib/philomena/static_pages.ex index 9960aa09..ca6600f2 100644 --- a/lib/philomena/static_pages.ex +++ b/lib/philomena/static_pages.ex @@ -61,7 +61,7 @@ defmodule Philomena.StaticPages do |> Version.changeset(attrs) |> repo.insert() end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ @@ -88,7 +88,7 @@ defmodule Philomena.StaticPages do Multi.new() |> Multi.update(:static_page, static_page) |> Multi.insert(:version, version) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end @doc """ diff --git a/lib/philomena/tags.ex b/lib/philomena/tags.ex index 878566d1..c49314b8 100644 --- a/lib/philomena/tags.ex +++ b/lib/philomena/tags.ex @@ -4,7 +4,6 @@ defmodule Philomena.Tags do """ import Ecto.Query, warn: false - alias Ecto.Multi alias Philomena.Repo alias Philomena.Elasticsearch @@ -120,30 +119,34 @@ defmodule Philomena.Tags do end def update_tag_image(%Tag{} = tag, attrs) do - changeset = Uploader.analyze_upload(tag, attrs) + tag + |> Uploader.analyze_upload(attrs) + |> Repo.update() + |> case do + {:ok, tag} -> + Uploader.persist_upload(tag) + Uploader.unpersist_old_upload(tag) - Multi.new() - |> Multi.update(:tag, changeset) - |> Multi.run(:update_file, fn _repo, %{tag: tag} -> - Uploader.persist_upload(tag) - Uploader.unpersist_old_upload(tag) + {:ok, tag} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end def remove_tag_image(%Tag{} = tag) do - changeset = Tag.remove_image_changeset(tag) + tag + |> Tag.remove_image_changeset() + |> Repo.update() + |> case do + {:ok, tag} -> + Uploader.unpersist_old_upload(tag) - Multi.new() - |> Multi.update(:tag, changeset) - |> Multi.run(:remove_file, fn _repo, %{tag: tag} -> - Uploader.unpersist_old_upload(tag) + {:ok, tag} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end @doc """ diff --git a/lib/philomena/topics.ex b/lib/philomena/topics.ex index 649ab174..f46f36ef 100644 --- a/lib/philomena/topics.ex +++ b/lib/philomena/topics.ex @@ -70,7 +70,7 @@ defmodule Philomena.Topics do |> Multi.run(:subscribe, fn _repo, %{topic: topic} -> create_subscription(topic, attribution[:user]) end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def notify_topic(topic) do @@ -239,7 +239,7 @@ defmodule Philomena.Topics do {:ok, count} end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def hide_topic(topic, deletion_reason, user) do diff --git a/lib/philomena/user_ips.ex b/lib/philomena/user_ips.ex index adef4f99..d3cd4072 100644 --- a/lib/philomena/user_ips.ex +++ b/lib/philomena/user_ips.ex @@ -8,19 +8,6 @@ defmodule Philomena.UserIps do alias Philomena.UserIps.UserIp - @doc """ - Returns the list of user_ips. - - ## Examples - - iex> list_user_ips() - [%UserIp{}, ...] - - """ - def list_user_ips do - Repo.all(UserIp) - end - @doc """ Gets a single user_ip. @@ -37,6 +24,31 @@ defmodule Philomena.UserIps do """ def get_user_ip!(id), do: Repo.get!(UserIp, id) + @doc """ + Gets this user's most recent IP address, if the user has one + recorded. + """ + def get_ip_for_user(user_id) do + UserIp + |> where(user_id: ^user_id) + |> order_by(desc: :updated_at) + |> limit(1) + |> select([u], u.ip) + |> Repo.one() + end + + @doc """ + Sets the appropriate netmask for correctly banning an IPv6-enabled + client per RFC4941. IPv4 addresses are not changed. + """ + def masked_ip(%Postgrex.INET{address: {_1, _2, _3, _4}} = ip) do + ip + end + + def masked_ip(%Postgrex.INET{address: {h1, h2, h3, h4, _5, _6, _7, _8}} = ip) do + %{ip | address: {h1, h2, h3, h4, 0, 0, 0, 0}, netmask: 64} + end + @doc """ Creates a user_ip. diff --git a/lib/philomena/user_links.ex b/lib/philomena/user_links.ex index 1fe30fbf..cd71402e 100644 --- a/lib/philomena/user_links.ex +++ b/lib/philomena/user_links.ex @@ -106,7 +106,7 @@ defmodule Philomena.UserLinks do {:ok, nil} end end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end def reject_user_link(%UserLink{} = user_link) do diff --git a/lib/philomena/users.ex b/lib/philomena/users.ex index 9e87035d..b02ed816 100644 --- a/lib/philomena/users.ex +++ b/lib/philomena/users.ex @@ -515,7 +515,7 @@ defmodule Philomena.Users do |> Multi.run(:unsubscribe, fn _repo, %{user: user} -> unsubscribe_restricted_actors(user) end) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() end defp clean_roles(nil), do: [] @@ -562,30 +562,34 @@ defmodule Philomena.Users do end def update_avatar(%User{} = user, attrs) do - changeset = Uploader.analyze_upload(user, attrs) + user + |> Uploader.analyze_upload(attrs) + |> Repo.update() + |> case do + {:ok, user} -> + Uploader.persist_upload(user) + Uploader.unpersist_old_upload(user) - Multi.new() - |> Multi.update(:user, changeset) - |> Multi.run(:update_file, fn _repo, %{user: user} -> - Uploader.persist_upload(user) - Uploader.unpersist_old_upload(user) + {:ok, user} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end def remove_avatar(%User{} = user) do - changeset = User.remove_avatar_changeset(user) + user + |> User.remove_avatar_changeset() + |> Repo.update() + |> case do + {:ok, user} -> + Uploader.unpersist_old_upload(user) - Multi.new() - |> Multi.update(:user, changeset) - |> Multi.run(:remove_file, fn _repo, %{user: user} -> - Uploader.unpersist_old_upload(user) + {:ok, user} - {:ok, nil} - end) - |> Repo.isolated_transaction(:serializable) + error -> + error + end end def update_name(user, user_params) do @@ -597,7 +601,7 @@ defmodule Philomena.Users do Multi.new() |> Multi.insert(:name_change, name_change) |> Multi.update(:account, account) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() |> case do {:ok, %{account: %{name: new_name}}} = result -> spawn(fn -> diff --git a/lib/philomena_web/controllers/admin/badge_controller.ex b/lib/philomena_web/controllers/admin/badge_controller.ex index 1c30136d..2aa79e31 100644 --- a/lib/philomena_web/controllers/admin/badge_controller.ex +++ b/lib/philomena_web/controllers/admin/badge_controller.ex @@ -25,7 +25,7 @@ defmodule PhilomenaWeb.Admin.BadgeController do def create(conn, %{"badge" => badge_params}) do case Badges.create_badge(badge_params) do - {:ok, %{badge: _badge}} -> + {:ok, _badge} -> conn |> put_flash(:info, "Badge created successfully.") |> redirect(to: Routes.admin_badge_path(conn, :index)) diff --git a/lib/philomena_web/controllers/image/destroy_controller.ex b/lib/philomena_web/controllers/image/destroy_controller.ex index f2557660..8caf631f 100644 --- a/lib/philomena_web/controllers/image/destroy_controller.ex +++ b/lib/philomena_web/controllers/image/destroy_controller.ex @@ -12,7 +12,7 @@ defmodule PhilomenaWeb.Image.DestroyController do image = conn.assigns.image case Images.destroy_image(image) do - {:ok, %{image: image}} -> + {:ok, image} -> conn |> put_flash(:info, "Image contents destroyed.") |> redirect(to: Routes.image_path(conn, :show, image)) diff --git a/lib/philomena_web/controllers/image/fave_controller.ex b/lib/philomena_web/controllers/image/fave_controller.ex index 26c2fb8f..2a8a3d77 100644 --- a/lib/philomena_web/controllers/image/fave_controller.ex +++ b/lib/philomena_web/controllers/image/fave_controller.ex @@ -27,7 +27,7 @@ defmodule PhilomenaWeb.Image.FaveController do ) |> Multi.append(ImageVotes.delete_vote_transaction(image, user)) |> Multi.append(ImageVotes.create_vote_transaction(image, user, true)) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() |> case do {:ok, _result} -> image = @@ -49,7 +49,7 @@ defmodule PhilomenaWeb.Image.FaveController do image = conn.assigns.image ImageFaves.delete_fave_transaction(image, user) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() |> case do {:ok, _result} -> image = diff --git a/lib/philomena_web/controllers/image/file_controller.ex b/lib/philomena_web/controllers/image/file_controller.ex index 32159871..a8d6175a 100644 --- a/lib/philomena_web/controllers/image/file_controller.ex +++ b/lib/philomena_web/controllers/image/file_controller.ex @@ -11,10 +11,7 @@ defmodule PhilomenaWeb.Image.FileController do def update(conn, %{"image" => image_params}) do case Images.update_file(conn.assigns.image, image_params) do - {:ok, %{image: image}} -> - Images.repair_image(image) - Images.reindex_image(image) - + {:ok, image} -> conn |> put_flash(:info, "Successfully updated file.") |> redirect(to: Routes.image_path(conn, :show, image)) diff --git a/lib/philomena_web/controllers/image/tamper_controller.ex b/lib/philomena_web/controllers/image/tamper_controller.ex index cb115862..93c259d9 100644 --- a/lib/philomena_web/controllers/image/tamper_controller.ex +++ b/lib/philomena_web/controllers/image/tamper_controller.ex @@ -18,7 +18,7 @@ defmodule PhilomenaWeb.Image.TamperController do {:ok, _result} = ImageVotes.delete_vote_transaction(image, user) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() Images.reindex_image(image) diff --git a/lib/philomena_web/controllers/image/vote_controller.ex b/lib/philomena_web/controllers/image/vote_controller.ex index 53275371..b3796c86 100644 --- a/lib/philomena_web/controllers/image/vote_controller.ex +++ b/lib/philomena_web/controllers/image/vote_controller.ex @@ -25,7 +25,7 @@ defmodule PhilomenaWeb.Image.VoteController do ImageVotes.delete_vote_transaction(image, user), ImageVotes.create_vote_transaction(image, user, params["up"] == true) ) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() |> case do {:ok, _result} -> image = @@ -47,7 +47,7 @@ defmodule PhilomenaWeb.Image.VoteController do image = conn.assigns.image ImageVotes.delete_vote_transaction(image, user) - |> Repo.isolated_transaction(:serializable) + |> Repo.transaction() |> case do {:ok, _result} -> image = diff --git a/lib/philomena_web/controllers/tag/image_controller.ex b/lib/philomena_web/controllers/tag/image_controller.ex index 6f34ec1a..a33633af 100644 --- a/lib/philomena_web/controllers/tag/image_controller.ex +++ b/lib/philomena_web/controllers/tag/image_controller.ex @@ -20,7 +20,7 @@ defmodule PhilomenaWeb.Tag.ImageController do def update(conn, %{"tag" => tag_params}) do case Tags.update_tag_image(conn.assigns.tag, tag_params) do - {:ok, %{tag: tag}} -> + {:ok, tag} -> conn |> put_flash(:info, "Tag image successfully updated.") |> redirect(to: Routes.tag_path(conn, :show, tag))