philomena/lib/philomena_web/controllers/activity_controller.ex

120 lines
3 KiB
Elixir
Raw Normal View History

2019-10-04 18:48:00 +02:00
defmodule PhilomenaWeb.ActivityController do
use PhilomenaWeb, :controller
alias Philomena.{Images, Images.Image, Images.Feature, Comments.Comment, Channels.Channel, Topics.Topic, Forums.Forum}
2019-10-04 18:48:00 +02:00
alias Philomena.Repo
import Ecto.Query
def index(conn, _params) do
user = conn.assigns.current_user
filter = conn.assigns.compiled_filter
{:ok, image_query} = Images.Query.compile(user, "created_at.lte:3 minutes ago")
images =
Image.search_records(
%{
query: %{
bool: %{
must_not: filter,
must: image_query
}
},
sort: %{created_at: :desc}
},
2019-10-09 01:19:57 +02:00
%{page_number: 1, page_size: 25},
2019-10-04 18:48:00 +02:00
Image |> preload([:tags])
)
top_scoring =
Image.search_records(
%{
query: %{
bool: %{
must_not: filter,
must: %{range: %{first_seen_at: %{gt: "now-3d"}}}
}
},
sort: [%{score: :desc}, %{first_seen_at: :desc}]
},
2019-10-09 01:19:57 +02:00
%{page_number: :rand.uniform(6), page_size: 4},
2019-10-04 18:48:00 +02:00
Image |> preload([:tags])
)
comments =
Comment.search_records(
%{
query: %{
bool: %{
must: %{
range: %{posted_at: %{gt: "now-1w"}}
},
must_not: [
%{terms: %{image_tag_ids: conn.assigns.current_filter.hidden_tag_ids}},
%{term: %{hidden_from_users: true}}
]
}
},
sort: %{posted_at: :desc}
},
2019-10-09 01:19:57 +02:00
%{page_number: 1, page_size: 6},
Comment |> preload([:user, :image])
)
2019-10-05 00:52:44 +02:00
watched = if !!user do
{:ok, watched_query} = Images.Query.compile(user, "my:watched")
2019-10-04 18:48:00 +02:00
Image.search_records(
%{
query: %{
bool: %{
must_not: filter,
must: watched_query
}
},
sort: %{created_at: :desc}
},
2019-10-09 01:19:57 +02:00
%{page_number: 1, page_size: 25},
2019-10-04 18:48:00 +02:00
Image |> preload([:tags])
)
end
featured_image =
Image
2019-10-05 01:18:43 +02:00
|> join(:inner, [i], f in Feature, on: [image_id: i.id])
2019-10-04 18:48:00 +02:00
|> order_by([i, f], desc: f.created_at)
|> limit(1)
|> Repo.one()
2019-10-05 00:52:44 +02:00
streams =
Channel
|> where([c], c.nsfw == false)
|> where([c], not is_nil(c.last_fetched_at))
|> order_by(desc: :is_live, asc: :title)
|> limit(6)
|> Repo.all()
2019-10-04 18:48:00 +02:00
topics =
Topic
2019-10-05 00:52:44 +02:00
|> join(:inner, [t], f in Forum, on: [id: t.forum_id])
2019-10-04 18:48:00 +02:00
|> where([t, _f], t.hidden_from_users == false)
2019-10-05 00:56:51 +02:00
|> where([t, _f], fragment("? !~ ?", t.title, "NSFW"))
2019-10-04 18:48:00 +02:00
|> where([_t, f], f.access_level == "normal")
|> order_by(desc: :last_replied_to_at)
|> preload([:forum, last_post: :user])
|> limit(6)
2019-10-05 00:52:44 +02:00
|> Repo.all()
2019-10-04 18:48:00 +02:00
render(
conn,
"index.html",
images: images,
comments: comments,
2019-10-04 18:48:00 +02:00
top_scoring: top_scoring,
watched: watched,
featured_image: featured_image,
2019-10-05 00:52:44 +02:00
streams: streams,
2019-10-04 18:48:00 +02:00
topics: topics
)
end
end