philomena/lib/philomena_web/controllers/profile_controller.ex

102 lines
2.7 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-01 03:22:05 +01:00
alias Philomena.Users.User
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
plug :load_and_authorize_resource, model: User, only: :show, id_field: "slug", preload: [awards: :badge, public_links: :tag]
def show(conn, _params) do
current_user = conn.assigns.current_user
user = conn.assigns.user
2019-12-01 03:22:05 +01:00
{:ok, recent_uploads} =
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-01 03:22:05 +01:00
{:ok, recent_faves} =
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-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 06:02:08 +01:00
%{term: %{hidden_from_users: false}},
%{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))
recent_galleries =
Gallery
|> where(creator_id: ^user.id)
|> preload([:creator, thumbnail: :tags])
|> limit(5)
|> Repo.all()
2019-12-01 03:22:05 +01:00
interactions =
Interactions.user_interactions([recent_uploads, recent_faves], current_user)
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-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-11-18 15:14:26 +01:00
layout_class: "layout--wide"
2019-11-12 02:27:09 +01:00
)
end
end