2018-03-26 23:32:43 +02:00
|
|
|
// @flow
|
2018-05-25 20:05:30 +02:00
|
|
|
import {
|
|
|
|
ACTIONS,
|
|
|
|
Lbry,
|
|
|
|
doNotify,
|
|
|
|
MODALS,
|
|
|
|
selectMyChannelClaims,
|
2018-06-14 22:28:50 +02:00
|
|
|
THUMBNAIL_STATUSES,
|
2018-06-08 06:05:45 +02:00
|
|
|
batchActions,
|
2018-05-25 20:05:30 +02:00
|
|
|
} from 'lbry-redux';
|
2018-03-26 23:32:43 +02:00
|
|
|
import { selectPendingPublishes } from 'redux/selectors/publish';
|
|
|
|
import type {
|
|
|
|
UpdatePublishFormData,
|
|
|
|
UpdatePublishFormAction,
|
|
|
|
PublishParams,
|
|
|
|
} from 'redux/reducers/publish';
|
2018-04-02 15:38:06 +02:00
|
|
|
import fs from 'fs';
|
|
|
|
import path from 'path';
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
type Action = UpdatePublishFormAction | { type: ACTIONS.CLEAR_PUBLISH };
|
|
|
|
type PromiseAction = Promise<Action>;
|
|
|
|
type Dispatch = (action: Action | PromiseAction | Array<Action>) => any;
|
|
|
|
type GetState = () => {};
|
|
|
|
|
2018-06-13 06:19:39 +02:00
|
|
|
export const doClearPublish = () => (dispatch: Dispatch): PromiseAction => {
|
2018-03-26 23:32:43 +02:00
|
|
|
dispatch({ type: ACTIONS.CLEAR_PUBLISH });
|
2018-06-13 06:19:39 +02:00
|
|
|
return dispatch(doResetThumbnailStatus());
|
|
|
|
};
|
2018-03-26 23:32:43 +02:00
|
|
|
|
|
|
|
export const doUpdatePublishForm = (publishFormValue: UpdatePublishFormData) => (
|
|
|
|
dispatch: Dispatch
|
|
|
|
): UpdatePublishFormAction =>
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.UPDATE_PUBLISH_FORM,
|
|
|
|
data: { ...publishFormValue },
|
|
|
|
});
|
|
|
|
|
2018-06-08 06:05:45 +02:00
|
|
|
export const doResetThumbnailStatus = () => (dispatch: Dispatch): PromiseAction => {
|
|
|
|
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: '',
|
|
|
|
nsfw: false,
|
|
|
|
},
|
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: '',
|
|
|
|
nsfw: false,
|
|
|
|
},
|
2018-04-02 15:38:06 +02:00
|
|
|
})
|
|
|
|
);
|
2018-06-08 06:05:45 +02:00
|
|
|
};
|
2018-04-02 15:38:06 +02:00
|
|
|
|
|
|
|
export const doUploadThumbnail = (filePath: string, nsfw: boolean) => (dispatch: Dispatch) => {
|
|
|
|
const thumbnail = fs.readFileSync(filePath);
|
|
|
|
const fileExt = path.extname(filePath);
|
2018-07-05 02:00:49 +02:00
|
|
|
const fileName = path.basename(filePath);
|
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,
|
2018-06-14 22:28:50 +02:00
|
|
|
data: { uploadThumbnailStatus: THUMBNAIL_STATUSES.API_DOWN },
|
2018-04-02 15:38:06 +02:00
|
|
|
},
|
2018-04-02 22:27:20 +02:00
|
|
|
dispatch(doNotify({ id: MODALS.ERROR, 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();
|
2018-07-05 02:00:49 +02:00
|
|
|
const file = new File([thumbnail], fileName, { type: `image/${fileExt.slice(1)}` });
|
2018-04-02 15:38:06 +02:00
|
|
|
data.append('name', name);
|
2018-07-05 02:00:49 +02:00
|
|
|
data.append('file', file);
|
2018-04-02 15:38:06 +02:00
|
|
|
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: {
|
2018-06-14 22:28:50 +02:00
|
|
|
uploadThumbnailStatus: THUMBNAIL_STATUSES.COMPLETE,
|
2018-07-17 15:42:47 +02:00
|
|
|
thumbnail: `${json.data.url}${fileExt}`,
|
2018-04-02 15:38:06 +02:00
|
|
|
},
|
|
|
|
})
|
2018-04-02 22:30:18 +02:00
|
|
|
: uploadError('Upload failed')
|
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
|
|
|
};
|
|
|
|
|
2018-06-08 06:05:45 +02:00
|
|
|
export const doPrepareEdit = (claim: any, uri: string) => (dispatch: Dispatch) => {
|
|
|
|
const {
|
|
|
|
name,
|
|
|
|
amount,
|
|
|
|
channel_name: channelName,
|
|
|
|
value: {
|
|
|
|
stream: { metadata },
|
|
|
|
},
|
|
|
|
} = 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',
|
|
|
|
},
|
2018-03-26 23:32:43 +02:00
|
|
|
language,
|
|
|
|
license,
|
|
|
|
licenseUrl,
|
|
|
|
nsfw,
|
|
|
|
thumbnail,
|
|
|
|
title,
|
|
|
|
} = metadata;
|
|
|
|
|
|
|
|
const publishData = {
|
|
|
|
name,
|
|
|
|
channel: channelName,
|
|
|
|
bid: amount,
|
|
|
|
price: { amount: fee.amount, currency: fee.currency },
|
|
|
|
contentIsFree: !fee.amount,
|
|
|
|
author,
|
|
|
|
description,
|
|
|
|
fee,
|
|
|
|
language,
|
2018-06-25 20:23:25 +02:00
|
|
|
licenseType: license,
|
2018-03-26 23:32:43 +02:00
|
|
|
licenseUrl,
|
|
|
|
nsfw,
|
|
|
|
thumbnail,
|
|
|
|
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-03-26 23:32:43 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
dispatch({ type: ACTIONS.DO_PREPARE_EDIT, data: publishData });
|
|
|
|
};
|
|
|
|
|
2018-04-06 08:00:36 +02:00
|
|
|
export const doPublish = (params: PublishParams) => (dispatch: Dispatch, getState: () => {}) => {
|
|
|
|
const state = getState();
|
2018-05-25 20:05:30 +02:00
|
|
|
const myChannels = selectMyChannelClaims(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,
|
|
|
|
nsfw,
|
|
|
|
channel,
|
|
|
|
title,
|
|
|
|
contentIsFree,
|
|
|
|
price,
|
|
|
|
uri,
|
2018-04-04 19:18:52 +02:00
|
|
|
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 = {
|
|
|
|
title,
|
|
|
|
nsfw,
|
|
|
|
license,
|
|
|
|
licenseUrl,
|
|
|
|
language,
|
|
|
|
thumbnail,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (fee) {
|
|
|
|
metadata.fee = fee;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (description) {
|
|
|
|
metadata.description = description;
|
|
|
|
}
|
|
|
|
|
|
|
|
const publishPayload = {
|
|
|
|
name,
|
2018-05-16 01:01:53 +02:00
|
|
|
channel_id: channelId,
|
2018-03-26 23:32:43 +02:00
|
|
|
bid,
|
|
|
|
metadata,
|
|
|
|
};
|
|
|
|
|
2018-04-04 01:46:03 +02:00
|
|
|
if (filePath) {
|
|
|
|
publishPayload.file_path = filePath;
|
|
|
|
} else {
|
2018-04-04 19:18:52 +02:00
|
|
|
publishPayload.sources = sources;
|
2018-04-04 01:46:03 +02:00
|
|
|
}
|
|
|
|
|
2018-04-06 08:00:36 +02:00
|
|
|
dispatch({ type: ACTIONS.PUBLISH_START });
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-04-06 08:00:36 +02:00
|
|
|
const success = () => {
|
|
|
|
dispatch({
|
|
|
|
type: ACTIONS.PUBLISH_SUCCESS,
|
2018-06-20 21:30:52 +02:00
|
|
|
data: { pendingPublish: { ...publishPayload } },
|
2018-04-06 08:00:36 +02:00
|
|
|
});
|
2018-04-19 18:51:18 +02:00
|
|
|
dispatch(doNotify({ id: MODALS.PUBLISH }, { uri }));
|
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-04-24 03:15:15 +02:00
|
|
|
dispatch(doNotify({ id: MODALS.ERROR, error: 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
|
2018-04-03 06:21:33 +02:00
|
|
|
export const doCheckPendingPublishes = () => (dispatch: Dispatch, getState: GetState) => {
|
|
|
|
const state = getState();
|
|
|
|
const pendingPublishes = selectPendingPublishes(state);
|
|
|
|
|
|
|
|
let publishCheckInterval;
|
|
|
|
|
|
|
|
const checkFileList = () => {
|
|
|
|
Lbry.claim_list_mine().then(claims => {
|
2018-06-12 09:12:22 +02:00
|
|
|
const pendingPublishMap = {};
|
|
|
|
pendingPublishes.forEach(({ name }) => {
|
|
|
|
pendingPublishMap[name] = name;
|
|
|
|
});
|
|
|
|
|
2018-06-13 05:28:06 +02:00
|
|
|
const actions = [];
|
2018-06-12 09:12:22 +02:00
|
|
|
claims.forEach(claim => {
|
|
|
|
if (pendingPublishMap[claim.name]) {
|
2018-06-13 05:28:06 +02:00
|
|
|
actions.push({
|
2018-06-12 09:12:22 +02:00
|
|
|
type: ACTIONS.REMOVE_PENDING_PUBLISH,
|
|
|
|
data: {
|
|
|
|
name: claim.name,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
delete pendingPublishMap[claim.name];
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2018-06-13 05:28:06 +02:00
|
|
|
actions.push({
|
|
|
|
type: ACTIONS.FETCH_CLAIM_LIST_MINE_COMPLETED,
|
|
|
|
data: {
|
|
|
|
claims,
|
|
|
|
},
|
|
|
|
});
|
|
|
|
|
|
|
|
dispatch(batchActions(...actions));
|
|
|
|
|
2018-06-16 01:16:23 +02:00
|
|
|
if (!Object.keys(pendingPublishMap).length) {
|
2018-04-03 06:21:33 +02:00
|
|
|
clearInterval(publishCheckInterval);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
};
|
2018-03-26 23:32:43 +02:00
|
|
|
|
2018-04-03 06:21:33 +02:00
|
|
|
if (pendingPublishes.length) {
|
|
|
|
checkFileList();
|
|
|
|
publishCheckInterval = setInterval(() => {
|
2018-03-26 23:32:43 +02:00
|
|
|
checkFileList();
|
2018-06-16 01:16:23 +02:00
|
|
|
}, 30000);
|
2018-04-03 06:21:33 +02:00
|
|
|
}
|
2018-03-26 23:32:43 +02:00
|
|
|
};
|