mirror of
https://github.com/philomena-dev/philomena.git
synced 2024-11-27 21:47:59 +01:00
Squashed commit of the following:
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
This commit is contained in:
parent
93e6979202
commit
4dcb2958d3
24 changed files with 223 additions and 231 deletions
|
@ -4,7 +4,6 @@ defmodule Philomena.Adverts do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Ecto.Multi
|
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
|
||||||
alias Philomena.Adverts.Advert
|
alias Philomena.Adverts.Advert
|
||||||
|
@ -74,7 +73,7 @@ defmodule Philomena.Adverts do
|
||||||
def get_advert!(id), do: Repo.get!(Advert, id)
|
def get_advert!(id), do: Repo.get!(Advert, id)
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Creates a advert.
|
Creates an advert.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
@ -86,24 +85,24 @@ defmodule Philomena.Adverts do
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_advert(attrs \\ %{}) do
|
def create_advert(attrs \\ %{}) do
|
||||||
advert =
|
%Advert{}
|
||||||
%Advert{}
|
|> Advert.save_changeset(attrs)
|
||||||
|> Advert.save_changeset(attrs)
|
|> Uploader.analyze_upload(attrs)
|
||||||
|> Uploader.analyze_upload(attrs)
|
|> Repo.insert()
|
||||||
|
|> case do
|
||||||
|
{:ok, advert} ->
|
||||||
|
Uploader.persist_upload(advert)
|
||||||
|
Uploader.unpersist_old_upload(advert)
|
||||||
|
|
||||||
Multi.new()
|
{:ok, advert}
|
||||||
|> Multi.insert(:advert, advert)
|
|
||||||
|> Multi.run(:after, fn _repo, %{advert: advert} ->
|
|
||||||
Uploader.persist_upload(advert)
|
|
||||||
Uploader.unpersist_old_upload(advert)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Updates a advert.
|
Updates an advert.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
@ -121,24 +120,24 @@ defmodule Philomena.Adverts do
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_advert_image(%Advert{} = advert, attrs) do
|
def update_advert_image(%Advert{} = advert, attrs) do
|
||||||
advert =
|
advert
|
||||||
advert
|
|> Advert.changeset(attrs)
|
||||||
|> Advert.changeset(attrs)
|
|> Uploader.analyze_upload(attrs)
|
||||||
|> Uploader.analyze_upload(attrs)
|
|> Repo.update()
|
||||||
|
|> case do
|
||||||
|
{:ok, advert} ->
|
||||||
|
Uploader.persist_upload(advert)
|
||||||
|
Uploader.unpersist_old_upload(advert)
|
||||||
|
|
||||||
Multi.new()
|
{:ok, advert}
|
||||||
|> Multi.update(:advert, advert)
|
|
||||||
|> Multi.run(:after, fn _repo, %{advert: advert} ->
|
|
||||||
Uploader.persist_upload(advert)
|
|
||||||
Uploader.unpersist_old_upload(advert)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Deletes a Advert.
|
Deletes an Advert.
|
||||||
|
|
||||||
## Examples
|
## Examples
|
||||||
|
|
||||||
|
|
|
@ -4,7 +4,6 @@ defmodule Philomena.Badges do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Ecto.Multi
|
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
|
||||||
alias Philomena.Badges.Badge
|
alias Philomena.Badges.Badge
|
||||||
|
@ -52,20 +51,20 @@ defmodule Philomena.Badges do
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_badge(attrs \\ %{}) do
|
def create_badge(attrs \\ %{}) do
|
||||||
badge =
|
%Badge{}
|
||||||
%Badge{}
|
|> Badge.changeset(attrs)
|
||||||
|> Badge.changeset(attrs)
|
|> Uploader.analyze_upload(attrs)
|
||||||
|> Uploader.analyze_upload(attrs)
|
|> Repo.insert()
|
||||||
|
|> case do
|
||||||
|
{:ok, badge} ->
|
||||||
|
Uploader.persist_upload(badge)
|
||||||
|
Uploader.unpersist_old_upload(badge)
|
||||||
|
|
||||||
Multi.new()
|
{:ok, badge}
|
||||||
|> Multi.insert(:badge, badge)
|
|
||||||
|> Multi.run(:after, fn _repo, %{badge: badge} ->
|
|
||||||
Uploader.persist_upload(badge)
|
|
||||||
Uploader.unpersist_old_upload(badge)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -87,20 +86,20 @@ defmodule Philomena.Badges do
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_badge_image(%Badge{} = badge, attrs) do
|
def update_badge_image(%Badge{} = badge, attrs) do
|
||||||
badge =
|
badge
|
||||||
badge
|
|> Badge.changeset(attrs)
|
||||||
|> Badge.changeset(attrs)
|
|> Uploader.analyze_upload(attrs)
|
||||||
|> Uploader.analyze_upload(attrs)
|
|> Repo.update()
|
||||||
|
|> case do
|
||||||
|
{:ok, badge} ->
|
||||||
|
Uploader.persist_upload(badge)
|
||||||
|
Uploader.unpersist_old_upload(badge)
|
||||||
|
|
||||||
Multi.new()
|
{:ok, badge}
|
||||||
|> Multi.update(:badge, badge)
|
|
||||||
|> Multi.run(:after, fn _repo, %{badge: badge} ->
|
|
||||||
Uploader.persist_upload(badge)
|
|
||||||
Uploader.unpersist_old_upload(badge)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -4,10 +4,9 @@ defmodule Philomena.Bans do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Ecto.Multi
|
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
|
||||||
alias Philomena.UserIps.UserIp
|
alias Philomena.UserIps
|
||||||
alias Philomena.Bans.Fingerprint
|
alias Philomena.Bans.Fingerprint
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -244,40 +243,29 @@ defmodule Philomena.Bans do
|
||||||
|
|
||||||
"""
|
"""
|
||||||
def create_user(creator, attrs \\ %{}) do
|
def create_user(creator, attrs \\ %{}) do
|
||||||
user_ban =
|
%User{banning_user_id: creator.id}
|
||||||
%User{banning_user_id: creator.id}
|
|> User.save_changeset(attrs)
|
||||||
|> User.save_changeset(attrs)
|
|> Repo.insert()
|
||||||
|
|> case do
|
||||||
|
{:ok, user_ban} ->
|
||||||
|
ip = UserIps.get_ip_for_user(user_ban.user_id)
|
||||||
|
|
||||||
Multi.new()
|
if ip do
|
||||||
|> Multi.insert(:user_ban, user_ban)
|
# Automatically create associated IP ban.
|
||||||
|> Multi.run(:auto_ip_ban, fn repo, %{user_ban: user_ban} ->
|
ip = UserIps.masked_ip(ip)
|
||||||
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)
|
|
||||||
|
|
||||||
%Subnet{banning_user_id: creator.id, specification: ip}
|
%Subnet{banning_user_id: creator.id, specification: ip}
|
||||||
|> Subnet.save_changeset(attrs)
|
|> Subnet.save_changeset(attrs)
|
||||||
|> repo.insert()
|
|> Repo.insert()
|
||||||
end
|
end
|
||||||
end)
|
|
||||||
|> Repo.isolated_transaction(:serializable)
|
{:ok, user_ban}
|
||||||
|
|
||||||
|
error ->
|
||||||
|
error
|
||||||
|
end
|
||||||
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 """
|
@doc """
|
||||||
Updates a user.
|
Updates a user.
|
||||||
|
|
||||||
|
|
|
@ -59,7 +59,7 @@ defmodule Philomena.Comments do
|
||||||
|> Multi.run(:subscribe, fn _repo, _changes ->
|
|> Multi.run(:subscribe, fn _repo, _changes ->
|
||||||
Images.create_subscription(image, attribution[:user])
|
Images.create_subscription(image, attribution[:user])
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_comment(comment) do
|
def notify_comment(comment) do
|
||||||
|
@ -117,7 +117,7 @@ defmodule Philomena.Comments do
|
||||||
"edit_reason" => current_reason
|
"edit_reason" => current_reason
|
||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -9,19 +9,6 @@ defmodule Philomena.Commissions do
|
||||||
|
|
||||||
alias Philomena.Commissions.Commission
|
alias Philomena.Commissions.Commission
|
||||||
|
|
||||||
@doc """
|
|
||||||
Returns the list of commissions.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
iex> list_commissions()
|
|
||||||
[%Commission{}, ...]
|
|
||||||
|
|
||||||
"""
|
|
||||||
def list_commissions do
|
|
||||||
Repo.all(Commission)
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single commission.
|
Gets a single commission.
|
||||||
|
|
||||||
|
@ -105,19 +92,6 @@ defmodule Philomena.Commissions do
|
||||||
|
|
||||||
alias Philomena.Commissions.Item
|
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 """
|
@doc """
|
||||||
Gets a single item.
|
Gets a single item.
|
||||||
|
|
||||||
|
@ -159,7 +133,7 @@ defmodule Philomena.Commissions do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.insert(:item, changeset)
|
|> Multi.insert(:item, changeset)
|
||||||
|> Multi.update_all(:commission, update, [])
|
|> Multi.update_all(:commission, update, [])
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -201,7 +175,7 @@ defmodule Philomena.Commissions do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.delete(:item, item)
|
|> Multi.delete(:item, item)
|
||||||
|> Multi.update_all(:commission, update, [])
|
|> Multi.update_all(:commission, update, [])
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -198,7 +198,7 @@ defmodule Philomena.Conversations do
|
||||||
|> Multi.update_all(:conversation, conversation_query,
|
|> Multi.update_all(:conversation, conversation_query,
|
||||||
set: [from_read: false, to_read: false, last_message_at: now]
|
set: [from_read: false, to_read: false, last_message_at: now]
|
||||||
)
|
)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -14,19 +14,6 @@ defmodule Philomena.Galleries do
|
||||||
alias Philomena.Notifications
|
alias Philomena.Notifications
|
||||||
alias Philomena.Images
|
alias Philomena.Images
|
||||||
|
|
||||||
@doc """
|
|
||||||
Returns the list of galleries.
|
|
||||||
|
|
||||||
## Examples
|
|
||||||
|
|
||||||
iex> list_galleries()
|
|
||||||
[%Gallery{}, ...]
|
|
||||||
|
|
||||||
"""
|
|
||||||
def list_galleries do
|
|
||||||
Repo.all(Gallery)
|
|
||||||
end
|
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
Gets a single gallery.
|
Gets a single gallery.
|
||||||
|
|
||||||
|
@ -155,6 +142,15 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
def add_image_to_gallery(gallery, image) do
|
def add_image_to_gallery(gallery, image) do
|
||||||
Multi.new()
|
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, %{} ->
|
|> Multi.run(:interaction, fn repo, %{} ->
|
||||||
position = (last_position(gallery.id) || -1) + 1
|
position = (last_position(gallery.id) || -1) + 1
|
||||||
|
|
||||||
|
@ -172,11 +168,20 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_image_from_gallery(gallery, image) do
|
def remove_image_from_gallery(gallery, image) do
|
||||||
Multi.new()
|
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, %{} ->
|
|> Multi.run(:interaction, fn repo, %{} ->
|
||||||
{count, nil} =
|
{count, nil} =
|
||||||
Interaction
|
Interaction
|
||||||
|
@ -195,7 +200,7 @@ defmodule Philomena.Galleries do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp last_position(gallery_id) do
|
defp last_position(gallery_id) do
|
||||||
|
|
|
@ -90,15 +90,12 @@ defmodule Philomena.Images do
|
||||||
|> Multi.run(:subscribe, fn _repo, %{image: image} ->
|
|> Multi.run(:subscribe, fn _repo, %{image: image} ->
|
||||||
create_subscription(image, attribution[:user])
|
create_subscription(image, attribution[:user])
|
||||||
end)
|
end)
|
||||||
|> Multi.run(:after, fn _repo, %{image: image} ->
|
|> Repo.transaction()
|
||||||
Uploader.persist_upload(image)
|
|
||||||
Uploader.unpersist_old_upload(image)
|
|
||||||
|
|
||||||
{:ok, nil}
|
|
||||||
end)
|
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{image: image}} = result ->
|
{:ok, %{image: image}} = result ->
|
||||||
|
Uploader.persist_upload(image)
|
||||||
|
Uploader.unpersist_old_upload(image)
|
||||||
|
|
||||||
repair_image(image)
|
repair_image(image)
|
||||||
reindex_image(image)
|
reindex_image(image)
|
||||||
Tags.reindex_tags(image.added_tags)
|
Tags.reindex_tags(image.added_tags)
|
||||||
|
@ -135,21 +132,23 @@ defmodule Philomena.Images do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def destroy_image(%Image{} = image) do
|
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()
|
{:ok, image}
|
||||||
|> Multi.update(:image, changeset)
|
|
||||||
|> Multi.run(:remove_file, fn _repo, %{image: image} ->
|
|
||||||
Uploader.unpersist_old_upload(image)
|
|
||||||
Hider.destroy_thumbnails(image)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def lock_comments(%Image{} = image, locked) do
|
def lock_comments(%Image{} = image, locked) do
|
||||||
|
@ -201,20 +200,23 @@ defmodule Philomena.Images do
|
||||||
defp queue(_mime_type), do: "images"
|
defp queue(_mime_type), do: "images"
|
||||||
|
|
||||||
def update_file(%Image{} = image, attrs) do
|
def update_file(%Image{} = image, attrs) do
|
||||||
image =
|
image
|
||||||
image
|
|> Image.changeset(attrs)
|
||||||
|> Image.changeset(attrs)
|
|> Uploader.analyze_upload(attrs)
|
||||||
|> Uploader.analyze_upload(attrs)
|
|> Repo.update()
|
||||||
|
|> case do
|
||||||
|
{:ok, image} ->
|
||||||
|
Uploader.persist_upload(image)
|
||||||
|
Uploader.unpersist_old_upload(image)
|
||||||
|
|
||||||
Multi.new()
|
repair_image(image)
|
||||||
|> Multi.update(:image, image)
|
reindex_image(image)
|
||||||
|> Multi.run(:after, fn _repo, %{image: image} ->
|
|
||||||
Uploader.persist_upload(image)
|
|
||||||
Uploader.unpersist_old_upload(image)
|
|
||||||
|
|
||||||
{:ok, nil}
|
{:ok, image}
|
||||||
end)
|
|
||||||
|> Repo.isolated_transaction(:serializable)
|
error ->
|
||||||
|
error
|
||||||
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -261,7 +263,7 @@ defmodule Philomena.Images do
|
||||||
{:ok, nil}
|
{:ok, nil}
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_tags(%Image{} = image, attribution, attrs) do
|
def update_tags(%Image{} = image, attribution, attrs) do
|
||||||
|
@ -324,7 +326,7 @@ defmodule Philomena.Images do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp tag_change_attributes(attribution, image, tag, added, user) do
|
defp tag_change_attributes(attribution, image, tag, added, user) do
|
||||||
|
|
|
@ -128,7 +128,7 @@ defmodule Philomena.Interactions do
|
||||||
|
|
||||||
{:ok, nil}
|
{:ok, nil}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp union_all_queries([query]),
|
defp union_all_queries([query]),
|
||||||
|
|
|
@ -44,6 +44,15 @@ defmodule Philomena.PollVotes do
|
||||||
poll_votes = filter_options(user, poll, now, attrs)
|
poll_votes = filter_options(user, poll, now, attrs)
|
||||||
|
|
||||||
Multi.new()
|
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 ->
|
|> Multi.run(:existing_votes, fn _repo, _changes ->
|
||||||
# Don't proceed if any votes exist
|
# Don't proceed if any votes exist
|
||||||
case voted?(poll, user) do
|
case voted?(poll, user) do
|
||||||
|
@ -76,7 +85,7 @@ defmodule Philomena.PollVotes do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp filter_options(user, poll, now, %{"option_ids" => options}) when is_list(options) do
|
defp filter_options(user, poll, now, %{"option_ids" => options}) when is_list(options) do
|
||||||
|
|
|
@ -88,7 +88,7 @@ defmodule Philomena.Posts do
|
||||||
|> Multi.run(:subscribe, fn _repo, _changes ->
|
|> Multi.run(:subscribe, fn _repo, _changes ->
|
||||||
Topics.create_subscription(topic, attributes[:user])
|
Topics.create_subscription(topic, attributes[:user])
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_post(post) do
|
def notify_post(post) do
|
||||||
|
@ -146,7 +146,7 @@ defmodule Philomena.Posts do
|
||||||
"edit_reason" => current_reason
|
"edit_reason" => current_reason
|
||||||
})
|
})
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -61,7 +61,7 @@ defmodule Philomena.StaticPages do
|
||||||
|> Version.changeset(attrs)
|
|> Version.changeset(attrs)
|
||||||
|> repo.insert()
|
|> repo.insert()
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
@ -88,7 +88,7 @@ defmodule Philomena.StaticPages do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.update(:static_page, static_page)
|
|> Multi.update(:static_page, static_page)
|
||||||
|> Multi.insert(:version, version)
|
|> Multi.insert(:version, version)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -4,7 +4,6 @@ defmodule Philomena.Tags do
|
||||||
"""
|
"""
|
||||||
|
|
||||||
import Ecto.Query, warn: false
|
import Ecto.Query, warn: false
|
||||||
alias Ecto.Multi
|
|
||||||
alias Philomena.Repo
|
alias Philomena.Repo
|
||||||
|
|
||||||
alias Philomena.Elasticsearch
|
alias Philomena.Elasticsearch
|
||||||
|
@ -120,30 +119,34 @@ defmodule Philomena.Tags do
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_tag_image(%Tag{} = tag, attrs) do
|
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()
|
{:ok, tag}
|
||||||
|> Multi.update(:tag, changeset)
|
|
||||||
|> Multi.run(:update_file, fn _repo, %{tag: tag} ->
|
|
||||||
Uploader.persist_upload(tag)
|
|
||||||
Uploader.unpersist_old_upload(tag)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_tag_image(%Tag{} = tag) do
|
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()
|
{:ok, tag}
|
||||||
|> Multi.update(:tag, changeset)
|
|
||||||
|> Multi.run(:remove_file, fn _repo, %{tag: tag} ->
|
|
||||||
Uploader.unpersist_old_upload(tag)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@doc """
|
@doc """
|
||||||
|
|
|
@ -70,7 +70,7 @@ defmodule Philomena.Topics do
|
||||||
|> Multi.run(:subscribe, fn _repo, %{topic: topic} ->
|
|> Multi.run(:subscribe, fn _repo, %{topic: topic} ->
|
||||||
create_subscription(topic, attribution[:user])
|
create_subscription(topic, attribution[:user])
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def notify_topic(topic) do
|
def notify_topic(topic) do
|
||||||
|
@ -239,7 +239,7 @@ defmodule Philomena.Topics do
|
||||||
|
|
||||||
{:ok, count}
|
{:ok, count}
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def hide_topic(topic, deletion_reason, user) do
|
def hide_topic(topic, deletion_reason, user) do
|
||||||
|
|
|
@ -8,19 +8,6 @@ defmodule Philomena.UserIps do
|
||||||
|
|
||||||
alias Philomena.UserIps.UserIp
|
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 """
|
@doc """
|
||||||
Gets a single user_ip.
|
Gets a single user_ip.
|
||||||
|
|
||||||
|
@ -37,6 +24,31 @@ defmodule Philomena.UserIps do
|
||||||
"""
|
"""
|
||||||
def get_user_ip!(id), do: Repo.get!(UserIp, id)
|
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 """
|
@doc """
|
||||||
Creates a user_ip.
|
Creates a user_ip.
|
||||||
|
|
||||||
|
|
|
@ -106,7 +106,7 @@ defmodule Philomena.UserLinks do
|
||||||
{:ok, nil}
|
{:ok, nil}
|
||||||
end
|
end
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
def reject_user_link(%UserLink{} = user_link) do
|
def reject_user_link(%UserLink{} = user_link) do
|
||||||
|
|
|
@ -515,7 +515,7 @@ defmodule Philomena.Users do
|
||||||
|> Multi.run(:unsubscribe, fn _repo, %{user: user} ->
|
|> Multi.run(:unsubscribe, fn _repo, %{user: user} ->
|
||||||
unsubscribe_restricted_actors(user)
|
unsubscribe_restricted_actors(user)
|
||||||
end)
|
end)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
end
|
end
|
||||||
|
|
||||||
defp clean_roles(nil), do: []
|
defp clean_roles(nil), do: []
|
||||||
|
@ -562,30 +562,34 @@ defmodule Philomena.Users do
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_avatar(%User{} = user, attrs) do
|
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()
|
{:ok, user}
|
||||||
|> Multi.update(:user, changeset)
|
|
||||||
|> Multi.run(:update_file, fn _repo, %{user: user} ->
|
|
||||||
Uploader.persist_upload(user)
|
|
||||||
Uploader.unpersist_old_upload(user)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def remove_avatar(%User{} = user) do
|
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()
|
{:ok, user}
|
||||||
|> Multi.update(:user, changeset)
|
|
||||||
|> Multi.run(:remove_file, fn _repo, %{user: user} ->
|
|
||||||
Uploader.unpersist_old_upload(user)
|
|
||||||
|
|
||||||
{:ok, nil}
|
error ->
|
||||||
end)
|
error
|
||||||
|> Repo.isolated_transaction(:serializable)
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def update_name(user, user_params) do
|
def update_name(user, user_params) do
|
||||||
|
@ -597,7 +601,7 @@ defmodule Philomena.Users do
|
||||||
Multi.new()
|
Multi.new()
|
||||||
|> Multi.insert(:name_change, name_change)
|
|> Multi.insert(:name_change, name_change)
|
||||||
|> Multi.update(:account, account)
|
|> Multi.update(:account, account)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, %{account: %{name: new_name}}} = result ->
|
{:ok, %{account: %{name: new_name}}} = result ->
|
||||||
spawn(fn ->
|
spawn(fn ->
|
||||||
|
|
|
@ -25,7 +25,7 @@ defmodule PhilomenaWeb.Admin.BadgeController do
|
||||||
|
|
||||||
def create(conn, %{"badge" => badge_params}) do
|
def create(conn, %{"badge" => badge_params}) do
|
||||||
case Badges.create_badge(badge_params) do
|
case Badges.create_badge(badge_params) do
|
||||||
{:ok, %{badge: _badge}} ->
|
{:ok, _badge} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Badge created successfully.")
|
|> put_flash(:info, "Badge created successfully.")
|
||||||
|> redirect(to: Routes.admin_badge_path(conn, :index))
|
|> redirect(to: Routes.admin_badge_path(conn, :index))
|
||||||
|
|
|
@ -12,7 +12,7 @@ defmodule PhilomenaWeb.Image.DestroyController do
|
||||||
image = conn.assigns.image
|
image = conn.assigns.image
|
||||||
|
|
||||||
case Images.destroy_image(image) do
|
case Images.destroy_image(image) do
|
||||||
{:ok, %{image: image}} ->
|
{:ok, image} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Image contents destroyed.")
|
|> put_flash(:info, "Image contents destroyed.")
|
||||||
|> redirect(to: Routes.image_path(conn, :show, image))
|
|> redirect(to: Routes.image_path(conn, :show, image))
|
||||||
|
|
|
@ -27,7 +27,7 @@ defmodule PhilomenaWeb.Image.FaveController do
|
||||||
)
|
)
|
||||||
|> Multi.append(ImageVotes.delete_vote_transaction(image, user))
|
|> Multi.append(ImageVotes.delete_vote_transaction(image, user))
|
||||||
|> Multi.append(ImageVotes.create_vote_transaction(image, user, true))
|
|> Multi.append(ImageVotes.create_vote_transaction(image, user, true))
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, _result} ->
|
{:ok, _result} ->
|
||||||
image =
|
image =
|
||||||
|
@ -49,7 +49,7 @@ defmodule PhilomenaWeb.Image.FaveController do
|
||||||
image = conn.assigns.image
|
image = conn.assigns.image
|
||||||
|
|
||||||
ImageFaves.delete_fave_transaction(image, user)
|
ImageFaves.delete_fave_transaction(image, user)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, _result} ->
|
{:ok, _result} ->
|
||||||
image =
|
image =
|
||||||
|
|
|
@ -11,10 +11,7 @@ defmodule PhilomenaWeb.Image.FileController do
|
||||||
|
|
||||||
def update(conn, %{"image" => image_params}) do
|
def update(conn, %{"image" => image_params}) do
|
||||||
case Images.update_file(conn.assigns.image, image_params) do
|
case Images.update_file(conn.assigns.image, image_params) do
|
||||||
{:ok, %{image: image}} ->
|
{:ok, image} ->
|
||||||
Images.repair_image(image)
|
|
||||||
Images.reindex_image(image)
|
|
||||||
|
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Successfully updated file.")
|
|> put_flash(:info, "Successfully updated file.")
|
||||||
|> redirect(to: Routes.image_path(conn, :show, image))
|
|> redirect(to: Routes.image_path(conn, :show, image))
|
||||||
|
|
|
@ -18,7 +18,7 @@ defmodule PhilomenaWeb.Image.TamperController do
|
||||||
|
|
||||||
{:ok, _result} =
|
{:ok, _result} =
|
||||||
ImageVotes.delete_vote_transaction(image, user)
|
ImageVotes.delete_vote_transaction(image, user)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
|
|
||||||
Images.reindex_image(image)
|
Images.reindex_image(image)
|
||||||
|
|
||||||
|
|
|
@ -25,7 +25,7 @@ defmodule PhilomenaWeb.Image.VoteController do
|
||||||
ImageVotes.delete_vote_transaction(image, user),
|
ImageVotes.delete_vote_transaction(image, user),
|
||||||
ImageVotes.create_vote_transaction(image, user, params["up"] == true)
|
ImageVotes.create_vote_transaction(image, user, params["up"] == true)
|
||||||
)
|
)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, _result} ->
|
{:ok, _result} ->
|
||||||
image =
|
image =
|
||||||
|
@ -47,7 +47,7 @@ defmodule PhilomenaWeb.Image.VoteController do
|
||||||
image = conn.assigns.image
|
image = conn.assigns.image
|
||||||
|
|
||||||
ImageVotes.delete_vote_transaction(image, user)
|
ImageVotes.delete_vote_transaction(image, user)
|
||||||
|> Repo.isolated_transaction(:serializable)
|
|> Repo.transaction()
|
||||||
|> case do
|
|> case do
|
||||||
{:ok, _result} ->
|
{:ok, _result} ->
|
||||||
image =
|
image =
|
||||||
|
|
|
@ -20,7 +20,7 @@ defmodule PhilomenaWeb.Tag.ImageController do
|
||||||
|
|
||||||
def update(conn, %{"tag" => tag_params}) do
|
def update(conn, %{"tag" => tag_params}) do
|
||||||
case Tags.update_tag_image(conn.assigns.tag, tag_params) do
|
case Tags.update_tag_image(conn.assigns.tag, tag_params) do
|
||||||
{:ok, %{tag: tag}} ->
|
{:ok, tag} ->
|
||||||
conn
|
conn
|
||||||
|> put_flash(:info, "Tag image successfully updated.")
|
|> put_flash(:info, "Tag image successfully updated.")
|
||||||
|> redirect(to: Routes.tag_path(conn, :show, tag))
|
|> redirect(to: Routes.tag_path(conn, :show, tag))
|
||||||
|
|
Loading…
Reference in a new issue