lbry-desktop/src/renderer/redux/actions/publish.js

357 lines
9 KiB
JavaScript
Raw Normal View History

2018-03-26 23:32:43 +02:00
// @flow
2018-10-26 06:20:18 +02:00
import type { Dispatch, GetState } from 'types/redux';
import type { Source, Metadata } from 'types/claim';
import type {
UpdatePublishFormData,
UpdatePublishFormAction,
PublishParams,
} from 'redux/reducers/publish';
2018-05-25 20:05:30 +02:00
import {
ACTIONS,
Lbry,
doNotify,
MODALS,
2018-05-25 20:05:30 +02:00
selectMyChannelClaims,
THUMBNAIL_STATUSES,
2018-06-08 06:05:45 +02:00
batchActions,
2018-10-26 06:20:18 +02:00
creditsToString,
selectPendingById,
selectMyClaimsWithoutChannels,
2018-05-25 20:05:30 +02:00
} from 'lbry-redux';
import { selectosNotificationsEnabled } from 'redux/selectors/settings';
import { doNavigate } from 'redux/actions/navigation';
import fs from 'fs';
import path from 'path';
import { CC_LICENSES, COPYRIGHT, OTHER } from 'constants/licenses';
2018-03-26 23:32:43 +02:00
type Action = UpdatePublishFormAction | { type: ACTIONS.CLEAR_PUBLISH };
export const doResetThumbnailStatus = () => (dispatch: Dispatch<Action>): Promise<Action> => {
2018-06-08 06:05:45 +02:00
dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: {
thumbnailPath: '',
},
});
return fetch('https://spee.ch/api/config/site/publishing')
.then(res => res.json())
.then(status => {
if (status.disabled) {
throw Error();
}
return dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: {
uploadThumbnailStatus: THUMBNAIL_STATUSES.READY,
thumbnail: '',
nsfw: false,
},
});
})
.catch(() =>
dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: {
uploadThumbnailStatus: THUMBNAIL_STATUSES.API_DOWN,
thumbnail: '',
nsfw: false,
},
})
);
2018-06-08 06:05:45 +02:00
};
export const doClearPublish = () => (dispatch: Dispatch<Action>): Promise<Action> => {
dispatch({ type: ACTIONS.CLEAR_PUBLISH });
return dispatch(doResetThumbnailStatus());
};
export const doUpdatePublishForm = (publishFormValue: UpdatePublishFormData) => (
dispatch: Dispatch<Action>
): UpdatePublishFormAction =>
2018-10-13 17:49:47 +02:00
dispatch(
({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { ...publishFormValue },
}: UpdatePublishFormAction)
);
export const doUploadThumbnail = (filePath: string, nsfw: boolean) => (
dispatch: Dispatch<Action>
) => {
const thumbnail = fs.readFileSync(filePath);
const fileExt = path.extname(filePath);
const fileName = path.basename(filePath);
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 = '') =>
dispatch(
batchActions(
{
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { uploadThumbnailStatus: THUMBNAIL_STATUSES.API_DOWN },
},
dispatch(doNotify({ id: MODALS.ERROR, error }))
)
);
dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: { uploadThumbnailStatus: THUMBNAIL_STATUSES.IN_PROGRESS },
});
const data = new FormData();
const name = makeid();
const file = new File([thumbnail], fileName, { type: `image/${fileExt.slice(1)}` });
data.append('name', name);
data.append('file', file);
data.append('nsfw', nsfw.toString());
return fetch('https://spee.ch/api/claim/publish', {
method: 'POST',
body: data,
})
.then(response => response.json())
.then(
json =>
json.success
? dispatch({
type: ACTIONS.UPDATE_PUBLISH_FORM,
data: {
uploadThumbnailStatus: THUMBNAIL_STATUSES.COMPLETE,
thumbnail: `${json.data.url}${fileExt}`,
},
})
: uploadError(json.message)
)
2018-04-02 22:30:18 +02:00
.catch(err => uploadError(err.message));
};
export const doPrepareEdit = (claim: any, uri: string) => (dispatch: Dispatch<Action>) => {
2018-06-08 06:05:45 +02:00
const {
name,
amount,
channel_name: channelName,
value: {
stream: { metadata },
},
} = claim;
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',
},
2018-03-26 23:32:43 +02:00
language,
license,
licenseUrl,
nsfw,
thumbnail,
title,
} = metadata;
2018-10-13 17:49:47 +02:00
const publishData: UpdatePublishFormData = {
2018-03-26 23:32:43 +02:00
name,
channel: channelName,
bid: amount,
price: { amount: fee.amount, currency: fee.currency },
contentIsFree: !fee.amount,
author,
description,
fee,
language,
nsfw,
thumbnail,
title,
uri,
uploadThumbnailStatus: thumbnail ? THUMBNAIL_STATUSES.MANUAL : undefined,
licenseUrl,
2018-03-26 23:32:43 +02:00
};
// Make sure custom liscence's are mapped properly
if (!CC_LICENSES.some(({ value }) => value === license)) {
if (!licenseUrl) {
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 });
};
export const doPublish = (params: PublishParams) => (
dispatch: Dispatch<Action>,
getState: () => {}
) => {
const state = getState();
2018-05-25 20:05:30 +02:00
const myChannels = selectMyChannelClaims(state);
const myClaims = selectMyClaimsWithoutChannels(state);
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,
nsfw,
channel,
title,
contentIsFree,
price,
uri,
sources,
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
const fee = contentIsFree || !price.amount ? undefined : { ...price };
const metadata: Metadata = {
2018-03-26 23:32:43 +02:00
title,
nsfw,
license,
licenseUrl,
language,
thumbnail,
2018-10-13 17:49:47 +02:00
description: description || undefined,
2018-03-26 23:32:43 +02:00
};
2018-10-13 17:49:47 +02:00
const publishPayload: {
name: ?string,
channel_id: string,
bid: ?number,
2018-10-26 06:20:18 +02:00
metadata: ?Metadata,
2018-10-13 17:49:47 +02:00
file_path?: string,
sources?: Source,
} = {
2018-03-26 23:32:43 +02:00
name,
channel_id: channelId,
2018-10-26 06:20:18 +02:00
bid: creditsToString(bid),
2018-03-26 23:32:43 +02:00
metadata,
};
2018-10-26 06:20:18 +02:00
if (fee) {
metadata.fee = {
currency: fee.currency,
amount: creditsToString(fee.amount),
};
}
2018-04-04 01:46:03 +02:00
if (filePath) {
publishPayload.file_path = filePath;
} else {
publishPayload.sources = sources;
2018-04-04 01:46:03 +02:00
}
dispatch({ type: ACTIONS.PUBLISH_START });
2018-03-26 23:32:43 +02:00
const success = pendingClaim => {
const actions = [];
actions.push({
type: ACTIONS.PUBLISH_SUCCESS,
});
actions.push(doNotify({ id: MODALS.PUBLISH }, { uri }));
// 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
? myClaims.map(claim => (isMatch(claim) ? pendingClaim.output : claim))
: myClaims.concat(pendingClaim.output);
actions.push({
type: ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED,
data: {
claims: myNewClaims,
},
});
dispatch(batchActions(...actions));
};
2018-03-26 23:32:43 +02:00
const failure = error => {
dispatch({ type: ACTIONS.PUBLISH_FAIL });
dispatch(doNotify({ id: MODALS.ERROR, error: error.message }));
2018-03-26 23:32:43 +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
export const doCheckPendingPublishes = () => (dispatch: Dispatch<Action>, 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);
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 = () => {
Lbry.claim_list_mine().then(claims => {
2018-06-12 09:12:22 +02:00
claims.forEach(claim => {
// 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
if (selectosNotificationsEnabled(getState())) {
const notif = new window.Notification('LBRY Publish Complete', {
2018-08-28 01:04:07 +02:00
body: `${claim.value.stream.metadata.title} has been published to lbry://${
claim.name
}. Click here to view it`,
silent: false,
});
notif.onclick = () => {
dispatch(
doNavigate('/show', {
2018-08-28 01:04:07 +02:00
uri: claim.permanent_url,
})
);
};
}
2018-06-12 09:12:22 +02:00
}
});
2018-10-26 06:20:18 +02:00
dispatch({
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
};