philomena/lib/philomena_web/controllers/profile_controller.ex

177 lines
4.8 KiB
Elixir
Raw Normal View History

2019-11-12 02:27:09 +01:00
defmodule PhilomenaWeb.ProfileController do
use PhilomenaWeb, :controller
2019-12-01 03:22:05 +01:00
alias PhilomenaWeb.ImageLoader
2019-12-05 06:00:41 +01:00
alias Philomena.Textile.Renderer
2019-12-05 14:55:49 +01:00
alias Philomena.UserStatistics.UserStatistic
2019-12-01 03:22:05 +01:00
alias Philomena.Users.User
2019-12-08 21:32:54 +01:00
alias Philomena.Bans
2019-12-05 05:53:56 +01:00
alias Philomena.Galleries.Gallery
alias Philomena.Posts.Post
alias Philomena.Comments.Comment
2019-12-01 03:22:05 +01:00
alias Philomena.Interactions
2019-12-05 05:53:56 +01:00
alias Philomena.Repo
import Ecto.Query
2019-11-12 02:27:09 +01:00
2019-12-06 00:11:15 +01:00
plug :load_and_authorize_resource, model: User, only: :show, id_field: "slug", preload: [
awards: :badge, public_links: :tag, commission: [sheet_image: :tags, items: [example_image: :tags]]
]
2019-11-12 02:27:09 +01:00
def show(conn, _params) do
current_user = conn.assigns.current_user
user = conn.assigns.user
2019-12-05 22:57:59 +01:00
{:ok, {recent_uploads, _tags}} =
2019-12-01 03:22:05 +01:00
ImageLoader.search_string(
conn,
"uploader_id:#{user.id}",
pagination: %{page_number: 1, page_size: 6}
2019-11-12 02:27:09 +01:00
)
2019-12-05 22:57:59 +01:00
{:ok, {recent_faves, _tags}} =
2019-12-01 03:22:05 +01:00
ImageLoader.search_string(
conn,
"faved_by_id:#{user.id}",
pagination: %{page_number: 1, page_size: 6}
2019-11-12 02:27:09 +01:00
)
2019-12-07 23:07:53 +01:00
tags = tags(conn.assigns.user.public_links)
recent_artwork = recent_artwork(conn, tags)
2019-12-05 05:53:56 +01:00
recent_comments =
Comment.search_records(
%{
query: %{
bool: %{
must: [
%{term: %{user_id: user.id}},
%{term: %{anonymous: false}},
%{term: %{hidden_from_users: false}}
]
}
2019-12-05 06:02:08 +01:00
},
sort: %{posted_at: :desc}
2019-12-05 05:53:56 +01:00
},
%{page_size: 3},
Comment |> preload(user: [awards: :badge], image: :tags)
)
|> Enum.filter(&Canada.Can.can?(current_user, :show, &1.image))
2019-12-05 06:00:41 +01:00
recent_comments =
recent_comments
|> Renderer.render_collection(conn)
|> Enum.zip(recent_comments)
2019-12-05 05:53:56 +01:00
recent_posts =
Post.search_records(
%{
query: %{
bool: %{
must: [
%{term: %{user_id: user.id}},
%{term: %{anonymous: false}},
2019-12-05 19:32:53 +01:00
%{term: %{deleted: false}},
2019-12-05 06:02:08 +01:00
%{term: %{access_level: "normal"}}
2019-12-05 05:53:56 +01:00
]
}
2019-12-05 06:02:08 +01:00
},
sort: %{created_at: :desc}
2019-12-05 05:53:56 +01:00
},
%{page_size: 6},
Post |> preload(user: [awards: :badge], topic: :forum)
)
|> Enum.filter(&Canada.Can.can?(current_user, :show, &1.topic))
2019-12-05 19:32:53 +01:00
about_me =
Renderer.render_one(%{body: user.description || ""}, conn)
2019-12-06 00:11:15 +01:00
commission_information =
commission_info(user.commission, conn)
2019-12-05 05:53:56 +01:00
recent_galleries =
Gallery
|> where(creator_id: ^user.id)
|> preload([:creator, thumbnail: :tags])
|> limit(5)
|> Repo.all()
2019-12-05 14:55:49 +01:00
statistics = calculate_statistics(user)
2019-12-01 03:22:05 +01:00
interactions =
Interactions.user_interactions([recent_uploads, recent_faves], current_user)
2019-12-08 21:32:54 +01:00
bans =
Bans.User
|> where(user_id: ^user.id)
|> Repo.all()
2019-11-12 02:27:09 +01:00
render(
conn,
"show.html",
user: user,
2019-12-01 03:22:05 +01:00
interactions: interactions,
2019-12-06 00:11:15 +01:00
commission_information: commission_information,
2019-12-07 23:07:53 +01:00
recent_artwork: recent_artwork,
2019-11-12 02:27:09 +01:00
recent_uploads: recent_uploads,
2019-11-18 15:14:26 +01:00
recent_faves: recent_faves,
2019-12-05 05:53:56 +01:00
recent_comments: recent_comments,
recent_posts: recent_posts,
recent_galleries: recent_galleries,
2019-12-05 14:55:49 +01:00
statistics: statistics,
2019-12-05 19:32:53 +01:00
about_me: about_me,
2019-12-07 23:07:53 +01:00
tags: tags,
2019-12-08 21:32:54 +01:00
bans: bans,
2019-12-05 19:32:53 +01:00
layout_class: "layout--medium"
2019-11-12 02:27:09 +01:00
)
end
2019-12-05 14:55:49 +01:00
defp calculate_statistics(user) do
now =
DateTime.utc_now()
|> DateTime.to_unix(:second)
|> div(86400)
last_90 =
UserStatistic
|> where(user_id: ^user.id)
|> where([us], us.day > ^(now - 89))
|> Repo.all()
2019-12-05 15:01:06 +01:00
|> Map.new(&{now - &1.day, &1})
2019-12-05 14:55:49 +01:00
%{
uploads: individual_stat(last_90, :uploads),
images_favourited: individual_stat(last_90, :images_favourited),
comments_posted: individual_stat(last_90, :comments_posted),
votes_cast: individual_stat(last_90, :votes_cast),
metadata_updates: individual_stat(last_90, :metadata_updates),
forum_posts: individual_stat(last_90, :forum_posts)
}
end
defp individual_stat(mapping, stat_name) do
Enum.map((89..0), &map_fetch(mapping[&1], stat_name) || 0)
end
defp map_fetch(nil, _field_name), do: nil
defp map_fetch(map, field_name), do: Map.get(map, field_name)
2019-12-06 00:11:15 +01:00
defp commission_info(%{information: info}, conn) when info not in [nil, ""],
do: Renderer.render_one(%{body: info}, conn)
defp commission_info(_commission, _conn), do: ""
2019-12-07 23:07:53 +01:00
defp tags([]), do: []
2019-12-07 23:08:24 +01:00
defp tags(links), do: Enum.map(links, & &1.tag) |> Enum.reject(&is_nil/1)
2019-12-07 23:07:53 +01:00
defp recent_artwork(_conn, []), do: []
defp recent_artwork(conn, tags) do
{images, _tags} =
ImageLoader.query(
conn,
%{terms: %{tag_ids: Enum.map(tags, & &1.id)}},
pagination: %{page_number: 1, page_size: 6}
)
images
end
2019-11-12 02:27:09 +01:00
end