2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2019-02-05 13:41:19 +01:00
|
|
|
import { CC_LICENSES, COPYRIGHT, OTHER, NONE, PUBLIC_DOMAIN } from 'constants/licenses';
|
2018-10-29 18:23:53 +01:00
|
|
|
import * as MODALS from 'constants/modal_types';
|
2018-05-25 20:05:30 +02:00
|
|
|
import {
|
|
|
|
ACTIONS,
|
|
|
|
Lbry,
|
|
|
|
selectMyChannelClaims,
|
2018-06-14 22:28:50 +02:00
|
|
|
THUMBNAIL_STATUSES,
|
2018-06-08 06:05:45 +02:00
|
|
|
batchActions,
|
2018-10-26 06:20:18 +02:00
|
|
|
creditsToString,
|
|
|
|
selectPendingById,
|
2018-11-02 19:33:00 +01:00
|
|
|
selectMyClaimsWithoutChannels,
|
2018-11-29 06:12:34 +01:00
|
|
|
doError,
|
2019-05-10 01:26:03 +02:00
|
|
|
isClaimNsfw,
|
2018-05-25 20:05:30 +02:00
|
|
|
} from 'lbry-redux';
|
2018-10-29 18:23:53 +01:00
|
|
|
import { doOpenModal } from 'redux/actions/app';
|
2018-08-20 23:48:14 +02:00
|
|
|
import { selectosNotificationsEnabled } from 'redux/selectors/settings';
|
2019-04-04 23:05:23 +02:00
|
|
|
import { push } from 'connected-react-router';
|
2019-03-05 05:46:57 +01:00
|
|
|
import analytics from 'analytics';
|
2019-03-28 17:53:13 +01:00
|
|
|
import { formatLbryUriForWeb } from 'util/uri';
|
2019-03-05 05:46:57 +01:00
|
|
|
// @if TARGET='app'
|
2018-04-02 15:38:06 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2019-03-05 05:46:57 +01:00
|
|
|
// @endif
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
export const doResetThumbnailStatus = () => (dispatch: Dispatch) => {
|
2018-06-08 06:05:45 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
|
|
|
data: {
|
|
|
|
thumbnailPath: '',
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-06-25 19:39:35 +02:00
|
|
|
return fetch('https://spee.ch/api/config/site/publishing')
|
|
|
|
.then(res => res.json())
|
|
|
|
.then(status => {
|
|
|
|
if (status.disabled) {
|
|
|
|
throw Error();
|
|
|
|
}
|
|
|
|
|
|
|
|
return dispatch({
|
2018-04-02 15:38:06 +02:00
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
2018-04-02 18:05:14 +02:00
|
|
|
data: {
|
2018-06-14 22:28:50 +02:00
|
|
|
uploadThumbnailStatus: THUMBNAIL_STATUSES.READY,
|
2018-04-02 18:05:14 +02:00
|
|
|
thumbnail: '',
|
|
|
|
},
|
2018-06-25 19:39:35 +02:00
|
|
|
});
|
|
|
|
})
|
2018-04-02 15:38:06 +02:00
|
|
|
.catch(() =>
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
2018-04-02 18:05:14 +02:00
|
|
|
data: {
|
2018-06-14 22:28:50 +02:00
|
|
|
uploadThumbnailStatus: THUMBNAIL_STATUSES.API_DOWN,
|
2018-04-02 18:05:14 +02:00
|
|
|
thumbnail: '',
|
|
|
|
},
|
2018-04-02 15:38:06 +02:00
|
|
|
})
|
|
|
|
);
|
2018-06-08 06:05:45 +02:00
|
|
|
};
|
2018-04-02 15:38:06 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
export const doClearPublish = () => (dispatch: Dispatch) => {
|
2018-10-03 21:00:06 +02:00
|
|
|
dispatch({ type: ACTIONS.CLEAR_PUBLISH });
|
|
|
|
return dispatch(doResetThumbnailStatus());
|
|
|
|
};
|
|
|
|
|
2019-05-07 23:38:29 +02:00
|
|
|
export const doUpdatePublishForm = (publishFormValue: UpdatePublishFormData) => (dispatch: Dispatch) =>
|
2019-04-24 16:02:08 +02:00
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
|
|
|
data: { ...publishFormValue },
|
|
|
|
});
|
|
|
|
|
2019-05-21 23:18:11 +02:00
|
|
|
export const doUploadThumbnail = (filePath: string, thumbnailBuffer: Uint8Array) => (dispatch: Dispatch) => {
|
|
|
|
let thumbnail, fileExt, fileName, fileType;
|
|
|
|
|
|
|
|
if (filePath) {
|
|
|
|
thumbnail = fs.readFileSync(filePath);
|
|
|
|
fileExt = path.extname(filePath);
|
|
|
|
fileName = path.basename(filePath);
|
|
|
|
fileType = `image/${fileExt.slice(1)}`;
|
|
|
|
} else if (thumbnailBuffer) {
|
|
|
|
thumbnail = thumbnailBuffer;
|
|
|
|
fileExt = '.png';
|
|
|
|
fileName = 'thumbnail.png';
|
|
|
|
fileType = 'image/png';
|
|
|
|
} else {
|
|
|
|
return null;
|
|
|
|
}
|
2018-04-02 15:38:06 +02:00
|
|
|
|
|
|
|
const makeid = () => {
|
|
|
|
let text = '';
|
|
|
|
const possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
|
|
|
|
for (let i = 0; i < 24; i += 1) text += possible.charAt(Math.floor(Math.random() * 62));
|
|
|
|
return text;
|
|
|
|
};
|
|
|
|
|
2018-04-02 22:30:18 +02:00
|
|
|
const uploadError = (error = '') =>
|
2018-04-02 15:38:06 +02:00
|
|
|
dispatch(
|
|
|
|
batchActions(
|
|
|
|
{
|
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
2019-02-01 08:17:23 +01:00
|
|
|
data: {
|
|
|
|
uploadThumbnailStatus: THUMBNAIL_STATUSES.READY,
|
|
|
|
thumbnail: '',
|
|
|
|
nsfw: false,
|
|
|
|
},
|
2018-04-02 15:38:06 +02:00
|
|
|
},
|
2019-02-01 08:17:23 +01:00
|
|
|
doError(error)
|
2018-04-02 15:38:06 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
2018-06-14 22:28:50 +02:00
|
|
|
data: { uploadThumbnailStatus: THUMBNAIL_STATUSES.IN_PROGRESS },
|
2018-04-02 15:38:06 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
const data = new FormData();
|
|
|
|
const name = makeid();
|
2019-05-21 23:18:11 +02:00
|
|
|
const file = new File([thumbnail], fileName, { type: fileType });
|
2018-04-02 15:38:06 +02:00
|
|
|
data.append('name', name);
|
2018-07-05 02:00:49 +02:00
|
|
|
data.append('file', file);
|
2019-04-24 16:02:08 +02:00
|
|
|
|
2018-04-02 15:38:06 +02:00
|
|
|
return fetch('https://spee.ch/api/claim/publish', {
|
|
|
|
method: 'POST',
|
|
|
|
body: data,
|
|
|
|
})
|
|
|
|
.then(response => response.json())
|
2019-03-05 05:46:57 +01:00
|
|
|
.then(json =>
|
|
|
|
json.success
|
|
|
|
? dispatch({
|
2019-05-07 23:38:29 +02:00
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
|
|
|
data: {
|
|
|
|
uploadThumbnailStatus: THUMBNAIL_STATUSES.COMPLETE,
|
|
|
|
thumbnail: `${json.data.url}${fileExt}`,
|
|
|
|
},
|
|
|
|
})
|
2019-03-05 05:46:57 +01:00
|
|
|
: uploadError(json.message)
|
2018-04-02 15:38:06 +02:00
|
|
|
)
|
2018-04-02 22:30:18 +02:00
|
|
|
.catch(err => uploadError(err.message));
|
2018-04-02 15:38:06 +02:00
|
|
|
};
|
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
export const doPrepareEdit = (claim: StreamClaim, uri: string) => (dispatch: Dispatch) => {
|
|
|
|
const { name, amount, channel_name: channelName, value } = claim;
|
2018-04-02 15:38:06 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
const {
|
|
|
|
author,
|
|
|
|
description,
|
2018-04-03 06:21:33 +02:00
|
|
|
// use same values as default state
|
|
|
|
// fee will be undefined for free content
|
|
|
|
fee = {
|
|
|
|
amount: 0,
|
|
|
|
currency: 'LBC',
|
|
|
|
},
|
2019-04-24 16:02:08 +02:00
|
|
|
languages,
|
2018-03-26 23:32:43 +02:00
|
|
|
license,
|
2019-04-24 16:02:08 +02:00
|
|
|
license_url: licenseUrl,
|
2018-03-26 23:32:43 +02:00
|
|
|
thumbnail,
|
|
|
|
title,
|
2019-04-24 16:02:08 +02:00
|
|
|
} = value;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-10-13 17:49:47 +02:00
|
|
|
const publishData: UpdatePublishFormData = {
|
2018-03-26 23:32:43 +02:00
|
|
|
name,
|
|
|
|
channel: channelName,
|
|
|
|
bid: amount,
|
|
|
|
contentIsFree: !fee.amount,
|
|
|
|
author,
|
|
|
|
description,
|
2019-05-10 01:26:03 +02:00
|
|
|
fee: { amount: fee.amount, currency: fee.currency },
|
2019-04-24 16:02:08 +02:00
|
|
|
languages,
|
|
|
|
thumbnail: thumbnail ? thumbnail.url : null,
|
2018-03-26 23:32:43 +02:00
|
|
|
title,
|
2018-04-06 08:00:36 +02:00
|
|
|
uri,
|
2018-06-14 22:28:50 +02:00
|
|
|
uploadThumbnailStatus: thumbnail ? THUMBNAIL_STATUSES.MANUAL : undefined,
|
2018-10-03 21:00:06 +02:00
|
|
|
licenseUrl,
|
2019-05-10 16:50:33 +02:00
|
|
|
nsfw: isClaimNsfw(claim),
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2018-10-03 21:00:06 +02:00
|
|
|
// Make sure custom liscence's are mapped properly
|
2018-11-08 05:14:35 +01:00
|
|
|
// If the license isn't one of the standard licenses, map the custom license and description/url
|
2018-10-03 21:00:06 +02:00
|
|
|
if (!CC_LICENSES.some(({ value }) => value === license)) {
|
2018-11-08 05:14:35 +01:00
|
|
|
if (!license || license === NONE || license === PUBLIC_DOMAIN) {
|
|
|
|
publishData.licenseType = license;
|
|
|
|
} else if (license && !licenseUrl && license !== NONE) {
|
2018-10-03 21:00:06 +02:00
|
|
|
publishData.licenseType = COPYRIGHT;
|
|
|
|
} else {
|
|
|
|
publishData.licenseType = OTHER;
|
|
|
|
}
|
|
|
|
|
|
|
|
publishData.otherLicenseDescription = license;
|
|
|
|
} else {
|
|
|
|
publishData.licenseType = license;
|
|
|
|
}
|
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
dispatch({ type: ACTIONS.DO_PREPARE_EDIT, data: publishData });
|
|
|
|
};
|
|
|
|
|
2019-03-18 06:09:50 +01:00
|
|
|
export const doPublish = (params: PublishParams) => (dispatch: Dispatch, getState: () => {}) => {
|
2019-04-24 16:02:08 +02:00
|
|
|
dispatch({ type: ACTIONS.PUBLISH_START });
|
|
|
|
|
2018-04-06 08:00:36 +02:00
|
|
|
const state = getState();
|
2018-05-25 20:05:30 +02:00
|
|
|
const myChannels = selectMyChannelClaims(state);
|
2018-11-02 19:33:00 +01:00
|
|
|
const myClaims = selectMyClaimsWithoutChannels(state);
|
2018-04-06 08:00:36 +02:00
|
|
|
|
2018-03-26 23:32:43 +02:00
|
|
|
const {
|
|
|
|
name,
|
|
|
|
bid,
|
2018-04-03 06:21:33 +02:00
|
|
|
filePath,
|
2018-03-26 23:32:43 +02:00
|
|
|
description,
|
|
|
|
language,
|
|
|
|
license,
|
|
|
|
licenseUrl,
|
|
|
|
thumbnail,
|
|
|
|
channel,
|
|
|
|
title,
|
|
|
|
contentIsFree,
|
2019-05-10 08:27:51 +02:00
|
|
|
fee,
|
2018-03-26 23:32:43 +02:00
|
|
|
uri,
|
2019-04-24 16:02:08 +02:00
|
|
|
nsfw,
|
2019-05-10 01:26:03 +02:00
|
|
|
claim,
|
2018-03-26 23:32:43 +02:00
|
|
|
} = params;
|
|
|
|
|
2018-05-25 20:05:30 +02:00
|
|
|
// get the claim id from the channel name, we will use that instead
|
|
|
|
const namedChannelClaim = myChannels.find(myChannel => myChannel.name === channel);
|
|
|
|
const channelId = namedChannelClaim ? namedChannelClaim.claim_id : '';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-10-13 17:49:47 +02:00
|
|
|
const publishPayload: {
|
|
|
|
name: ?string,
|
2019-04-24 16:02:08 +02:00
|
|
|
channel_id?: string,
|
|
|
|
bid: number,
|
2018-10-13 17:49:47 +02:00
|
|
|
file_path?: string,
|
2019-05-10 16:50:33 +02:00
|
|
|
tags: Array<string>,
|
2019-05-10 01:26:03 +02:00
|
|
|
locations?: Array<Location>,
|
2019-05-10 06:13:17 +02:00
|
|
|
license_url?: string,
|
|
|
|
thumbnail_url?: string,
|
|
|
|
release_time?: number,
|
2019-05-10 16:50:33 +02:00
|
|
|
fee_currency?: string,
|
|
|
|
fee_amount?: string,
|
2018-10-13 17:49:47 +02:00
|
|
|
} = {
|
2018-03-26 23:32:43 +02:00
|
|
|
name,
|
2018-10-26 06:20:18 +02:00
|
|
|
bid: creditsToString(bid),
|
2019-04-24 16:02:08 +02:00
|
|
|
title,
|
|
|
|
license,
|
|
|
|
languages: [language],
|
|
|
|
description,
|
2019-05-10 08:27:51 +02:00
|
|
|
tags: (claim && claim.value.tags) || [],
|
2019-05-10 06:13:17 +02:00
|
|
|
locations: claim && claim.value.locations,
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
// Temporary solution to keep the same publish flow with the new tags api
|
|
|
|
// Eventually we will allow users to enter their own tags on publish
|
|
|
|
// `nsfw` will probably be removed
|
2019-05-10 01:26:03 +02:00
|
|
|
|
|
|
|
if (licenseUrl) {
|
|
|
|
publishPayload.license_url = licenseUrl;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (thumbnail) {
|
|
|
|
publishPayload.thumbnail_url = thumbnail;
|
|
|
|
}
|
|
|
|
|
2019-05-10 06:13:17 +02:00
|
|
|
if (claim && claim.value.release_time) {
|
2019-05-10 16:50:33 +02:00
|
|
|
publishPayload.release_time = Number(claim.value.release_time);
|
2019-05-10 01:26:03 +02:00
|
|
|
}
|
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
if (nsfw) {
|
2019-05-10 08:27:51 +02:00
|
|
|
if (!publishPayload.tags.includes('mature')) {
|
2019-05-10 01:26:03 +02:00
|
|
|
publishPayload.tags.push('mature');
|
|
|
|
}
|
|
|
|
} else {
|
2019-05-10 16:50:33 +02:00
|
|
|
const indexToRemove = publishPayload.tags.indexOf('mature');
|
|
|
|
if (indexToRemove > -1) {
|
|
|
|
publishPayload.tags.splice(indexToRemove, 1);
|
2019-05-10 01:26:03 +02:00
|
|
|
}
|
2019-04-24 16:02:08 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (channelId) {
|
|
|
|
publishPayload.channel_id = channelId;
|
|
|
|
}
|
|
|
|
|
2019-05-10 16:50:33 +02:00
|
|
|
if (!contentIsFree && fee && (fee.currency && Number(fee.amount) > 0)) {
|
2019-05-10 08:27:51 +02:00
|
|
|
publishPayload.fee_currency = fee.currency;
|
|
|
|
publishPayload.fee_amount = creditsToString(fee.amount);
|
2018-10-26 06:20:18 +02:00
|
|
|
}
|
2018-04-04 01:46:03 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
// Only pass file on new uploads, not metadata only edits.
|
|
|
|
// The sdk will figure it out
|
|
|
|
if (filePath) publishPayload.file_path = filePath;
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2019-04-24 16:02:08 +02:00
|
|
|
const success = successResponse => {
|
2019-02-05 19:36:40 +01:00
|
|
|
analytics.apiLogPublish();
|
2019-04-24 16:02:08 +02:00
|
|
|
|
|
|
|
const pendingClaim = successResponse.outputs[0];
|
2018-11-02 19:33:00 +01:00
|
|
|
const actions = [];
|
|
|
|
|
|
|
|
actions.push({
|
2018-04-06 08:00:36 +02:00
|
|
|
type: ACTIONS.PUBLISH_SUCCESS,
|
|
|
|
});
|
2018-11-02 19:33:00 +01:00
|
|
|
|
2018-10-29 18:23:53 +01:00
|
|
|
actions.push(doOpenModal(MODALS.PUBLISH, { uri }));
|
2018-11-02 19:33:00 +01:00
|
|
|
|
|
|
|
// We have to fake a temp claim until the new pending one is returned by claim_list_mine
|
|
|
|
// We can't rely on claim_list_mine because there might be some delay before the new claims are returned
|
|
|
|
// Doing this allows us to show the pending claim immediately, it will get overwritten by the real one
|
|
|
|
const isMatch = claim => claim.claim_id === pendingClaim.claim_id;
|
|
|
|
const isEdit = myClaims.some(isMatch);
|
|
|
|
const myNewClaims = isEdit
|
2019-04-24 16:02:08 +02:00
|
|
|
? myClaims.map(claim => (isMatch(claim) ? pendingClaim : claim))
|
|
|
|
: myClaims.concat(pendingClaim);
|
2018-11-02 19:33:00 +01:00
|
|
|
|
|
|
|
actions.push({
|
|
|
|
type: ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED,
|
|
|
|
data: {
|
|
|
|
claims: myNewClaims,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(batchActions(...actions));
|
2018-04-06 08:00:36 +02:00
|
|
|
};
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-04-06 08:00:36 +02:00
|
|
|
const failure = error => {
|
|
|
|
dispatch({ type: ACTIONS.PUBLISH_FAIL });
|
2018-11-29 06:12:34 +01:00
|
|
|
dispatch(doError(error.message));
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
2018-04-06 08:00:36 +02:00
|
|
|
|
|
|
|
return Lbry.publish(publishPayload).then(success, failure);
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// Calls claim_list_mine until any pending publishes are confirmed
|
2019-03-18 06:09:50 +01:00
|
|
|
export const doCheckPendingPublishes = () => (dispatch: Dispatch, getState: GetState) => {
|
2018-04-03 06:21:33 +02:00
|
|
|
const state = getState();
|
2018-10-26 06:20:18 +02:00
|
|
|
const pendingById = selectPendingById(state);
|
2018-11-02 19:33:00 +01:00
|
|
|
|
|
|
|
if (!Object.keys(pendingById).length) {
|
2018-10-26 06:20:18 +02:00
|
|
|
return;
|
|
|
|
}
|
2018-04-03 06:21:33 +02:00
|
|
|
|
|
|
|
let publishCheckInterval;
|
|
|
|
|
|
|
|
const checkFileList = () => {
|
2019-04-24 16:02:08 +02:00
|
|
|
Lbry.claim_list().then(claims => {
|
2018-06-12 09:12:22 +02:00
|
|
|
claims.forEach(claim => {
|
2018-11-02 19:33:00 +01:00
|
|
|
// If it's confirmed, check if it was pending previously
|
2018-10-26 06:20:18 +02:00
|
|
|
if (claim.confirmations > 0 && pendingById[claim.claim_id]) {
|
|
|
|
delete pendingById[claim.claim_id];
|
|
|
|
|
|
|
|
// If it's confirmed, check if we should notify the user
|
2018-08-20 23:48:14 +02:00
|
|
|
if (selectosNotificationsEnabled(getState())) {
|
|
|
|
const notif = new window.Notification('LBRY Publish Complete', {
|
2019-05-07 23:38:29 +02:00
|
|
|
body: `${claim.value.title} has been published to lbry://${claim.name}. Click here to view it`,
|
2018-08-28 01:04:07 +02:00
|
|
|
silent: false,
|
2018-08-20 23:48:14 +02:00
|
|
|
});
|
|
|
|
notif.onclick = () => {
|
2019-04-04 23:05:23 +02:00
|
|
|
dispatch(push(formatLbryUriForWeb(claim.permanent_url)));
|
2018-08-20 23:48:14 +02:00
|
|
|
};
|
|
|
|
}
|
2018-06-12 09:12:22 +02:00
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-10-26 06:20:18 +02:00
|
|
|
dispatch({
|
2018-06-13 05:28:06 +02:00
|
|
|
type: ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED,
|
|
|
|
data: {
|
|
|
|
claims,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
2018-10-26 06:20:18 +02:00
|
|
|
if (!Object.keys(pendingById).length) {
|
2018-04-03 06:21:33 +02:00
|
|
|
clearInterval(publishCheckInterval);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-10-26 06:20:18 +02:00
|
|
|
publishCheckInterval = setInterval(() => {
|
2018-04-03 06:21:33 +02:00
|
|
|
checkFileList();
|
2018-10-26 06:20:18 +02:00
|
|
|
}, 30000);
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|