Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions lib/philomena/images.ex
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,26 @@ defmodule Philomena.Images do
on_delete: :clear_image_notification,
id_name: :image_id

def create_subscription(object, user) do
result = super(object, user)

if match?({:ok, _}, result) do
Image |> where(id: ^object.id) |> Repo.update_all(inc: [subscriptions_count: 1])
end

result
end

def delete_subscription(object, user) do
result = super(object, user)

if match?({:ok, _}, result) do
Image |> where(id: ^object.id) |> Repo.update_all(inc: [subscriptions_count: -1])
end

result
end

@doc """
Gets a single image.

Expand Down
1 change: 1 addition & 0 deletions lib/philomena/images/image.ex
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ defmodule Philomena.Images.Image do
field :hidden_image_key, :string
field :scratchpad, :string
field :hides_count, :integer, default: 0
field :subscriptions_count, :integer, default: 0
field :approved, :boolean

field :removed_tags, {:array, :any}, default: [], virtual: true
Expand Down
2 changes: 1 addition & 1 deletion lib/philomena/images/query.ex
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ defmodule Philomena.Images.Query do
defp anonymous_fields do
[
int_fields:
~W(id width height score upvotes downvotes hides faves pixels size orig_size comment_count source_count tag_count) ++
~W(id width height score upvotes downvotes hides subscriptions faves pixels size orig_size comment_count source_count tag_count) ++
tag_count_fields(),
numeric_fields: ~W(uploader_id faved_by_id duplicate_id),
float_fields: ~W(aspect_ratio wilson_score duration),
Expand Down
4 changes: 3 additions & 1 deletion lib/philomena/images/search_index.ex
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,8 @@ defmodule Philomena.Images.SearchIndex do
content_official_tag_count: %{type: "integer"},
spoiler_tag_count: %{type: "integer"},
scratchpad: %{type: "text", analyzer: "snowball"},
hides: %{type: "integer"}
hides: %{type: "integer"},
subscriptions: %{type: "integer"}
}
}
}
Expand All @@ -104,6 +105,7 @@ defmodule Philomena.Images.SearchIndex do
score: image.score,
faves: image.faves_count,
hides: image.hides_count,
subscriptions: image.subscriptions_count,
comment_count: image.comments_count,
width: image.image_width,
height: image.image_height,
Expand Down
2 changes: 2 additions & 0 deletions lib/philomena/subscriptions.ex
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,8 @@ defmodule Philomena.Subscriptions do
def maybe_subscribe_on(multi, change_name, user, field) do
Philomena.Subscriptions.maybe_subscribe_on(multi, __MODULE__, change_name, user, field)
end

defoverridable create_subscription: 2, delete_subscription: 2
end
end

Expand Down
1 change: 1 addition & 0 deletions lib/philomena_web/image_sorter.ex
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ defmodule PhilomenaWeb.ImageSorter do
size
duration
hides
subscriptions
)

def parse_sort(params, query) do
Expand Down
1 change: 1 addition & 0 deletions lib/philomena_web/templates/search/_form.html.slime
Original file line number Diff line number Diff line change
Expand Up @@ -156,6 +156,7 @@ h1 Search
"Sort by score": :score,
"Sort by Wilson score": :wilson_score,
"Sort by hides": :hides,
"Sort by subscriptions": :subscriptions,
"Sort by relevance": :_score,
"Sort by width": :width,
"Sort by height": :height,
Expand Down
1 change: 1 addition & 0 deletions lib/philomena_web/views/api/json/image_view.ex
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ defmodule PhilomenaWeb.Api.Json.ImageView do
downvotes: image.downvotes_count,
faves: image.faves_count,
hides: image.hides_count,
subscriptions: image.subscriptions_count,
comment_count: image.comments_count,
tag_count: length(image.tags),
description: image.description,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
defmodule Philomena.Repo.Migrations.AddSubscriptionsCountToImages do
use Ecto.Migration

def change do
alter table(:images) do
add :subscriptions_count, :integer, default: 0, null: false
end

execute("""
UPDATE images
SET subscriptions_count = (
SELECT COUNT(*) FROM image_subscriptions WHERE image_id = images.id
)
""")
end
end
Loading