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,

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

View file

@ -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
_ ->

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

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] =

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,6 +331,7 @@ defmodule Philomena.Bans do
"""
def exists_for?(user, ip, fingerprint) do
now = DateTime.utc_now()
queries =
subnet_query(ip, now) ++
fingerprint_query(fingerprint, now) ++
@ -349,32 +350,50 @@ defmodule Philomena.Bans do
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

@ -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

@ -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

@ -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

@ -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,7 +111,9 @@ 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

View file

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

View file

@ -26,9 +26,11 @@ defmodule Philomena.ImageVotes do
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)
@ -296,6 +296,7 @@ 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
@ -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)
@ -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,17 +452,24 @@ 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] ->
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] ->
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)
@ -464,7 +478,7 @@ defmodule Philomena.Images do
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

@ -24,7 +24,8 @@ 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], "/")

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,21 +81,26 @@ 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], %{
aliases:
Map.merge(fields[:aliases], %{
"upvoted_by" => "upvoters",
"downvoted_by" => "downvoters",
"upvoted_by_id" => "upvoter_ids",
@ -102,7 +109,7 @@ defmodule Philomena.Images.Query do
"hidden_by_id" => "hidden_by_user_ids",
"deleted" => "hidden_from_users"
})
])
)
end
defp parse(fields, context, query_string) do

View file

@ -13,8 +13,7 @@ defmodule Philomena.Images.TagDiffer do
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")

View file

@ -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

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

@ -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

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

@ -52,8 +52,7 @@ defmodule Philomena.PollVotes do
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)
@ -94,10 +93,12 @@ defmodule Philomena.PollVotes do
_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

@ -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

@ -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})
@ -77,7 +100,18 @@ defmodule Philomena.Processors.Gif do
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
@ -87,9 +121,46 @@ defmodule Philomena.Processors.Gif do
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"},

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,8 +59,8 @@ 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

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,8 +45,8 @@ 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

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
@ -72,9 +88,52 @@ defmodule Philomena.Processors.Webm do
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
@ -87,7 +146,20 @@ defmodule Philomena.Processors.Webm do
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

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

@ -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

@ -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,8 +58,8 @@ defmodule Philomena.Scrapers.Deviantart do
# This is the high resolution URL.
%{
data |
images: [
data
| images: [
%{
url: built_url,
camo_url: image.camo_url
@ -76,8 +77,8 @@ defmodule Philomena.Scrapers.Deviantart do
cond do
String.match?(image.url, @png_regex) ->
%{
data |
images: [
data
| images: [
%{
url: String.replace(image.url, @png_regex, "\\1.png\\3"),
camo_url: image.camo_url
@ -87,8 +88,8 @@ defmodule Philomena.Scrapers.Deviantart do
String.match?(image.url, @jpg_regex) ->
%{
data |
images: [
data
| images: [
%{
url: String.replace(image.url, @jpg_regex, "\\g{1}100\\3"),
camo_url: image.camo_url
@ -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,8 +120,8 @@ defmodule Philomena.Scrapers.Deviantart do
{_location, link} = Enum.find(headers, fn {header, _val} -> header == "Location" end)
%{
data |
images: [
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

@ -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)

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

@ -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

@ -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

@ -42,20 +42,23 @@ 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] ->
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] ->
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)
@ -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

@ -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)
@ -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)
@ -275,8 +283,10 @@ defmodule Philomena.Tags do
|> 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])
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,12 +126,13 @@ 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(&{
|> Enum.sort_by(
&{
&1.category != "error",
&1.category != "rating",
&1.category != "origin",
@ -134,7 +143,8 @@ defmodule Philomena.Tags.Tag do
&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, "_", " ")

View file

@ -25,6 +25,7 @@ defmodule Philomena.Textile.Renderer do
|> Enum.flat_map(fn
{:text, text} ->
[text]
_ ->
[]
end)
@ -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(%{})
@ -215,7 +221,7 @@ defmodule Philomena.Topics 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

@ -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()

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)

View file

@ -92,9 +92,13 @@ defmodule Philomena.UserLinks do
now = DateTime.utc_now() |> DateTime.truncate(:second)
with badge when not is_nil(badge) <- repo.get_by(limit(Badge, 1), title: "Artist"),
nil <- repo.get_by(limit(Award, 1), badge_id: badge.id, user_id: user_link.user_id)
do
%Award{badge_id: badge.id, user_id: user_link.user_id, awarded_by_id: user.id, awarded_on: now}
nil <- repo.get_by(limit(Award, 1), badge_id: badge.id, user_id: user_link.user_id) do
%Award{
badge_id: badge.id,
user_id: user_link.user_id,
awarded_by_id: user.id,
awarded_on: now
}
|> Award.changeset(%{})
|> repo.insert()
else

View file

@ -24,9 +24,16 @@ defmodule Philomena.UserStatistics do
def inc_stat(user, action, amount \\ 1)
def inc_stat(nil, _action, _amount), do: {:ok, nil}
def inc_stat(%{id: user_id}, action, amount)
when action in [:uploads, :images_favourited, :comments_posted, :votes_cast, :metadata_updates, :forum_posts]
do
when action in [
:uploads,
:images_favourited,
:comments_posted,
:votes_cast,
:metadata_updates,
:forum_posts
] do
now =
DateTime.utc_now()
|> DateTime.to_unix(:second)
@ -37,6 +44,7 @@ defmodule Philomena.UserStatistics do
run = fn ->
Repo.update_all(user, inc: [{action_count, amount}])
Repo.insert(
Map.put(%UserStatistic{day: now, user_id: user_id}, action, amount),
on_conflict: [inc: [{action, amount}]],

View file

@ -20,8 +20,12 @@ defmodule Philomena.UserStatistics.UserStatistic do
def changeset(user_statistic, attrs) do
user_statistic
|> cast(attrs, [
:uploads, :votes_cast, :comments_posted, :metadata_updates,
:images_favourited, :forum_posts
:uploads,
:votes_cast,
:comments_posted,
:metadata_updates,
:images_favourited,
:forum_posts
])
end
end

View file

@ -33,7 +33,13 @@ defmodule Philomena.UserWipe do
User
|> where(id: ^user.id)
|> Repo.update_all(set: [email: "deactivated#{random_hex}@example.com", current_sign_in_ip: @wipe_ip, last_sign_in_ip: @wipe_ip])
|> Repo.update_all(
set: [
email: "deactivated#{random_hex}@example.com",
current_sign_in_ip: @wipe_ip,
last_sign_in_ip: @wipe_ip
]
)
end
defp in_batches(queryable, mapper) do

View file

@ -86,7 +86,7 @@ defmodule Philomena.Users do
end
defp clean_roles(nil), do: []
defp clean_roles(roles), do: Enum.filter(roles, &"" != &1)
defp clean_roles(roles), do: Enum.filter(roles, &("" != &1))
def update_spoiler_type(%User{} = user, attrs) do
user
@ -131,7 +131,7 @@ defmodule Philomena.Users do
def update_avatar(%User{} = user, attrs) do
changeset = Uploader.analyze_upload(user, attrs)
Multi.new
Multi.new()
|> Multi.update(:user, changeset)
|> Multi.run(:update_file, fn _repo, %{user: user} ->
Uploader.persist_upload(user)
@ -145,7 +145,7 @@ defmodule Philomena.Users do
def remove_avatar(%User{} = user) do
changeset = User.remove_avatar_changeset(user)
Multi.new
Multi.new()
|> Multi.update(:user, changeset)
|> Multi.run(:remove_file, fn _repo, %{user: user} ->
Uploader.unpersist_old_upload(user)
@ -203,12 +203,13 @@ defmodule Philomena.Users do
User
|> join(:left, [u], _ in assoc(u, :roles))
|> join(:left, [u, _], _ in assoc(u, :current_filter))
|> preload([_, r, cf], [current_filter: cf, roles: r])
|> preload([_, r, cf], current_filter: cf, roles: r)
|> Repo.get_by(clauses)
|> setup_roles()
end
defp setup_roles(nil), do: nil
defp setup_roles(user) do
role_map = Map.new(user.roles, &{&1.resource_type || &1.name, &1.name})

View file

@ -52,7 +52,9 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# View forums
def can?(%User{role: "moderator"}, :show, %Forum{access_level: level})
when level in ["normal", "assistant", "staff"], do: true
when level in ["normal", "assistant", "staff"],
do: true
def can?(%User{role: "moderator"}, :show, %Topic{hidden_from_users: true}), do: true
# View conversations
@ -116,8 +118,11 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# And some privileged moderators can...
# Manage site notices
def can?(%User{role: "moderator", role_map: %{"SiteNotice" => "admin"}}, _action, SiteNotice), do: true
def can?(%User{role: "moderator", role_map: %{"SiteNotice" => "admin"}}, _action, %SiteNotice{}), do: true
def can?(%User{role: "moderator", role_map: %{"SiteNotice" => "admin"}}, _action, SiteNotice),
do: true
def can?(%User{role: "moderator", role_map: %{"SiteNotice" => "admin"}}, _action, %SiteNotice{}),
do: true
# Manage badges
def can?(%User{role: "moderator", role_map: %{"Badge" => "admin"}}, _action, Badge), do: true
@ -132,58 +137,127 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# Manage users
def can?(%User{role: "moderator", role_map: %{"User" => "moderator"}}, _action, User), do: true
def can?(%User{role: "moderator", role_map: %{"User" => "moderator"}}, _action, %User{}), do: true
def can?(%User{role: "moderator", role_map: %{"User" => "moderator"}}, _action, %User{}),
do: true
# Manage advertisements
def can?(%User{role: "moderator", role_map: %{"Advert" => "admin"}}, _action, Advert), do: true
def can?(%User{role: "moderator", role_map: %{"Advert" => "admin"}}, _action, %Advert{}), do: true
def can?(%User{role: "moderator", role_map: %{"Advert" => "admin"}}, _action, %Advert{}),
do: true
# Manage static pages
def can?(%User{role: "moderator", role_map: %{"StaticPage" => "admin"}}, _action, StaticPage), do: true
def can?(%User{role: "moderator", role_map: %{"StaticPage" => "admin"}}, _action, %StaticPage{}), do: true
def can?(%User{role: "moderator", role_map: %{"StaticPage" => "admin"}}, _action, StaticPage),
do: true
def can?(%User{role: "moderator", role_map: %{"StaticPage" => "admin"}}, _action, %StaticPage{}),
do: true
#
# Assistants can...
#
# Image assistant actions
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :show, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :hide, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :edit, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :edit_description, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :show, %Image{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :hide, %Image{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Image" => "moderator"}}, :edit, %Image{}),
do: true
def can?(
%User{role: "assistant", role_map: %{"Image" => "moderator"}},
:edit_description,
%Image{}
),
do: true
# Dupe assistant actions
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :index, DuplicateReport), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :edit, %DuplicateReport{}), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :show, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :edit, %Image{}), do: true
def can?(%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}}, :hide, %Comment{}), do: true
def can?(
%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}},
:index,
DuplicateReport
),
do: true
def can?(
%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}},
:edit,
%DuplicateReport{}
),
do: true
def can?(
%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}},
:show,
%Image{}
),
do: true
def can?(
%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}},
:edit,
%Image{}
),
do: true
def can?(
%User{role: "assistant", role_map: %{"DuplicateReport" => "moderator"}},
:hide,
%Comment{}
),
do: true
# Comment assistant actions
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :show, %Comment{}), do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :edit, %Comment{}), do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :hide, %Comment{}), do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :show, %Comment{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :edit, %Comment{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Comment" => "moderator"}}, :hide, %Comment{}),
do: true
# Topic assistant actions
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :show, %Topic{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :edit, %Topic{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Topic{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :show, %Post{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :edit, %Post{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Post{}), do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :show, %Topic{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :edit, %Topic{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Topic{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :show, %Post{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :edit, %Post{}),
do: true
def can?(%User{role: "assistant", role_map: %{"Topic" => "moderator"}}, :hide, %Post{}),
do: true
# Tag assistant actions
def can?(%User{role: "assistant", role_map: %{"Tag" => "moderator"}}, :edit, %Tag{}), do: true
def can?(%User{role: "assistant", role_map: %{"Tag" => "moderator"}}, :batch_update, Tag), do: true
def can?(%User{role: "assistant", role_map: %{"Tag" => "moderator"}}, :batch_update, Tag),
do: true
# User link assistant actions
def can?(%User{role: "assistant", role_map: %{"UserLink" => "moderator"}}, :show, %UserLink{}), do: true
def can?(%User{role: "assistant", role_map: %{"UserLink" => "moderator"}}, :edit, %UserLink{}), do: true
def can?(%User{role: "assistant", role_map: %{"UserLink" => "moderator"}}, :show, %UserLink{}),
do: true
def can?(%User{role: "assistant", role_map: %{"UserLink" => "moderator"}}, :edit, %UserLink{}),
do: true
# View forums
def can?(%User{role: "assistant"}, :show, %Forum{access_level: level})
when level in ["normal", "assistant"], do: true
when level in ["normal", "assistant"],
do: true
def can?(%User{role: "assistant"}, :show, %Topic{hidden_from_users: true}), do: true
#
@ -205,17 +279,22 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
def can?(_user, :show, %Filter{system: true}), do: true
def can?(_user, :show, %Filter{public: true}), do: true
def can?(%User{}, action, Filter) when action in [:index, :new, :create], do: true
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:show, :edit, :update], do: true
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:show, :edit, :update],
do: true
# Edit filters they own
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:edit, :update, :delete], do: true
def can?(%User{id: id}, action, %Filter{user_id: id}) when action in [:edit, :update, :delete],
do: true
# View user links they've created
def can?(%User{id: id}, :create_links, %User{id: id}), do: true
def can?(%User{id: id}, :show, %UserLink{user_id: id}), do: true
# Edit their commissions
def can?(%User{id: id}, action, %Commission{user_id: id}) when action in [:edit, :update, :delete], do: true
def can?(%User{id: id}, action, %Commission{user_id: id})
when action in [:edit, :update, :delete],
do: true
# View non-deleted images
def can?(_user, action, Image)
@ -229,10 +308,12 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
def can?(_user, :show, %Tag{}), do: true
# Comment on images where that is allowed
def can?(_user, :create_comment, %Image{hidden_from_users: false, commenting_allowed: true}), do: true
def can?(_user, :create_comment, %Image{hidden_from_users: false, commenting_allowed: true}),
do: true
# Edit comments on images
def can?(%User{id: id}, action, %Comment{hidden_from_users: false, user_id: id} = comment) when action in [:edit, :update] do
def can?(%User{id: id}, action, %Comment{hidden_from_users: false, user_id: id} = comment)
when action in [:edit, :update] do
# comment must have been made no later than 15 minutes ago
time_ago = NaiveDateTime.utc_now() |> NaiveDateTime.add(-15 * 60)
@ -240,8 +321,15 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
end
# Edit metadata on images where that is allowed
def can?(_user, :edit_metadata, %Image{hidden_from_users: false, tag_editing_allowed: true}), do: true
def can?(%User{id: id}, :edit_description, %Image{user_id: id, hidden_from_users: false, description_editing_allowed: true}), do: true
def can?(_user, :edit_metadata, %Image{hidden_from_users: false, tag_editing_allowed: true}),
do: true
def can?(%User{id: id}, :edit_description, %Image{
user_id: id,
hidden_from_users: false,
description_editing_allowed: true
}),
do: true
# Vote on images they can see
def can?(user, :vote, image), do: can?(user, :show, image)
@ -257,7 +345,10 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# Create and edit posts
def can?(_user, :create_post, %Topic{locked_at: nil, hidden_from_users: false}), do: true
def can?(%User{id: id}, action, %Post{hidden_from_users: false, user_id: id}) when action in [:edit, :update], do: true
def can?(%User{id: id}, action, %Post{hidden_from_users: false, user_id: id})
when action in [:edit, :update],
do: true
# View profile pages
def can?(_user, :show, %User{}), do: true
@ -274,7 +365,10 @@ defimpl Canada.Can, for: [Atom, Philomena.Users.User] do
# Create and edit galleries
def can?(_user, :show, %Gallery{}), do: true
def can?(%User{}, action, Gallery) when action in [:new, :create], do: true
def can?(%User{id: id}, action, %Gallery{creator_id: id}) when action in [:edit, :update, :delete], do: true
def can?(%User{id: id}, action, %Gallery{creator_id: id})
when action in [:edit, :update, :delete],
do: true
# Show static pages
def can?(_user, :show, %StaticPage{}), do: true

View file

@ -183,7 +183,10 @@ defmodule Philomena.Users.User do
changeset
|> put_change(:current_filter_id, filter.id)
|> put_change(:recent_filter_ids, Enum.take(Enum.uniq([filter.id | user.recent_filter_ids]), 10))
|> put_change(
:recent_filter_ids,
Enum.take(Enum.uniq([filter.id | user.recent_filter_ids]), 10)
)
end
def spoiler_type_changeset(user, attrs) do
@ -196,16 +199,32 @@ defmodule Philomena.Users.User do
def settings_changeset(user, attrs) do
user
|> cast(attrs, [
:watched_tag_list, :images_per_page, :fancy_tag_field_on_upload,
:fancy_tag_field_on_edit, :anonymous_by_default, :scale_large_images,
:comments_per_page, :theme, :watched_images_query_str,
:no_spoilered_in_watched, :watched_images_exclude_str,
:use_centered_layout, :hide_vote_counts, :comments_newest_first
:watched_tag_list,
:images_per_page,
:fancy_tag_field_on_upload,
:fancy_tag_field_on_edit,
:anonymous_by_default,
:scale_large_images,
:comments_per_page,
:theme,
:watched_images_query_str,
:no_spoilered_in_watched,
:watched_images_exclude_str,
:use_centered_layout,
:hide_vote_counts,
:comments_newest_first
])
|> validate_required([
:images_per_page, :fancy_tag_field_on_upload, :fancy_tag_field_on_edit,
:anonymous_by_default, :scale_large_images, :comments_per_page, :theme,
:no_spoilered_in_watched, :use_centered_layout, :hide_vote_counts
:images_per_page,
:fancy_tag_field_on_upload,
:fancy_tag_field_on_edit,
:anonymous_by_default,
:scale_large_images,
:comments_per_page,
:theme,
:no_spoilered_in_watched,
:use_centered_layout,
:hide_vote_counts
])
|> TagList.propagate_tag_list(:watched_tag_list, :watched_tag_ids)
|> validate_inclusion(:theme, ~W(default dark red))
@ -220,7 +239,10 @@ defmodule Philomena.Users.User do
|> cast(attrs, [:description, :personal_title])
|> validate_length(:description, max: 10_000, count: :bytes)
|> validate_length(:personal_title, max: 24, count: :bytes)
|> validate_format(:personal_title, ~r/\A((?!site|admin|moderator|assistant|developer|\p{C}).)*\z/iu)
|> validate_format(
:personal_title,
~r/\A((?!site|admin|moderator|assistant|developer|\p{C}).)*\z/iu
)
end
def scratchpad_changeset(user, attrs) do
@ -231,11 +253,19 @@ defmodule Philomena.Users.User do
def avatar_changeset(user, attrs) do
user
|> cast(attrs, [
:avatar, :avatar_width, :avatar_height, :avatar_size, :uploaded_avatar,
:avatar,
:avatar_width,
:avatar_height,
:avatar_size,
:uploaded_avatar,
:removed_avatar
])
|> validate_required([
:avatar, :avatar_width, :avatar_height, :avatar_size, :uploaded_avatar
:avatar,
:avatar_width,
:avatar_height,
:avatar_size,
:uploaded_avatar
])
|> validate_number(:avatar_size, greater_than: 0, less_than_or_equal_to: 300_000)
|> validate_number(:avatar_width, greater_than: 0, less_than_or_equal_to: 1000)
@ -321,7 +351,7 @@ defmodule Philomena.Users.User do
end
def random_backup_codes do
(1..10)
1..10
|> Enum.map(fn _i ->
:crypto.strong_rand_bytes(6) |> Base.encode16(case: :lower)
end)
@ -329,11 +359,13 @@ defmodule Philomena.Users.User do
def totp_qrcode(user) do
secret = totp_secret(user)
provisioning_uri = %URI{
scheme: "otpauth",
host: "totp",
path: "/Derpibooru:" <> user.email,
query: URI.encode_query(%{
query:
URI.encode_query(%{
secret: secret,
issuer: "Derpibooru"
})
@ -354,7 +386,6 @@ defmodule Philomena.Users.User do
)
end
defp enable_totp_changeset(user, backup_codes) do
hashed_codes =
backup_codes
@ -409,7 +440,8 @@ defmodule Philomena.Users.User do
defp totp_valid?(user, token) do
case Integer.parse(token) do
{int_token, _rest} ->
int_token != user.consumed_timestep and :pot.valid_totp(token, totp_secret(user), window: 1)
int_token != user.consumed_timestep and
:pot.valid_totp(token, totp_secret(user), window: 1)
_error ->
false

View file

@ -29,10 +29,9 @@ defmodule Philomena.Versions do
body = yaml["body"] || ""
edit_reason = yaml["edit_reason"]
v =
%{
version |
parent: parent,
v = %{
version
| parent: parent,
user: users[version.whodunnit],
body: body,
edit_reason: edit_reason,
@ -61,7 +60,12 @@ defmodule Philomena.Versions do
"""
def create_version(item_type, item_id, whodunnit, attrs \\ %{}) do
%Version{item_type: item_type, item_id: item_id, event: "update", whodunnit: whodunnit(whodunnit)}
%Version{
item_type: item_type,
item_id: item_id,
event: "update",
whodunnit: whodunnit(whodunnit)
}
|> Version.changeset(attrs, item_id)
|> Repo.insert()
end

View file

@ -31,7 +31,8 @@ defmodule Philomena.Versions.Version do
body = get_field(changeset, :body)
edit_reason = get_field(changeset, :edit_reason)
object = Jason.encode!(%{
object =
Jason.encode!(%{
id: item_id,
body: body,
edit_reason: edit_reason

View file

@ -20,7 +20,11 @@ defmodule PhilomenaWeb.CommentJson do
id: comment.id,
image_id: comment.image_id,
user_id: if(not comment.anonymous, do: comment.user_id),
author: if(comment.anonymous or is_nil(comment.user), do: UserAttributionView.anonymous_name(comment), else: comment.user.name),
author:
if(comment.anonymous or is_nil(comment.user),
do: UserAttributionView.anonymous_name(comment),
else: comment.user.name
),
body: nil
}
end
@ -30,7 +34,11 @@ defmodule PhilomenaWeb.CommentJson do
id: comment.id,
image_id: comment.image_id,
user_id: if(not comment.anonymous, do: comment.user_id),
author: if(comment.anonymous or is_nil(comment.user), do: UserAttributionView.anonymous_name(comment), else: comment.user.name),
author:
if(comment.anonymous or is_nil(comment.user),
do: UserAttributionView.anonymous_name(comment),
else: comment.user.name
),
body: comment.body
}
end

View file

@ -3,7 +3,16 @@ defmodule PhilomenaWeb.ActivityController do
alias PhilomenaWeb.ImageLoader
alias Philomena.Elasticsearch
alias Philomena.{Images.Image, ImageFeatures.ImageFeature, Comments.Comment, Channels.Channel, Topics.Topic, Forums.Forum}
alias Philomena.{
Images.Image,
ImageFeatures.ImageFeature,
Comments.Comment,
Channels.Channel,
Topics.Topic,
Forums.Forum
}
alias Philomena.Interactions
alias Philomena.Repo
import Ecto.Query
@ -47,7 +56,8 @@ defmodule PhilomenaWeb.ActivityController do
Comment |> preload([:user, image: [:tags]])
)
watched = if !!user do
watched =
if !!user do
{:ok, {watched_images, _tags}} =
ImageLoader.search_string(
conn,

View file

@ -15,7 +15,11 @@ defmodule PhilomenaWeb.Admin.AdvertController do
|> order_by(desc: :finish_date)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", title: "Admin - Adverts", layout_class: "layout--wide", adverts: adverts)
render(conn, "index.html",
title: "Admin - Adverts",
layout_class: "layout--wide",
adverts: adverts
)
end
def new(conn, _params) do

View file

@ -14,6 +14,7 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
tags = Tag.parse_tag_list(tags)
added_tag_names = Enum.reject(tags, &String.starts_with?(&1, "-"))
removed_tag_names =
tags
|> Enum.filter(&String.starts_with?(&1, "-"))
@ -24,7 +25,7 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
|> where([t], t.name in ^added_tag_names)
|> preload([:implied_tags, aliased_tag: :implied_tags])
|> Repo.all()
|> Enum.map(& &1.aliased_tag || &1)
|> Enum.map(&(&1.aliased_tag || &1))
|> Enum.flat_map(&[&1 | &1.implied_tags])
removed_tags =
@ -33,6 +34,7 @@ defmodule PhilomenaWeb.Admin.Batch.TagController do
|> Repo.all()
attributes = conn.assigns.attributes
attributes = %{
ip: attributes[:ip],
fingerprint: attributes[:fingerprint],

View file

@ -8,7 +8,11 @@ defmodule PhilomenaWeb.Admin.DnpEntry.TransitionController do
plug :load_resource, model: DnpEntry, only: [:create], id_name: "dnp_entry_id", persisted: true
def create(conn, %{"state" => new_state}) do
case DnpEntries.transition_dnp_entry(conn.assigns.dnp_entry, conn.assigns.current_user, new_state) do
case DnpEntries.transition_dnp_entry(
conn.assigns.dnp_entry,
conn.assigns.current_user,
new_state
) do
{:ok, dnp_entry} ->
conn
|> put_flash(:info, "Successfully updated DNP entry.")

View file

@ -21,7 +21,11 @@ defmodule PhilomenaWeb.Admin.DnpEntryController do
DnpEntry
|> join(:inner, [d], _ in assoc(d, :tag))
|> join(:inner, [d, _t], _ in assoc(d, :requesting_user))
|> where([d, t, u], ilike(u.name, ^q) or ilike(t.name, ^q) or ilike(d.reason, ^q) or ilike(d.conditions, ^q) or ilike(d.instructions, ^q))
|> where(
[d, t, u],
ilike(u.name, ^q) or ilike(t.name, ^q) or ilike(d.reason, ^q) or ilike(d.conditions, ^q) or
ilike(d.instructions, ^q)
)
|> load_entries(conn)
end
@ -43,10 +47,13 @@ defmodule PhilomenaWeb.Admin.DnpEntryController do
|> Enum.map(&%{body: &1.conditions})
|> Renderer.render_collection(conn)
dnp_entries =
%{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
dnp_entries = %{dnp_entries | entries: Enum.zip(bodies, dnp_entries.entries)}
render(conn, "index.html", layout_class: "layout--wide", title: "Admin - DNP Entries", dnp_entries: dnp_entries)
render(conn, "index.html",
layout_class: "layout--wide",
title: "Admin - DNP Entries",
dnp_entries: dnp_entries
)
end
defp verify_authorized(conn, _opts) do

View file

@ -11,7 +11,12 @@ defmodule PhilomenaWeb.Admin.Donation.UserController do
def show(conn, _params) do
user = conn.assigns.user
changeset = Donations.change_donation(%Donation{})
render(conn, "index.html", title: "Donations for User `#{user.name}'", donations: user.donations, changeset: changeset)
render(conn, "index.html",
title: "Donations for User `#{user.name}'",
donations: user.donations,
changeset: changeset
)
end
defp verify_authorized(conn, _opts) do

View file

@ -11,11 +11,12 @@ defmodule PhilomenaWeb.Admin.FingerprintBanController do
def index(conn, %{"q" => q}) when is_binary(q) do
FingerprintBan
|> where([fb],
ilike(fb.fingerprint, ^"%#{q}%")
or fb.generated_ban_id == ^q
or fragment("to_tsvector(?) @@ plainto_tsquery(?)", fb.reason, ^q)
or fragment("to_tsvector(?) @@ plainto_tsquery(?)", fb.note, ^q)
|> where(
[fb],
ilike(fb.fingerprint, ^"%#{q}%") or
fb.generated_ban_id == ^q or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", fb.reason, ^q) or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", fb.note, ^q)
)
|> load_bans(conn)
end
@ -84,7 +85,11 @@ defmodule PhilomenaWeb.Admin.FingerprintBanController do
|> preload(:banning_user)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", layout_class: "layout--wide", title: "Admin - Fingerprint Bans", fingerprint_bans: fingerprint_bans)
render(conn, "index.html",
layout_class: "layout--wide",
title: "Admin - Fingerprint Bans",
fingerprint_bans: fingerprint_bans
)
end
defp verify_authorized(conn, _opts) do

View file

@ -11,7 +11,12 @@ defmodule PhilomenaWeb.Admin.ReportController do
import Ecto.Query
plug :verify_authorized
plug :load_and_authorize_resource, model: Report, only: [:show], preload: [:admin, user: [:linked_tags, awards: :badge]]
plug :load_and_authorize_resource,
model: Report,
only: [:show],
preload: [:admin, user: [:linked_tags, awards: :badge]]
plug :set_mod_notes when action in [:show]
def index(conn, %{"rq" => query_string}) do
@ -19,14 +24,18 @@ defmodule PhilomenaWeb.Admin.ReportController do
reports = load_reports(conn, query)
render(conn, "index.html", title: "Admin - Reports", layout_class: "layout--wide", reports: reports, my_reports: [])
render(conn, "index.html",
title: "Admin - Reports",
layout_class: "layout--wide",
reports: reports,
my_reports: []
)
end
def index(conn, _params) do
user = conn.assigns.current_user
query =
%{
query = %{
bool: %{
should: [
%{term: %{open: false}},
@ -50,11 +59,20 @@ defmodule PhilomenaWeb.Admin.ReportController do
|> Repo.all()
|> Polymorphic.load_polymorphic(reportable: [reportable_id: :reportable_type])
render(conn, "index.html", title: "Admin - Reports", layout_class: "layout--wide", reports: reports, my_reports: my_reports)
render(conn, "index.html",
title: "Admin - Reports",
layout_class: "layout--wide",
reports: reports,
my_reports: my_reports
)
end
def show(conn, _params) do
[report] = Polymorphic.load_polymorphic([conn.assigns.report], reportable: [reportable_id: :reportable_type])
[report] =
Polymorphic.load_polymorphic([conn.assigns.report],
reportable: [reportable_id: :reportable_type]
)
body = Renderer.render_one(%{body: report.reason}, conn)
render(conn, "show.html", title: "Showing Report", report: report, body: body)
@ -72,8 +90,7 @@ defmodule PhilomenaWeb.Admin.ReportController do
Report |> preload([:admin, user: :linked_tags])
)
entries =
Polymorphic.load_polymorphic(reports, reportable: [reportable_id: :reportable_type])
entries = Polymorphic.load_polymorphic(reports, reportable: [reportable_id: :reportable_type])
%{reports | entries: entries}
end

View file

@ -11,10 +11,11 @@ defmodule PhilomenaWeb.Admin.SubnetBanController do
def index(conn, %{"q" => q}) when is_binary(q) do
SubnetBan
|> where([sb],
sb.generated_ban_id == ^q
or fragment("to_tsvector(?) @@ plainto_tsquery(?)", sb.reason, ^q)
or fragment("to_tsvector(?) @@ plainto_tsquery(?)", sb.note, ^q)
|> where(
[sb],
sb.generated_ban_id == ^q or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", sb.reason, ^q) or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", sb.note, ^q)
)
|> load_bans(conn)
end
@ -86,7 +87,11 @@ defmodule PhilomenaWeb.Admin.SubnetBanController do
|> preload(:banning_user)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", title: "Admin - Subnet Bans", layout_class: "layout--wide", subnet_bans: subnet_bans)
render(conn, "index.html",
title: "Admin - Subnet Bans",
layout_class: "layout--wide",
subnet_bans: subnet_bans
)
end
defp verify_authorized(conn, _opts) do

View file

@ -8,9 +8,9 @@ defmodule PhilomenaWeb.Admin.User.DownvoteController do
plug :load_resource, model: User, id_name: "user_id", id_field: "slug", persisted: true
def delete(conn, _params) do
spawn fn ->
spawn(fn ->
UserDownvoteWipe.perform(conn.assigns.user)
end
end)
conn
|> put_flash(:info, "Downvote wipe started.")

View file

@ -8,9 +8,9 @@ defmodule PhilomenaWeb.Admin.User.VoteController do
plug :load_resource, model: User, id_name: "user_id", id_field: "slug", persisted: true
def delete(conn, _params) do
spawn fn ->
spawn(fn ->
UserDownvoteWipe.perform(conn.assigns.user, true)
end
end)
conn
|> put_flash(:info, "Vote and fave wipe started.")

View file

@ -8,12 +8,15 @@ defmodule PhilomenaWeb.Admin.User.WipeController do
plug :load_resource, model: User, id_name: "user_id", id_field: "slug", persisted: true
def create(conn, _params) do
spawn fn ->
spawn(fn ->
UserWipe.perform(conn.assigns.user)
end
end)
conn
|> put_flash(:info, "PII wipe started, please verify and then deactivate the account as necessary.")
|> put_flash(
:info,
"PII wipe started, please verify and then deactivate the account as necessary."
)
|> redirect(to: Routes.profile_path(conn, :show, conn.assigns.user))
end

View file

@ -14,11 +14,12 @@ defmodule PhilomenaWeb.Admin.UserBanController do
UserBan
|> join(:inner, [ub], _ in assoc(ub, :user))
|> where([ub, u],
ilike(u.name, ^like_q)
or ub.generated_ban_id == ^q
or fragment("to_tsvector(?) @@ plainto_tsquery(?)", ub.reason, ^q)
or fragment("to_tsvector(?) @@ plainto_tsquery(?)", ub.note, ^q)
|> where(
[ub, u],
ilike(u.name, ^like_q) or
ub.generated_ban_id == ^q or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", ub.reason, ^q) or
fragment("to_tsvector(?) @@ plainto_tsquery(?)", ub.note, ^q)
)
|> load_bans(conn)
end
@ -87,7 +88,11 @@ defmodule PhilomenaWeb.Admin.UserBanController do
|> preload([:user, :banning_user])
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", title: "Admin - User Bans", layout_class: "layout--wide", user_bans: user_bans)
render(conn, "index.html",
title: "Admin - User Bans",
layout_class: "layout--wide",
user_bans: user_bans
)
end
defp verify_authorized(conn, _opts) do

View file

@ -8,7 +8,13 @@ defmodule PhilomenaWeb.Admin.UserController do
import Ecto.Query
plug :verify_authorized
plug :load_and_authorize_resource, model: User, only: [:edit, :update], id_field: "slug", preload: [:roles]
plug :load_and_authorize_resource,
model: User,
only: [:edit, :update],
id_field: "slug",
preload: [:roles]
plug :load_roles when action in [:edit]
def index(conn, %{"q" => q}) do
@ -39,7 +45,11 @@ defmodule PhilomenaWeb.Admin.UserController do
|> order_by(desc: :id)
|> Repo.paginate(conn.assigns.scrivener)
render(conn, "index.html", title: "Admin - Users", layout_class: "layout--medium", users: users)
render(conn, "index.html",
title: "Admin - Users",
layout_class: "layout--medium",
users: users
)
end
def edit(conn, _params) do

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