mirror of
https://github.com/discourse/discourse.git
synced 2025-01-30 03:21:02 +08:00
FEATURE: displays groups in menu search (#7090)
This commit is contained in:
parent
075b264338
commit
dc4001370c
|
@ -16,6 +16,7 @@ export function translateResults(results, opts) {
|
||||||
results.posts = results.posts || [];
|
results.posts = results.posts || [];
|
||||||
results.categories = results.categories || [];
|
results.categories = results.categories || [];
|
||||||
results.tags = results.tags || [];
|
results.tags = results.tags || [];
|
||||||
|
results.groups = results.groups || [];
|
||||||
|
|
||||||
const topicMap = {};
|
const topicMap = {};
|
||||||
results.topics = results.topics.map(function(topic) {
|
results.topics = results.topics.map(function(topic) {
|
||||||
|
@ -43,6 +44,17 @@ export function translateResults(results, opts) {
|
||||||
})
|
})
|
||||||
.compact();
|
.compact();
|
||||||
|
|
||||||
|
results.groups = results.groups
|
||||||
|
.map(group => {
|
||||||
|
const groupName = Handlebars.Utils.escapeExpression(group.name);
|
||||||
|
return {
|
||||||
|
id: group.id,
|
||||||
|
name: groupName,
|
||||||
|
url: Discourse.getURL(`/g/${groupName}`)
|
||||||
|
};
|
||||||
|
})
|
||||||
|
.compact();
|
||||||
|
|
||||||
results.tags = results.tags
|
results.tags = results.tags
|
||||||
.map(function(tag) {
|
.map(function(tag) {
|
||||||
const tagName = Handlebars.Utils.escapeExpression(tag.name);
|
const tagName = Handlebars.Utils.escapeExpression(tag.name);
|
||||||
|
@ -62,7 +74,8 @@ export function translateResults(results, opts) {
|
||||||
["topic", "posts"],
|
["topic", "posts"],
|
||||||
["category", "categories"],
|
["category", "categories"],
|
||||||
["tag", "tags"],
|
["tag", "tags"],
|
||||||
["user", "users"]
|
["user", "users"],
|
||||||
|
["group", "groups"]
|
||||||
].forEach(function(pair) {
|
].forEach(function(pair) {
|
||||||
const type = pair[0];
|
const type = pair[0];
|
||||||
const name = pair[1];
|
const name = pair[1];
|
||||||
|
|
|
@ -5,6 +5,7 @@ import { createWidget } from "discourse/widgets/widget";
|
||||||
import { h } from "virtual-dom";
|
import { h } from "virtual-dom";
|
||||||
import highlightText from "discourse/lib/highlight-text";
|
import highlightText from "discourse/lib/highlight-text";
|
||||||
import { escapeExpression, formatUsername } from "discourse/lib/utilities";
|
import { escapeExpression, formatUsername } from "discourse/lib/utilities";
|
||||||
|
import { iconNode } from "discourse-common/lib/icon-library";
|
||||||
|
|
||||||
class Highlighted extends RawHtml {
|
class Highlighted extends RawHtml {
|
||||||
constructor(html, term) {
|
constructor(html, term) {
|
||||||
|
@ -26,15 +27,15 @@ function createSearchResult({ type, linkField, builder }) {
|
||||||
let searchResultId;
|
let searchResultId;
|
||||||
|
|
||||||
if (type === "topic") {
|
if (type === "topic") {
|
||||||
searchResultId = r.get("topic_id");
|
searchResultId = r.topic_id;
|
||||||
} else {
|
} else {
|
||||||
searchResultId = r.get("id");
|
searchResultId = r.id;
|
||||||
}
|
}
|
||||||
|
|
||||||
return h(
|
return h(
|
||||||
"li.item",
|
"li.item",
|
||||||
this.attach("link", {
|
this.attach("link", {
|
||||||
href: r.get(linkField),
|
href: r[linkField],
|
||||||
contents: () => builder.call(this, r, attrs.term),
|
contents: () => builder.call(this, r, attrs.term),
|
||||||
className: "search-link",
|
className: "search-link",
|
||||||
searchResultId,
|
searchResultId,
|
||||||
|
@ -68,12 +69,11 @@ createSearchResult({
|
||||||
type: "tag",
|
type: "tag",
|
||||||
linkField: "url",
|
linkField: "url",
|
||||||
builder(t) {
|
builder(t) {
|
||||||
const tag = escapeExpression(t.get("id"));
|
const tag = escapeExpression(t.id);
|
||||||
return h(
|
return h(
|
||||||
"a",
|
"span",
|
||||||
{
|
{
|
||||||
attributes: { href: t.get("url") },
|
className: `tag-${tag} discourse-tag ${
|
||||||
className: `widget-link search-link tag-${tag} discourse-tag ${
|
|
||||||
Discourse.SiteSettings.tag_style
|
Discourse.SiteSettings.tag_style
|
||||||
}`
|
}`
|
||||||
},
|
},
|
||||||
|
@ -112,6 +112,21 @@ createSearchResult({
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
createSearchResult({
|
||||||
|
type: "group",
|
||||||
|
linkField: "url",
|
||||||
|
builder(group) {
|
||||||
|
const groupName = escapeExpression(group.name);
|
||||||
|
return h(
|
||||||
|
"span",
|
||||||
|
{
|
||||||
|
className: `group-${groupName} discourse-group`
|
||||||
|
},
|
||||||
|
[iconNode("users"), h("span", groupName)]
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
|
||||||
createSearchResult({
|
createSearchResult({
|
||||||
type: "topic",
|
type: "topic",
|
||||||
linkField: "url",
|
linkField: "url",
|
||||||
|
|
|
@ -38,6 +38,8 @@
|
||||||
flex-direction: row;
|
flex-direction: row;
|
||||||
|
|
||||||
.list {
|
.list {
|
||||||
|
min-width: 100px;
|
||||||
|
|
||||||
.item {
|
.item {
|
||||||
.blurb {
|
.blurb {
|
||||||
// https://css-tricks.com/snippets/css/prevent-long-urls-from-breaking-out-of-container/
|
// https://css-tricks.com/snippets/css/prevent-long-urls-from-breaking-out-of-container/
|
||||||
|
@ -97,6 +99,25 @@
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.search-result-group {
|
||||||
|
.search-link {
|
||||||
|
color: $primary-high;
|
||||||
|
|
||||||
|
&:hover {
|
||||||
|
color: $primary;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
.discourse-group {
|
||||||
|
display: inline-block;
|
||||||
|
word-break: break-all;
|
||||||
|
|
||||||
|
.d-icon {
|
||||||
|
margin-right: s(1);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
.search-result-user {
|
.search-result-user {
|
||||||
.user-result {
|
.user-result {
|
||||||
display: flex;
|
display: flex;
|
||||||
|
|
|
@ -3,6 +3,7 @@ class GroupedSearchResultSerializer < ApplicationSerializer
|
||||||
has_many :users, serializer: SearchResultUserSerializer
|
has_many :users, serializer: SearchResultUserSerializer
|
||||||
has_many :categories, serializer: BasicCategorySerializer
|
has_many :categories, serializer: BasicCategorySerializer
|
||||||
has_many :tags, serializer: TagSerializer
|
has_many :tags, serializer: TagSerializer
|
||||||
|
has_many :groups, serializer: BasicGroupSerializer
|
||||||
attributes :more_posts, :more_users, :more_categories, :term, :search_log_id, :more_full_page_results, :can_create_topic
|
attributes :more_posts, :more_users, :more_categories, :term, :search_log_id, :more_full_page_results, :can_create_topic
|
||||||
|
|
||||||
def search_log_id
|
def search_log_id
|
||||||
|
|
|
@ -598,6 +598,7 @@ class Search
|
||||||
user_search if @term.present?
|
user_search if @term.present?
|
||||||
category_search if @term.present?
|
category_search if @term.present?
|
||||||
tags_search if @term.present?
|
tags_search if @term.present?
|
||||||
|
groups_search if @term.present?
|
||||||
end
|
end
|
||||||
topic_search
|
topic_search
|
||||||
end
|
end
|
||||||
|
@ -683,6 +684,14 @@ class Search
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def groups_search
|
||||||
|
groups = Group
|
||||||
|
.visible_groups(@guardian.user, "name ASC", include_everyone: false)
|
||||||
|
.where("groups.name ILIKE ?", "%#{@term}%")
|
||||||
|
|
||||||
|
groups.each { |group| @results.add(group) }
|
||||||
|
end
|
||||||
|
|
||||||
def tags_search
|
def tags_search
|
||||||
return unless SiteSetting.tagging_enabled
|
return unless SiteSetting.tagging_enabled
|
||||||
|
|
||||||
|
|
|
@ -15,6 +15,7 @@ class Search
|
||||||
:categories,
|
:categories,
|
||||||
:users,
|
:users,
|
||||||
:tags,
|
:tags,
|
||||||
|
:groups,
|
||||||
:more_posts,
|
:more_posts,
|
||||||
:more_categories,
|
:more_categories,
|
||||||
:more_users,
|
:more_users,
|
||||||
|
@ -36,6 +37,7 @@ class Search
|
||||||
@categories = []
|
@categories = []
|
||||||
@users = []
|
@users = []
|
||||||
@tags = []
|
@tags = []
|
||||||
|
@groups = []
|
||||||
end
|
end
|
||||||
|
|
||||||
def find_user_data(guardian)
|
def find_user_data(guardian)
|
||||||
|
|
|
@ -430,6 +430,38 @@ describe Search do
|
||||||
|
|
||||||
end
|
end
|
||||||
|
|
||||||
|
context 'groups' do
|
||||||
|
def search(user = Fabricate(:user))
|
||||||
|
Search.execute(group.name, guardian: Guardian.new(user))
|
||||||
|
end
|
||||||
|
|
||||||
|
let!(:group) { Group[:trust_level_0] }
|
||||||
|
|
||||||
|
it 'shows group' do
|
||||||
|
expect(search.groups.map(&:name)).to eq([group.name])
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'group visibility' do
|
||||||
|
let!(:group) { Fabricate(:group) }
|
||||||
|
|
||||||
|
before do
|
||||||
|
group.update!(visibility_level: 3)
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'staff logged in' do
|
||||||
|
it 'shows group' do
|
||||||
|
expect(search(Fabricate(:admin)).groups.map(&:name)).to eq([group.name])
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
context 'non staff logged in' do
|
||||||
|
it 'shows doesn’t show group' do
|
||||||
|
expect(search.groups.map(&:name)).to be_empty
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
context 'tags' do
|
context 'tags' do
|
||||||
def search
|
def search
|
||||||
Search.execute(tag.name)
|
Search.execute(tag.name)
|
||||||
|
|
Loading…
Reference in New Issue
Block a user