-
-
Notifications
You must be signed in to change notification settings - Fork 56
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge remote-tracking branch 'upstream/master'
- Loading branch information
Showing
16 changed files
with
410 additions
and
75 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
defmodule Philomena.DuplicateReports.SearchQuery do | ||
use Ecto.Schema | ||
import Ecto.Changeset | ||
|
||
embedded_schema do | ||
field :distance, :float, default: 0.25 | ||
field :limit, :integer, default: 10 | ||
|
||
field :image_width, :integer | ||
field :image_height, :integer | ||
field :image_format, :string | ||
field :image_duration, :float | ||
field :image_mime_type, :string | ||
field :image_is_animated, :boolean | ||
field :image_aspect_ratio, :float | ||
field :uploaded_image, :string, virtual: true | ||
end | ||
|
||
@doc false | ||
def changeset(search_query, attrs \\ %{}) do | ||
search_query | ||
|> cast(attrs, [:distance, :limit]) | ||
|> validate_number(:distance, greater_than_or_equal_to: 0, less_than_or_equal_to: 1) | ||
|> validate_number(:limit, greater_than_or_equal_to: 1, less_than_or_equal_to: 50) | ||
end | ||
|
||
@doc false | ||
def image_changeset(search_query, attrs \\ %{}) do | ||
search_query | ||
|> cast(attrs, [ | ||
:image_width, | ||
:image_height, | ||
:image_format, | ||
:image_duration, | ||
:image_mime_type, | ||
:image_is_animated, | ||
:image_aspect_ratio, | ||
:uploaded_image | ||
]) | ||
|> validate_number(:image_width, greater_than: 0) | ||
|> validate_number(:image_height, greater_than: 0) | ||
|> validate_inclusion( | ||
:image_mime_type, | ||
~W(image/gif image/jpeg image/png image/svg+xml video/webm), | ||
message: "(#{attrs["image_mime_type"]}) is invalid" | ||
) | ||
end | ||
|
||
@doc false | ||
def to_analysis(search_query) do | ||
%PhilomenaMedia.Analyzers.Result{ | ||
animated?: search_query.image_is_animated, | ||
dimensions: {search_query.image_width, search_query.image_height}, | ||
duration: search_query.image_duration, | ||
extension: search_query.image_format, | ||
mime_type: search_query.image_mime_type | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
defmodule Philomena.DuplicateReports.Uploader do | ||
@moduledoc """ | ||
Upload and processing callback logic for SearchQuery images. | ||
""" | ||
|
||
alias Philomena.DuplicateReports.SearchQuery | ||
alias PhilomenaMedia.Uploader | ||
|
||
def analyze_upload(search_query, params) do | ||
Uploader.analyze_upload( | ||
search_query, | ||
"image", | ||
params["image"], | ||
&SearchQuery.image_changeset/2 | ||
) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,125 @@ | ||
defmodule Philomena.Users.Eraser do | ||
import Ecto.Query | ||
alias Philomena.Repo | ||
|
||
alias Philomena.Bans | ||
alias Philomena.Comments.Comment | ||
alias Philomena.Comments | ||
alias Philomena.Galleries.Gallery | ||
alias Philomena.Galleries | ||
alias Philomena.Posts.Post | ||
alias Philomena.Posts | ||
alias Philomena.Topics.Topic | ||
alias Philomena.Topics | ||
alias Philomena.Images | ||
alias Philomena.SourceChanges.SourceChange | ||
|
||
alias Philomena.Users | ||
|
||
@reason "Site abuse" | ||
@wipe_ip %Postgrex.INET{address: {127, 0, 1, 1}, netmask: 32} | ||
@wipe_fp "ffff" | ||
|
||
def erase_permanently!(user, moderator) do | ||
# Erase avatar | ||
{:ok, user} = Users.remove_avatar(user) | ||
|
||
# Erase "about me" and personal title | ||
{:ok, user} = Users.update_description(user, %{description: "", personal_title: ""}) | ||
|
||
# Delete all forum posts | ||
Post | ||
|> where(user_id: ^user.id) | ||
|> Repo.all() | ||
|> Enum.each(fn post -> | ||
{:ok, post} = Posts.hide_post(post, %{deletion_reason: @reason}, moderator) | ||
{:ok, _post} = Posts.destroy_post(post) | ||
end) | ||
|
||
# Delete all comments | ||
Comment | ||
|> where(user_id: ^user.id) | ||
|> Repo.all() | ||
|> Enum.each(fn comment -> | ||
{:ok, comment} = Comments.hide_comment(comment, %{deletion_reason: @reason}, moderator) | ||
{:ok, _comment} = Comments.destroy_comment(comment) | ||
end) | ||
|
||
# Delete all galleries | ||
Gallery | ||
|> where(creator_id: ^user.id) | ||
|> Repo.all() | ||
|> Enum.each(fn gallery -> | ||
{:ok, _gallery} = Galleries.delete_gallery(gallery) | ||
end) | ||
|
||
# Delete all posted topics | ||
Topic | ||
|> where(user_id: ^user.id) | ||
|> Repo.all() | ||
|> Enum.each(fn topic -> | ||
{:ok, _topic} = Topics.hide_topic(topic, @reason, moderator) | ||
end) | ||
|
||
# Revert all source changes | ||
SourceChange | ||
|> where(user_id: ^user.id) | ||
|> order_by(desc: :created_at) | ||
|> preload(:image) | ||
|> Repo.all() | ||
|> Enum.each(fn source_change -> | ||
if source_change.added do | ||
revert_added_source_change(source_change, user) | ||
else | ||
revert_removed_source_change(source_change, user) | ||
end | ||
end) | ||
|
||
# Delete all source changes | ||
SourceChange | ||
|> where(user_id: ^user.id) | ||
|> Repo.delete_all() | ||
|
||
# Ban the user | ||
{:ok, _ban} = | ||
Bans.create_user( | ||
moderator, | ||
%{ | ||
"user_id" => user.id, | ||
"reason" => @reason, | ||
"valid_until" => "permanent" | ||
} | ||
) | ||
|
||
# We succeeded | ||
:ok | ||
end | ||
|
||
defp revert_removed_source_change(source_change, user) do | ||
old_sources = %{} | ||
new_sources = %{"0" => %{"source" => source_change.source_url}} | ||
|
||
revert_source_change(source_change, user, old_sources, new_sources) | ||
end | ||
|
||
defp revert_added_source_change(source_change, user) do | ||
old_sources = %{"0" => %{"source" => source_change.source_url}} | ||
new_sources = %{} | ||
|
||
revert_source_change(source_change, user, old_sources, new_sources) | ||
end | ||
|
||
defp revert_source_change(source_change, user, old_sources, new_sources) do | ||
attrs = %{"old_sources" => old_sources, "sources" => new_sources} | ||
|
||
attribution = [ | ||
user: user, | ||
ip: @wipe_ip, | ||
fingerprint: @wipe_fp, | ||
user_agent: "", | ||
referrer: "" | ||
] | ||
|
||
{:ok, _} = Images.update_sources(source_change.image, attribution, attrs) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
defmodule Philomena.UserEraseWorker do | ||
alias Philomena.Users.Eraser | ||
alias Philomena.Users | ||
|
||
def perform(user_id, moderator_id) do | ||
moderator = Users.get_user!(moderator_id) | ||
user = Users.get_user!(user_id) | ||
|
||
Eraser.erase_permanently!(user, moderator) | ||
end | ||
end |
Oops, something went wrong.