run formatter

This commit is contained in:
byte[] 2020-01-10 23:20:19 -05:00
parent 4ac63f9f4e
commit ed44160603
368 changed files with 3392 additions and 1388 deletions

View file

@ -1,3 +1,13 @@
alias Philomena.{Repo, Comments.Comment, Galleries.Gallery, Posts.Post, Images.Image, Topics.Topic, Tags.Tag, Users.User}
alias Philomena.{
Repo,
Comments.Comment,
Galleries.Gallery,
Posts.Post,
Images.Image,
Topics.Topic,
Tags.Tag,
Users.User
}
import Ecto.Query
import Ecto.Changeset

View file

@ -68,11 +68,9 @@ config :logger, :console, format: "[$level] $message\n"
config :logger, compile_time_purge_matching: [[application: :remote_ip]]
# Set up mailer
config :philomena, PhilomenaWeb.Mailer,
adapter: Bamboo.LocalAdapter
config :philomena, PhilomenaWeb.Mailer, adapter: Bamboo.LocalAdapter
config :philomena, :mailer_address,
"noreply@philomena.lc"
config :philomena, :mailer_address, "noreply@philomena.lc"
# Use this to debug slime templates
# config :slime, :keep_lines, true

View file

@ -6,6 +6,7 @@ defmodule Camo.Image do
input
else
camo_digest = :crypto.hmac(:sha, camo_key(), input) |> Base.encode16(case: :lower)
camo_uri = %URI{
host: camo_host(),
path: "/" <> camo_digest,
@ -28,4 +29,4 @@ defmodule Camo.Image do
defp camo_host do
Application.get_env(:philomena, :camo_host)
end
end
end

View file

@ -2,7 +2,16 @@ defmodule Mix.Tasks.ReindexAll do
use Mix.Task
alias Philomena.Elasticsearch
alias Philomena.{Comments.Comment, Galleries.Gallery, Posts.Post, Images.Image, Reports.Report, Tags.Tag}
alias Philomena.{
Comments.Comment,
Galleries.Gallery,
Posts.Post,
Images.Image,
Reports.Report,
Tags.Tag
}
alias Philomena.{Comments, Galleries, Posts, Images, Tags}
alias Philomena.Polymorphic
alias Philomena.Repo
@ -10,13 +19,19 @@ defmodule Mix.Tasks.ReindexAll do
@shortdoc "Destroys and recreates all Elasticsearch indices."
def run(_) do
if Mix.env == "prod" do
if Mix.env() == "prod" do
raise "do not run this task in production"
end
{:ok, _apps} = Application.ensure_all_started(:philomena)
for {context, schema} <- [{Images, Image}, {Comments, Comment}, {Galleries, Gallery}, {Tags, Tag}, {Posts, Post}] do
for {context, schema} <- [
{Images, Image},
{Comments, Comment},
{Galleries, Gallery},
{Tags, Tag},
{Posts, Post}
] do
Elasticsearch.delete_index!(schema)
Elasticsearch.create_index!(schema)

View file

@ -10,7 +10,6 @@ defmodule Philomena.Adverts do
alias Philomena.Adverts.Advert
alias Philomena.Adverts.Uploader
def random_live do
now = DateTime.utc_now()
@ -38,18 +37,19 @@ defmodule Philomena.Adverts do
end
def click(%Advert{} = advert) do
spawn fn ->
spawn(fn ->
query = where(Advert, id: ^advert.id)
Repo.update_all(query, inc: [clicks: 1])
end
end)
end
defp record_impression(nil), do: nil
defp record_impression(advert) do
spawn fn ->
spawn(fn ->
query = where(Advert, id: ^advert.id)
Repo.update_all(query, inc: [impressions: 1])
end
end)
advert
end

View file

@ -50,8 +50,13 @@ defmodule Philomena.Adverts.Advert do
def image_changeset(advert, attrs) do
advert
|> cast(attrs, [
:image, :image_mime_type, :image_size, :image_width, :image_height,
:uploaded_image, :removed_image
:image,
:image_mime_type,
:image_size,
:image_width,
:image_height,
:uploaded_image,
:removed_image
])
|> validate_required([:image])
|> validate_inclusion(:image_mime_type, ["image/png", "image/jpeg", "image/gif"])

View file

@ -41,15 +41,16 @@ defmodule Philomena.Analyzers do
"""
@spec analyze(Plug.Upload.t() | String.t()) :: {:ok, map()} | :error
def analyze(%Plug.Upload{path: path}), do: analyze(path)
def analyze(path) when is_binary(path) do
with {:ok, mime} <- Mime.file(path),
{:ok, analyzer} <- analyzer(mime)
do
{:ok, analyzer} <- analyzer(mime) do
{:ok, analyzer.analyze(path)}
else
error ->
error
end
end
def analyze(_path), do: :error
end
end

View file

@ -18,7 +18,7 @@ defmodule Philomena.Analyzers.Gif do
{output, 0} ->
len =
output
|> String.split("\n", parts: 2, trim: true)
|> String.split("\n", parts: 2, trim: true)
|> length()
len > 1
@ -29,10 +29,20 @@ defmodule Philomena.Analyzers.Gif do
end
defp duration(false, _file), do: 0.0
defp duration(true, file) do
with {output, 0} <- System.cmd("ffprobe", ["-i", file, "-show_entries", "format=duration", "-v", "quiet", "-of", "csv=p=0"]),
{duration, _} <- Float.parse(output)
do
with {output, 0} <-
System.cmd("ffprobe", [
"-i",
file,
"-show_entries",
"format=duration",
"-v",
"quiet",
"-of",
"csv=p=0"
]),
{duration, _} <- Float.parse(output) do
duration
else
_ ->
@ -58,4 +68,4 @@ defmodule Philomena.Analyzers.Gif do
{0, 0}
end
end
end
end

View file

@ -25,4 +25,4 @@ defmodule Philomena.Analyzers.Jpeg do
{0, 0}
end
end
end
end

View file

@ -13,7 +13,16 @@ defmodule Philomena.Analyzers.Png do
end
defp animated?(file) do
System.cmd("ffprobe", ["-i", file, "-v", "quiet", "-show_entries", "stream=codec_name", "-of", "csv=p=0"])
System.cmd("ffprobe", [
"-i",
file,
"-v",
"quiet",
"-show_entries",
"stream=codec_name",
"-of",
"csv=p=0"
])
|> case do
{"apng\n", 0} ->
true
@ -42,4 +51,4 @@ defmodule Philomena.Analyzers.Png do
{0, 0}
end
end
end
end

View file

@ -26,4 +26,4 @@ defmodule Philomena.Analyzers.Svg do
{0, 0}
end
end
end
end

View file

@ -10,9 +10,18 @@ defmodule Philomena.Analyzers.Webm do
end
defp duration(file) do
with {output, 0} <- System.cmd("ffprobe", ["-i", file, "-show_entries", "format=duration", "-v", "quiet", "-of", "csv=p=0"]),
{duration, _} <- Float.parse(output)
do
with {output, 0} <-
System.cmd("ffprobe", [
"-i",
file,
"-show_entries",
"format=duration",
"-v",
"quiet",
"-of",
"csv=p=0"
]),
{duration, _} <- Float.parse(output) do
duration
else
_ ->
@ -21,7 +30,16 @@ defmodule Philomena.Analyzers.Webm do
end
defp dimensions(file) do
System.cmd("ffprobe", ["-i", file, "-show_entries", "stream=width,height", "-v", "quiet", "-of", "csv=p=0"])
System.cmd("ffprobe", [
"-i",
file,
"-show_entries",
"stream=width,height",
"-v",
"quiet",
"-of",
"csv=p=0"
])
|> case do
{output, 0} ->
[width, height] =
@ -36,4 +54,4 @@ defmodule Philomena.Analyzers.Webm do
{0, 0}
end
end
end
end

View file

@ -20,4 +20,4 @@ defprotocol Philomena.Attribution do
"""
@spec anonymous?(struct()) :: true | false
def anonymous?(object)
end
end

View file

@ -30,5 +30,6 @@ defmodule Philomena.Badges.Award do
put_change(changeset, :awarded_on, now)
end
defp put_awarded_on(changeset), do: changeset
end

View file

@ -331,10 +331,11 @@ defmodule Philomena.Bans do
"""
def exists_for?(user, ip, fingerprint) do
now = DateTime.utc_now()
queries =
subnet_query(ip, now) ++
fingerprint_query(fingerprint, now) ++
user_query(user, now)
fingerprint_query(fingerprint, now) ++
user_query(user, now)
bans =
queries
@ -343,38 +344,56 @@ defmodule Philomena.Bans do
# Don't return a ban if the user is currently signed in.
case is_nil(user) do
true -> Enum.at(bans, 0)
true -> Enum.at(bans, 0)
false -> user_ban(bans)
end
end
defp fingerprint_query(nil, _now), do: []
defp fingerprint_query(fingerprint, now) do
[
Fingerprint
|> select([f], %{reason: f.reason, valid_until: f.valid_until, generated_ban_id: f.generated_ban_id, type: ^"FingerprintBan"})
|> select([f], %{
reason: f.reason,
valid_until: f.valid_until,
generated_ban_id: f.generated_ban_id,
type: ^"FingerprintBan"
})
|> where([f], f.enabled and f.valid_until > ^now)
|> where([f], f.fingerprint == ^fingerprint)
]
end
defp subnet_query(nil, _now), do: []
defp subnet_query(ip, now) do
{:ok, inet} = EctoNetwork.INET.cast(ip)
[
Subnet
|> select([s], %{reason: s.reason, valid_until: s.valid_until, generated_ban_id: s.generated_ban_id, type: ^"SubnetBan"})
|> select([s], %{
reason: s.reason,
valid_until: s.valid_until,
generated_ban_id: s.generated_ban_id,
type: ^"SubnetBan"
})
|> where([s], s.enabled and s.valid_until > ^now)
|> where(fragment("specification >>= ?", ^inet))
]
end
defp user_query(nil, _now), do: []
defp user_query(user, now) do
[
User
|> select([u], %{reason: u.reason, valid_until: u.valid_until, generated_ban_id: u.generated_ban_id, type: ^"UserBan"})
|> select([u], %{
reason: u.reason,
valid_until: u.valid_until,
generated_ban_id: u.generated_ban_id,
type: ^"UserBan"
})
|> where([u], u.enabled and u.valid_until > ^now)
|> where([u], u.user_id == ^user.id)
]
@ -382,12 +401,13 @@ defmodule Philomena.Bans do
defp union_all_queries([query]),
do: query
defp union_all_queries([query | rest]),
do: query |> union_all(^union_all_queries(rest))
defp user_ban(bans) do
bans
|> Enum.filter(& &1.type == "UserBan")
|> Enum.filter(&(&1.type == "UserBan"))
|> Enum.at(0)
end
end

View file

@ -27,6 +27,7 @@ defmodule Philomena.Batch do
end
defp query_batches(_queryable, _opts, _callback, []), do: []
defp query_batches(queryable, opts, callback, ids) do
id_field = Keyword.get(opts, :id_field, :id)

View file

@ -1,7 +1,7 @@
defmodule Philomena.Captcha do
defstruct [:image_base64, :solution, :solution_id]
@numbers ~W(1 2 3 4 5 6)
@numbers ~W(1 2 3 4 5 6)
@images ~W(1 2 3 4 5 6)
@base_path File.cwd!() <> "/assets/static/images/captcha"
@ -26,9 +26,15 @@ defmodule Philomena.Captcha do
@background_file @base_path <> "/background.png"
@geometry %{
1 => "+0+0", 2 => "+120+0", 3 => "+240+0",
4 => "+0+120", 5 => "+120+120", 6 => "+240+120",
7 => "+0+240", 8 => "+120+240", 9 => "+240+240"
1 => "+0+0",
2 => "+120+0",
3 => "+240+0",
4 => "+0+120",
5 => "+120+120",
6 => "+240+120",
7 => "+0+240",
8 => "+120+240",
9 => "+240+240"
}
@distortion_1 [
@ -60,7 +66,8 @@ defmodule Philomena.Captcha do
# Base arguments
args = [
"-page", "360x360",
"-page",
"360x360",
@background_file
]
@ -71,8 +78,18 @@ defmodule Philomena.Captcha do
|> Enum.flat_map(fn {num, index} ->
if num do
[
"(", @image_files[solution[num]], ")", "-geometry", @geometry[index + 1], "-composite",
"(", @number_files[num], ")", "-geometry", @geometry[index + 1], "-composite"
"(",
@image_files[solution[num]],
")",
"-geometry",
@geometry[index + 1],
"-composite",
"(",
@number_files[num],
")",
"-geometry",
@geometry[index + 1],
"-composite"
]
else
[]
@ -99,6 +116,7 @@ defmodule Philomena.Captcha do
solution_id =
:crypto.strong_rand_bytes(12)
|> Base.encode16(case: :lower)
solution_id = "cp_" <> solution_id
{:ok, _ok} = Redix.command(:redix, ["SET", solution_id, Jason.encode!(solution)])
@ -116,8 +134,7 @@ defmodule Philomena.Captcha do
# have minimal impact if the race succeeds.
with {:ok, sol} <- Redix.command(:redix, ["GET", solution_id]),
{:ok, _del} <- Redix.command(:redix, ["DEL", solution_id]),
{:ok, sol} <- Jason.decode(to_string(sol))
do
{:ok, sol} <- Jason.decode(to_string(sol)) do
Map.equal?(solution, sol)
else
_ ->

View file

@ -118,6 +118,7 @@ defmodule Philomena.Channels do
"""
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(%{})
@ -144,6 +145,7 @@ defmodule Philomena.Channels do
end
def subscribed?(_channel, nil), do: false
def subscribed?(channel, user) do
Subscription
|> where(channel_id: ^channel.id, user_id: ^user.id)
@ -151,6 +153,7 @@ defmodule Philomena.Channels do
end
def subscriptions(_channels, nil), do: %{}
def subscriptions(channels, user) do
channel_ids = Enum.map(channels, & &1.id)

View file

@ -52,7 +52,7 @@ defmodule Philomena.Comments do
Image
|> where(id: ^image.id)
Multi.new
Multi.new()
|> Multi.insert(:comment, comment)
|> Multi.update_all(:image, image_query, inc: [comments_count: 1])
|> Multi.run(:subscribe, fn _repo, _changes ->
@ -62,7 +62,7 @@ defmodule Philomena.Comments do
end
def notify_comment(comment) do
spawn fn ->
spawn(fn ->
image =
comment
|> Repo.preload(:image)
@ -84,7 +84,7 @@ defmodule Philomena.Comments do
action: "commented on"
}
)
end
end)
comment
end
@ -106,10 +106,9 @@ defmodule Philomena.Comments do
current_body = comment.body
current_reason = comment.edit_reason
comment_changes =
Comment.changeset(comment, attrs, now)
comment_changes = Comment.changeset(comment, attrs, now)
Multi.new
Multi.new()
|> Multi.update(:comment, comment_changes)
|> Multi.run(:version, fn _repo, _changes ->
Versions.create_version("Comment", comment.id, editor.id, %{
@ -190,24 +189,24 @@ defmodule Philomena.Comments do
end
def reindex_comment(%Comment{} = comment) do
spawn fn ->
spawn(fn ->
Comment
|> preload(^indexing_preloads())
|> where(id: ^comment.id)
|> Repo.one()
|> Elasticsearch.index_document(Comment)
end
end)
comment
end
def reindex_comments(image) do
spawn fn ->
spawn(fn ->
Comment
|> preload(^indexing_preloads())
|> where(image_id: ^image.id)
|> Elasticsearch.reindex(Comment)
end
end)
image
end

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.Comments.Comment do
def anonymous?(comment) do
!!comment.anonymous
end
end
end

View file

@ -33,8 +33,10 @@ defmodule Philomena.Comments.ElasticsearchIndex do
user_id: %{type: "keyword"},
author: %{type: "keyword"},
image_tag_ids: %{type: "keyword"},
anonymous: %{type: "keyword"}, # boolean
hidden_from_users: %{type: "keyword"}, # boolean
# boolean
anonymous: %{type: "keyword"},
# boolean
hidden_from_users: %{type: "keyword"},
body: %{type: "text", analyzer: "snowball"}
}
}

View file

@ -60,23 +60,23 @@ defmodule Philomena.Comments.Query do
defp user_fields do
fields = anonymous_fields()
Keyword.merge(fields, [
Keyword.merge(fields,
custom_fields: fields[:custom_fields] ++ ~W(my),
transforms: Map.merge(fields[:transforms], %{"my" => &user_my_transform/2})
])
)
end
defp moderator_fields do
fields = user_fields()
Keyword.merge(fields, [
Keyword.merge(fields,
literal_fields: ~W(image_id user_id author fingerprint),
ip_fields: ~W(ip),
bool_fields: ~W(anonymous deleted),
custom_fields: fields[:custom_fields] -- ~W(author user_id),
aliases: Map.merge(fields[:aliases], %{"deleted" => "hidden_from_users"}),
transforms: Map.drop(fields[:transforms], ~W(author user_id))
])
)
end
defp parse(fields, context, query_string) do

View file

@ -25,7 +25,15 @@ defmodule Philomena.Commissions.Commission do
@doc false
def changeset(commission, attrs) do
commission
|> cast(attrs, [:information, :contact, :will_create, :will_not_create, :open, :sheet_image_id, :categories])
|> cast(attrs, [
:information,
:contact,
:will_create,
:will_not_create,
:open,
:sheet_image_id,
:categories
])
|> drop_blank_categories()
|> validate_required([:user_id, :information, :contact, :open])
|> validate_length(:information, max: 700, count: :bytes)
@ -37,25 +45,25 @@ defmodule Philomena.Commissions.Commission do
categories =
changeset
|> get_field(:categories)
|> Enum.filter(& &1 not in [nil, ""])
|> Enum.filter(&(&1 not in [nil, ""]))
change(changeset, categories: categories)
end
def categories do
[
"Anthro": "Anthro",
Anthro: "Anthro",
"Canon Characters": "Canon Characters",
"Comics": "Comics",
Comics: "Comics",
"Fetish Art": "Fetish Art",
"Human and EqG": "Human and EqG",
"NSFW": "NSFW",
NSFW: "NSFW",
"Original Characters": "Original Characters",
"Original Species": "Original Species",
"Pony": "Pony",
"Requests": "Requests",
"Safe": "Safe",
"Shipping": "Shipping",
Pony: "Pony",
Requests: "Requests",
Safe: "Safe",
Shipping: "Shipping",
"Violence and Gore": "Violence and Gore"
]
end

View file

@ -92,52 +92,64 @@ defmodule Philomena.Conversations do
def count_unread_conversations(user) do
Conversation
|> where([c],
(
(c.to_id == ^user.id and c.to_read == false) or
(c.from_id == ^user.id and c.from_read == false)
)
and not (
(c.to_id == ^user.id and c.to_hidden == true) or
(c.from_id == ^user.id and c.from_hidden == true)
)
|> where(
[c],
((c.to_id == ^user.id and c.to_read == false) or
(c.from_id == ^user.id and c.from_read == false)) and
not ((c.to_id == ^user.id and c.to_hidden == true) or
(c.from_id == ^user.id and c.from_hidden == true))
)
|> Repo.aggregate(:count, :id)
end
def mark_conversation_read(conversation, user, read \\ true)
def mark_conversation_read(%Conversation{to_id: user_id, from_id: user_id} = conversation, %{id: user_id}, read) do
def mark_conversation_read(
%Conversation{to_id: user_id, from_id: user_id} = conversation,
%{id: user_id},
read
) do
conversation
|> Conversation.read_changeset(%{to_read: read, from_read: read})
|> Repo.update()
end
def mark_conversation_read(%Conversation{to_id: user_id} = conversation, %{id: user_id}, read) do
conversation
|> Conversation.read_changeset(%{to_read: read})
|> Repo.update()
end
def mark_conversation_read(%Conversation{from_id: user_id} = conversation, %{id: user_id}, read) do
conversation
|> Conversation.read_changeset(%{from_read: read})
|> Repo.update()
end
def mark_conversation_read(_conversation, _user, _read), do: {:ok, nil}
def mark_conversation_read(_conversation, _user, _read), do: {:ok, nil}
def mark_conversation_hidden(conversation, user, hidden \\ true)
def mark_conversation_hidden(%Conversation{to_id: user_id} = conversation, %{id: user_id}, hidden) do
def mark_conversation_hidden(
%Conversation{to_id: user_id} = conversation,
%{id: user_id},
hidden
) do
conversation
|> Conversation.hidden_changeset(%{to_hidden: hidden})
|> Repo.update()
end
def mark_conversation_hidden(%Conversation{from_id: user_id} = conversation, %{id: user_id}, hidden) do
def mark_conversation_hidden(
%Conversation{from_id: user_id} = conversation,
%{id: user_id},
hidden
) do
conversation
|> Conversation.hidden_changeset(%{from_hidden: hidden})
|> Repo.update()
end
def mark_conversation_hidden(_conversation, _user, _read), do: {:ok, nil}
alias Philomena.Conversations.Message
@ -181,9 +193,11 @@ defmodule Philomena.Conversations do
now = DateTime.utc_now()
Multi.new
Multi.new()
|> Multi.insert(:message, message)
|> Multi.update_all(:conversation, conversation_query, set: [from_read: false, to_read: false, last_message_at: now])
|> Multi.update_all(:conversation, conversation_query,
set: [from_read: false, to_read: false, last_message_at: now]
)
|> Repo.isolated_transaction(:serializable)
end

View file

@ -50,7 +50,7 @@ defmodule Philomena.DnpEntries do
"""
def create_dnp_entry(user, tags, attrs \\ %{}) do
tag = Enum.find(tags, &to_string(&1.id) == attrs["tag_id"])
tag = Enum.find(tags, &(to_string(&1.id) == attrs["tag_id"]))
%DnpEntry{}
|> DnpEntry.creation_changeset(attrs, tag, user)
@ -70,7 +70,7 @@ defmodule Philomena.DnpEntries do
"""
def update_dnp_entry(%DnpEntry{} = dnp_entry, tags, attrs) do
tag = Enum.find(tags, &to_string(&1.id) == attrs["tag_id"])
tag = Enum.find(tags, &(to_string(&1.id) == attrs["tag_id"]))
dnp_entry
|> DnpEntry.update_changeset(attrs, tag)

View file

@ -53,6 +53,7 @@ defmodule Philomena.DnpEntries.DnpEntry do
defp validate_conditions(%Ecto.Changeset{changes: %{dnp_type: "Other"}} = changeset),
do: validate_required(changeset, [:conditions])
defp validate_conditions(changeset),
do: changeset
@ -70,12 +71,18 @@ defmodule Philomena.DnpEntries.DnpEntry do
def reasons do
[
{"No Edits", "I would like to prevent edited versions of my artwork from being uploaded in the future"},
{"Artist Tag Change", "I would like my artist tag to be changed to something that can not be connected to my current name"},
{"Uploader Credit Change", "I would like the uploader credit for already existing uploads of my art to be assigned to me"},
{"Certain Type/Location Only", "I only want to allow art of a certain type or from a certain location to be uploaded to Derpibooru"},
{"With Permission Only", "I only want people with my permission to be allowed to upload my art to Derpibooru"},
{"Artist Upload Only", "I want to be the only person allowed to upload my art to Derpibooru"},
{"No Edits",
"I would like to prevent edited versions of my artwork from being uploaded in the future"},
{"Artist Tag Change",
"I would like my artist tag to be changed to something that can not be connected to my current name"},
{"Uploader Credit Change",
"I would like the uploader credit for already existing uploads of my art to be assigned to me"},
{"Certain Type/Location Only",
"I only want to allow art of a certain type or from a certain location to be uploaded to Derpibooru"},
{"With Permission Only",
"I only want people with my permission to be allowed to upload my art to Derpibooru"},
{"Artist Upload Only",
"I want to be the only person allowed to upload my art to Derpibooru"},
{"Other", "I would like a DNP entry under other conditions"}
]
end

View file

@ -20,7 +20,9 @@ defmodule Philomena.DuplicateReports do
|> where([i, _it], i.duplication_checked != true)
|> Repo.all()
|> Enum.map(fn target ->
create_duplicate_report(source, target, %{}, %{"reason" => "Automated Perceptual dedupe match"})
create_duplicate_report(source, target, %{}, %{
"reason" => "Automated Perceptual dedupe match"
})
end)
end
@ -32,7 +34,9 @@ defmodule Philomena.DuplicateReports do
where: it.ne >= ^(intensities.ne - dist) and it.ne <= ^(intensities.ne + dist),
where: it.sw >= ^(intensities.sw - dist) and it.sw <= ^(intensities.sw + dist),
where: it.se >= ^(intensities.se - dist) and it.se <= ^(intensities.se + dist),
where: i.image_aspect_ratio >= ^(aspect_ratio - aspect_dist) and i.image_aspect_ratio <= ^(aspect_ratio + aspect_dist),
where:
i.image_aspect_ratio >= ^(aspect_ratio - aspect_dist) and
i.image_aspect_ratio <= ^(aspect_ratio + aspect_dist),
limit: 20
end
@ -83,8 +87,10 @@ defmodule Philomena.DuplicateReports do
DuplicateReport
|> where(
[dr],
(dr.image_id == ^duplicate_report.image_id and dr.duplicate_of_image_id == ^duplicate_report.duplicate_of_image_id) or
(dr.image_id == ^duplicate_report.duplicate_of_image_id and dr.duplicate_of_image_id == ^duplicate_report.image_id)
(dr.image_id == ^duplicate_report.image_id and
dr.duplicate_of_image_id == ^duplicate_report.duplicate_of_image_id) or
(dr.image_id == ^duplicate_report.duplicate_of_image_id and
dr.duplicate_of_image_id == ^duplicate_report.image_id)
)
|> where([dr], dr.id != ^duplicate_report.id)
|> repo.update_all(set: [state: "rejected"])

View file

@ -62,7 +62,7 @@ defmodule Philomena.DuplicateReports.DuplicateReport do
target_id = get_field(changeset, :duplicate_of_image_id)
case source_id == target_id do
true -> add_error(changeset, :image_id, "must be different from the target")
true -> add_error(changeset, :image_id, "must be different from the target")
false -> changeset
end
end

View file

@ -109,7 +109,13 @@ defmodule Philomena.Elasticsearch do
def search_results(module, elastic_query, pagination_params \\ %{}) do
page_number = pagination_params[:page_number] || 1
page_size = pagination_params[:page_size] || 25
elastic_query = Map.merge(elastic_query, %{from: (page_number - 1) * page_size, size: page_size, _source: false})
elastic_query =
Map.merge(elastic_query, %{
from: (page_number - 1) * page_size,
size: page_size,
_source: false
})
results = search(module, elastic_query)
time = results["took"]

View file

@ -32,4 +32,4 @@ defmodule Philomena.Filename do
|> to_string()
|> String.replace(~r/[^0-9]/, "")
end
end
end

View file

@ -120,7 +120,7 @@ defmodule Philomena.Filters do
end
def recent_and_user_filters(user) do
user_filters =
user_filters =
Filter
|> select([f], %{id: f.id, name: f.name, recent: ^"f"})
|> where(user_id: ^user.id)
@ -139,8 +139,8 @@ defmodule Philomena.Filters do
end)
|> Enum.group_by(
fn
%{recent: "t"} -> "Recent Filters"
_user -> "Your Filters"
%{recent: "t"} -> "Recent Filters"
_user -> "Your Filters"
end,
fn %{id: id, name: name} ->
[key: name, value: id]

View file

@ -34,7 +34,14 @@ defmodule Philomena.Filters.Filter do
|> Map.get(:user)
filter
|> cast(attrs, [:spoilered_tag_list, :hidden_tag_list, :description, :name, :spoilered_complex_str, :hidden_complex_str])
|> cast(attrs, [
:spoilered_tag_list,
:hidden_tag_list,
:description,
:name,
:spoilered_complex_str,
:hidden_complex_str
])
|> TagList.propagate_tag_list(:spoilered_tag_list, :spoilered_tag_ids)
|> TagList.propagate_tag_list(:hidden_tag_list, :hidden_tag_ids)
|> validate_required([:name, :description])

View file

@ -105,6 +105,7 @@ defmodule Philomena.Forums do
end
def subscribed?(_forum, nil), do: false
def subscribed?(forum, user) do
Subscription
|> where(forum_id: ^forum.id, user_id: ^user.id)
@ -112,6 +113,7 @@ defmodule Philomena.Forums do
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(%{})
@ -138,6 +140,7 @@ defmodule Philomena.Forums do
end
def clear_notification(_forum, nil), do: nil
def clear_notification(forum, user) do
Notifications.delete_unread_notification("Forum", forum.id, user)
end

View file

@ -28,7 +28,9 @@ defmodule Philomena.Forums.Forum do
|> cast(attrs, [:name, :short_name, :description, :access_level])
|> validate_required([:name, :short_name, :description, :access_level])
|> validate_inclusion(:access_level, ~W(normal assistant staff))
|> validate_format(:short_name, ~r/\A[a-z]+\z/, message: "must consist only of lowercase letters")
|> validate_format(:short_name, ~r/\A[a-z]+\z/,
message: "must consist only of lowercase letters"
)
|> unique_constraint(:short_name, name: :index_forums_on_short_name)
end
end

View file

@ -123,21 +123,21 @@ defmodule Philomena.Galleries do
end
def reindex_gallery(%Gallery{} = gallery) do
spawn fn ->
spawn(fn ->
Gallery
|> preload(^indexing_preloads())
|> where(id: ^gallery.id)
|> Repo.one()
|> Elasticsearch.index_document(Gallery)
end
end)
gallery
end
def unindex_gallery(%Gallery{} = gallery) do
spawn fn ->
spawn(fn ->
Elasticsearch.delete_document(gallery.id, Gallery)
end
end)
gallery
end
@ -198,7 +198,7 @@ defmodule Philomena.Galleries do
end
def notify_gallery(gallery) do
spawn fn ->
spawn(fn ->
subscriptions =
gallery
|> Repo.preload(:subscriptions)
@ -215,13 +215,13 @@ defmodule Philomena.Galleries do
action: "added images to"
}
)
end
end)
gallery
end
def reorder_gallery(gallery, image_ids) do
spawn fn ->
spawn(fn ->
interactions =
Interaction
|> where([gi], gi.image_id in ^image_ids)
@ -234,6 +234,7 @@ defmodule Philomena.Galleries do
|> Map.new(fn {interaction, index} -> {index, interaction.position} end)
images_present = Map.new(interactions, &{&1.image_id, true})
requested =
image_ids
|> Enum.filter(&images_present[&1])
@ -272,7 +273,7 @@ defmodule Philomena.Galleries do
# Now update all the associated images
Images.reindex_images(Map.keys(requested))
end
end)
gallery
end
@ -283,6 +284,7 @@ defmodule Philomena.Galleries do
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)
@ -325,6 +327,7 @@ defmodule Philomena.Galleries do
end
def clear_notification(_gallery, nil), do: nil
def clear_notification(gallery, user) do
Notifications.delete_unread_notification("Gallery", gallery.id, user)
end

View file

@ -25,15 +25,18 @@ defmodule Philomena.Galleries.ElasticsearchIndex do
_all: %{enabled: false},
dynamic: false,
properties: %{
id: %{type: "integer"}, # keyword
# keyword
id: %{type: "integer"},
image_count: %{type: "integer"},
watcher_count: %{type: "integer"},
updated_at: %{type: "date"},
created_at: %{type: "date"},
title: %{type: "keyword"},
creator: %{type: "keyword"}, # missing creator_id
# missing creator_id
creator: %{type: "keyword"},
image_ids: %{type: "keyword"},
watcher_ids: %{type: "keyword"}, # ???
# ???
watcher_ids: %{type: "keyword"},
description: %{type: "text", analyzer: "snowball"}
}
}

View file

@ -1,5 +1,8 @@
defmodule Philomena.Http do
@user_agent ["User-Agent": "Mozilla/5.0 (X11; Philomena; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0"]
@user_agent [
"User-Agent":
"Mozilla/5.0 (X11; Philomena; Linux x86_64; rv:70.0) Gecko/20100101 Firefox/70.0"
]
def get!(url, headers \\ [], options \\ []) do
headers = Keyword.merge(@user_agent, headers) |> add_host(url)
@ -19,7 +22,7 @@ defmodule Philomena.Http do
defp add_host(headers, url) do
%{host: host} = URI.parse(url)
Keyword.merge(["Host": host, "Connection": "close"], headers)
Keyword.merge([Host: host, Connection: "close"], headers)
end
defp proxy_host do

View file

@ -23,7 +23,7 @@ defmodule Philomena.ImageFaves do
Image
|> where(id: ^image.id)
Multi.new
Multi.new()
|> Multi.insert(:fave, fave)
|> Multi.update_all(:inc_faves_count, image_query, inc: [faves_count: 1])
|> Multi.run(:inc_fave_stat, fn _repo, _changes ->
@ -45,7 +45,7 @@ defmodule Philomena.ImageFaves do
Image
|> where(id: ^image.id)
Multi.new
Multi.new()
|> Multi.delete_all(:unfave, fave_query)
|> Multi.run(:dec_faves_count, fn repo, %{unfave: {faves, nil}} ->
{count, nil} =

View file

@ -22,7 +22,7 @@ defmodule Philomena.ImageHides do
Image
|> where(id: ^image.id)
Multi.new
Multi.new()
|> Multi.insert(:hide, hide)
|> Multi.update_all(:inc_hides_count, image_query, inc: [hides_count: 1])
end
@ -41,7 +41,7 @@ defmodule Philomena.ImageHides do
Image
|> where(id: ^image.id)
Multi.new
Multi.new()
|> Multi.delete_all(:unhide, hide_query)
|> Multi.run(:dec_hides_count, fn repo, %{unhide: {hides, nil}} ->
{count, nil} =

View file

@ -70,7 +70,7 @@ defmodule Philomena.ImageNavigator do
defp extract_filters(%{"galleries.position" => term} = sort, image, rel) do
# Extract gallery ID and current position
gid = term.nested.filter.term["galleries.id"]
pos = Enum.find(image[:galleries], & &1.id == gid).position
pos = Enum.find(image[:galleries], &(&1.id == gid)).position
# Sort in the other direction if we are going backwards
sd = term.order
@ -111,9 +111,11 @@ defmodule Philomena.ImageNavigator do
filters
|> Enum.with_index()
|> Enum.map(fn
{filter, 0} -> filter.this
{filter, 0} ->
filter.this
{filter, i} ->
filters_so_far =
filters_so_far =
filters
|> Enum.take(i)
|> Enum.map(& &1.for_next)
@ -144,13 +146,13 @@ defmodule Philomena.ImageNavigator do
%{
this: %{
nested: %{
path: :galleries,
path: :galleries,
query: %{range: %{"galleries.position" => %{dir => val}}}
}
},
next: %{
nested: %{
path: :galleries,
path: :galleries,
query: %{range: %{"galleries.position" => %{@range_map[dir] => val}}}
}
}

View file

@ -11,7 +11,7 @@ defmodule Philomena.ImageScope do
defp scope(list, conn, key, key_atom) do
case conn.params[key] do
nil -> list
"" -> list
"" -> list
val -> [{key_atom, val} | list]
end
end

View file

@ -52,17 +52,19 @@ defmodule Philomena.ImageSorter do
{gallery, _rest} ->
%{
queries: [],
sorts: [%{
"galleries.position" => %{
order: sd,
nested: %{
path: :galleries,
filter: %{
term: %{"galleries.id" => gallery}
sorts: [
%{
"galleries.position" => %{
order: sd,
nested: %{
path: :galleries,
filter: %{
term: %{"galleries.id" => gallery}
}
}
}
}
}],
],
constant_score: true
}
@ -77,13 +79,15 @@ defmodule Philomena.ImageSorter do
defp random_query(seed, sd) do
%{
queries: [%{
function_score: %{
query: %{match_all: %{}},
random_score: %{seed: seed, field: :id},
boost_mode: :replace
queries: [
%{
function_score: %{
query: %{match_all: %{}},
random_score: %{seed: seed, field: :id},
boost_mode: :replace
}
}
}],
],
sorts: [%{"_score" => sd}],
constant_score: true
}

View file

@ -23,12 +23,14 @@ defmodule Philomena.ImageVotes do
Image
|> where(id: ^image.id)
upvotes = if up, do: 1, else: 0
upvotes = if up, do: 1, else: 0
downvotes = if up, do: 0, else: 1
Multi.new
Multi.new()
|> Multi.insert(:vote, vote)
|> Multi.update_all(:inc_vote_count, image_query, inc: [upvotes_count: upvotes, downvotes_count: downvotes, score: upvotes - downvotes])
|> Multi.update_all(:inc_vote_count, image_query,
inc: [upvotes_count: upvotes, downvotes_count: downvotes, score: upvotes - downvotes]
)
|> Multi.run(:inc_vote_stat, fn _repo, _changes ->
UserStatistics.inc_stat(user, :votes_cast, 1)
end)
@ -55,13 +57,16 @@ defmodule Philomena.ImageVotes do
Image
|> where(id: ^image.id)
Multi.new
Multi.new()
|> Multi.delete_all(:unupvote, upvote_query)
|> Multi.delete_all(:undownvote, downvote_query)
|> Multi.run(:dec_votes_count, fn repo, %{unupvote: {upvotes, nil}, undownvote: {downvotes, nil}} ->
|> Multi.run(:dec_votes_count, fn repo,
%{unupvote: {upvotes, nil}, undownvote: {downvotes, nil}} ->
{count, nil} =
image_query
|> repo.update_all(inc: [upvotes_count: -upvotes, downvotes_count: -downvotes, score: downvotes - upvotes])
|> repo.update_all(
inc: [upvotes_count: -upvotes, downvotes_count: -downvotes, score: downvotes - upvotes]
)
UserStatistics.inc_stat(user, :votes_cast, -(upvotes + downvotes))

View file

@ -63,7 +63,7 @@ defmodule Philomena.Images do
|> Image.tag_changeset(attrs, [], tags)
|> Uploader.analyze_upload(attrs)
Multi.new
Multi.new()
|> Multi.insert(:image, image)
|> Multi.run(:name_caches, fn repo, %{image: image} ->
image
@ -217,7 +217,7 @@ defmodule Philomena.Images do
Ecto.build_assoc(image, :source_changes)
|> SourceChange.creation_changeset(attrs, attribution)
Multi.new
Multi.new()
|> Multi.update(:image, image_changes)
|> Multi.run(:source_change, fn repo, _changes ->
case image_changes.changes do
@ -235,7 +235,7 @@ defmodule Philomena.Images do
old_tags = Tags.get_or_create_tags(attrs["old_tag_input"])
new_tags = Tags.get_or_create_tags(attrs["tag_input"])
Multi.new
Multi.new()
|> Multi.run(:image, fn repo, _chg ->
image
|> repo.preload(:tags)
@ -253,7 +253,7 @@ defmodule Philomena.Images do
tag_changes =
added_tags
|> Enum.map(&tag_change_attributes(attribution, image, &1, true, attribution[:user]))
{count, nil} = repo.insert_all(TagChange, tag_changes)
{:ok, count}
@ -296,9 +296,10 @@ defmodule Philomena.Images do
defp tag_change_attributes(attribution, image, tag, added, user) do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
user_id =
case user do
nil -> nil
nil -> nil
user -> user.id
end
@ -340,7 +341,12 @@ defmodule Philomena.Images do
case result do
{:ok, changes} ->
update_first_seen_at(duplicate_of_image, image.first_seen_at, duplicate_of_image.first_seen_at)
update_first_seen_at(
duplicate_of_image,
image.first_seen_at,
duplicate_of_image.first_seen_at
)
tags = Tags.copy_tags(image, duplicate_of_image)
Comments.migrate_comments(image, duplicate_of_image)
Interactions.migrate_interactions(image, duplicate_of_image)
@ -356,7 +362,7 @@ defmodule Philomena.Images do
min_time =
case NaiveDateTime.compare(time_1, time_2) do
:gt -> time_2
_ -> time_1
_ -> time_1
end
Image
@ -398,7 +404,7 @@ defmodule Philomena.Images do
def unhide_image(%Image{hidden_from_users: true} = image) do
key = image.hidden_image_key
Multi.new
Multi.new()
|> Multi.update(:image, Image.unhide_changeset(image))
|> Multi.run(:tags, fn repo, %{image: image} ->
image = Repo.preload(image, :tags, force: true)
@ -417,6 +423,7 @@ defmodule Philomena.Images do
end)
|> Repo.isolated_transaction(:serializable)
end
def unhide_image(image), do: {:ok, image}
def batch_update(image_ids, added_tags, removed_tags, tag_change_attributes) do
@ -445,26 +452,33 @@ defmodule Philomena.Images do
now = NaiveDateTime.utc_now() |> NaiveDateTime.truncate(:second)
tag_change_attributes = Map.merge(tag_change_attributes, %{created_at: now, updated_at: now})
Repo.transaction fn ->
{added_count, inserted} = Repo.insert_all(Tagging, insertions, on_conflict: :nothing, returning: [:image_id, :tag_id])
Repo.transaction(fn ->
{added_count, inserted} =
Repo.insert_all(Tagging, insertions,
on_conflict: :nothing,
returning: [:image_id, :tag_id]
)
{removed_count, deleted} = Repo.delete_all(deletions)
inserted = Enum.map(inserted, &[&1.image_id, &1.tag_id])
added_changes = Enum.map(inserted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: true})
end)
added_changes =
Enum.map(inserted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: true})
end)
removed_changes = Enum.map(deleted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: false})
end)
removed_changes =
Enum.map(deleted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: false})
end)
changes = added_changes ++ removed_changes
Repo.insert_all(TagChange, changes)
Repo.update_all(where(Tag, [t], t.id in ^added_tags), inc: [images_count: added_count])
Repo.update_all(where(Tag, [t], t.id in ^removed_tags), inc: [images_count: -removed_count])
end
end)
end
@doc """
@ -503,23 +517,33 @@ defmodule Philomena.Images do
end
def reindex_images(image_ids) do
spawn fn ->
spawn(fn ->
Image
|> preload(^indexing_preloads())
|> where([i], i.id in ^image_ids)
|> Elasticsearch.reindex(Image)
end
end)
image_ids
end
def indexing_preloads do
[:user, :favers, :downvoters, :upvoters, :hiders, :deleter, :gallery_interactions, tags: [:aliases, :aliased_tag]]
[
:user,
:favers,
:downvoters,
:upvoters,
:hiders,
:deleter,
:gallery_interactions,
tags: [:aliases, :aliased_tag]
]
end
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)
@ -539,6 +563,7 @@ defmodule Philomena.Images do
"""
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(%{})
@ -565,6 +590,7 @@ 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

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.Images.Image do
def anonymous?(image) do
!!image.anonymous
end
end
end

View file

@ -24,11 +24,12 @@ defmodule Philomena.Images.Hider do
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id)])
defp image_thumb_dir(%Image{created_at: created_at, id: id}, key),
do: Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id) <> "-" <> key])
do:
Path.join([image_thumbnail_root(), time_identifier(created_at), to_string(id) <> "-" <> key])
defp time_identifier(time),
do: Enum.join([time.year, time.month, time.day], "/")
defp image_thumbnail_root,
do: Application.get_env(:philomena, :image_file_root) <> "/thumbs"
end
end

View file

@ -122,22 +122,41 @@ defmodule Philomena.Images.Image do
def image_changeset(image, attrs) do
image
|> cast(attrs, [
:image, :image_name, :image_width, :image_height, :image_size,
:image_format, :image_mime_type, :image_aspect_ratio,
:image_orig_sha512_hash, :image_sha512_hash, :uploaded_image,
:removed_image, :image_is_animated
])
|> validate_required([
:image, :image_width, :image_height, :image_size,
:image_format, :image_mime_type, :image_aspect_ratio,
:image_orig_sha512_hash, :image_sha512_hash, :uploaded_image,
:image,
:image_name,
:image_width,
:image_height,
:image_size,
:image_format,
:image_mime_type,
:image_aspect_ratio,
:image_orig_sha512_hash,
:image_sha512_hash,
:uploaded_image,
:removed_image,
:image_is_animated
])
|> validate_number(:image_size, greater_than: 0, less_than_or_equal_to: 26214400)
|> validate_required([
:image,
:image_width,
:image_height,
:image_size,
:image_format,
:image_mime_type,
:image_aspect_ratio,
:image_orig_sha512_hash,
:image_sha512_hash,
:uploaded_image,
:image_is_animated
])
|> validate_number(:image_size, greater_than: 0, less_than_or_equal_to: 26_214_400)
|> validate_number(:image_width, greater_than: 0, less_than_or_equal_to: 32767)
|> validate_number(:image_height, greater_than: 0, less_than_or_equal_to: 32767)
|> validate_length(:image_name, max: 255, count: :bytes)
|> validate_inclusion(:image_mime_type, ~W(image/gif image/jpeg image/png image/svg+xml video/webm))
|> validate_inclusion(
:image_mime_type,
~W(image/gif image/jpeg image/png image/svg+xml video/webm)
)
|> unsafe_validate_unique([:image_orig_sha512_hash], Repo)
end
@ -260,8 +279,7 @@ defmodule Philomena.Images.Image do
tags
|> Enum.map_join(", ", & &1.name)
tag_ids =
tags |> Enum.map(& &1.id)
tag_ids = tags |> Enum.map(& &1.id)
aliases =
Tag

View file

@ -61,10 +61,12 @@ defmodule Philomena.Images.Query do
defp anonymous_fields do
[
int_fields: ~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count),
int_fields:
~W(id width height comment_count score upvotes downvotes faves uploader_id faved_by_id tag_count),
float_fields: ~W(aspect_ratio wilson_score),
date_fields: ~W(created_at updated_at first_seen_at),
literal_fields: ~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format),
literal_fields:
~W(faved_by orig_sha512_hash sha512_hash uploader source_url original_format),
ngram_fields: ~W(description),
custom_fields: ~W(gallery_id),
default_field: {"namespaced_tags.name", :term},
@ -79,30 +81,35 @@ defmodule Philomena.Images.Query do
defp user_fields do
fields = anonymous_fields()
Keyword.merge(fields, [
Keyword.merge(fields,
custom_fields: fields[:custom_fields] ++ ~W(my),
transforms: Map.merge(fields[:transforms], %{"my" => &user_my_transform/2})
])
)
end
defp moderator_fields do
fields = user_fields()
Keyword.merge(fields, [
int_fields: fields[:int_fields] ++ ~W(upvoted_by_id downvoted_by_id true_uploader_id hidden_by_id deleted_by_user_id),
literal_fields: fields[:literal_fields] ++ ~W(fingerprint upvoted_by downvoted_by true_uploader hidden_by deleted_by_user),
Keyword.merge(fields,
int_fields:
fields[:int_fields] ++
~W(upvoted_by_id downvoted_by_id true_uploader_id hidden_by_id deleted_by_user_id),
literal_fields:
fields[:literal_fields] ++
~W(fingerprint upvoted_by downvoted_by true_uploader hidden_by deleted_by_user),
ip_fields: ~W(ip),
bool_fields: ~W(deleted),
aliases: Map.merge(fields[:aliases], %{
"upvoted_by" => "upvoters",
"downvoted_by" => "downvoters",
"upvoted_by_id" => "upvoter_ids",
"downvoted_by_id" => "downvoter_ids",
"hidden_by" => "hidden_by_users",
"hidden_by_id" => "hidden_by_user_ids",
"deleted" => "hidden_from_users"
})
])
aliases:
Map.merge(fields[:aliases], %{
"upvoted_by" => "upvoters",
"downvoted_by" => "downvoters",
"upvoted_by_id" => "upvoter_ids",
"downvoted_by_id" => "downvoter_ids",
"hidden_by" => "hidden_by_users",
"hidden_by_id" => "hidden_by_user_ids",
"deleted" => "hidden_from_users"
})
)
end
defp parse(fields, context, query_string) do

View file

@ -9,12 +9,11 @@ defmodule Philomena.Images.TagDiffer do
old_set = to_set(old_tags)
new_set = to_set(new_tags)
tags = changeset |> get_field(:tags)
added_tags = added_set(old_set, new_set)
tags = changeset |> get_field(:tags)
added_tags = added_set(old_set, new_set)
removed_tags = removed_set(old_set, new_set)
{tags, actually_added, actually_removed} =
apply_changes(tags, added_tags, removed_tags)
{tags, actually_added, actually_removed} = apply_changes(tags, added_tags, removed_tags)
changeset
|> put_change(:added_tags, actually_added)
@ -34,8 +33,7 @@ defmodule Philomena.Images.TagDiffer do
|> List.flatten()
|> to_set()
added_and_implied_set =
Map.merge(added_set, implied_set)
added_and_implied_set = Map.merge(added_set, implied_set)
oc_set =
added_and_implied_set
@ -51,6 +49,7 @@ defmodule Philomena.Images.TagDiffer do
end
defp get_oc_tag([]), do: Map.new()
defp get_oc_tag(_any_oc_tag) do
Tag
|> where(name: "oc")
@ -82,10 +81,10 @@ defmodule Philomena.Images.TagDiffer do
tag_set
|> Map.drop(Map.keys(desired_tags))
tags = desired_tags |> to_tag_list()
actually_added = actually_added |> to_tag_list()
tags = desired_tags |> to_tag_list()
actually_added = actually_added |> to_tag_list()
actually_removed = actually_removed |> to_tag_list()
{tags, actually_added, actually_removed}
end
end
end

View file

@ -1,11 +1,11 @@
defmodule Philomena.Images.TagValidator do
import Ecto.Changeset
@safe_rating MapSet.new(["safe"])
@safe_rating MapSet.new(["safe"])
@sexual_ratings MapSet.new(["suggestive", "questionable", "explicit"])
@horror_ratings MapSet.new(["semi-grimdark", "grimdark"])
@gross_rating MapSet.new(["grotesque"])
@empty MapSet.new()
@gross_rating MapSet.new(["grotesque"])
@empty MapSet.new()
def validate_tags(changeset) do
tags = changeset |> get_field(:tags)
@ -14,7 +14,7 @@ defmodule Philomena.Images.TagValidator do
end
defp validate_tag_input(changeset, tags) do
tag_set = extract_names(tags)
tag_set = extract_names(tags)
rating_set = ratings(tag_set)
changeset
@ -26,10 +26,10 @@ defmodule Philomena.Images.TagValidator do
end
defp ratings(%MapSet{} = tag_set) do
safe = MapSet.intersection(tag_set, @safe_rating)
safe = MapSet.intersection(tag_set, @safe_rating)
sexual = MapSet.intersection(tag_set, @sexual_ratings)
horror = MapSet.intersection(tag_set, @horror_ratings)
gross = MapSet.intersection(tag_set, @gross_rating)
gross = MapSet.intersection(tag_set, @gross_rating)
%{
safe: safe,
@ -50,16 +50,20 @@ defmodule Philomena.Images.TagValidator do
end
end
defp validate_has_rating(changeset, %{safe: s, sexual: x, horror: h, gross: g}) when s == @empty and x == @empty and h == @empty and g == @empty do
defp validate_has_rating(changeset, %{safe: s, sexual: x, horror: h, gross: g})
when s == @empty and x == @empty and h == @empty and g == @empty do
changeset
|> add_error(:tag_input, "must contain at least one rating tag")
end
defp validate_has_rating(changeset, _ratings), do: changeset
defp validate_safe(changeset, %{safe: s, sexual: x, horror: h, gross: g}) when s != @empty and (x != @empty or h != @empty or g != @empty) do
defp validate_safe(changeset, %{safe: s, sexual: x, horror: h, gross: g})
when s != @empty and (x != @empty or h != @empty or g != @empty) do
changeset
|> add_error(:tag_input, "may not contain any other rating if safe")
end
defp validate_safe(changeset, _ratings), do: changeset
defp validate_sexual_exclusion(changeset, %{sexual: x}) do
@ -89,4 +93,4 @@ defmodule Philomena.Images.TagValidator do
|> Enum.map(& &1.name)
|> MapSet.new()
end
end
end

View file

@ -22,7 +22,6 @@ defmodule Philomena.Images.Thumbnailer do
full: nil
]
def generate_thumbnails(image_id) do
image = Repo.get!(Image, image_id)
file = image_file(image)
@ -36,11 +35,9 @@ defmodule Philomena.Images.Thumbnailer do
recompute_meta(image, file, &Image.process_changeset/2)
end
defp apply_edit_script(image, changes),
do: Enum.map(changes, &apply_change(image, &1))
defp apply_change(image, {:intensities, intensities}),
do: ImageIntensities.create_image_intensity(image, intensities)
@ -50,14 +47,12 @@ defmodule Philomena.Images.Thumbnailer do
defp apply_change(image, {:thumbnails, thumbnails}),
do: Enum.map(thumbnails, &apply_thumbnail(image, image_thumb_dir(image), &1))
defp apply_thumbnail(_image, thumb_dir, {:copy, new_file, destination}),
do: copy(new_file, Path.join(thumb_dir, destination))
defp apply_thumbnail(image, thumb_dir, {:symlink_original, destination}),
do: symlink(image_file(image), Path.join(thumb_dir, destination))
defp generate_dupe_reports(image),
do: DuplicateReports.generate_reports(image)
@ -70,7 +65,6 @@ defmodule Philomena.Images.Thumbnailer do
|> Repo.update!()
end
# Copy from source to destination, creating parent directories along
# the way and setting the appropriate permission bits when necessary.
defp copy(source, destination) do
@ -109,7 +103,6 @@ defmodule Philomena.Images.Thumbnailer do
|> File.mkdir_p!()
end
defp image_file(%Image{image: image}),
do: Path.join(image_file_root(), image)
@ -119,7 +112,6 @@ defmodule Philomena.Images.Thumbnailer do
defp time_identifier(time),
do: Enum.join([time.year, time.month, time.day], "/")
defp image_file_root,
do: Application.get_env(:philomena, :image_file_root)

View file

@ -21,4 +21,4 @@ defmodule Philomena.Images.Uploader do
defp image_file_root do
Application.get_env(:philomena, :image_file_root)
end
end
end

View file

@ -20,4 +20,4 @@ defmodule Philomena.Intensities do
:error
end
end
end
end

View file

@ -13,7 +13,7 @@ defmodule Philomena.Interactions do
def user_interactions(images, user) do
ids =
images
images
|> Enum.flat_map(fn
nil -> []
%{id: id} -> [id]
@ -23,25 +23,45 @@ defmodule Philomena.Interactions do
hide_interactions =
ImageHide
|> select([h], %{image_id: h.image_id, user_id: h.user_id, interaction_type: ^"hidden", value: ^""})
|> select([h], %{
image_id: h.image_id,
user_id: h.user_id,
interaction_type: ^"hidden",
value: ^""
})
|> where([h], h.image_id in ^ids)
|> where(user_id: ^user.id)
fave_interactions =
ImageFave
|> select([f], %{image_id: f.image_id, user_id: f.user_id, interaction_type: ^"faved", value: ^""})
|> select([f], %{
image_id: f.image_id,
user_id: f.user_id,
interaction_type: ^"faved",
value: ^""
})
|> where([f], f.image_id in ^ids)
|> where(user_id: ^user.id)
upvote_interactions =
ImageVote
|> select([v], %{image_id: v.image_id, user_id: v.user_id, interaction_type: ^"voted", value: ^"up"})
|> select([v], %{
image_id: v.image_id,
user_id: v.user_id,
interaction_type: ^"voted",
value: ^"up"
})
|> where([v], v.image_id in ^ids)
|> where(user_id: ^user.id, up: true)
downvote_interactions =
ImageVote
|> select([v], %{image_id: v.image_id, user_id: v.user_id, interaction_type: ^"voted", value: ^"down"})
|> select([v], %{
image_id: v.image_id,
user_id: v.user_id,
interaction_type: ^"voted",
value: ^"down"
})
|> where([v], v.image_id in ^ids)
|> where(user_id: ^user.id, up: false)
@ -61,10 +81,20 @@ defmodule Philomena.Interactions do
new_hides = Enum.map(source.hiders, &%{image_id: target.id, user_id: &1.id, created_at: now})
new_faves = Enum.map(source.favers, &%{image_id: target.id, user_id: &1.id, created_at: now})
new_upvotes = Enum.map(source.upvoters, &%{image_id: target.id, user_id: &1.id, created_at: now, up: true})
new_downvotes = Enum.map(source.downvoters, &%{image_id: target.id, user_id: &1.id, created_at: now, up: false})
Multi.new
new_upvotes =
Enum.map(
source.upvoters,
&%{image_id: target.id, user_id: &1.id, created_at: now, up: true}
)
new_downvotes =
Enum.map(
source.downvoters,
&%{image_id: target.id, user_id: &1.id, created_at: now, up: false}
)
Multi.new()
|> Multi.run(:hides, fn repo, %{} ->
{count, nil} = repo.insert_all(ImageHide, new_hides, on_conflict: :nothing)
@ -85,7 +115,8 @@ defmodule Philomena.Interactions do
{:ok, count}
end)
|> Multi.run(:image, fn repo, %{hides: hides, faves: faves, upvotes: upvotes, downvotes: downvotes} ->
|> Multi.run(:image, fn repo,
%{hides: hides, faves: faves, upvotes: upvotes, downvotes: downvotes} ->
image_query = where(Image, id: ^target.id)
repo.update_all(
@ -106,6 +137,7 @@ defmodule Philomena.Interactions do
defp union_all_queries([query]),
do: query
defp union_all_queries([query | rest]),
do: query |> union_all(^union_all_queries(rest))
end
end

View file

@ -30,8 +30,8 @@ defmodule Philomena.Mime do
def true_mime("audio/webm"), do: {:ok, "video/webm"}
def true_mime(mime)
when mime in ~W(image/gif image/jpeg image/png image/svg+xml video/webm),
do: {:ok, mime}
when mime in ~W(image/gif image/jpeg image/png image/svg+xml video/webm),
do: {:ok, mime}
def true_mime(_mime), do: :error
end
end

View file

@ -185,13 +185,14 @@ defmodule Philomena.Notifications do
end
def notify(_actor_child, [], _params), do: nil
def notify(actor_child, subscriptions, params) do
# Don't push to the user that created the notification
subscriptions =
case actor_child do
%{user_id: id} ->
subscriptions
|> Enum.reject(& &1.user_id == id)
|> Enum.reject(&(&1.user_id == id))
_ ->
subscriptions

View file

@ -32,6 +32,7 @@ defmodule Philomena.PollOptions.PollOption do
defp ignore_if_blank(%{valid?: false, changes: changes} = changeset) when changes == %{},
do: %{changeset | action: :ignore}
defp ignore_if_blank(changeset),
do: changeset
end

View file

@ -47,13 +47,12 @@ defmodule Philomena.PollVotes do
|> Multi.run(:existing_votes, fn _repo, _changes ->
# Don't proceed if any votes exist
case voted?(poll, user) do
true -> {:error, []}
true -> {:error, []}
_false -> {:ok, []}
end
end)
|> Multi.run(:new_votes, fn repo, _changes ->
{_count, votes} =
repo.insert_all(PollVote, poll_votes, returning: true)
{_count, votes} = repo.insert_all(PollVote, poll_votes, returning: true)
{:ok, votes}
end)
@ -91,13 +90,15 @@ defmodule Philomena.PollVotes do
case poll.vote_method do
"single" -> Enum.take(votes, 1)
_other -> votes
_other -> votes
end
end
defp filter_options(_user, _poll, _now, _attrs), do: []
def voted?(nil, _user), do: false
def voted?(_poll, nil), do: false
def voted?(%{id: poll_id}, %{id: user_id}) do
PollVote
|> join(:inner, [pv], _ in assoc(pv, :poll_option))

View file

@ -44,6 +44,7 @@ defmodule Philomena.Polls.Poll do
defp ignore_if_blank(%{valid?: false, changes: changes} = changeset) when changes == %{},
do: %{changeset | action: :ignore}
defp ignore_if_blank(changeset),
do: changeset

View file

@ -49,6 +49,7 @@ defmodule Philomena.Polymorphic do
{type, ids} ->
pre = Map.get(@preloads, type, [])
rows =
@classes[type]
|> where([m], m.id in ^ids)

View file

@ -55,7 +55,7 @@ defmodule Philomena.Posts do
Forum
|> where(id: ^topic.forum_id)
Multi.new
Multi.new()
|> Multi.run(:post, fn repo, _ ->
last_position =
Post
@ -71,7 +71,10 @@ defmodule Philomena.Posts do
end)
|> Multi.run(:update_topic, fn repo, %{post: %{id: post_id}} ->
{count, nil} =
repo.update_all(topic_query, inc: [post_count: 1], set: [last_post_id: post_id, last_replied_to_at: now])
repo.update_all(topic_query,
inc: [post_count: 1],
set: [last_post_id: post_id, last_replied_to_at: now]
)
{:ok, count}
end)
@ -88,7 +91,7 @@ defmodule Philomena.Posts do
end
def notify_post(post) do
spawn fn ->
spawn(fn ->
topic =
post
|> Repo.preload(:topic)
@ -110,7 +113,7 @@ defmodule Philomena.Posts do
action: "posted a new reply in"
}
)
end
end)
post
end
@ -132,10 +135,9 @@ defmodule Philomena.Posts do
current_body = post.body
current_reason = post.edit_reason
post_changes =
Post.changeset(post, attrs, now)
post_changes = Post.changeset(post, attrs, now)
Multi.new
Multi.new()
|> Multi.update(:post, post_changes)
|> Multi.run(:version, fn _repo, _changes ->
Versions.create_version("Post", post.id, editor.id, %{
@ -203,13 +205,13 @@ defmodule Philomena.Posts do
end
def reindex_post(%Post{} = post) do
spawn fn ->
spawn(fn ->
Post
|> preload(^indexing_preloads())
|> where(id: ^post.id)
|> Repo.one()
|> Elasticsearch.index_document(Post)
end
end)
post
end

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.Posts.Post do
def anonymous?(post) do
!!post.anonymous
end
end
end

View file

@ -59,22 +59,22 @@ defmodule Philomena.Posts.Query do
defp user_fields do
fields = anonymous_fields()
Keyword.merge(fields, [
Keyword.merge(fields,
custom_fields: fields[:custom_fields] ++ ~W(my),
transforms: Map.merge(fields[:transforms], %{"my" => &user_my_transform/2})
])
)
end
defp moderator_fields do
fields = user_fields()
Keyword.merge(fields, [
Keyword.merge(fields,
literal_fields: ~W(forum_id topic_id user_id author fingerprint),
ip_fields: ~W(ip),
bool_fields: ~W(anonymous deleted),
custom_fields: fields[:custom_fields] -- ~W(author user_id),
transforms: Map.drop(fields[:transforms], ["user_id", "author"])
])
)
end
defp parse(fields, context, query_string) do

View file

@ -66,4 +66,4 @@ defmodule Philomena.Processors do
def intensities(analysis, file) do
processor(analysis.mime_type).intensities(analysis, file)
end
end
end

View file

@ -26,12 +26,10 @@ defmodule Philomena.Processors.Gif do
intensities
end
defp optimize(file) do
optimized = Briefly.create!(extname: ".gif")
{_output, 0} =
System.cmd("gifsicle", ["--careful", "-O2", file, "-o", optimized])
{_output, 0} = System.cmd("gifsicle", ["--careful", "-O2", file, "-o", optimized])
optimized
end
@ -40,7 +38,18 @@ defmodule Philomena.Processors.Gif do
preview = Briefly.create!(extname: ".png")
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-ss", to_string(duration / 2), "-frames:v", "1", preview])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-ss",
to_string(duration / 2),
"-frames:v",
"1",
preview
])
preview
end
@ -49,7 +58,16 @@ defmodule Philomena.Processors.Gif do
palette = Briefly.create!(extname: ".png")
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", "palettegen=stats_mode=diff", palette])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-vf",
"palettegen=stats_mode=diff",
palette
])
palette
end
@ -59,7 +77,12 @@ defmodule Philomena.Processors.Gif do
[{:symlink_original, "full.gif"}] ++ generate_videos(file)
end
defp scale_if_smaller(palette, file, {width, height}, {thumb_name, {target_width, target_height}}) do
defp scale_if_smaller(
palette,
file,
{width, height},
{thumb_name, {target_width, target_height}}
) do
if width > target_width or height > target_height do
scaled = scale(palette, file, {target_width, target_height})
@ -72,25 +95,73 @@ defmodule Philomena.Processors.Gif do
defp scale(palette, file, {width, height}) do
scaled = Briefly.create!(extname: ".gif")
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
palette_filter = "paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle"
filter_graph = "[0:v] #{scale_filter} [x]; [x][1:v] #{palette_filter}"
filter_graph = "[0:v] #{scale_filter} [x]; [x][1:v] #{palette_filter}"
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-i", palette, "-lavfi", filter_graph, scaled])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-i",
palette,
"-lavfi",
filter_graph,
scaled
])
scaled
end
defp generate_videos(file) do
webm = Briefly.create!(extname: ".webm")
mp4 = Briefly.create!(extname: ".mp4")
mp4 = Briefly.create!(extname: ".mp4")
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-pix_fmt", "yuv420p", "-c:v", "libvpx", "-quality", "good", "-b:v", "5M", webm])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-pix_fmt",
"yuv420p",
"-c:v",
"libvpx",
"-quality",
"good",
"-b:v",
"5M",
webm
])
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", "scale=ceil(iw/2)*2:ceil(ih/2)*2", "-c:v", "libx264", "-preset", "medium", "-pix_fmt", "yuv420p", "-profile:v", "main", "-crf", "18", "-b:v", "5M", mp4])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-vf",
"scale=ceil(iw/2)*2:ceil(ih/2)*2",
"-c:v",
"libx264",
"-preset",
"medium",
"-pix_fmt",
"yuv420p",
"-profile:v",
"main",
"-crf",
"18",
"-b:v",
"5M",
mp4
])
[
{:copy, webm, "full.webm"},
{:copy, mp4, "full.mp4"}

View file

@ -23,12 +23,10 @@ defmodule Philomena.Processors.Jpeg do
intensities
end
defp strip(file) do
stripped = Briefly.create!(extname: ".jpg")
{_output, 0} =
System.cmd("convert", [file, "-auto-orient", "-strip", stripped])
{_output, 0} = System.cmd("convert", [file, "-auto-orient", "-strip", stripped])
stripped
end
@ -36,8 +34,7 @@ defmodule Philomena.Processors.Jpeg do
defp optimize(file) do
optimized = Briefly.create!(extname: ".jpg")
{_output, 0} =
System.cmd("jpegtran", ["-optimize", "-outfile", optimized, file])
{_output, 0} = System.cmd("jpegtran", ["-optimize", "-outfile", optimized, file])
optimized
end
@ -62,9 +59,9 @@ defmodule Philomena.Processors.Jpeg do
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", scale_filter, scaled])
{_output, 0} =
System.cmd("jpegtran", ["-optimize", "-outfile", scaled, scaled])
{_output, 0} = System.cmd("jpegtran", ["-optimize", "-outfile", scaled, scaled])
scaled
end
end
end

View file

@ -23,7 +23,6 @@ defmodule Philomena.Processors.Png do
intensities
end
defp optimize(file) do
optimized = Briefly.create!(extname: ".png")
@ -52,12 +51,14 @@ defmodule Philomena.Processors.Png do
defp scale(file, {width, height}) do
scaled = Briefly.create!(extname: ".png")
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease,format=rgb32"
scale_filter =
"scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease,format=rgb32"
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", scale_filter, scaled])
{_output, 0} =
System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled])
{_output, 0} = System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled])
scaled
end

View file

@ -21,12 +21,10 @@ defmodule Philomena.Processors.Svg do
intensities
end
defp preview(file) do
preview = Briefly.create!(extname: ".png")
{_output, 0} =
System.cmd("safe-rsvg-convert", [file, preview])
{_output, 0} = System.cmd("safe-rsvg-convert", [file, preview])
preview
end
@ -47,9 +45,9 @@ defmodule Philomena.Processors.Svg do
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", preview, "-vf", scale_filter, scaled])
{_output, 0} =
System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled])
{_output, 0} = System.cmd("optipng", ["-i0", "-o1", "-quiet", "-clobber", scaled])
scaled
end
end
end

View file

@ -25,12 +25,22 @@ defmodule Philomena.Processors.Webm do
intensities
end
defp preview(duration, file) do
preview = Briefly.create!(extname: ".png")
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-ss", to_string(duration / 2), "-frames:v", "1", preview])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-ss",
to_string(duration / 2),
"-frames:v",
"1",
preview
])
preview
end
@ -44,7 +54,13 @@ defmodule Philomena.Processors.Webm do
]
end
defp scale_if_smaller(file, palette, duration, dimensions, {thumb_name, {target_width, target_height}}) do
defp scale_if_smaller(
file,
palette,
duration,
dimensions,
{thumb_name, {target_width, target_height}}
) do
{webm, mp4} = scale_videos(file, palette, dimensions, {target_width, target_height})
cond do
@ -68,27 +84,83 @@ defmodule Philomena.Processors.Webm do
defp scale_videos(file, _palette, dimensions, target_dimensions) do
{width, height} = box_dimensions(dimensions, target_dimensions)
webm = Briefly.create!(extname: ".webm")
mp4 = Briefly.create!(extname: ".mp4")
mp4 = Briefly.create!(extname: ".mp4")
scale_filter = "scale=w=#{width}:h=#{height}"
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-c:v", "libvpx", "-quality", "good", "-cpu-used", "3", "-auto-alt-ref", "0", "-crf", "10", "-b:v", "5M", "-vf", scale_filter, webm])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-c:v",
"libvpx",
"-quality",
"good",
"-cpu-used",
"3",
"-auto-alt-ref",
"0",
"-crf",
"10",
"-b:v",
"5M",
"-vf",
scale_filter,
webm
])
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-c:v", "libx264", "-pix_fmt", "yuv420p", "-profile:v", "main", "-preset", "medium", "-crf", "18", "-b:v", "5M", "-vf", scale_filter, mp4])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-c:v",
"libx264",
"-pix_fmt",
"yuv420p",
"-profile:v",
"main",
"-preset",
"medium",
"-crf",
"18",
"-b:v",
"5M",
"-vf",
scale_filter,
mp4
])
{webm, mp4}
end
defp scale_gif(file, palette, duration, {width, height}) do
gif = Briefly.create!(extname: ".gif")
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
scale_filter = "scale=w=#{width}:h=#{height}:force_original_aspect_ratio=decrease"
palette_filter = "paletteuse=dither=bayer:bayer_scale=5:diff_mode=rectangle"
rate_filter = "fps=1/#{duration / 10},settb=1/2,setpts=N"
filter_graph = "[0:v] #{scale_filter},#{rate_filter} [x]; [x][1:v] #{palette_filter}"
rate_filter = "fps=1/#{duration / 10},settb=1/2,setpts=N"
filter_graph = "[0:v] #{scale_filter},#{rate_filter} [x]; [x][1:v] #{palette_filter}"
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-i", palette, "-lavfi", filter_graph, "-r", "2", gif])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-i",
palette,
"-lavfi",
filter_graph,
"-r",
"2",
gif
])
gif
end
@ -96,7 +168,16 @@ defmodule Philomena.Processors.Webm do
palette = Briefly.create!(extname: ".png")
{_output, 0} =
System.cmd("ffmpeg", ["-loglevel", "0", "-y", "-i", file, "-vf", "palettegen=stats_mode=diff", palette])
System.cmd("ffmpeg", [
"-loglevel",
"0",
"-y",
"-i",
file,
"-vf",
"palettegen=stats_mode=diff",
palette
])
palette
end
@ -104,8 +185,8 @@ defmodule Philomena.Processors.Webm do
# x264 requires image dimensions to be a multiple of 2
# -2 = ~1
def box_dimensions({width, height}, {target_width, target_height}) do
ratio = min(target_width / width, target_height / height)
new_width = min(max(trunc(width * ratio) &&& -2, 2), target_width)
ratio = min(target_width / width, target_height / height)
new_width = min(max(trunc(width * ratio) &&& -2, 2), target_width)
new_height = min(max(trunc(height * ratio) &&& -2, 2), target_height)
{new_width, new_height}

View file

@ -15,7 +15,8 @@ defmodule Philomena.Repo do
def isolated_transaction(%Multi{} = multi, level) do
Multi.append(
Multi.new |> Multi.run(:isolate, fn repo, _chg ->
Multi.new()
|> Multi.run(:isolate, fn repo, _chg ->
repo.query!("SET TRANSACTION ISOLATION LEVEL #{@levels[level]}")
{:ok, nil}
end),

View file

@ -123,14 +123,14 @@ defmodule Philomena.Reports do
end
def reindex_reports(report_ids) do
spawn fn ->
spawn(fn ->
Report
|> where([r], r.id in ^report_ids)
|> preload([:user, :admin])
|> Repo.all()
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
|> Enum.map(&Elasticsearch.index_document(&1, Report))
end
end)
report_ids
end

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.Reports.Report do
def anonymous?(_report) do
false
end
end
end

View file

@ -5,7 +5,8 @@ defmodule Philomena.Reports.Query do
[
int_fields: ~W(id image_id),
date_fields: ~W(created_at),
literal_fields: ~W(state user user_id admin admin_id reportable_type reportable_id fingerprint),
literal_fields:
~W(state user user_id admin admin_id reportable_type reportable_id fingerprint),
ip_fields: ~W(ip),
bool_fields: ~W(open),
ngram_fields: ~W(reason),

View file

@ -61,7 +61,15 @@ defmodule Philomena.Reports.Report do
|> cast(attrs, [:category, :reason])
|> merge_category()
|> change(attribution)
|> validate_required([:reportable_id, :reportable_type, :category, :reason, :ip, :fingerprint, :user_agent])
|> validate_required([
:reportable_id,
:reportable_type,
:category,
:reason,
:ip,
:fingerprint,
:user_agent
])
end
defp merge_category(changeset) do

View file

@ -6,5 +6,6 @@ defmodule Philomena.Schema.BanId do
put_change(changeset, :generated_ban_id, "#{prefix}#{ban_id}")
end
def put_ban_id(changeset, _prefix), do: changeset
end

View file

@ -11,7 +11,7 @@ defmodule Philomena.Schema.Search do
{:ok, _} ->
changeset
_ ->
_ ->
add_error(changeset, field, "is invalid")
end
end

View file

@ -47,6 +47,6 @@ defmodule Philomena.Schema.TagList do
(list || "")
|> String.split(",")
|> Enum.map(&String.trim(&1))
|> Enum.filter(& &1 != "")
|> Enum.filter(&(&1 != ""))
end
end

View file

@ -48,7 +48,8 @@ defmodule Philomena.Scrapers.Deviantart do
end
defp try_intermediary_hires!(%{images: [image]} = data) do
[domain, object_uuid, object_name] = Regex.run(@cdnint_regex, image.url, capture: :all_but_first)
[domain, object_uuid, object_name] =
Regex.run(@cdnint_regex, image.url, capture: :all_but_first)
built_url = "#{domain}/intermediary/f/#{object_uuid}/#{object_name}"
@ -57,13 +58,13 @@ defmodule Philomena.Scrapers.Deviantart do
# This is the high resolution URL.
%{
data |
images: [
%{
url: built_url,
camo_url: image.camo_url
}
]
data
| images: [
%{
url: built_url,
camo_url: image.camo_url
}
]
}
_ ->
@ -76,24 +77,24 @@ defmodule Philomena.Scrapers.Deviantart do
cond do
String.match?(image.url, @png_regex) ->
%{
data |
images: [
%{
url: String.replace(image.url, @png_regex, "\\1.png\\3"),
camo_url: image.camo_url
}
]
data
| images: [
%{
url: String.replace(image.url, @png_regex, "\\1.png\\3"),
camo_url: image.camo_url
}
]
}
String.match?(image.url, @jpg_regex) ->
%{
data |
images: [
%{
url: String.replace(image.url, @jpg_regex, "\\g{1}100\\3"),
camo_url: image.camo_url
}
]
data
| images: [
%{
url: String.replace(image.url, @jpg_regex, "\\g{1}100\\3"),
camo_url: image.camo_url
}
]
}
true ->
@ -104,6 +105,7 @@ defmodule Philomena.Scrapers.Deviantart do
defp try_old_hires!(%{source_url: source, images: [image]} = data) do
[serial] = Regex.run(@serial_regex, source, capture: :all_but_first)
base36 =
serial
|> String.to_integer()
@ -118,13 +120,13 @@ defmodule Philomena.Scrapers.Deviantart do
{_location, link} = Enum.find(headers, fn {header, _val} -> header == "Location" end)
%{
data |
images: [
%{
url: link,
camo_url: image.camo_url
}
]
data
| images: [
%{
url: link,
camo_url: image.camo_url
}
]
}
_ ->
@ -135,6 +137,7 @@ defmodule Philomena.Scrapers.Deviantart do
# Workaround for benoitc/hackney#273
defp follow_redirect(_url, 0), do: nil
defp follow_redirect(url, max_times) do
case Philomena.Http.get!(url) do
%HTTPoison.Response{headers: headers, status_code: code} when code in [301, 302] ->

View file

@ -27,4 +27,4 @@ defmodule Philomena.Scrapers.Raw do
]
}
end
end
end

View file

@ -16,7 +16,10 @@ defmodule Philomena.Scrapers.Tumblr do
def scrape(uri, url) do
[post_id] = Regex.run(@url_regex, url, capture: :all_but_first)
api_url = "https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{tumblr_api_key()}"
api_url =
"https://api.tumblr.com/v2/blog/#{uri.host}/posts/photo?id=#{post_id}&api_key=#{
tumblr_api_key()
}"
Philomena.Http.get!(api_url)
|> json!()
@ -36,7 +39,7 @@ defmodule Philomena.Scrapers.Tumblr do
image = upsize(photo["original_size"]["url"])
%{"url" => preview} =
Enum.find(photo["alt_sizes"], & &1["width"] == 400) || %{"url" => image}
Enum.find(photo["alt_sizes"], &(&1["width"] == 400)) || %{"url" => image}
%{url: image, camo_url: Camo.Image.image_url(preview)}
end)
@ -94,4 +97,4 @@ defmodule Philomena.Scrapers.Tumblr do
defp tumblr_api_key do
Application.get_env(:philomena, :tumblr_api_key)
end
end
end

View file

@ -17,7 +17,12 @@ defmodule Philomena.Scrapers.Twitter do
defp extract_data(tweet) do
images =
tweet["entities"]["media"]
|> Enum.map(&%{url: &1["media_url_https"] <> ":orig", camo_url: Camo.Image.image_url(&1["media_url_https"])})
|> Enum.map(
&%{
url: &1["media_url_https"] <> ":orig",
camo_url: Camo.Image.image_url(&1["media_url_https"])
}
)
%{
source_url: tweet["url"],
@ -34,7 +39,10 @@ defmodule Philomena.Scrapers.Twitter do
[user, status_id] = Regex.run(@url_regex, url, capture: :all_but_first)
mobile_url = "https://mobile.twitter.com/#{user}/status/#{status_id}"
api_url = "https://api.twitter.com/2/timeline/conversation/#{status_id}.json?tweet_mode=extended"
api_url =
"https://api.twitter.com/2/timeline/conversation/#{status_id}.json?tweet_mode=extended"
url = "https://twitter.com/#{user}/status/#{status_id}"
{gt, bearer} =
@ -42,7 +50,7 @@ defmodule Philomena.Scrapers.Twitter do
|> Map.get(:body)
|> extract_guest_token_and_bearer()
Philomena.Http.get!(api_url, ["Authorization": "Bearer #{bearer}", "x-guest-token": gt])
Philomena.Http.get!(api_url, Authorization: "Bearer #{bearer}", "x-guest-token": gt)
|> Map.get(:body)
|> Jason.decode!()
|> Map.get("globalObjects")

View file

@ -43,12 +43,12 @@ defmodule Philomena.Servers.Config do
defp maybe_update_state(state, key, false) do
Map.put(state, key, load_config(key))
end
defp maybe_update_state(state, _key, _true), do: state
defp load_config(name) do
with {:ok, text} <- File.read("#{app_dir()}/config/#{name}.json"),
{:ok, json} <- Jason.decode(text)
do
{:ok, json} <- Jason.decode(text) do
json
end
end

View file

@ -42,4 +42,4 @@ defmodule Philomena.Servers.ImageProcessor do
def handle_call(:wait, _from, []) do
{:reply, nil, []}
end
end
end

View file

@ -14,10 +14,12 @@ defmodule Philomena.Servers.UserFingerprintUpdater do
{:ok, spawn_link(&init/0)}
end
def cast(user_id, <<"c", _rest::binary>> = fingerprint, updated_at) when byte_size(fingerprint) <= 12 do
def cast(user_id, <<"c", _rest::binary>> = fingerprint, updated_at)
when byte_size(fingerprint) <= 12 do
pid = Process.whereis(:fingerprint_updater)
if pid, do: send(pid, {user_id, fingerprint, updated_at})
end
def cast(_user_id, _fingerprint, _updated_at), do: nil
defp init do
@ -27,9 +29,14 @@ defmodule Philomena.Servers.UserFingerprintUpdater do
defp run do
user_fps = Enum.map(receive_all(), &into_insert_all/1)
update_query = update(UserFingerprint, inc: [uses: 1], set: [updated_at: fragment("EXCLUDED.updated_at")])
Repo.insert_all(UserFingerprint, user_fps, on_conflict: update_query, conflict_target: [:user_id, :fingerprint])
update_query =
update(UserFingerprint, inc: [uses: 1], set: [updated_at: fragment("EXCLUDED.updated_at")])
Repo.insert_all(UserFingerprint, user_fps,
on_conflict: update_query,
conflict_target: [:user_id, :fingerprint]
)
:timer.sleep(:timer.seconds(60))

View file

@ -26,7 +26,9 @@ defmodule Philomena.Servers.UserIpUpdater do
defp run do
user_ips = Enum.map(receive_all(), &into_insert_all/1)
update_query = update(UserIp, inc: [uses: 1], set: [updated_at: fragment("EXCLUDED.updated_at")])
update_query =
update(UserIp, inc: [uses: 1], set: [updated_at: fragment("EXCLUDED.updated_at")])
Repo.insert_all(UserIp, user_ips, on_conflict: update_query, conflict_target: [:user_id, :ip])

View file

@ -3,11 +3,11 @@ defmodule Philomena.Servers.UserLinkUpdater do
alias Philomena.Repo
import Ecto.Query
@seven_days 7*24*60*60
@three_days 3*24*60*60
@twelve_hours 12*60*60
@one_hour 60*60
@two_minutes 2*60
@seven_days 7 * 24 * 60 * 60
@three_days 3 * 24 * 60 * 60
@twelve_hours 12 * 60 * 60
@one_hour 60 * 60
@two_minutes 2 * 60
def child_spec([]) do
%{

View file

@ -8,4 +8,4 @@ defmodule Philomena.Sha512 do
|> :crypto.hash_final()
|> Base.encode16(case: :lower)
end
end
end

View file

@ -27,11 +27,16 @@ defmodule Philomena.Slug do
@spec destructive_slug(String.t()) :: String.t()
def destructive_slug(input) when is_binary(input) do
input
|> String.replace(~r/[^ -~]/, "") # 1
|> String.replace(~r/[^a-zA-Z0-9]+/, "-") # 2
|> String.replace(~r/\A-|-\z/, "") # 3
|> String.downcase() # 4
# 1
|> String.replace(~r/[^ -~]/, "")
# 2
|> String.replace(~r/[^a-zA-Z0-9]+/, "-")
# 3
|> String.replace(~r/\A-|-\z/, "")
# 4
|> String.downcase()
end
def destructive_slug(_input), do: ""
def slug(string) when is_binary(string) do

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.SourceChanges.SourceChange do
def anonymous?(source_change) do
source_change.user_id == source_change.image.user_id and !!source_change.image.anonymous
end
end
end

View file

@ -53,7 +53,7 @@ defmodule Philomena.StaticPages do
"""
def create_static_page(user, attrs \\ %{}) do
static_page = StaticPage.changeset(%StaticPage{}, attrs)
Multi.new()
|> Multi.insert(:static_page, static_page)
|> Multi.run(:version, fn repo, %{static_page: static_page} ->

View file

@ -42,22 +42,25 @@ defmodule Philomena.TagChanges do
end)
|> select([t], [t.image_id, t.tag_id])
to_add =
Enum.map(removed, &%{image_id: &1.image_id, tag_id: &1.tag_id})
to_add = Enum.map(removed, &%{image_id: &1.image_id, tag_id: &1.tag_id})
Repo.transaction(fn ->
{_count, inserted} = Repo.insert_all(Tagging, to_add, on_conflict: :nothing, returning: [:image_id, :tag_id])
{_count, inserted} =
Repo.insert_all(Tagging, to_add, on_conflict: :nothing, returning: [:image_id, :tag_id])
{_count, deleted} = Repo.delete_all(to_remove)
inserted = Enum.map(inserted, &[&1.image_id, &1.tag_id])
added_changes = Enum.map(inserted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: true})
end)
added_changes =
Enum.map(inserted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: true})
end)
removed_changes = Enum.map(deleted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: false})
end)
removed_changes =
Enum.map(deleted, fn [image_id, tag_id] ->
Map.merge(tag_change_attributes, %{image_id: image_id, tag_id: tag_id, added: false})
end)
Repo.insert_all(TagChange, added_changes ++ removed_changes)
@ -68,15 +71,18 @@ defmodule Philomena.TagChanges do
added_upserts =
inserted
|> Enum.group_by(fn [_image_id, tag_id] -> tag_id end)
|> Enum.map(fn {tag_id, instances} -> Map.merge(tag_attributes, %{id: tag_id, images_count: length(instances)}) end)
|> Enum.map(fn {tag_id, instances} ->
Map.merge(tag_attributes, %{id: tag_id, images_count: length(instances)})
end)
removed_upserts =
deleted
|> Enum.group_by(fn [_image_id, tag_id] -> tag_id end)
|> Enum.map(fn {tag_id, instances} -> Map.merge(tag_attributes, %{id: tag_id, images_count: -length(instances)}) end)
|> Enum.map(fn {tag_id, instances} ->
Map.merge(tag_attributes, %{id: tag_id, images_count: -length(instances)})
end)
update_query =
update(Tag, inc: [images_count: fragment("EXCLUDED.images_count")])
update_query = update(Tag, inc: [images_count: fragment("EXCLUDED.images_count")])
upserts = added_upserts ++ removed_upserts

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.TagChanges.TagChange do
def anonymous?(tag_change) do
tag_change.user_id == tag_change.image.user_id and !!tag_change.image.anonymous
end
end
end

View file

@ -40,7 +40,7 @@ defmodule Philomena.Tags do
# Now get rid of the aliases
existent_tags =
existent_tags
|> Enum.map(& &1.aliased_tag || &1)
|> Enum.map(&(&1.aliased_tag || &1))
new_tags =
nonexistent_tag_names
@ -107,6 +107,7 @@ defmodule Philomena.Tags do
"""
def update_tag(%Tag{} = tag, attrs) do
tag_input = Tag.parse_tag_list(attrs["implied_tag_list"])
implied_tags =
Tag
|> where([t], t.name in ^tag_input)
@ -120,7 +121,7 @@ defmodule Philomena.Tags do
def update_tag_image(%Tag{} = tag, attrs) do
changeset = Uploader.analyze_upload(tag, attrs)
Multi.new
Multi.new()
|> Multi.update(:tag, changeset)
|> Multi.run(:update_file, fn _repo, %{tag: tag} ->
Uploader.persist_upload(tag)
@ -134,7 +135,7 @@ defmodule Philomena.Tags do
def remove_tag_image(%Tag{} = tag) do
changeset = Tag.remove_image_changeset(tag)
Multi.new
Multi.new()
|> Multi.update(:tag, changeset)
|> Multi.run(:remove_file, fn _repo, %{tag: tag} ->
Uploader.unpersist_old_upload(tag)
@ -177,9 +178,14 @@ defmodule Philomena.Tags do
def alias_tag(%Tag{} = tag, attrs) do
target_tag = Repo.get_by!(Tag, name: attrs["target_tag"])
filters_hidden = where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.hidden_tag_ids, ^tag.id))
filters_spoilered = where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.spoilered_tag_ids, ^tag.id))
users_watching = where(User, [u], fragment("? @> ARRAY[?]::integer[]", u.watched_tag_ids, ^tag.id))
filters_hidden =
where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.hidden_tag_ids, ^tag.id))
filters_spoilered =
where(Filter, [f], fragment("? @> ARRAY[?]::integer[]", f.spoilered_tag_ids, ^tag.id))
users_watching =
where(User, [u], fragment("? @> ARRAY[?]::integer[]", u.watched_tag_ids, ^tag.id))
array_replace(filters_hidden, :hidden_tag_ids, tag.id, target_tag.id)
array_replace(filters_spoilered, :spoilered_tag_ids, tag.id, target_tag.id)
@ -188,10 +194,10 @@ defmodule Philomena.Tags do
# Manual insert all because ecto won't do it for us
Repo.query!(
"INSERT INTO image_taggings (image_id, tag_id) " <>
"SELECT i.id, #{target_tag.id} FROM images i " <>
"INNER JOIN image_taggings it on it.image_id = i.id " <>
"WHERE it.tag_id = #{tag.id} " <>
"ON CONFLICT DO NOTHING"
"SELECT i.id, #{target_tag.id} FROM images i " <>
"INNER JOIN image_taggings it on it.image_id = i.id " <>
"WHERE it.tag_id = #{tag.id} " <>
"ON CONFLICT DO NOTHING"
)
# Delete taggings on the source tag
@ -211,7 +217,9 @@ defmodule Philomena.Tags do
# Update counter
Tag
|> where(id: ^tag.id)
|> Repo.update_all(set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()])
|> Repo.update_all(
set: [images_count: 0, aliased_tag_id: target_tag.id, updated_at: DateTime.utc_now()]
)
# Finally, reindex
reindex_tag_images(target_tag)
@ -274,9 +282,11 @@ defmodule Philomena.Tags do
|> Repo.all()
|> Enum.map(&%{&1 | image_id: String.to_integer(&1.image_id)})
{:ok, tag_ids} =
Repo.transaction fn ->
{_count, taggings} = Repo.insert_all(Tagging, taggings, on_conflict: :nothing, returning: [:tag_id])
{:ok, tag_ids} =
Repo.transaction(fn ->
{_count, taggings} =
Repo.insert_all(Tagging, taggings, on_conflict: :nothing, returning: [:tag_id])
tag_ids = Enum.map(taggings, & &1.tag_id)
Tag
@ -284,7 +294,7 @@ defmodule Philomena.Tags do
|> Repo.update_all(inc: [images_count: 1])
tag_ids
end
end)
Tag
|> where([t], t.id in ^tag_ids)
@ -309,7 +319,7 @@ defmodule Philomena.Tags do
end
def reindex_tags(tags) do
spawn fn ->
spawn(fn ->
ids =
tags
|> Enum.map(& &1.id)
@ -318,7 +328,7 @@ defmodule Philomena.Tags do
|> preload(^indexing_preloads())
|> where([t], t.id in ^ids)
|> Elasticsearch.reindex(Tag)
end
end)
tags
end

View file

@ -4,7 +4,8 @@ defmodule Philomena.Tags.Query do
defp fields do
[
int_fields: ~W(id images),
literal_fields: ~W(slug name name_in_namespace namespace implies alias_of implied_by aliases category analyzed_name),
literal_fields:
~W(slug name name_in_namespace namespace implies alias_of implied_by aliases category analyzed_name),
bool_fields: ~W(aliased),
ngram_fields: ~W(description short_description),
default_field: {"analyzed_name", :ngram},

View file

@ -45,8 +45,16 @@ defmodule Philomena.Tags.Tag do
schema "tags" do
belongs_to :aliased_tag, Tag, source: :aliased_tag_id
has_many :aliases, Tag, foreign_key: :aliased_tag_id
many_to_many :implied_tags, Tag, join_through: "tags_implied_tags", join_keys: [tag_id: :id, implied_tag_id: :id], on_replace: :delete
many_to_many :implied_by_tags, Tag, join_through: "tags_implied_tags", join_keys: [implied_tag_id: :id, tag_id: :id]
many_to_many :implied_tags, Tag,
join_through: "tags_implied_tags",
join_keys: [tag_id: :id, implied_tag_id: :id],
on_replace: :delete
many_to_many :implied_by_tags, Tag,
join_through: "tags_implied_tags",
join_keys: [implied_tag_id: :id, tag_id: :id]
has_many :public_links, UserLink, where: [public: true, aasm_state: "verified"]
has_many :hidden_links, UserLink, where: [public: false, aasm_state: "verified"]
has_many :dnp_entries, DnpEntry, where: [aasm_state: "listed"]
@ -118,23 +126,25 @@ defmodule Philomena.Tags.Tag do
|> to_string()
|> String.split(",")
|> Enum.map(&clean_tag_name/1)
|> Enum.reject(&"" == &1)
|> Enum.reject(&("" == &1))
end
def display_order(tags) do
tags
|> Enum.sort_by(&{
&1.category != "error",
&1.category != "rating",
&1.category != "origin",
&1.category != "character",
&1.category != "oc",
&1.category != "species",
&1.category != "content-fanmade",
&1.category != "content-official",
&1.category != "spoiler",
&1.name
})
|> Enum.sort_by(
&{
&1.category != "error",
&1.category != "rating",
&1.category != "origin",
&1.category != "character",
&1.category != "oc",
&1.category != "species",
&1.category != "content-fanmade",
&1.category != "content-official",
&1.category != "spoiler",
&1.name
}
)
end
def categories do
@ -175,13 +185,16 @@ defmodule Philomena.Tags.Tag do
defp join_namespace_parts([_name], original_name),
do: original_name
defp join_namespace_parts([namespace, name], _original_name) when namespace in @namespaces,
do: namespace <> ":" <> name
defp join_namespace_parts([_namespace, _name], original_name),
do: original_name
defp ununderscore(<<"artist:", _rest::binary>> = name),
do: name
defp ununderscore(name),
do: String.replace(name, "_", " ")
@ -222,7 +235,7 @@ defmodule Philomena.Tags.Tag do
namespace = changeset |> get_field(:namespace)
case @namespace_categories[namespace] do
nil -> changeset
nil -> changeset
category -> change(changeset, category: category)
end
end

View file

@ -23,11 +23,12 @@ defmodule Philomena.Textile.Renderer do
|> Enum.flat_map(fn tree ->
tree
|> Enum.flat_map(fn
{:text, text} ->
[text]
_ ->
[]
end)
{:text, text} ->
[text]
_ ->
[]
end)
end)
|> find_images
@ -75,15 +76,27 @@ defmodule Philomena.Textile.Renderer do
match
[image, "p"] ->
Phoenix.View.render(@image_view, "_image_target.html", image: image, size: :medium, conn: conn)
Phoenix.View.render(@image_view, "_image_target.html",
image: image,
size: :medium,
conn: conn
)
|> safe_to_string()
[image, "t"] ->
Phoenix.View.render(@image_view, "_image_target.html", image: image, size: :small, conn: conn)
Phoenix.View.render(@image_view, "_image_target.html",
image: image,
size: :small,
conn: conn
)
|> safe_to_string()
[image, "s"] ->
Phoenix.View.render(@image_view, "_image_target.html", image: image, size: :thumb_small, conn: conn)
Phoenix.View.render(@image_view, "_image_target.html",
image: image,
size: :thumb_small,
conn: conn
)
|> safe_to_string()
[image] ->
@ -103,6 +116,7 @@ defmodule Philomena.Textile.Renderer do
end
defp load_images([]), do: %{}
defp load_images(ids) do
Image
|> where([i], i.id in ^ids)

View file

@ -41,11 +41,12 @@ defmodule Philomena.Topics do
"""
def create_topic(forum, attribution, attrs \\ %{}) do
now = DateTime.utc_now() |> DateTime.truncate(:second)
topic =
%Topic{}
|> Topic.creation_changeset(attrs, forum, attribution)
Multi.new
Multi.new()
|> Multi.insert(:topic, topic)
|> Multi.run(:update_topic, fn repo, %{topic: topic} ->
{count, nil} =
@ -59,7 +60,10 @@ defmodule Philomena.Topics do
{count, nil} =
Forum
|> where(id: ^topic.forum_id)
|> repo.update_all(inc: [post_count: 1, topic_count: 1], set: [last_post_id: hd(topic.posts).id])
|> repo.update_all(
inc: [post_count: 1, topic_count: 1],
set: [last_post_id: hd(topic.posts).id]
)
{:ok, count}
end)
@ -70,7 +74,7 @@ defmodule Philomena.Topics do
end
def notify_topic(topic) do
spawn fn ->
spawn(fn ->
forum =
topic
|> Repo.preload(:forum)
@ -92,7 +96,7 @@ defmodule Philomena.Topics do
action: "posted a new topic in"
}
)
end
end)
topic
end
@ -147,6 +151,7 @@ defmodule Philomena.Topics do
alias Philomena.Topics.Subscription
def subscribed?(_topic, nil), do: false
def subscribed?(topic, user) do
Subscription
|> where(topic_id: ^topic.id, user_id: ^user.id)
@ -166,6 +171,7 @@ defmodule Philomena.Topics do
"""
def create_subscription(_topic, nil), do: {:ok, nil}
def create_subscription(topic, user) do
%Subscription{topic_id: topic.id, user_id: user.id}
|> Subscription.changeset(%{})
@ -210,12 +216,12 @@ defmodule Philomena.Topics do
Topic.unlock_changeset(topic)
|> Repo.update()
end
def move_topic(topic, new_forum_id) do
old_forum_id = topic.forum_id
topic_changes = Topic.move_changeset(topic, new_forum_id)
Multi.new
Multi.new()
|> Multi.update(:topic, topic_changes)
|> Multi.run(:update_old_forum, fn repo, %{topic: topic} ->
{count, nil} =
@ -247,6 +253,7 @@ defmodule Philomena.Topics do
end
def clear_notification(_topic, nil), do: nil
def clear_notification(topic, user) do
Notifications.delete_unread_notification("Topic", topic.id, user)
end

View file

@ -10,4 +10,4 @@ defimpl Philomena.Attribution, for: Philomena.Topics.Topic do
def anonymous?(topic) do
!!topic.anonymous
end
end
end

View file

@ -13,11 +13,11 @@ defmodule Philomena.Uploader do
callback on the model or changeset passed in with attributes set on
the field_name.
"""
@spec analyze_upload(any(), String.t(), Plug.Upload.t(), (any(), map() -> Ecto.Changeset.t())) :: Ecto.Changeset.t()
@spec analyze_upload(any(), String.t(), Plug.Upload.t(), (any(), map() -> Ecto.Changeset.t())) ::
Ecto.Changeset.t()
def analyze_upload(model_or_changeset, field_name, upload_parameter, changeset_fn) do
with {:ok, analysis} <- Analyzers.analyze(upload_parameter),
analysis <- extra_attributes(analysis, upload_parameter)
do
analysis <- extra_attributes(analysis, upload_parameter) do
removed =
model_or_changeset
|> change()
@ -25,16 +25,16 @@ defmodule Philomena.Uploader do
attributes =
%{
"name" => analysis.name,
"width" => analysis.width,
"height" => analysis.height,
"size" => analysis.size,
"format" => analysis.extension,
"mime_type" => analysis.mime_type,
"aspect_ratio" => analysis.aspect_ratio,
"name" => analysis.name,
"width" => analysis.width,
"height" => analysis.height,
"size" => analysis.size,
"format" => analysis.extension,
"mime_type" => analysis.mime_type,
"aspect_ratio" => analysis.aspect_ratio,
"orig_sha512_hash" => analysis.sha512,
"sha512_hash" => analysis.sha512,
"is_animated" => analysis.animated?
"sha512_hash" => analysis.sha512,
"is_animated" => analysis.animated?
}
|> prefix_attributes(field_name)
|> Map.put(field_name, analysis.new_name)
@ -55,9 +55,9 @@ defmodule Philomena.Uploader do
@spec persist_upload(any(), String.t(), String.t()) :: any()
def persist_upload(model, file_root, field_name) do
source = Map.get(model, field(upload_key(field_name)))
dest = Map.get(model, field(field_name))
dest = Map.get(model, field(field_name))
target = Path.join(file_root, dest)
dir = Path.dirname(target)
dir = Path.dirname(target)
# Create the target directory if it doesn't exist yet,
# then write the file.
@ -78,10 +78,10 @@ defmodule Philomena.Uploader do
defp extra_attributes(analysis, %Plug.Upload{path: path, filename: filename}) do
{width, height} = analysis.dimensions
aspect_ratio = aspect_ratio(width, height)
aspect_ratio = aspect_ratio(width, height)
stat = File.stat!(path)
sha512 = Sha512.file(path)
stat = File.stat!(path)
sha512 = Sha512.file(path)
new_name = Filename.build(analysis.extension)
analysis
@ -109,4 +109,4 @@ defmodule Philomena.Uploader do
defp remove_key(field_name), do: "removed_#{field_name}"
defp field(field_name), do: String.to_existing_atom(field_name)
end
end

View file

@ -23,6 +23,7 @@ defmodule Philomena.UserDownvoteWipe do
|> where(user_id: ^user.id, up: true)
|> Batch.query_batches([id_field: :image_id], fn queryable ->
{_, image_ids} = Repo.delete_all(select(queryable, [i_v], i_v.image_id))
Repo.update_all(where(Image, [i], i.id in ^image_ids), inc: [upvotes_count: -1, score: -1])
reindex(image_ids)
@ -33,7 +34,7 @@ defmodule Philomena.UserDownvoteWipe do
|> Batch.query_batches([id_field: :image_id], fn queryable ->
{_, image_ids} = Repo.delete_all(select(queryable, [i_f], i_f.image_id))
Repo.update_all(where(Image, [i], i.id in ^image_ids), inc: [faves_count: -1])
reindex(image_ids)
end)
end

Some files were not shown because too many files have changed in this diff Show more