search.js 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215
  1. import { fromJS } from 'immutable';
  2. import { searchHistory } from 'mastodon/settings';
  3. import api from '../api';
  4. import { fetchRelationships } from './accounts';
  5. import { importFetchedAccounts, importFetchedStatuses } from './importer';
  6. export const SEARCH_CHANGE = 'SEARCH_CHANGE';
  7. export const SEARCH_CLEAR = 'SEARCH_CLEAR';
  8. export const SEARCH_SHOW = 'SEARCH_SHOW';
  9. export const SEARCH_FETCH_REQUEST = 'SEARCH_FETCH_REQUEST';
  10. export const SEARCH_FETCH_SUCCESS = 'SEARCH_FETCH_SUCCESS';
  11. export const SEARCH_FETCH_FAIL = 'SEARCH_FETCH_FAIL';
  12. export const SEARCH_EXPAND_REQUEST = 'SEARCH_EXPAND_REQUEST';
  13. export const SEARCH_EXPAND_SUCCESS = 'SEARCH_EXPAND_SUCCESS';
  14. export const SEARCH_EXPAND_FAIL = 'SEARCH_EXPAND_FAIL';
  15. export const SEARCH_HISTORY_UPDATE = 'SEARCH_HISTORY_UPDATE';
  16. export function changeSearch(value) {
  17. return {
  18. type: SEARCH_CHANGE,
  19. value,
  20. };
  21. }
  22. export function clearSearch() {
  23. return {
  24. type: SEARCH_CLEAR,
  25. };
  26. }
  27. export function submitSearch(type) {
  28. return (dispatch, getState) => {
  29. const value = getState().getIn(['search', 'value']);
  30. const signedIn = !!getState().getIn(['meta', 'me']);
  31. if (value.length === 0) {
  32. dispatch(fetchSearchSuccess({ accounts: [], statuses: [], hashtags: [] }, '', type));
  33. return;
  34. }
  35. dispatch(fetchSearchRequest(type));
  36. api(getState).get('/api/v2/search', {
  37. params: {
  38. q: value,
  39. resolve: signedIn,
  40. limit: 11,
  41. type,
  42. },
  43. }).then(response => {
  44. if (response.data.accounts) {
  45. dispatch(importFetchedAccounts(response.data.accounts));
  46. }
  47. if (response.data.statuses) {
  48. dispatch(importFetchedStatuses(response.data.statuses));
  49. }
  50. dispatch(fetchSearchSuccess(response.data, value, type));
  51. dispatch(fetchRelationships(response.data.accounts.map(item => item.id)));
  52. }).catch(error => {
  53. dispatch(fetchSearchFail(error));
  54. });
  55. };
  56. }
  57. export function fetchSearchRequest(searchType) {
  58. return {
  59. type: SEARCH_FETCH_REQUEST,
  60. searchType,
  61. };
  62. }
  63. export function fetchSearchSuccess(results, searchTerm, searchType) {
  64. return {
  65. type: SEARCH_FETCH_SUCCESS,
  66. results,
  67. searchType,
  68. searchTerm,
  69. };
  70. }
  71. export function fetchSearchFail(error) {
  72. return {
  73. type: SEARCH_FETCH_FAIL,
  74. error,
  75. };
  76. }
  77. export const expandSearch = type => (dispatch, getState) => {
  78. const value = getState().getIn(['search', 'value']);
  79. const offset = getState().getIn(['search', 'results', type]).size - 1;
  80. dispatch(expandSearchRequest(type));
  81. api(getState).get('/api/v2/search', {
  82. params: {
  83. q: value,
  84. type,
  85. offset,
  86. limit: 11,
  87. },
  88. }).then(({ data }) => {
  89. if (data.accounts) {
  90. dispatch(importFetchedAccounts(data.accounts));
  91. }
  92. if (data.statuses) {
  93. dispatch(importFetchedStatuses(data.statuses));
  94. }
  95. dispatch(expandSearchSuccess(data, value, type));
  96. dispatch(fetchRelationships(data.accounts.map(item => item.id)));
  97. }).catch(error => {
  98. dispatch(expandSearchFail(error));
  99. });
  100. };
  101. export const expandSearchRequest = (searchType) => ({
  102. type: SEARCH_EXPAND_REQUEST,
  103. searchType,
  104. });
  105. export const expandSearchSuccess = (results, searchTerm, searchType) => ({
  106. type: SEARCH_EXPAND_SUCCESS,
  107. results,
  108. searchTerm,
  109. searchType,
  110. });
  111. export const expandSearchFail = error => ({
  112. type: SEARCH_EXPAND_FAIL,
  113. error,
  114. });
  115. export const showSearch = () => ({
  116. type: SEARCH_SHOW,
  117. });
  118. export const openURL = (value, history, onFailure) => (dispatch, getState) => {
  119. const signedIn = !!getState().getIn(['meta', 'me']);
  120. if (!signedIn) {
  121. if (onFailure) {
  122. onFailure();
  123. }
  124. return;
  125. }
  126. dispatch(fetchSearchRequest());
  127. api(getState).get('/api/v2/search', { params: { q: value, resolve: true } }).then(response => {
  128. if (response.data.accounts?.length > 0) {
  129. dispatch(importFetchedAccounts(response.data.accounts));
  130. history.push(`/@${response.data.accounts[0].acct}`);
  131. } else if (response.data.statuses?.length > 0) {
  132. dispatch(importFetchedStatuses(response.data.statuses));
  133. history.push(`/@${response.data.statuses[0].account.acct}/${response.data.statuses[0].id}`);
  134. } else if (onFailure) {
  135. onFailure();
  136. }
  137. dispatch(fetchSearchSuccess(response.data, value));
  138. }).catch(err => {
  139. dispatch(fetchSearchFail(err));
  140. if (onFailure) {
  141. onFailure();
  142. }
  143. });
  144. };
  145. export const clickSearchResult = (q, type) => (dispatch, getState) => {
  146. const previous = getState().getIn(['search', 'recent']);
  147. if (previous.some(x => x.get('q') === q && x.get('type') === type)) {
  148. return;
  149. }
  150. const me = getState().getIn(['meta', 'me']);
  151. const current = previous.add(fromJS({ type, q })).takeLast(4);
  152. searchHistory.set(me, current.toJS());
  153. dispatch(updateSearchHistory(current));
  154. };
  155. export const forgetSearchResult = q => (dispatch, getState) => {
  156. const previous = getState().getIn(['search', 'recent']);
  157. const me = getState().getIn(['meta', 'me']);
  158. const current = previous.filterNot(result => result.get('q') === q);
  159. searchHistory.set(me, current.toJS());
  160. dispatch(updateSearchHistory(current));
  161. };
  162. export const updateSearchHistory = recent => ({
  163. type: SEARCH_HISTORY_UPDATE,
  164. recent,
  165. });
  166. export const hydrateSearch = () => (dispatch, getState) => {
  167. const me = getState().getIn(['meta', 'me']);
  168. const history = searchHistory.get(me);
  169. if (history !== null) {
  170. dispatch(updateSearchHistory(history));
  171. }
  172. };