Skip to content

Commit

Permalink
Merge remote-tracking branch 'upstream/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
liamwhite committed Jul 5, 2024
2 parents 40068b9 + 3c0c126 commit 07cf1c5
Show file tree
Hide file tree
Showing 9 changed files with 259 additions and 324 deletions.
10 changes: 5 additions & 5 deletions docker/app/run-development
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ export CARGO_HOME=/srv/philomena/.cargo

background() {
while :; do
mix run -e 'Philomena.Release.update_channels()'
mix run -e 'Philomena.Release.verify_artist_links()'
mix run -e 'Philomena.Release.update_stats()'
mix run -e 'Philomena.Release.clean_moderation_logs()'
mix run -e 'Philomena.Release.generate_autocomplete()'
mix run -e 'Philomena.Release.update_channels()' > /dev/null
mix run -e 'Philomena.Release.verify_artist_links()' > /dev/null
mix run -e 'Philomena.Release.update_stats()' > /dev/null
mix run -e 'Philomena.Release.clean_moderation_logs()' > /dev/null
mix run -e 'Philomena.Release.generate_autocomplete()' > /dev/null

sleep 300
done
Expand Down
70 changes: 4 additions & 66 deletions lib/philomena/channels.ex
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ defmodule Philomena.Channels do

alias Philomena.Channels.AutomaticUpdater
alias Philomena.Channels.Channel
alias Philomena.Notifications

use Philomena.Subscriptions,
actor_types: ~w(Channel LivestreamChannel),
id_name: :channel_id

@doc """
Updates all the tracked channels for which an update scheme is known.
Expand Down Expand Up @@ -115,69 +118,4 @@ defmodule Philomena.Channels do
def change_channel(%Channel{} = channel) do
Channel.changeset(channel, %{})
end

alias Philomena.Channels.Subscription

@doc """
Creates a subscription.
## Examples
iex> create_subscription(%{field: value})
{:ok, %Subscription{}}
iex> create_subscription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_subscription(_channel, nil), do: {:ok, nil}

def create_subscription(channel, user) do
%Subscription{channel_id: channel.id, user_id: user.id}
|> Subscription.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end

@doc """
Deletes a Subscription.
## Examples
iex> delete_subscription(subscription)
{:ok, %Subscription{}}
iex> delete_subscription(subscription)
{:error, %Ecto.Changeset{}}
"""
def delete_subscription(channel, user) do
clear_notification(channel, user)

%Subscription{channel_id: channel.id, user_id: user.id}
|> Repo.delete()
end

def subscribed?(_channel, nil), do: false

def subscribed?(channel, user) do
Subscription
|> where(channel_id: ^channel.id, user_id: ^user.id)
|> Repo.exists?()
end

def subscriptions(_channels, nil), do: %{}

def subscriptions(channels, user) do
channel_ids = Enum.map(channels, & &1.id)

Subscription
|> where([s], s.channel_id in ^channel_ids and s.user_id == ^user.id)
|> Repo.all()
|> Map.new(&{&1.channel_id, true})
end

def clear_notification(channel, user) do
Notifications.delete_unread_notification("Channel", channel.id, user)
Notifications.delete_unread_notification("LivestreamChannel", channel.id, user)
end
end
19 changes: 6 additions & 13 deletions lib/philomena/comments.ex
Original file line number Diff line number Diff line change
Expand Up @@ -59,24 +59,17 @@ defmodule Philomena.Comments do
Image
|> where(id: ^image.id)

image_lock_query =
lock(image_query, "FOR UPDATE")

Multi.new()
|> Multi.one(:image, image_lock_query)
|> Multi.insert(:comment, comment)
|> Multi.update_all(:image, image_query, inc: [comments_count: 1])
|> maybe_create_subscription_on_reply(image, attribution[:user])
|> Multi.update_all(:update_image, image_query, inc: [comments_count: 1])
|> Images.maybe_subscribe_on(:image, attribution[:user], :watch_on_reply)
|> Repo.transaction()
end

defp maybe_create_subscription_on_reply(multi, image, %User{watch_on_reply: true} = user) do
multi
|> Multi.run(:subscribe, fn _repo, _changes ->
Images.create_subscription(image, user)
end)
end

defp maybe_create_subscription_on_reply(multi, _image, _user) do
multi
end

def notify_comment(comment) do
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Comments", comment.id])
end
Expand Down
47 changes: 4 additions & 43 deletions lib/philomena/forums.ex
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,10 @@ defmodule Philomena.Forums do
alias Philomena.Repo

alias Philomena.Forums.Forum
alias Philomena.Forums.Subscription
alias Philomena.Notifications

use Philomena.Subscriptions,
actor_types: ~w(Forum),
id_name: :forum_id

@doc """
Returns the list of forums.
Expand Down Expand Up @@ -103,45 +105,4 @@ defmodule Philomena.Forums do
def change_forum(%Forum{} = forum) do
Forum.changeset(forum, %{})
end

def subscribed?(_forum, nil), do: false

def subscribed?(forum, user) do
Subscription
|> where(forum_id: ^forum.id, user_id: ^user.id)
|> Repo.exists?()
end

def create_subscription(_forum, nil), do: {:ok, nil}

def create_subscription(forum, user) do
%Subscription{forum_id: forum.id, user_id: user.id}
|> Subscription.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end

@doc """
Deletes a Subscription.
## Examples
iex> delete_subscription(subscription)
{:ok, %Subscription{}}
iex> delete_subscription(subscription)
{:error, %Ecto.Changeset{}}
"""
def delete_subscription(forum, user) do
clear_notification(forum, user)

%Subscription{forum_id: forum.id, user_id: user.id}
|> Repo.delete()
end

def clear_notification(_forum, nil), do: nil

def clear_notification(forum, user) do
Notifications.delete_unread_notification("Forum", forum.id, user)
end
end
55 changes: 4 additions & 51 deletions lib/philomena/galleries.ex
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ defmodule Philomena.Galleries do
alias Philomena.Notifications.{Notification, UnreadNotification}
alias Philomena.Images

use Philomena.Subscriptions,
actor_types: ~w(Gallery),
id_name: :gallery_id

@doc """
Gets a single gallery.
Expand Down Expand Up @@ -356,55 +360,4 @@ defmodule Philomena.Galleries do

defp position_order(%{order_position_asc: true}), do: [asc: :position]
defp position_order(_gallery), do: [desc: :position]

alias Philomena.Galleries.Subscription

def subscribed?(_gallery, nil), do: false

def subscribed?(gallery, user) do
Subscription
|> where(gallery_id: ^gallery.id, user_id: ^user.id)
|> Repo.exists?()
end

@doc """
Creates a subscription.
## Examples
iex> create_subscription(%{field: value})
{:ok, %Subscription{}}
iex> create_subscription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_subscription(gallery, user) do
%Subscription{gallery_id: gallery.id, user_id: user.id}
|> Subscription.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end

@doc """
Deletes a Subscription.
## Examples
iex> delete_subscription(subscription)
{:ok, %Subscription{}}
iex> delete_subscription(subscription)
{:error, %Ecto.Changeset{}}
"""
def delete_subscription(gallery, user) do
%Subscription{gallery_id: gallery.id, user_id: user.id}
|> Repo.delete()
end

def clear_notification(_gallery, nil), do: nil

def clear_notification(gallery, user) do
Notifications.delete_unread_notification("Gallery", gallery.id, user)
end
end
70 changes: 5 additions & 65 deletions lib/philomena/images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ defmodule Philomena.Images do
alias Philomena.Users.User
alias Philomena.Games.{Player, Team}

use Philomena.Subscriptions,
actor_types: ~w(Image),
id_name: :image_id

@doc """
Gets a single image.
Expand Down Expand Up @@ -104,7 +108,7 @@ defmodule Philomena.Images do

{:ok, count}
end)
|> maybe_create_subscription_on_upload(attribution[:user])
|> maybe_subscribe_on(:image, attribution[:user], :watch_on_upload)
|> Repo.transaction()
|> case do
{:ok, %{image: image}} = result ->
Expand Down Expand Up @@ -158,17 +162,6 @@ defmodule Philomena.Images do
Logger.error("Aborting upload of #{image.id} after #{retry_count} retries")
end

defp maybe_create_subscription_on_upload(multi, %User{watch_on_upload: true} = user) do
multi
|> Multi.run(:subscribe, fn _repo, %{image: image} ->
create_subscription(image, user)
end)
end

defp maybe_create_subscription_on_upload(multi, _user) do
multi
end

def approve_image(image) do
image
|> Repo.preload(:user)
Expand Down Expand Up @@ -897,53 +890,6 @@ defmodule Philomena.Images do

alias Philomena.Images.Subscription

def subscribed?(_image, nil), do: false

def subscribed?(image, user) do
Subscription
|> where(image_id: ^image.id, user_id: ^user.id)
|> Repo.exists?()
end

@doc """
Creates a subscription.
## Examples
iex> create_subscription(%{field: value})
{:ok, %Subscription{}}
iex> create_subscription(%{field: bad_value})
{:error, %Ecto.Changeset{}}
"""
def create_subscription(_image, nil), do: {:ok, nil}

def create_subscription(image, user) do
%Subscription{image_id: image.id, user_id: user.id}
|> Subscription.changeset(%{})
|> Repo.insert(on_conflict: :nothing)
end

@doc """
Deletes a subscription.
## Examples
iex> delete_subscription(subscription)
{:ok, %Subscription{}}
iex> delete_subscription(subscription)
{:error, %Ecto.Changeset{}}
"""
def delete_subscription(image, user) do
clear_notification(image, user)

%Subscription{image_id: image.id, user_id: user.id}
|> Repo.delete()
end

def migrate_subscriptions(source, target) do
subscriptions =
Subscription
Expand Down Expand Up @@ -997,10 +943,4 @@ defmodule Philomena.Images do
}
)
end

def clear_notification(_image, nil), do: nil

def clear_notification(image, user) do
Notifications.delete_unread_notification("Image", image.id, user)
end
end
15 changes: 2 additions & 13 deletions lib/philomena/posts.ex
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ defmodule Philomena.Posts do
|> where(id: ^topic.forum_id)

Multi.new()
|> Multi.all(:topic_lock, topic_lock_query)
|> Multi.one(:topic, topic_lock_query)
|> Multi.run(:post, fn repo, _ ->
last_position =
Post
Expand Down Expand Up @@ -96,7 +96,7 @@ defmodule Philomena.Posts do

{:ok, count}
end)
|> maybe_create_subscription_on_reply(topic, attributes[:user])
|> Topics.maybe_subscribe_on(:topic, attributes[:user], :watch_on_reply)
|> Repo.transaction()
|> case do
{:ok, %{post: post}} = result ->
Expand All @@ -109,17 +109,6 @@ defmodule Philomena.Posts do
end
end

defp maybe_create_subscription_on_reply(multi, topic, %User{watch_on_reply: true} = user) do
multi
|> Multi.run(:subscribe, fn _repo, _changes ->
Topics.create_subscription(topic, user)
end)
end

defp maybe_create_subscription_on_reply(multi, _topic, _user) do
multi
end

def notify_post(post) do
Exq.enqueue(Exq, "notifications", NotificationWorker, ["Posts", post.id])
end
Expand Down
Loading

0 comments on commit 07cf1c5

Please sign in to comment.