2020-07-27 22:04:12 +02:00
|
|
|
// @flow
|
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2022-03-16 12:56:08 +01:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
|
|
|
import { doOpenModal } from 'redux/actions/app';
|
2022-03-15 20:07:31 +01:00
|
|
|
import { doToast } from 'redux/actions/notifications';
|
2021-09-16 22:00:44 +02:00
|
|
|
import { selectShowMatureContent } from 'redux/selectors/settings';
|
2022-03-15 20:07:31 +01:00
|
|
|
import { selectClaimForUri, selectClaimIdForUri, selectClaimIsNsfwForUri } from 'redux/selectors/claims';
|
2022-03-23 03:42:12 +01:00
|
|
|
import { doClaimSearch, doResolveUris } from 'redux/actions/claims';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { buildURI, isURIValid } from 'util/lbryURI';
|
|
|
|
import { batchActions } from 'util/batch-actions';
|
2022-03-15 20:07:31 +01:00
|
|
|
import { makeSelectSearchUrisForQuery, selectPersonalRecommendations, selectSearchValue } from 'redux/selectors/search';
|
|
|
|
import { selectUser } from 'redux/selectors/user';
|
2020-07-27 22:04:12 +02:00
|
|
|
import handleFetchResponse from 'util/handle-fetch';
|
2021-07-13 08:28:09 +02:00
|
|
|
import { getSearchQueryString } from 'util/query-params';
|
2021-10-01 04:40:33 +02:00
|
|
|
import { getRecommendationSearchOptions } from 'util/search';
|
2022-03-15 20:07:31 +01:00
|
|
|
import { SEARCH_SERVER_API, SEARCH_SERVER_API_ALT, RECSYS_FYP_ENDPOINT } from 'config';
|
2021-11-24 21:25:22 +01:00
|
|
|
import { SEARCH_OPTIONS } from 'constants/search';
|
2022-03-15 20:07:31 +01:00
|
|
|
import { X_LBRY_AUTH_TOKEN } from 'constants/token';
|
|
|
|
import { getAuthToken } from 'util/saved-passwords';
|
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// FYP
|
|
|
|
// ****************************************************************************
|
|
|
|
// TODO: This should be part of `extras/recsys/recsys`, but due to the circular
|
|
|
|
// dependency problem with `extras`, I'm temporarily placing it. The recsys
|
|
|
|
// object should be moved into `ui`, but that change will require more testing.
|
|
|
|
|
|
|
|
console.assert(RECSYS_FYP_ENDPOINT, 'RECSYS_FYP_ENDPOINT not defined!');
|
|
|
|
|
|
|
|
const recsysFyp = {
|
|
|
|
fetchPersonalRecommendations: (userId: string) => {
|
|
|
|
return fetch(`${RECSYS_FYP_ENDPOINT}/${userId}/fyp`, { headers: { [X_LBRY_AUTH_TOKEN]: getAuthToken() } })
|
|
|
|
.then((response) => response.json())
|
|
|
|
.then((result) => result)
|
|
|
|
.catch((error) => {
|
|
|
|
console.log('FYP: fetch', { error, userId });
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
|
|
|
markPersonalRecommendations: (userId: string, gid: string) => {
|
|
|
|
return fetch(`${RECSYS_FYP_ENDPOINT}/${userId}/fyp/${gid}/mark`, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: { [X_LBRY_AUTH_TOKEN]: getAuthToken() },
|
|
|
|
}).catch((error) => {
|
|
|
|
console.log('FYP: mark', { error, userId, gid });
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2022-03-16 12:56:08 +01:00
|
|
|
ignoreRecommendation: (userId: string, gid: string, claimId: string, ignoreChannel: boolean) => {
|
|
|
|
let endpoint = `${RECSYS_FYP_ENDPOINT}/${userId}/fyp/${gid}/c/${claimId}/ignore`;
|
|
|
|
if (ignoreChannel) {
|
|
|
|
endpoint += '?entire_channel=1';
|
|
|
|
}
|
|
|
|
|
|
|
|
return fetch(endpoint, {
|
2022-03-15 20:07:31 +01:00
|
|
|
method: 'POST',
|
|
|
|
headers: { [X_LBRY_AUTH_TOKEN]: getAuthToken() },
|
|
|
|
})
|
|
|
|
.then((result) => result)
|
|
|
|
.catch((error) => {
|
|
|
|
console.log('FYP: ignore', { error, userId, gid, claimId });
|
|
|
|
return {};
|
|
|
|
});
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
// ****************************************************************************
|
|
|
|
// ****************************************************************************
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
type Dispatch = (action: any) => any;
|
2022-03-17 05:55:40 +01:00
|
|
|
type GetState = () => { claims: any, search: SearchState, user: UserState };
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
type SearchOptions = {
|
|
|
|
size?: number,
|
|
|
|
from?: number,
|
|
|
|
related_to?: string,
|
|
|
|
nsfw?: boolean,
|
|
|
|
isBackgroundSearch?: boolean,
|
2022-03-15 20:07:31 +01:00
|
|
|
gid?: string, // for fyp only
|
|
|
|
uuid?: string, // for fyp only
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
let lighthouse = {
|
2021-07-29 19:19:12 +02:00
|
|
|
CONNECTION_STRING: SEARCH_SERVER_API,
|
2021-11-24 21:25:22 +01:00
|
|
|
user_id: '',
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
search: (queryString: string) => fetch(`${lighthouse.CONNECTION_STRING}?${queryString}`).then(handleFetchResponse),
|
2021-11-24 21:25:22 +01:00
|
|
|
|
|
|
|
searchRecommendations: (queryString: string) => {
|
|
|
|
if (lighthouse.user_id) {
|
|
|
|
return fetch(`${SEARCH_SERVER_API_ALT}?${queryString}${lighthouse.user_id}`).then(handleFetchResponse);
|
|
|
|
} else {
|
|
|
|
return fetch(`${SEARCH_SERVER_API_ALT}?${queryString}`).then(handleFetchResponse);
|
|
|
|
}
|
|
|
|
},
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
|
2020-12-03 18:29:47 +01:00
|
|
|
export const setSearchApi = (endpoint: string) => {
|
|
|
|
lighthouse.CONNECTION_STRING = endpoint.replace(/\/*$/, '/'); // exactly one slash at the end;
|
2020-07-27 22:04:12 +02:00
|
|
|
};
|
|
|
|
|
2021-11-24 21:25:22 +01:00
|
|
|
export const setSearchUserId = (userId: ?string) => {
|
|
|
|
lighthouse.user_id = userId ? `&user_id=${userId}` : '';
|
|
|
|
};
|
|
|
|
|
2022-03-15 20:07:31 +01:00
|
|
|
/**
|
|
|
|
* Processes a lighthouse-formatted search result to an array of uris.
|
|
|
|
* @param results
|
|
|
|
*/
|
|
|
|
const processLighthouseResults = (results: Array<any>) => {
|
|
|
|
const uris = [];
|
|
|
|
|
|
|
|
results.forEach((item) => {
|
|
|
|
if (item) {
|
|
|
|
const { name, claimId } = item;
|
|
|
|
const urlObj: LbryUrlObj = {};
|
|
|
|
|
|
|
|
if (name.startsWith('@')) {
|
|
|
|
urlObj.channelName = name;
|
|
|
|
urlObj.channelClaimId = claimId;
|
|
|
|
} else {
|
|
|
|
urlObj.streamName = name;
|
|
|
|
urlObj.streamClaimId = claimId;
|
|
|
|
}
|
|
|
|
|
|
|
|
const url = buildURI(urlObj);
|
|
|
|
if (isURIValid(url)) {
|
|
|
|
uris.push(url);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
return uris;
|
|
|
|
};
|
|
|
|
|
2020-07-27 22:04:12 +02:00
|
|
|
export const doSearch = (rawQuery: string, searchOptions: SearchOptions) => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
) => {
|
|
|
|
const query = rawQuery.replace(/^lbry:\/\//i, '').replace(/\//, ' ');
|
|
|
|
|
|
|
|
if (!query) {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SEARCH_FAIL,
|
|
|
|
});
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
const state = getState();
|
|
|
|
|
2021-07-13 08:28:09 +02:00
|
|
|
const queryWithOptions = getSearchQueryString(query, searchOptions);
|
2021-03-26 09:33:30 +01:00
|
|
|
|
2021-07-13 08:28:09 +02:00
|
|
|
const size = searchOptions.size;
|
2021-03-26 09:33:30 +01:00
|
|
|
const from = searchOptions.from;
|
2020-07-27 22:04:12 +02:00
|
|
|
|
|
|
|
// If we have already searched for something, we don't need to do anything
|
2021-08-17 16:03:25 +02:00
|
|
|
const urisForQuery = makeSelectSearchUrisForQuery(queryWithOptions)(state);
|
2020-07-27 22:04:12 +02:00
|
|
|
if (urisForQuery && !!urisForQuery.length) {
|
2021-03-26 09:33:30 +01:00
|
|
|
if (!size || !from || from + size < urisForQuery.length) {
|
|
|
|
return;
|
|
|
|
}
|
2020-07-27 22:04:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SEARCH_START,
|
|
|
|
});
|
|
|
|
|
2021-11-24 21:25:22 +01:00
|
|
|
const cmd = searchOptions.hasOwnProperty(SEARCH_OPTIONS.RELATED_TO)
|
|
|
|
? lighthouse.searchRecommendations
|
|
|
|
: lighthouse.search;
|
|
|
|
|
|
|
|
cmd(queryWithOptions)
|
2021-08-17 16:03:25 +02:00
|
|
|
.then((data: { body: Array<{ name: string, claimId: string }>, poweredBy: string }) => {
|
|
|
|
const { body: result, poweredBy } = data;
|
2022-03-15 20:07:31 +01:00
|
|
|
const uris = processLighthouseResults(result);
|
2020-07-27 22:04:12 +02:00
|
|
|
|
2022-03-15 20:07:31 +01:00
|
|
|
const actions = [];
|
2020-08-12 19:02:19 +02:00
|
|
|
actions.push(doResolveUris(uris));
|
2020-07-27 22:04:12 +02:00
|
|
|
actions.push({
|
|
|
|
type: ACTIONS.SEARCH_SUCCESS,
|
|
|
|
data: {
|
|
|
|
query: queryWithOptions,
|
2021-04-07 12:50:15 +02:00
|
|
|
from: from,
|
2021-03-26 09:33:30 +01:00
|
|
|
size: size,
|
2020-07-27 22:04:12 +02:00
|
|
|
uris,
|
2021-08-17 16:03:25 +02:00
|
|
|
recsys: poweredBy,
|
2020-07-27 22:04:12 +02:00
|
|
|
},
|
|
|
|
});
|
2022-03-15 20:07:31 +01:00
|
|
|
|
2020-07-27 22:04:12 +02:00
|
|
|
dispatch(batchActions(...actions));
|
|
|
|
})
|
2021-07-13 08:28:09 +02:00
|
|
|
.catch(() => {
|
2020-07-27 22:04:12 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.SEARCH_FAIL,
|
|
|
|
});
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doUpdateSearchOptions = (newOptions: SearchOptions, additionalOptions: SearchOptions) => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
) => {
|
|
|
|
const state = getState();
|
|
|
|
const searchValue = selectSearchValue(state);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_SEARCH_OPTIONS,
|
|
|
|
data: newOptions,
|
|
|
|
});
|
|
|
|
|
|
|
|
if (searchValue) {
|
|
|
|
// After updating, perform a search with the new options
|
|
|
|
dispatch(doSearch(searchValue, additionalOptions));
|
|
|
|
}
|
|
|
|
};
|
2020-12-03 18:29:47 +01:00
|
|
|
|
2021-12-07 19:17:29 +01:00
|
|
|
export const doSetMentionSearchResults = (query: string, uris: Array<string>) => (dispatch: Dispatch) => {
|
2021-12-06 20:39:39 +01:00
|
|
|
dispatch({
|
2021-12-07 15:51:55 +01:00
|
|
|
type: ACTIONS.SET_MENTION_SEARCH_RESULTS,
|
2021-12-07 19:17:29 +01:00
|
|
|
data: { query, uris },
|
2021-12-06 20:39:39 +01:00
|
|
|
});
|
|
|
|
};
|
|
|
|
|
2022-03-15 20:07:31 +01:00
|
|
|
export const doFetchRecommendedContent = (uri: string, fyp: ?FypParam = null) => (
|
|
|
|
dispatch: Dispatch,
|
|
|
|
getState: GetState
|
|
|
|
) => {
|
2021-03-19 16:04:12 +01:00
|
|
|
const state = getState();
|
2021-11-16 02:10:03 +01:00
|
|
|
const claim = selectClaimForUri(state, uri);
|
2021-09-16 22:00:44 +02:00
|
|
|
const matureEnabled = selectShowMatureContent(state);
|
2021-11-16 02:10:03 +01:00
|
|
|
const claimIsMature = selectClaimIsNsfwForUri(state, uri);
|
2021-03-19 16:04:12 +01:00
|
|
|
|
|
|
|
if (claim && claim.value && claim.claim_id) {
|
2021-10-01 04:40:33 +02:00
|
|
|
const options: SearchOptions = getRecommendationSearchOptions(matureEnabled, claimIsMature, claim.claim_id);
|
2022-03-15 20:07:31 +01:00
|
|
|
|
|
|
|
if (fyp) {
|
|
|
|
options['gid'] = fyp.gid;
|
|
|
|
options['uuid'] = fyp.uuid;
|
|
|
|
}
|
|
|
|
|
2021-03-19 16:04:12 +01:00
|
|
|
const { title } = claim.value;
|
2021-09-16 22:00:44 +02:00
|
|
|
|
2021-03-19 16:04:12 +01:00
|
|
|
if (title && options) {
|
|
|
|
dispatch(doSearch(title, options));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-03-15 20:07:31 +01:00
|
|
|
export const doFetchPersonalRecommendations = () => (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const user = selectUser(state);
|
|
|
|
|
|
|
|
if (!user || !user.id) {
|
|
|
|
dispatch({ type: ACTIONS.FYP_FETCH_FAILED });
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
recsysFyp
|
|
|
|
.fetchPersonalRecommendations(user.id)
|
|
|
|
.then((data) => {
|
|
|
|
const { gid, recs } = data;
|
|
|
|
if (gid && recs) {
|
2022-03-16 03:57:26 +01:00
|
|
|
const uris = processLighthouseResults(recs);
|
2022-03-23 03:42:12 +01:00
|
|
|
dispatch(
|
|
|
|
doClaimSearch({
|
|
|
|
claim_ids: recs.map((r) => r.claimId),
|
|
|
|
page: 1,
|
|
|
|
page_size: 50,
|
|
|
|
no_totals: true,
|
|
|
|
})
|
|
|
|
).finally(() => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.FYP_FETCH_SUCCESS,
|
|
|
|
data: { gid, uris },
|
|
|
|
});
|
2022-03-15 20:07:31 +01:00
|
|
|
});
|
|
|
|
} else {
|
|
|
|
dispatch({ type: ACTIONS.FYP_FETCH_FAILED });
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch(() => {
|
|
|
|
dispatch({ type: ACTIONS.FYP_FETCH_FAILED });
|
|
|
|
});
|
|
|
|
};
|
|
|
|
|
|
|
|
export const doRemovePersonalRecommendation = (uri: string) => (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const user = selectUser(state);
|
|
|
|
const personalRecommendations = selectPersonalRecommendations(state);
|
|
|
|
const claimId = selectClaimIdForUri(state, uri);
|
|
|
|
|
|
|
|
if (!user || !user.id || !personalRecommendations.gid || !claimId) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-03-16 12:56:08 +01:00
|
|
|
dispatch(
|
|
|
|
doOpenModal(MODALS.HIDE_RECOMMENDATION, {
|
|
|
|
uri,
|
|
|
|
onConfirm: (hideChannel) => {
|
|
|
|
recsysFyp
|
|
|
|
.ignoreRecommendation(user.id, personalRecommendations.gid, claimId, hideChannel)
|
|
|
|
.then((res) => {
|
|
|
|
dispatch({ type: ACTIONS.FYP_HIDE_URI, data: { uri } });
|
|
|
|
dispatch(doToast({ message: __('Recommendation removed. Thanks for the feedback!') }));
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log('doRemovePersonalRecommendation:', err);
|
|
|
|
});
|
|
|
|
},
|
2022-03-15 20:07:31 +01:00
|
|
|
})
|
2022-03-16 12:56:08 +01:00
|
|
|
);
|
2022-03-15 20:07:31 +01:00
|
|
|
};
|
|
|
|
|
|
|
|
export { lighthouse, recsysFyp };
|