diff --git a/src/providers/steem/dsteem.js b/src/providers/steem/dsteem.js index a20e4276b..77a2a389d 100644 --- a/src/providers/steem/dsteem.js +++ b/src/providers/steem/dsteem.js @@ -1178,6 +1178,73 @@ export const boost = (currentAccount, pinCode, point, permlink, author) => { return Promise.reject(new Error('Private key permission issue!')); }; +export const grantPostingPermission = async (params, pin, currentAccount) => { + const digitPinCode = getDigitPinCode(pin); + const key = getActiveKey(get(currentAccount, 'local'), digitPinCode); + + const newPosting = Object.assign( + {}, + { ...get(currentAccount, 'posting') }, + { + account_auths: [ + ...get(currentAccount, 'posting.account_auths'), + ['esteemapp', get(currentAccount, 'posting.weight_threshold')], + ], + }, + ); + if (get(currentAccount, 'local.authType') === AUTH_TYPE.STEEM_CONNECT) { + const token = decryptKey(get(currentAccount, 'local.accessToken'), digitPinCode); + const api = new steemconnect.Client({ + accessToken: token, + }); + const _params = { + account: get(currentAccount, 'name'), + posting: newPosting, + memo_key: get(currentAccount, 'memo_key'), + json_metadata: jsonStringify(params), + }; + + const opArray = [['account_update', _params]]; + + return api + .broadcast(opArray) + .then(resp => resp.result) + .catch(error => console.log(error)); + } + + if (key) { + newPosting.account_auths.sort(); + const opArray = [ + [ + 'account_update', + { + account: get(currentAccount, 'name'), + memo_key: get(currentAccount, 'memo_key'), + json_metadata: jsonStringify(params), + posting: newPosting, + }, + ], + ]; + const privateKey = PrivateKey.fromString(key); + + return new Promise((resolve, reject) => { + client.broadcast + .sendOperations(opArray, privateKey) + .then(result => { + resolve(result); + }) + .catch(error => { + if (get(error, 'jse_info.code') === 4030100) { + error.message = getDsteemDateErrorMessage(error); + } + reject(error); + }); + }); + } + + return Promise.reject(new Error('Private key permission issue!')); +}; + export const profileUpdate = async (params, pin, currentAccount) => { const digitPinCode = getDigitPinCode(pin); const key = getActiveKey(get(currentAccount, 'local'), digitPinCode); diff --git a/src/screens/editor/container/editorContainer.js b/src/screens/editor/container/editorContainer.js index 150206164..94e184430 100644 --- a/src/screens/editor/container/editorContainer.js +++ b/src/screens/editor/container/editorContainer.js @@ -10,7 +10,7 @@ import AsyncStorage from '@react-native-community/async-storage'; import { Buffer } from 'buffer'; import { uploadImage, addDraft, updateDraft, schedule } from '../../../providers/esteem/esteem'; import { toastNotification } from '../../../redux/actions/uiAction'; -import { postContent, getPurePost } from '../../../providers/steem/dsteem'; +import { postContent, getPurePost, grantPostingPermission } from '../../../providers/steem/dsteem'; import { setDraftPost, getDraftPost } from '../../../realm/realm'; // Constants @@ -449,12 +449,38 @@ class EditorContainer extends Component { } }; - _handleDatePickerChange = (datePickerValue, fields) => { - this._submitPost(fields, datePickerValue); + _handleDatePickerChange = async (datePickerValue, fields) => { + const { currentAccount, pinCode, intl } = this.props; + + const params = get(currentAccount, 'json_metadata', ''); + + let hasPostingPerm = false; + + if (currentAccount && currentAccount.posting) { + hasPostingPerm = + currentAccount.posting.account_auths.filter(x => x[0] === 'esteemapp').length > 0; + } + + if (hasPostingPerm) { + this._submitPost(fields, datePickerValue); + } else { + await grantPostingPermission(params, pinCode, currentAccount) + .then(() => { + this._submitPost(fields, datePickerValue); + }) + .catch(error => { + Alert.alert( + intl.formatMessage({ + id: 'alert.fail', + }), + get(error, 'message', error.toString()), + ); + }); + } }; _setScheduledPost = data => { - const { dispatch, intl } = this.props; + const { dispatch, intl, currentAccount, navigation } = this.props; schedule( data.author, @@ -476,6 +502,13 @@ class EditorContainer extends Component { }), ), ); + setDraftPost({ title: '', body: '', tags: '' }, currentAccount.name); + setTimeout(() => { + navigation.navigate({ + routeName: ROUTES.SCREENS.DRAFTS, + key: currentAccount.name, + }); + }, 3000); }) .catch(() => { this.setState({ isPostSending: false });