philomena/lib/philomena_web/controllers/activity_controller.ex

174 lines
4.4 KiB
Elixir
Raw Normal View History

2019-10-04 18:48:00 +02:00
defmodule PhilomenaWeb.ActivityController do
use PhilomenaWeb, :controller
2019-12-01 03:22:05 +01:00
alias PhilomenaWeb.ImageLoader
alias Philomena.Elasticsearch
2020-01-11 05:20:19 +01:00
alias Philomena.{
Images.Image,
ImageFeatures.ImageFeature,
Comments.Comment,
Channels.Channel,
Topics.Topic,
Forums.Forum
}
2019-11-17 03:20:33 +01:00
alias Philomena.Interactions
2019-10-04 18:48:00 +02:00
alias Philomena.Repo
import Ecto.Query
def index(conn, _params) do
user = conn.assigns.current_user
2019-12-05 22:57:59 +01:00
{:ok, {images, _tags}} =
2019-12-01 03:22:05 +01:00
ImageLoader.search_string(
conn,
"created_at.lte:3 minutes ago, thumbnails_generated:true",
2019-12-01 03:22:05 +01:00
pagination: %{conn.assigns.image_pagination | page_number: 1}
2019-10-04 18:48:00 +02:00
)
2019-12-05 22:57:59 +01:00
{top_scoring, _tags} =
2019-12-01 03:22:05 +01:00
ImageLoader.query(
conn,
%{range: %{first_seen_at: %{gt: "now-3d"}}},
2020-05-29 01:43:17 +02:00
sorts: &%{query: &1, sorts: [%{wilson_score: :desc}, %{first_seen_at: :desc}]},
2019-12-01 03:22:05 +01:00
pagination: %{page_number: :rand.uniform(6), page_size: 4}
2019-10-04 18:48:00 +02:00
)
comments =
Elasticsearch.search_definition(
Comment,
%{
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}
},
%{page_number: 1, page_size: 6}
)
2020-01-11 05:20:19 +01:00
watched =
if !!user do
{:ok, {watched_images, _tags}} =
ImageLoader.search_string(
conn,
"my:watched",
pagination: %{conn.assigns.image_pagination | page_number: 1}
)
2019-10-04 18:48:00 +02:00
watched_images
2020-01-11 05:20:19 +01:00
end
2019-10-04 18:48:00 +02:00
[images, top_scoring, comments, watched] =
multi_search(images, top_scoring, comments, watched)
2019-10-04 18:48:00 +02:00
featured_image =
Image
2019-11-17 03:20:33 +01:00
|> join(:inner, [i], f in ImageFeature, on: [image_id: i.id])
|> where([i], i.hidden_from_users == false)
|> filter_hidden(user, conn.params["hidden"])
2019-10-04 18:48:00 +02:00
|> order_by([i, f], desc: f.created_at)
|> limit(1)
2020-12-06 16:08:13 +01:00
|> preload(tags: :aliases)
2019-10-04 18:48:00 +02:00
|> Repo.one()
2019-10-05 00:52:44 +02:00
streams =
Channel
2020-01-11 05:20:19 +01:00
|> where([c], c.nsfw == false)
2019-10-05 00:52:44 +02:00
|> 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
2019-11-17 03:20:33 +01:00
interactions =
Interactions.user_interactions(
[images, top_scoring, watched, featured_image],
user
)
2019-10-04 18:48:00 +02:00
render(
conn,
"index.html",
2019-12-16 20:24:38 +01:00
title: "Homepage",
2019-10-04 18:48:00 +02:00
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-11-17 03:20:33 +01:00
topics: topics,
2019-11-17 23:51:14 +01:00
interactions: interactions,
layout_class: "layout--wide",
2021-02-09 23:28:24 +01:00
show_sidebar: show_sidebar?(user)
2019-10-04 18:48:00 +02:00
)
end
2020-02-01 17:04:11 +01:00
def filter_hidden(featured_image, nil, _hidden) do
featured_image
end
def filter_hidden(featured_image, _user, "1") do
featured_image
end
def filter_hidden(featured_image, user, _hidden) do
featured_image
2020-02-01 17:04:11 +01:00
|> where(
[i],
fragment(
"NOT EXISTS(SELECT 1 FROM image_hides WHERE image_id = ? AND user_id = ?)",
i.id,
^user.id
)
)
end
defp multi_search(images, top_scoring, comments, nil) do
responses =
Elasticsearch.msearch_records(
[images, top_scoring, comments],
2020-12-06 16:08:13 +01:00
[
preload(Image, tags: :aliases),
preload(Image, tags: :aliases),
preload(Comment, [:user, image: [tags: :aliases]])
]
)
responses ++ [nil]
end
defp multi_search(images, top_scoring, comments, watched) do
Elasticsearch.msearch_records(
[images, top_scoring, comments, watched],
[
preload(Image, tags: :aliases),
preload(Image, tags: :aliases),
preload(Comment, [:user, image: [tags: :aliases]]),
preload(Image, tags: :aliases)
]
)
end
defp show_sidebar?(%{show_sidebar_and_watched_images: false}), do: false
defp show_sidebar?(_user), do: true
2019-10-04 18:48:00 +02:00
end