2022-05-24 08:52:54 +02:00
|
|
|
// @flow
|
2022-02-07 12:49:11 +01:00
|
|
|
import { RECSYS_ENDPOINT } from 'config';
|
2021-09-03 00:39:40 +02:00
|
|
|
import { selectUser } from 'redux/selectors/user';
|
2022-06-22 15:43:54 +02:00
|
|
|
import { selectRecommendedMetaForClaimId } from 'redux/selectors/search';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { parseURI } from 'util/lbryURI';
|
2022-03-14 15:27:09 +01:00
|
|
|
import { getAuthToken } from 'util/saved-passwords';
|
2022-05-24 14:24:04 +02:00
|
|
|
import * as ACTIONS from 'constants/action_types';
|
2021-10-17 10:36:14 +02:00
|
|
|
import * as SETTINGS from 'constants/settings';
|
2022-03-14 15:27:09 +01:00
|
|
|
import { X_LBRY_AUTH_TOKEN } from 'constants/token';
|
2021-10-17 10:36:14 +02:00
|
|
|
import { makeSelectClaimForUri } from 'redux/selectors/claims';
|
2021-09-03 00:39:40 +02:00
|
|
|
import { selectPlayingUri, selectPrimaryUri } from 'redux/selectors/content';
|
2021-11-23 05:29:04 +01:00
|
|
|
import { selectClientSetting, selectDaemonSettings } from 'redux/selectors/settings';
|
2022-05-11 13:13:21 +02:00
|
|
|
import { selectIsSubscribedForClaimId } from 'redux/selectors/subscriptions';
|
2022-05-24 08:52:54 +02:00
|
|
|
// $FlowFixMe: cannot resolve..
|
2021-10-17 10:36:14 +02:00
|
|
|
import { history } from 'ui/store';
|
2021-09-03 00:39:40 +02:00
|
|
|
|
2022-02-07 12:49:11 +01:00
|
|
|
const recsysEndpoint = RECSYS_ENDPOINT;
|
2022-06-22 15:43:54 +02:00
|
|
|
const DEFAULT_RECSYS_ID = 'lighthouse-v0';
|
2021-09-03 00:39:40 +02:00
|
|
|
|
|
|
|
const getClaimIdsFromUris = (uris) => {
|
|
|
|
return uris
|
|
|
|
? uris.map((uri) => {
|
|
|
|
try {
|
|
|
|
const { claimId } = parseURI(uri);
|
|
|
|
return claimId;
|
|
|
|
} catch (e) {
|
|
|
|
return [];
|
|
|
|
}
|
|
|
|
})
|
|
|
|
: [];
|
|
|
|
};
|
|
|
|
|
2022-05-24 08:52:54 +02:00
|
|
|
const recsys: Recsys = {
|
2021-09-03 00:39:40 +02:00
|
|
|
entries: {},
|
|
|
|
debug: false,
|
2022-05-24 14:24:04 +02:00
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
/**
|
|
|
|
* Provides for creating, updating, and sending Clickstream data object Entries.
|
|
|
|
* Entries are Created either when recommendedContent loads, or when recommendedContent is clicked.
|
|
|
|
* If recommended content is clicked, An Entry with parentUuid is created.
|
|
|
|
* On page load, find an empty entry with your claimId, or create a new entry and record to it.
|
|
|
|
*/
|
|
|
|
|
2022-05-24 14:24:04 +02:00
|
|
|
/**
|
|
|
|
* Saves existing entries to persistence storage (in this case, Redux).
|
|
|
|
*/
|
|
|
|
saveEntries: function () {
|
|
|
|
if (window && window.store) {
|
|
|
|
window.store.dispatch({
|
|
|
|
type: ACTIONS.SET_RECSYS_ENTRIES,
|
|
|
|
data: recsys.entries,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
/**
|
|
|
|
* Called when RecommendedContent was clicked.
|
|
|
|
* Adds index of clicked recommendation to parent entry
|
|
|
|
* Adds new Entry with parentUuid for destination page
|
|
|
|
* @param parentClaimId: string,
|
|
|
|
* @param newClaimId: string,
|
|
|
|
*/
|
|
|
|
onClickedRecommended: function (parentClaimId, newClaimId) {
|
|
|
|
const parentEntry = recsys.entries[parentClaimId] ? recsys.entries[parentClaimId] : null;
|
2022-05-24 08:52:54 +02:00
|
|
|
const parentUuid = parentEntry ? parentEntry['uuid'] : '';
|
|
|
|
const parentRecommendedClaims = parentEntry ? parentEntry['recClaimIds'] : [];
|
|
|
|
const parentClickedIndexes = parentEntry ? parentEntry['recClickedVideoIdx'] : [];
|
2021-09-03 00:39:40 +02:00
|
|
|
const indexClicked = parentRecommendedClaims.indexOf(newClaimId);
|
|
|
|
|
|
|
|
if (parentUuid) {
|
|
|
|
recsys.createRecsysEntry(newClaimId, parentUuid);
|
|
|
|
}
|
2022-05-24 08:52:54 +02:00
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
parentClickedIndexes.push(indexClicked);
|
2022-05-24 08:52:54 +02:00
|
|
|
// recsys.log('onClickedRecommended', { parentClaimId, newClaimId });
|
|
|
|
recsys.log('onClickedRecommended', newClaimId);
|
2021-09-03 00:39:40 +02:00
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
2022-05-24 08:52:54 +02:00
|
|
|
* Page was loaded. Get or Create entry and populate it with default data,
|
|
|
|
* plus recommended content, recsysId, etc.
|
2021-09-03 00:39:40 +02:00
|
|
|
* Called from recommendedContent component
|
2022-06-22 15:43:54 +02:00
|
|
|
*
|
|
|
|
* @param claimId The ID of the content the recommendations are for.
|
|
|
|
* @param uris The recommended uris for `claimId`.
|
|
|
|
* @param uuid Specific uuid to use (e.g. for FYP); uses the recommendation's
|
|
|
|
* uuid otherwise.
|
2021-09-03 00:39:40 +02:00
|
|
|
*/
|
2022-06-01 15:55:38 +02:00
|
|
|
onRecsLoaded: function (claimId, uris, uuid = '') {
|
2021-10-11 02:19:54 +02:00
|
|
|
if (window && window.store) {
|
2021-09-03 00:39:40 +02:00
|
|
|
const state = window.store.getState();
|
2022-06-22 15:43:54 +02:00
|
|
|
const recommendedMeta = selectRecommendedMetaForClaimId(state, claimId);
|
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
if (!recsys.entries[claimId]) {
|
2022-06-22 15:43:54 +02:00
|
|
|
recsys.createRecsysEntry(claimId, null, uuid || recommendedMeta.uuid);
|
2021-09-03 00:39:40 +02:00
|
|
|
}
|
2022-06-22 15:43:54 +02:00
|
|
|
|
2022-06-01 15:55:38 +02:00
|
|
|
const claimIds = getClaimIdsFromUris(uris);
|
2022-06-22 15:43:54 +02:00
|
|
|
recsys.entries[claimId]['recsysId'] = recommendedMeta.poweredBy || DEFAULT_RECSYS_ID;
|
2021-09-03 00:39:40 +02:00
|
|
|
recsys.entries[claimId]['pageLoadedAt'] = Date.now();
|
2022-05-24 08:52:54 +02:00
|
|
|
|
|
|
|
// It is possible that `claimIds` include `null | undefined` entries
|
|
|
|
// instead of all being strings. I don't know if we should filter it,
|
|
|
|
// or change the `recClaimIds` definition. Leaving as is for now since
|
|
|
|
// any changes could affect existing recsys data set.
|
|
|
|
// -----------
|
|
|
|
// $FlowFixMe:
|
2021-09-03 00:39:40 +02:00
|
|
|
recsys.entries[claimId]['recClaimIds'] = claimIds;
|
|
|
|
}
|
|
|
|
recsys.log('onRecsLoaded', claimId);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Creates an Entry with optional parentUuid
|
|
|
|
* @param: claimId: string
|
|
|
|
* @param: parentUuid: string (optional)
|
2022-06-22 15:43:54 +02:00
|
|
|
* @param uuid Specific uuid to use (e.g. for FYP); uses the recommendation's
|
|
|
|
* uuid otherwise.
|
2021-09-03 00:39:40 +02:00
|
|
|
*/
|
2022-03-15 20:07:31 +01:00
|
|
|
createRecsysEntry: function (claimId, parentUuid, uuid = '') {
|
2021-10-11 02:19:54 +02:00
|
|
|
if (window && window.store && claimId) {
|
2021-09-03 00:39:40 +02:00
|
|
|
const state = window.store.getState();
|
2022-06-22 15:43:54 +02:00
|
|
|
const recommendedMeta = selectRecommendedMetaForClaimId(state, claimId);
|
2021-10-12 18:10:35 +02:00
|
|
|
const user = selectUser(state);
|
|
|
|
const userId = user ? user.id : null;
|
2022-05-11 12:42:34 +02:00
|
|
|
|
|
|
|
// Make a stub entry that will be filled out on page load
|
2022-05-24 08:52:54 +02:00
|
|
|
// $FlowIgnore: not everything is defined since this is a stub
|
2022-05-11 12:42:34 +02:00
|
|
|
recsys.entries[claimId] = {
|
2022-06-22 15:43:54 +02:00
|
|
|
uuid: uuid || recommendedMeta.uuid,
|
2022-05-11 12:42:34 +02:00
|
|
|
claimId: claimId,
|
|
|
|
recClickedVideoIdx: [],
|
|
|
|
pageLoadedAt: Date.now(),
|
|
|
|
events: [],
|
2022-05-11 13:13:21 +02:00
|
|
|
incognito: !(user && user.has_verified_email),
|
|
|
|
isFollowing: selectIsSubscribedForClaimId(state, claimId),
|
2022-05-11 12:42:34 +02:00
|
|
|
};
|
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
if (parentUuid) {
|
2022-05-24 08:52:54 +02:00
|
|
|
// $FlowFixMe: 'uid' should be a number, not null.
|
2022-05-11 12:42:34 +02:00
|
|
|
recsys.entries[claimId].uid = userId || null;
|
|
|
|
recsys.entries[claimId].parentUuid = parentUuid;
|
2021-09-03 00:39:40 +02:00
|
|
|
} else {
|
2022-05-24 08:52:54 +02:00
|
|
|
// $FlowFixMe: 'uid' should be a number, not null.
|
2022-05-11 12:42:34 +02:00
|
|
|
recsys.entries[claimId].uid = userId;
|
2022-05-24 08:52:54 +02:00
|
|
|
// $FlowFixMe: 'recsysId' should be a number, not null.
|
2022-05-11 12:42:34 +02:00
|
|
|
recsys.entries[claimId].recsysId = null;
|
|
|
|
recsys.entries[claimId].recClaimIds = [];
|
2021-09-03 00:39:40 +02:00
|
|
|
}
|
2022-05-24 14:24:04 +02:00
|
|
|
|
|
|
|
recsys.saveEntries();
|
2021-09-03 00:39:40 +02:00
|
|
|
}
|
|
|
|
recsys.log('createRecsysEntry', claimId);
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Send event for claimId
|
|
|
|
* @param claimId
|
2022-05-24 14:24:04 +02:00
|
|
|
* @param isTentative Visibility change rather than tab closed.
|
2021-09-03 00:39:40 +02:00
|
|
|
*/
|
|
|
|
sendRecsysEntry: function (claimId, isTentative) {
|
|
|
|
const shareTelemetry =
|
|
|
|
IS_WEB || (window && window.store && selectDaemonSettings(window.store.getState()).share_usage_data);
|
|
|
|
|
|
|
|
if (recsys.entries[claimId] && shareTelemetry) {
|
2022-06-22 15:43:54 +02:00
|
|
|
// Exclude `events` in the submission https://github.com/OdyseeTeam/odysee-frontend/issues/1317
|
2022-05-10 11:07:18 +02:00
|
|
|
const { events, ...entryData } = recsys.entries[claimId];
|
|
|
|
const data = JSON.stringify(entryData);
|
2022-03-14 15:27:09 +01:00
|
|
|
|
|
|
|
return fetch(recsysEndpoint, {
|
|
|
|
method: 'POST',
|
|
|
|
headers: {
|
|
|
|
[X_LBRY_AUTH_TOKEN]: getAuthToken(),
|
|
|
|
'Content-Type': 'application/json',
|
|
|
|
},
|
|
|
|
body: data,
|
2022-05-24 14:24:04 +02:00
|
|
|
})
|
|
|
|
.then(() => {
|
|
|
|
if (!isTentative) {
|
|
|
|
delete recsys.entries[claimId];
|
|
|
|
recsys.saveEntries();
|
|
|
|
}
|
|
|
|
})
|
|
|
|
.catch((err) => {
|
|
|
|
console.log('RECSYS: failed to send entry', err);
|
|
|
|
});
|
2021-09-03 00:39:40 +02:00
|
|
|
}
|
|
|
|
recsys.log('sendRecsysEntry', claimId);
|
|
|
|
},
|
|
|
|
|
2022-05-24 14:24:04 +02:00
|
|
|
sendEntries: function (entries, isResumedSend) {
|
|
|
|
if (entries) {
|
|
|
|
if (Object.keys(recsys.entries).length !== 0) {
|
|
|
|
// Typically called on startup only.
|
|
|
|
console.warn('RECSYS: sendEntries() called on non-empty state. Data will be overwritten.');
|
|
|
|
}
|
|
|
|
|
|
|
|
recsys.entries = entries;
|
|
|
|
}
|
|
|
|
|
|
|
|
Object.keys(recsys.entries).forEach((claimId) => {
|
|
|
|
recsys.entries[claimId].isResumedSend = isResumedSend;
|
|
|
|
recsys.sendRecsysEntry(claimId, false); // Send and delete.
|
|
|
|
});
|
|
|
|
},
|
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
/**
|
|
|
|
* A player event fired. Get the Entry for the claimId, and add the events
|
|
|
|
* @param claimId
|
|
|
|
* @param event
|
|
|
|
*/
|
|
|
|
onRecsysPlayerEvent: function (claimId, event, isEmbedded) {
|
2022-02-05 00:38:42 +01:00
|
|
|
const state = window.store.getState();
|
|
|
|
const autoPlayNext = state && selectClientSetting(state, SETTINGS.AUTOPLAY_NEXT);
|
|
|
|
// Check if played through (4 = onEnded) and handle multiple events at end
|
|
|
|
if (recsys.entries[claimId] && !recsys.entries[claimId]['autoplay'] === true) {
|
|
|
|
if (autoPlayNext && event.event === 4) {
|
|
|
|
recsys.entries[claimId]['autoplay'] = true;
|
|
|
|
} else {
|
|
|
|
recsys.entries[claimId]['autoplay'] = false;
|
|
|
|
}
|
|
|
|
}
|
2021-09-03 00:39:40 +02:00
|
|
|
if (!recsys.entries[claimId]) {
|
|
|
|
recsys.createRecsysEntry(claimId);
|
|
|
|
// do something to show it's floating or autoplay
|
|
|
|
}
|
|
|
|
if (isEmbedded) {
|
|
|
|
recsys.entries[claimId]['isEmbed'] = true;
|
|
|
|
}
|
|
|
|
recsys.entries[claimId].events.push(event);
|
|
|
|
recsys.log('onRecsysPlayerEvent', claimId);
|
|
|
|
},
|
2022-05-11 12:42:34 +02:00
|
|
|
|
2021-09-03 00:39:40 +02:00
|
|
|
log: function (callName, claimId) {
|
|
|
|
if (recsys.debug) {
|
|
|
|
console.log(`Call: ***${callName}***, ClaimId: ${claimId}, Recsys Entries`, Object.assign({}, recsys.entries));
|
|
|
|
}
|
|
|
|
},
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Player closed. Check to see if primaryUri = playingUri
|
|
|
|
* if so, send the Entry.
|
|
|
|
*/
|
2022-05-10 12:25:09 +02:00
|
|
|
onPlayerDispose: function (claimId, isEmbedded, totalPlayingTime) {
|
2021-10-11 02:19:54 +02:00
|
|
|
if (window && window.store) {
|
2021-09-03 00:39:40 +02:00
|
|
|
const state = window.store.getState();
|
2022-03-16 21:27:46 +01:00
|
|
|
const playingUri = selectPlayingUri(state);
|
2021-09-03 00:39:40 +02:00
|
|
|
const primaryUri = selectPrimaryUri(state);
|
|
|
|
const onFilePage = playingUri === primaryUri;
|
|
|
|
if (!onFilePage || isEmbedded) {
|
|
|
|
if (isEmbedded) {
|
|
|
|
recsys.entries[claimId]['isEmbed'] = true;
|
|
|
|
}
|
2022-05-12 20:30:03 +02:00
|
|
|
recsys.entries[claimId]['totalPlayTime'] = totalPlayingTime;
|
2021-09-03 00:39:40 +02:00
|
|
|
recsys.sendRecsysEntry(claimId);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
recsys.log('PlayerDispose', claimId);
|
|
|
|
},
|
|
|
|
|
|
|
|
// /**
|
|
|
|
// * File page unmount or change event
|
|
|
|
// * Check to see if playingUri, floatingEnabled, primaryUri === playingUri
|
|
|
|
// * If not, send the Entry.
|
|
|
|
// * If floating enabled, leaving file page will pop out player, leading to
|
|
|
|
// * more events until player is disposed. Don't send unless floatingPlayer playingUri
|
|
|
|
// */
|
|
|
|
// onLeaveFilePage: function (primaryUri) {
|
2021-10-11 02:19:54 +02:00
|
|
|
// if (window && window.store) {
|
2021-09-03 00:39:40 +02:00
|
|
|
// const state = window.store.getState();
|
|
|
|
// const claim = makeSelectClaimForUri(primaryUri)(state);
|
|
|
|
// const claimId = claim ? claim.claim_id : null;
|
|
|
|
// const playingUri = selectPlayingUri(state);
|
|
|
|
// const actualPlayingUri = playingUri && playingUri.uri;
|
|
|
|
// // const primaryUri = selectPrimaryUri(state);
|
|
|
|
// const floatingPlayer = makeSelectClientSetting(SETTINGS.FLOATING_PLAYER)(state);
|
|
|
|
// // When leaving page, if floating player is enabled, play will continue.
|
|
|
|
// if (claimId) {
|
|
|
|
// recsys.entries[claimId]['pageExitedAt'] = Date.now();
|
|
|
|
// }
|
|
|
|
// const shouldSend =
|
|
|
|
// (claimId && floatingPlayer && actualPlayingUri && actualPlayingUri !== primaryUri) || !floatingPlayer || !actualPlayingUri;
|
|
|
|
// if (shouldSend) {
|
|
|
|
// recsys.sendRecsysEntry(claimId);
|
|
|
|
// }
|
|
|
|
// recsys.log('LeaveFile', claimId);
|
|
|
|
// }
|
|
|
|
// },
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Navigate event
|
|
|
|
* Send all claimIds that aren't currently playing.
|
|
|
|
*/
|
|
|
|
onNavigate: function () {
|
2021-10-11 02:19:54 +02:00
|
|
|
if (window && window.store) {
|
2021-09-03 00:39:40 +02:00
|
|
|
const state = window.store.getState();
|
2022-03-16 21:27:46 +01:00
|
|
|
const playingUri = selectPlayingUri(state);
|
|
|
|
const actualPlayingUri = playingUri && playingUri.uri;
|
2022-05-24 08:52:54 +02:00
|
|
|
const claim = makeSelectClaimForUri(actualPlayingUri || '')(state);
|
2021-09-03 00:39:40 +02:00
|
|
|
const playingClaimId = claim ? claim.claim_id : null;
|
|
|
|
// const primaryUri = selectPrimaryUri(state);
|
2021-11-23 05:29:04 +01:00
|
|
|
const floatingPlayer = selectClientSetting(state, SETTINGS.FLOATING_PLAYER);
|
2021-09-03 00:39:40 +02:00
|
|
|
// When leaving page, if floating player is enabled, play will continue.
|
|
|
|
Object.keys(recsys.entries).forEach((claimId) => {
|
|
|
|
const shouldSkip = recsys.entries[claimId].parentUuid && !recsys.entries[claimId].recClaimIds;
|
|
|
|
if (!shouldSkip && ((claimId !== playingClaimId && floatingPlayer) || !floatingPlayer)) {
|
|
|
|
recsys.entries[claimId]['pageExitedAt'] = Date.now();
|
2022-05-24 14:24:04 +02:00
|
|
|
recsys.saveEntries();
|
2022-02-05 00:38:42 +01:00
|
|
|
// recsys.sendRecsysEntry(claimId); breaks pop out = off, not helping with browser close.
|
2021-09-03 00:39:40 +02:00
|
|
|
}
|
|
|
|
recsys.log('OnNavigate', claimId);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
},
|
|
|
|
};
|
|
|
|
|
|
|
|
history.listen(() => {
|
|
|
|
recsys.onNavigate();
|
|
|
|
});
|
|
|
|
|
|
|
|
export default recsys;
|