Return some BoR functionality to forums (#115)

* return some BoR functionality to forums

- Added searching by forum slug
- Display forum/topic/post-num in search results for each post
- Display who locked topic for moderators

* fix topic position bug (Fixes philomena-dev/philomena#110)
This commit is contained in:
VcSaJen 2021-05-10 02:21:45 +09:00 committed by GitHub
parent ecd29e0e0d
commit 269caabd5e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
10 changed files with 63 additions and 9 deletions

View file

@ -24,6 +24,7 @@ metadata: post_search_json
'referrer', p.referrer,
'fingerprint', p.fingerprint,
'topic_position', p.topic_position,
'forum', f.short_name,
'forum_id', t.forum_id,
'user_id', p.user_id,
'anonymous', p.anonymous,

View file

@ -27,6 +27,7 @@ defmodule Philomena.Posts.ElasticsearchIndex do
subject: %{type: "text", analyzer: "snowball"},
author: %{type: "keyword"},
topic_position: %{type: "integer"},
forum: %{type: "keyword"},
forum_id: %{type: "keyword"},
topic_id: %{type: "keyword"},
user_id: %{type: "keyword"},
@ -54,6 +55,7 @@ defmodule Philomena.Posts.ElasticsearchIndex do
referrer: post.referrer,
fingerprint: post.fingerprint,
topic_position: post.topic_position,
forum: post.topic.forum.short_name,
forum_id: post.topic.forum_id,
user_id: post.user_id,
anonymous: post.anonymous,

View file

@ -52,7 +52,7 @@ defmodule Philomena.Posts.Query do
[
int_fields: ~W(id topic_position),
date_fields: ~W(created_at updated_at),
literal_fields: ~W(forum_id topic_id),
literal_fields: ~W(forum forum_id topic_id),
ngram_fields: ~W(body subject),
custom_fields: ~W(author user_id),
default_field: {"body", :ngram},
@ -76,7 +76,7 @@ defmodule Philomena.Posts.Query do
fields = user_fields()
Keyword.merge(fields,
literal_fields: ~W(forum_id topic_id user_id author fingerprint),
literal_fields: fields[:literal_fields] ++ ~W(user_id author fingerprint),
ip_fields: ~W(ip),
bool_fields: ~W(anonymous deleted),
custom_fields: fields[:custom_fields] -- ~W(author user_id),

View file

@ -15,7 +15,7 @@ defmodule PhilomenaWeb.LoadTopicPlug do
Topic
|> where(forum_id: ^forum.id, slug: ^to_string(conn.params[param]))
|> preload([:user, :forum, :deleted_by, poll: :options])
|> preload([:user, :forum, :deleted_by, :locked_by, poll: :options])
|> Repo.one()
|> maybe_hide_topic(conn, show_hidden)
end

View file

@ -1,7 +1,9 @@
h1 Discussion Forums
.block
.block__header
/= icon_link 'Search Posts', 'fa fa-fw fa-search', posts_path
a href=Routes.post_path(@conn, :index)
i.fa.fa-fw.fa-search>
' Search Posts
span.block__header__item
=> @topic_count
' topics

View file

@ -10,7 +10,9 @@ h1 = @forum.name
i.fa.fa-fw.fa-edit>
' New Topic
/= icon_link 'Search Posts', 'fa fa-fw fa-search', posts_path(forum_id: @forum.id)
a href=Routes.post_path(@conn, :index, pq: "forum:#{@forum.short_name}")
i.fa.fa-fw.fa-search>
' Search Posts
span.spacing-left
=> @forum.topic_count
' topics

View file

@ -18,7 +18,18 @@ h2 Search Results
- pagination = render PhilomenaWeb.PaginationView, "_pagination.html", page: @posts, route: route, params: [pq: @conn.params["pq"]], conn: @conn
= for {body, post} <- @posts, post.topic.hidden_from_users == false do
= render PhilomenaWeb.PostView, "_post.html", body: body, post: post, conn: @conn
div
h3
=<> link post.topic.forum.name, to: Routes.forum_path(@conn, :show, post.topic.forum)
| &raquo;
=<> link post.topic.title, to: Routes.forum_topic_path(@conn, :show, post.topic.forum, post.topic)
| &raquo;
- post_link = Routes.forum_topic_path(@conn, :show, post.topic.forum, post.topic, post_id: post.id) <> "#post_#{post.id}"
= if post.topic_position == 0 do
=<> link "Topic Opener", to: post_link
- else
=<> link "Post #{post.topic_position}", to: post_link
= render PhilomenaWeb.PostView, "_post.html", body: body, post: post, conn: @conn
.block
.block__header.block__header--light.page__header
@ -134,3 +145,10 @@ table.table
td Matches posts with the specified user_id. Anonymous users will never match this term.
td
code = link "user_id:211190", to: Routes.post_path(@conn, :index, pq: "user_id:211190")
tr
td
code forum
td Literal
td Matches the short name for the forum this post belongs to.
td
code = link "forum:meta", to: Routes.post_path(@conn, :index, pq: "forum:meta")

View file

@ -81,6 +81,13 @@ h1 = @topic.title
' Lock reason:
em = @topic.lock_reason
= if can?(@conn, :hide, @topic) and not is_nil(@topic.locked_by) do
p
' Locked by:
em
strong
= link(@topic.locked_by.name, to: Routes.profile_path(@conn, :show, @topic.locked_by))
/ Post form
= cond do
- @conn.assigns.current_ban ->

View file

@ -0,0 +1,21 @@
defmodule Philomena.Repo.Migrations.AddIndexPostsOnTopicIdAndTopicPosition do
use Ecto.Migration
def up do
execute("drop index index_posts_on_topic_id_and_topic_position;");
execute(
"""
with mismatched_ranks as (select id,rank from (select id, topic_position, (rank() over (partition by topic_id order by created_at,id asc))-1 as rank from posts) s where s.topic_position <> s.rank)
update posts set topic_position=mismatched_ranks.rank
from mismatched_ranks
where posts.id=mismatched_ranks.id;
""");
execute("create unique index index_posts_on_topic_id_and_topic_position on posts (topic_id, topic_position);");
end
def down do
execute("drop index index_posts_on_topic_id_and_topic_position;");
execute("create index index_posts_on_topic_id_and_topic_position on public.posts (topic_id, topic_position);");
end
end

View file

@ -2,8 +2,8 @@
-- PostgreSQL database dump
--
-- Dumped from database version 13.1 (Debian 13.1-1.pgdg100+1)
-- Dumped by pg_dump version 13.1 (Debian 13.1-1.pgdg100+1)
-- Dumped from database version 13.2
-- Dumped by pg_dump version 13.2
SET statement_timeout = 0;
SET lock_timeout = 0;
@ -3503,7 +3503,7 @@ CREATE INDEX index_posts_on_topic_id_and_created_at ON public.posts USING btree
-- Name: index_posts_on_topic_id_and_topic_position; Type: INDEX; Schema: public; Owner: -
--
CREATE INDEX index_posts_on_topic_id_and_topic_position ON public.posts USING btree (topic_id, topic_position);
CREATE UNIQUE INDEX index_posts_on_topic_id_and_topic_position ON public.posts USING btree (topic_id, topic_position);
--
@ -4843,3 +4843,4 @@ INSERT INTO public."schema_migrations" (version) VALUES (20200905214139);
INSERT INTO public."schema_migrations" (version) VALUES (20201124224116);
INSERT INTO public."schema_migrations" (version) VALUES (20210121200815);
INSERT INTO public."schema_migrations" (version) VALUES (20210301012137);
INSERT INTO public."schema_migrations" (version) VALUES (20210427022351);