diff options
| author | JP Dillingham <jpdillingham@gmail.com> | 2021-06-08 07:13:16 -0500 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2021-06-08 07:13:16 -0500 |
| commit | 6c5a5f9cf066fc9732bafe68e6b166c2ac349d8e (patch) | |
| tree | ec785d1f86a6f8136436c6c54cea5ca353629d92 /src/web | |
| parent | ae2272ec60693a20d156bfa780839ef4ef885da7 (diff) | |
| parent | 6f08296a3a4fa4a43d33f672bb6e08c4fe781180 (diff) | |
| download | slskd-6c5a5f9cf066fc9732bafe68e6b166c2ac349d8e.tar.gz | |
Merge pull request #88 from slskd/search-filter
Add search result filter
Diffstat (limited to 'src/web')
| -rw-r--r-- | src/web/src/components/Search/Search.css | 5 | ||||
| -rw-r--r-- | src/web/src/components/Search/Search.js | 78 | ||||
| -rw-r--r-- | src/web/src/components/Users/Users.js | 21 | ||||
| -rw-r--r-- | src/web/src/lib/search.js | 112 | ||||
| -rw-r--r-- | src/web/src/lib/search.test.js | 266 |
5 files changed, 449 insertions, 33 deletions
diff --git a/src/web/src/components/Search/Search.css b/src/web/src/components/Search/Search.css index eb421948..7c91a08b 100644 --- a/src/web/src/components/Search/Search.css +++ b/src/web/src/components/Search/Search.css @@ -58,4 +58,9 @@ .search-loader { margin-top: 50px !important; +} + +.search-filter { + width: 100%; + margin-top: 1rem; }
\ No newline at end of file diff --git a/src/web/src/components/Search/Search.js b/src/web/src/components/Search/Search.js index 60720c1a..cb334e16 100644 --- a/src/web/src/components/Search/Search.js +++ b/src/web/src/components/Search/Search.js @@ -32,6 +32,7 @@ const initialState = { hiddenResults: [], hideLocked: true, fetching: false, + resultFilters: '' }; const sortOptions = { @@ -71,6 +72,14 @@ class Search extends Component { this.setState({ searchPhrase: data.value }); } + onResultFilterChange = (event, data) => { + this.setState({ resultFilters: data.value }, () => this.saveState()); + } + + clearResultFilter = () => { + this.setState({ resultFilters: '' }, () => this.saveState()); + } + saveState = () => { try { localStorage.setItem('soulseek-example-search-state', JSON.stringify({ ...this.state, results: [] })); @@ -151,9 +160,11 @@ class Search extends Component { } sortAndFilterResults = () => { - const { results = [], hideNoFreeSlots, resultSort, hideLocked, hiddenResults = [] } = this.state; + const { results = [], hideNoFreeSlots, resultSort, resultFilters = '', hideLocked, hiddenResults = [] } = this.state; const { field, order } = sortOptions[resultSort]; + const filters = search.parseFiltersFromString(resultFilters); + return results .filter(r => !hiddenResults.includes(r.username)) .map(r => { @@ -162,6 +173,7 @@ class Search extends Component { } return r; }) + .map(response => search.filterResponse({ response, filters })) .filter(r => r.fileCount + r.lockedFileCount > 0) .filter(r => !(hideNoFreeSlots && r.freeUploadSlots === 0)) .sort((a, b) => { @@ -184,7 +196,7 @@ class Search extends Component { } render = () => { - let { searchState, searchStatus, results = [], displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults = [], fetching } = this.state; + let { searchState, searchStatus, results = [], displayCount, resultSort, hideNoFreeSlots, hideLocked, hiddenResults = [], fetching, resultFilters } = this.state; let pending = fetching || searchState === 'pending'; const sortedAndFilteredResults = this.sortAndFilterResults(); @@ -220,34 +232,44 @@ class Search extends Component { </Loader> : <div> - {(results && results.length > 0) ? <Segment className='search-options' raised> - <Dropdown - button - className='search-options-sort icon' - floating - labeled - icon='sort' - options={sortDropdownOptions} - onChange={(e, { value }) => this.setState({ resultSort: value }, () => this.saveState())} - text={sortDropdownOptions.find(o => o.value === resultSort).text} - /> - <div className='search-option-toggles'> - <Checkbox - className='search-options-hide-locked' - toggle - onChange={() => this.setState({ hideLocked: !hideLocked }, () => this.saveState())} - checked={hideLocked} - label='Hide Locked Results' + {(results && results.length > 0) ? + <Segment className='search-options' raised> + <Dropdown + button + className='search-options-sort icon' + floating + labeled + icon='sort' + options={sortDropdownOptions} + onChange={(e, { value }) => this.setState({ resultSort: value }, () => this.saveState())} + text={sortDropdownOptions.find(o => o.value === resultSort).text} /> - <Checkbox - className='search-options-hide-no-slots' - toggle - onChange={() => this.setState({ hideNoFreeSlots: !hideNoFreeSlots }, () => this.saveState())} - checked={hideNoFreeSlots} - label='Hide Results with No Free Slots' + <div className='search-option-toggles'> + <Checkbox + className='search-options-hide-locked' + toggle + onChange={() => this.setState({ hideLocked: !hideLocked }, () => this.saveState())} + checked={hideLocked} + label='Hide Locked Results' + /> + <Checkbox + className='search-options-hide-no-slots' + toggle + onChange={() => this.setState({ hideNoFreeSlots: !hideNoFreeSlots }, () => this.saveState())} + checked={hideNoFreeSlots} + label='Hide Results with No Free Slots' + /> + </div> + <Input + className='search-filter' + placeholder='lackluster container -bothersome iscbr|isvbr minbitrate:320 minfilesize:10 minfilesinfolder:8 minlength:5000' + label={{ icon: 'filter', content: 'Filter' }} + value={resultFilters} + onChange={this.onResultFilterChange} + action={!!resultFilters && { icon: 'x', color: 'red', onClick: this.clearResultFilter }} /> - </div> - </Segment> : <PlaceholderSegment icon='search'/>} + </Segment> : <PlaceholderSegment icon='search'/> + } {sortedAndFilteredResults.slice(0, displayCount).map((r, i) => <Response key={i} diff --git a/src/web/src/components/Users/Users.js b/src/web/src/components/Users/Users.js index 37d9d82b..ef92d0d5 100644 --- a/src/web/src/components/Users/Users.js +++ b/src/web/src/components/Users/Users.js @@ -2,6 +2,7 @@ import React, { useState, useEffect, useLayoutEffect, useRef } from 'react'; import { Item, Segment, + Loader, Input } from 'semantic-ui-react'; @@ -10,6 +11,7 @@ import { activeUserInfoKey } from '../../config'; import * as peers from '../../lib/peers'; import './Users.css'; +import PlaceholderSegment from '../Shared/PlaceholderSegment'; const Users = (props) => { const inputRef = useRef(); @@ -93,11 +95,20 @@ const Users = (props) => { onKeyUp={(e) => e.key === 'Enter' ? setSelectedUsername(usernameInput) : ''} /> </Segment> - {!fetching && !error && !!user && <Segment className='users-user' raised> - <Item.Group> - <User {...user}/> - </Item.Group> - </Segment>} + {fetching ? + <Loader className='search-loader' active inline='centered' size='big'/> : + <div> + {error ? + <span>Failed to retrieve information for {selectedUsername}</span> : + !user ? + <PlaceholderSegment icon='users'/> : + <Segment className='users-user' raised> + <Item.Group> + <User {...user}/> + </Item.Group> + </Segment>} + </div> + } </div> ); }; diff --git a/src/web/src/lib/search.js b/src/web/src/lib/search.js index 2179dcd2..ff5f72de 100644 --- a/src/web/src/lib/search.js +++ b/src/web/src/lib/search.js @@ -10,4 +10,116 @@ export const getStatus = async ({ id, includeResponses = false }) => { export const getResponses = async ({ id }) => { return (await api.get(`/searches/${encodeURIComponent(id)}/responses`)).data; +}; + +export const isConstantBitRate = (bitRate) => { + switch (bitRate) { + case 8: + case 16: + case 24: + case 32: + case 40: + case 48: + case 56: + case 64: + case 80: + case 96: + case 112: + case 128: + case 144: + case 160: + case 192: + case 224: + case 256: + case 320: + return true; + default: + return false; + } +} + +export const parseFiltersFromString = (string) => { + const filters = { + minBitRate: 0, + minFileSize: 0, + minLength: 0, + minFilesInFolder: 0, + include: [], + exclude: [], + isVBR: false, + isCBR: false + }; + + const getNthMatch = (string, regex, n) => { + const match = string.match(regex); + + if (match) { + return parseInt(match[n], 10); + } + } + + filters.minBitRate = getNthMatch(string, /(minbr|minbitrate):([0-9]+)/i, 2) || filters.minBitRate; + filters.minFileSize = getNthMatch(string, /(minfs|minfilesize):([0-9]+)/i, 2) || filters.minFileSize; + filters.minLength = getNthMatch(string, /(minlen|minlength):([0-9]+)/i, 2) || filters.minLength; + filters.minFilesInFolder = getNthMatch(string, /(minfif|minfilesinfolder):([0-9]+)/i, 2) || filters.minFilesInFolder; + + filters.isVBR = !!string.match(/isvbr/i); + filters.isCBR = !!string.match(/iscbr/i); + + let terms = string.toLowerCase().split(' ') + .filter(term => !term.includes(':') && term !== 'isvbr' && term !== 'iscbr'); + + filters.include = terms.filter(term => !term.startsWith('-')); + filters.exclude = terms.filter(term => term.startsWith('-')).map(term => term.slice(1)); + + return filters; +}; + +export const filterResponse = ({ + filters = { + minBitRate: 0, + minFileSize: 0, + minLength: 0, + include: [], + exclude: [], + isVBR: false, + isCBR: false + }, + response = { + files: [], + lockedFiles: [] + } +}) => { + let { files = [], lockedFiles = [] } = response; + + if (response.fileCount + response.lockedFileCount < filters.minFilesInFolder) { + return { ...response, files: [] } + } + + const filterFiles = (files) => files.filter(file => { + const { bitRate, size, length, filename } = file; + const { isCBR, isVBR, minBitRate, minFileSize, minLength, include = [], exclude = [] } = filters; + + if (isCBR && !isConstantBitRate(bitRate)) return false; + if (isVBR && isConstantBitRate(bitRate)) return false; + if (bitRate < minBitRate) return false; + if (size < minFileSize) return false; + if (length < minLength) return false; + + if (include.length > 0 && include.filter(term => filename.toLowerCase().includes(term)).length !== include.length) return false; + if (exclude.length > 0 && exclude.filter(term => filename.toLowerCase().includes(term)).length !== 0) return false; + + return true; + }); + + const filteredFiles = filterFiles(files); + const filteredLockedFiles = filterFiles(lockedFiles); + + return { + ...response, + fileCount: filteredFiles.length, + lockedFileCount: filteredLockedFiles.length, + files: filteredFiles, + lockedFiles: filteredLockedFiles + }; };
\ No newline at end of file diff --git a/src/web/src/lib/search.test.js b/src/web/src/lib/search.test.js new file mode 100644 index 00000000..85e1d5f6 --- /dev/null +++ b/src/web/src/lib/search.test.js @@ -0,0 +1,266 @@ +import * as search from './search'; + +describe('isConstantBitRate', () => { + it('returns true given a valid MPEG-3 bitrate', () => { + const bitrates = [8,16,24,32,40,48,56,64,80,96,112,128,144,160,192,224,256,320]; + + bitrates.forEach(br => { + expect(search.isConstantBitRate(br)).toBe(true); + }); + }); + + it('returns false given an invalid MPEG-3 bitrate', () => { + const bitrates = [5,7,65,150,321]; + + bitrates.forEach(br => { + expect(search.isConstantBitRate(br)).toBe(false); + }); + }); + + it('returns false given null or undefined', () => { + expect(search.isConstantBitRate(null)).toBe(false); + expect(search.isConstantBitRate()).toBe(false); + }); +}); + +describe('filterResponse', () => { + it('removes VBR files if "iscbr" is specified', () => { + const response = { + files: [ + { bitRate: 123 }, + { bitRate: 320 } + ] + }; + + const filters = { isCBR: true }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { bitRate: 320 } + ] + }); + }); + + it('removes CBR files if "isvbr" is specified', () => { + const response = { + files: [ + { bitRate: 123 }, + { bitRate: 320 } + ] + }; + + const filters = { isVBR: true }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { bitRate: 123 } + ] + }); + }); + + it('removes all files if "iscbr" and "isvbr" are both specified', () => { + const response = { + files: [ + { bitRate: 123 }, + { bitRate: 320 } + ] + }; + + const filters = { isCBR: true, isVBR: true }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [] + }); + }); + + it('removes files with bitRate less than minBitRate', () => { + const response = { + files: [ + { bitRate: 100 }, + { bitRate: 99 } + ] + }; + + const filters = { minBitRate: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { bitRate: 100 } + ] + }); + }); + + it('removes files with size less than minFileSize', () => { + const response = { + files: [ + { size: 100 }, + { size: 99 } + ] + }; + + const filters = { minFileSize: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { size: 100 } + ] + }); + }); + + it('removes files with length less than minLength', () => { + const response = { + files: [ + { length: 100 }, + { length: 99 } + ] + }; + + const filters = { minLength: 100 }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { length: 100 } + ] + }); + }); + + describe('term filtering', () => { + const response = { + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/bar.mp3' }, + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/qux.mp3' }, + { filename: '/path/to/info.nfo' }, + { filename: '/path/to/folder.jpg' } + ] + }; + + it('removes files with filenames not containing included phrases', () => { + const filters = { include: ['path', 'to', '.nfo'] }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/info.nfo' }, + ] + }); + }); + + it('removes files with filenames containing excluded phrases', () => { + const filters = { exclude: ['bar', 'jpg', 'qux'] }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/foo.mp3' }, + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/info.nfo' }, + ] + }); + }); + + it('removes a mix of includes and excludes', () => { + const filters = { + include: ['path', '.mp3'], + exclude: ['foo', 'bar'] + }; + + expect(search.filterResponse({ response, filters })).toMatchObject({ + files: [ + { filename: '/path/to/baz.mp3' }, + { filename: '/path/to/qux.mp3' }, + ] + }); + }); + }); +}); + +describe('parseFiltersFromString', () => { + it('returns correct minBitrate', () => { + expect(search.parseFiltersFromString('foo minbr:42 bar')).toMatchObject({ + minBitRate: 42 + }); + + expect(search.parseFiltersFromString('foo minbitrate:123 bar')).toMatchObject({ + minBitRate: 123 + }); + }); + + it('returns correct minFileSize', () => { + expect(search.parseFiltersFromString('foo minfs:42 bar')).toMatchObject({ + minFileSize: 42 + }); + + expect(search.parseFiltersFromString('foo minfilesize:123 bar')).toMatchObject({ + minFileSize: 123 + }); + }); + + it('returns correct minLength', () => { + expect(search.parseFiltersFromString('foo minlen:42 bar')).toMatchObject({ + minLength: 42 + }); + + expect(search.parseFiltersFromString('foo minlength:123 bar')).toMatchObject({ + minLength: 123 + }); + }); + + it('returns correct minFilesInFolder', () => { + expect(search.parseFiltersFromString('foo minfif:42 bar')).toMatchObject({ + minFilesInFolder: 42 + }); + + expect(search.parseFiltersFromString('foo minfilesinfolder:123 bar')).toMatchObject({ + minFilesInFolder: 123 + }); + }); + + it('returns correct list of terms', () => { + expect(search.parseFiltersFromString('foo minbr:42 bar')).toMatchObject({ + include: [ 'foo', 'bar'] + }); + + expect(search.parseFiltersFromString('foo iscbr isvbr bar')).toMatchObject({ + include: [ 'foo', 'bar'] + }); + + expect(search.parseFiltersFromString('foo some:thing bar')).toMatchObject({ + include: [ 'foo', 'bar'] + }); + + expect(search.parseFiltersFromString('foo -bar')).toMatchObject({ + include: [ 'foo' ], + exclude: [ 'bar' ] + }); + + expect(search.parseFiltersFromString('-foo -bar -baz qux')).toMatchObject({ + exclude: [ 'foo', 'bar', 'baz' ], + include: [ 'qux' ] + }); + + expect(search.parseFiltersFromString('foo bar baz -qux')).toMatchObject({ + include: [ 'foo', 'bar', 'baz' ], + exclude: [ 'qux' ] + }); + }); + + it('returns isVBR and isCBR if terms are present', () => { + expect(search.parseFiltersFromString('isvbr')).toMatchObject({ + isVBR: true + }); + + expect(search.parseFiltersFromString('iscbr')).toMatchObject({ + isCBR: true + }); + }); + + it('returns expected filters given a bit of everything', () => { + expect(search.parseFiltersFromString('big -mix of:everything isvbr iscbr minbr:42')).toMatchObject({ + include: [ 'big' ], + exclude: [ 'mix' ], + isVBR: true, + isCBR: true, + minBitRate: 42 + }); + }); +});
\ No newline at end of file |
