Added link/PreviewCard search to search
• Added: - link/PreviewCard search to search
This commit is contained in:
parent
60f75ed676
commit
7e673d4e7f
@ -4,6 +4,8 @@ export const LINK_FETCH_REQUEST = 'LINK_FETCH_REQUEST'
|
|||||||
export const LINK_FETCH_SUCCESS = 'LINK_FETCH_SUCCESS'
|
export const LINK_FETCH_SUCCESS = 'LINK_FETCH_SUCCESS'
|
||||||
export const LINK_FETCH_FAIL = 'LINK_FETCH_FAIL'
|
export const LINK_FETCH_FAIL = 'LINK_FETCH_FAIL'
|
||||||
|
|
||||||
|
export const IMPORT_LINK_CARDS = 'IMPORT_LINK_CARDS'
|
||||||
|
|
||||||
export const fetchLinkCard = (cardId) => (dispatch, getState) => {
|
export const fetchLinkCard = (cardId) => (dispatch, getState) => {
|
||||||
dispatch(fetchLinkCardRequest(cardId))
|
dispatch(fetchLinkCardRequest(cardId))
|
||||||
|
|
||||||
@ -28,3 +30,8 @@ export const fetchLinkCardFail = (error, cardId) => ({
|
|||||||
error,
|
error,
|
||||||
cardId,
|
cardId,
|
||||||
})
|
})
|
||||||
|
|
||||||
|
export const importLinkCards = (cards) => ({
|
||||||
|
type: IMPORT_LINK_CARDS,
|
||||||
|
cards,
|
||||||
|
})
|
@ -1,7 +1,11 @@
|
|||||||
import api from '../api';
|
import api from '../api';
|
||||||
import { fetchRelationships } from './accounts';
|
import { fetchRelationships } from './accounts';
|
||||||
import { fetchGroupsSuccess, fetchGroupRelationships } from './groups'
|
import { fetchGroupsSuccess, fetchGroupRelationships } from './groups'
|
||||||
import { importFetchedAccounts, importFetchedStatuses } from './importer';
|
import {
|
||||||
|
importFetchedAccounts,
|
||||||
|
importFetchedStatuses,
|
||||||
|
} from './importer';
|
||||||
|
import { importLinkCards } from './links'
|
||||||
import { SEARCH_FILTERS } from '../constants'
|
import { SEARCH_FILTERS } from '../constants'
|
||||||
|
|
||||||
export const SEARCH_CHANGE = 'SEARCH_CHANGE';
|
export const SEARCH_CHANGE = 'SEARCH_CHANGE';
|
||||||
@ -52,6 +56,10 @@ export function submitSearch() {
|
|||||||
dispatch(importFetchedStatuses(response.data.statuses));
|
dispatch(importFetchedStatuses(response.data.statuses));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (response.data.links) {
|
||||||
|
dispatch(importLinkCards(response.data.links));
|
||||||
|
}
|
||||||
|
|
||||||
if (response.data.groups) {
|
if (response.data.groups) {
|
||||||
dispatch(fetchGroupsSuccess(response.data.groups))
|
dispatch(fetchGroupsSuccess(response.data.groups))
|
||||||
dispatch(fetchGroupRelationships(response.data.groups.map(item => item.id)))
|
dispatch(fetchGroupRelationships(response.data.groups.map(item => item.id)))
|
||||||
|
@ -7,6 +7,7 @@ import {
|
|||||||
LINK_FETCH_REQUEST,
|
LINK_FETCH_REQUEST,
|
||||||
LINK_FETCH_SUCCESS,
|
LINK_FETCH_SUCCESS,
|
||||||
LINK_FETCH_FAIL,
|
LINK_FETCH_FAIL,
|
||||||
|
IMPORT_LINK_CARDS,
|
||||||
} from '../actions/links'
|
} from '../actions/links'
|
||||||
|
|
||||||
const initialState = ImmutableMap({
|
const initialState = ImmutableMap({
|
||||||
@ -27,6 +28,13 @@ export default function links(state = initialState, action) {
|
|||||||
mutable.set('isFetched', false)
|
mutable.set('isFetched', false)
|
||||||
mutable.set('isError', false)
|
mutable.set('isError', false)
|
||||||
})
|
})
|
||||||
|
case IMPORT_LINK_CARDS:
|
||||||
|
return state.withMutations((mutable) => {
|
||||||
|
action.cards.forEach((card) => mutable.setIn(['items', `${card.id}`], fromJS(card)))
|
||||||
|
mutable.set('isLoading', false)
|
||||||
|
mutable.set('isFetched', false)
|
||||||
|
mutable.set('isError', false)
|
||||||
|
})
|
||||||
case LINK_FETCH_FAIL:
|
case LINK_FETCH_FAIL:
|
||||||
return state.withMutations((mutable) => {
|
return state.withMutations((mutable) => {
|
||||||
mutable.set('isLoading', false)
|
mutable.set('isLoading', false)
|
||||||
|
@ -60,6 +60,7 @@ export default function search(state = initialState, action) {
|
|||||||
return state.set('results', ImmutableMap({
|
return state.set('results', ImmutableMap({
|
||||||
accounts: ImmutableList(action.results.accounts.map(item => item.id)),
|
accounts: ImmutableList(action.results.accounts.map(item => item.id)),
|
||||||
statuses: ImmutableList(action.results.statuses.map(item => item.id)),
|
statuses: ImmutableList(action.results.statuses.map(item => item.id)),
|
||||||
|
links: ImmutableList(action.results.links.map(item => item.id)),
|
||||||
hashtags: fromJS(action.results.hashtags),
|
hashtags: fromJS(action.results.hashtags),
|
||||||
groups: fromJS(action.results.groups),
|
groups: fromJS(action.results.groups),
|
||||||
})).set('submitted', true).set('isLoading', false).set('isError', false);
|
})).set('submitted', true).set('isLoading', false).set('isError', false);
|
||||||
|
@ -57,6 +57,14 @@ class PreviewCard < ApplicationRecord
|
|||||||
end
|
end
|
||||||
|
|
||||||
class << self
|
class << self
|
||||||
|
def search_for(term, limit = 100, offset = 0)
|
||||||
|
pattern = '%' + sanitize_sql_like(term.strip) + '%'
|
||||||
|
|
||||||
|
PreviewCard.where(
|
||||||
|
"lower(title) LIKE lower('#{pattern}') OR lower(description) LIKE lower('#{pattern}') OR lower(url) LIKE lower('#{pattern}')"
|
||||||
|
).order('updated_at DESC').limit(limit).offset(offset)
|
||||||
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
||||||
def image_styles(f)
|
def image_styles(f)
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
# frozen_string_literal: true
|
# frozen_string_literal: true
|
||||||
|
|
||||||
class Search < ActiveModelSerializers::Model
|
class Search < ActiveModelSerializers::Model
|
||||||
attributes :accounts, :statuses, :hashtags, :groups
|
attributes :accounts, :statuses, :hashtags, :groups, :links
|
||||||
end
|
end
|
||||||
|
@ -6,6 +6,7 @@ class REST::SearchSerializer < ActiveModel::Serializer
|
|||||||
has_many :accounts, serializer: REST::AccountSerializer
|
has_many :accounts, serializer: REST::AccountSerializer
|
||||||
has_many :statuses, serializer: REST::StatusSerializer
|
has_many :statuses, serializer: REST::StatusSerializer
|
||||||
has_many :groups, serializer: REST::GroupSerializer
|
has_many :groups, serializer: REST::GroupSerializer
|
||||||
|
has_many :links, serializer: REST::PreviewCardSerializer
|
||||||
|
|
||||||
def hashtags
|
def hashtags
|
||||||
object.hashtags.map(&:name)
|
object.hashtags.map(&:name)
|
||||||
|
@ -5,4 +5,5 @@ class REST::V2::SearchSerializer < ActiveModel::Serializer
|
|||||||
has_many :statuses, serializer: REST::StatusSerializer
|
has_many :statuses, serializer: REST::StatusSerializer
|
||||||
has_many :hashtags, serializer: REST::TagSerializer
|
has_many :hashtags, serializer: REST::TagSerializer
|
||||||
has_many :groups, serializer: REST::GroupSerializer
|
has_many :groups, serializer: REST::GroupSerializer
|
||||||
|
has_many :links, serializer: REST::PreviewCardSerializer
|
||||||
end
|
end
|
||||||
|
@ -19,6 +19,7 @@ class SearchService < BaseService
|
|||||||
results[:accounts] = perform_accounts_search! if account_searchable?
|
results[:accounts] = perform_accounts_search! if account_searchable?
|
||||||
results[:statuses] = perform_statuses_search! if full_text_searchable? && !account.nil?
|
results[:statuses] = perform_statuses_search! if full_text_searchable? && !account.nil?
|
||||||
results[:hashtags] = perform_hashtags_search! if hashtag_searchable? && !account.nil?
|
results[:hashtags] = perform_hashtags_search! if hashtag_searchable? && !account.nil?
|
||||||
|
results[:links] = perform_links_search! if !account.nil?
|
||||||
results[:groups] = perform_groups_search!
|
results[:groups] = perform_groups_search!
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
@ -45,6 +46,14 @@ class SearchService < BaseService
|
|||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
def perform_links_search!
|
||||||
|
PreviewCard.search_for(
|
||||||
|
@query.gsub(/\A#/, ''),
|
||||||
|
@limit,
|
||||||
|
@offset
|
||||||
|
)
|
||||||
|
end
|
||||||
|
|
||||||
def perform_statuses_search!
|
def perform_statuses_search!
|
||||||
definition = StatusesIndex.filter(term: { searchable_by: @account.id })
|
definition = StatusesIndex.filter(term: { searchable_by: @account.id })
|
||||||
.query(multi_match: { type: 'most_fields', query: @query, operator: 'and', fields: %w(text text.stemmed) })
|
.query(multi_match: { type: 'most_fields', query: @query, operator: 'and', fields: %w(text text.stemmed) })
|
||||||
@ -79,7 +88,7 @@ class SearchService < BaseService
|
|||||||
end
|
end
|
||||||
|
|
||||||
def default_results
|
def default_results
|
||||||
{ accounts: [], hashtags: [], statuses: [] }
|
{ accounts: [], hashtags: [], statuses: [], links: [], groups: [] }
|
||||||
end
|
end
|
||||||
|
|
||||||
def url_query?
|
def url_query?
|
||||||
|
Loading…
x
Reference in New Issue
Block a user