diff --git a/lib/philomena/comments/comment.ex b/lib/philomena/comments/comment.ex index 231f6808..e4398de6 100644 --- a/lib/philomena/comments/comment.ex +++ b/lib/philomena/comments/comment.ex @@ -2,6 +2,11 @@ defmodule Philomena.Comments.Comment do use Ecto.Schema import Ecto.Changeset + use Philomena.Elasticsearch, + definition: Philomena.Comments.Elasticsearch, + index_name: "comments", + doc_type: "comment" + schema "comments" do belongs_to :user, Philomena.Users.User belongs_to :image, Philomena.Images.Image diff --git a/lib/philomena/comments/elasticsearch.ex b/lib/philomena/comments/elasticsearch.ex new file mode 100644 index 00000000..2c6dad43 --- /dev/null +++ b/lib/philomena/comments/elasticsearch.ex @@ -0,0 +1,50 @@ +defmodule Philomena.Comments.Elasticsearch do + def mapping do + %{ + settings: %{ + index: %{ + number_of_shards: 5, + max_result_window: 10_000_000 + } + }, + mappings: %{ + comment: %{ + _all: %{enabled: false}, + dynamic: false, + properties: %{ + id: %{type: "integer"}, + posted_at: %{type: "date"}, + ip: %{type: "ip"}, + fingerprint: %{type: "keyword"}, + image_id: %{type: "keyword"}, + user_id: %{type: "keyword"}, + author: %{type: "keyword"}, + image_tag_ids: %{type: "keyword"}, + anonymous: %{type: "keyword"}, # boolean + hidden_from_users: %{type: "keyword"}, # boolean + body: %{type: "text", analyzer: "snowball"} + } + } + } + } + end + + # preload([ + # :user, image: :tags + # ]) + def as_json(comment) do + %{ + id: comment.id, + posted_at: comment.created_at, + ip: comment.ip |> to_string, + fingerprint: comment.fingerprint, + image_id: comment.image_id, + user_id: comment.user_id, + author: if(!!comment.user and !comment.anonymous, do: comment.user.name), + image_tag_ids: comment.image.tags |> Enum.map(& &1.id), + anonymous: comment.anonymous, + hidden_from_users: comment.hidden_from_users, + body: comment.body + } + end +end diff --git a/lib/philomena_web/controllers/activity_controller.ex b/lib/philomena_web/controllers/activity_controller.ex index 93e9f7ad..8981f4b5 100644 --- a/lib/philomena_web/controllers/activity_controller.ex +++ b/lib/philomena_web/controllers/activity_controller.ex @@ -1,7 +1,7 @@ defmodule PhilomenaWeb.ActivityController do use PhilomenaWeb, :controller - alias Philomena.{Images, Images.Image, Images.Feature, Channels.Channel, Topics.Topic, Forums.Forum} + alias Philomena.{Images, Images.Image, Images.Feature, Comments.Comment, Channels.Channel, Topics.Topic, Forums.Forum} alias Philomena.Repo import Ecto.Query @@ -43,6 +43,26 @@ defmodule PhilomenaWeb.ActivityController do 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}} + ] + } + }, + size: 6, + sort: %{posted_at: :desc} + }, + Comment |> preload([:user, :image]) + ) + watched = if !!user do {:ok, watched_query} = Images.Query.compile(user, "my:watched") @@ -91,6 +111,7 @@ defmodule PhilomenaWeb.ActivityController do conn, "index.html", images: images, + comments: comments, top_scoring: top_scoring, watched: watched, featured_image: featured_image, diff --git a/lib/philomena_web/templates/activity/_comment_strip.html.slime b/lib/philomena_web/templates/activity/_comment_strip.html.slime new file mode 100644 index 00000000..01fadb14 --- /dev/null +++ b/lib/philomena_web/templates/activity/_comment_strip.html.slime @@ -0,0 +1,12 @@ +.block__content.flex.alternating-color + .flex__shrink.spacing-right + = render PhilomenaWeb.ImageView, "_image_container.html", image: @comment.image, size: :thumb_tiny + .flex__grow + a href="/#{@comment.image.id}#comment_#{@comment.id}" + | # + => @comment.image.id + ' by + span.hyphenate-breaks + = render PhilomenaWeb.UserAttributionView, "_anon_user.html", object: @comment + br + = pretty_time(@comment.created_at) diff --git a/lib/philomena_web/templates/activity/index.html.slime b/lib/philomena_web/templates/activity/index.html.slime index 0276f7c5..e38a6f97 100644 --- a/lib/philomena_web/templates/activity/index.html.slime +++ b/lib/philomena_web/templates/activity/index.html.slime @@ -12,6 +12,8 @@ | Issues? Want to chat? a< href="/pages/contact" Contact us! .block.hide-mobile + a.block__header--single-item.center href="/search?q=first_seen_at.gt:3 days ago&sf=score&sd=desc" + ' Trending Images .block__content.flex.flex--centered.flex--wrap.image-flex-grid = for image <- @top_scoring do = render PhilomenaWeb.ImageView, "_image_box.html", image: image, size: :thumb_small @@ -30,8 +32,10 @@ .block.hide-mobile a.block__header--single-item.center href="/lists/recent_comments" ' Recent Comments - /= render partial: 'comments/comment_activitypage', collection: @comments - /a.block__header--single-item.center href=search_path(q: 'first_seen_at.gt:3 days ago', sf: 'comments', sd: 'desc') Most Commented-on Images + = for comment <- @comments do + = render PhilomenaWeb.ActivityView, "_comment_strip.html", comment: comment + a.block__header--single-item.center href="/search?q=first_seen_at.gt:3 days ago&sf=comments&sd=desc" + ' Most Commented-on Images .column-layout__main = render PhilomenaWeb.ImageView, "index.html", images: @images, size: :thumb