Last active
March 6, 2018 00:55
-
-
Save elmariachi111/ce1bf713e0286a2a6c60aca9366af8e0 to your computer and use it in GitHub Desktop.
blockstagram / index.jsx
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const blockstack = require( 'blockstack' ); | |
const { getPublicKeyFromPrivate } = require('blockstack'); | |
const { encryptECIES, decryptECIES } = require('blockstack/lib/encryption') | |
... | |
class App extends React.Component { | |
constructor() { | |
super() | |
this.state = { | |
userData: null, | |
imageFeed: [], | |
subscribers: [], | |
aesKey: null, | |
tab: 'my' | |
} | |
} | |
componentDidMount() { | |
if (blockstack.isSignInPending()) { | |
blockstack.handlePendingSignIn().then((data) => { | |
this.setupUser() | |
this.setupKey() | |
this.setupSubscriber() | |
window.history.pushState(null, null, '/') | |
}) | |
} else if (blockstack.isUserSignedIn()) { | |
this.setupUser().then(() => { | |
this.setupSubscriber() | |
}) | |
} | |
} | |
setupUser() { | |
const userData = blockstack.loadUserData(); | |
this.setState({ | |
userData: userData, | |
loggedIn: true | |
}); | |
return blockstack.getFile('index.json').then(data => { | |
if (data && !(data instanceof ArrayBuffer)) { | |
let indexJson = JSON.parse(data) || {'images':[]}; | |
this.setState({index: indexJson}); | |
} | |
}) | |
.then(() => { | |
//get all the images | |
let promises = this.state.index.images.map((image) => { | |
return this.fetchFile(image.path) | |
}) | |
return Promise.all(promises) | |
}) | |
.then((images) => { | |
this.setState({ images: images }) | |
}) | |
.catch((e) => { | |
console.error(e) | |
}) | |
} | |
fetchFile(path) { | |
return blockstack.getFile(path) | |
} | |
updateIndexAndImages(path, image) { | |
let index = this.state.index | |
const created = moment().toISOString(); | |
index['images'] = [...index.images, {path, created}] | |
let images = [...this.state.images, image] | |
blockstack.putFile('index.json', JSON.stringify(index)) | |
.then(() => { | |
console.log('Index.json uploaded') | |
this.setState({ index, images }) | |
}) | |
.catch((e) => { | |
console.error(e) | |
}) | |
} | |
updateFeed(images) { | |
const newImageFeed = this.state.imageFeed; | |
newImageFeed.push(images); | |
newImageFeed.sort((imageA, imageB) => { return imageA.created < imageB.created}); | |
this.setState({imageFeed: newImageFeed}); | |
} | |
setupSubscribers() { | |
blockstack.getFile('subscribers.json').then((data) => { | |
console.log('data returned from subscribers.json', data); | |
this.setState({subscribers: JSON.parse(data || [])}) | |
this.readSubscribersImages() | |
}).catch(err => { | |
console.warn(err); | |
}) | |
} | |
readSingleSubscribersImages(username) { | |
blockstack.getFile('index.json', { | |
username: username | |
}).then(indexData => { | |
let data = JSON.parse(indexData); | |
data.images.map((indexEntry) => { | |
blockstack.getFile(indexEntry.path, {username}).then((imageData) => { | |
this.updateFeed({path: indexEntry.path, username: username, image: imageData, created: indexEntry.created}); | |
}) | |
}); | |
}).catch(err => { | |
console.warn(err); | |
}); | |
} | |
readSubscribersImages () { | |
this.state.subscribers.forEach(subscriber => { | |
this.readSingleSubscribersImages(subscriber.username); | |
}); | |
} | |
addSubscriber (newSubscriber) { | |
blockstack.getFile('key.json', { | |
username: newSubscriber | |
}).then(keyData => { | |
let subscribers = this.state.subscribers; | |
subscribers.push({username: newSubscriber, publicKey: JSON.parse(keyData)}); | |
this.setState({subscribers}) | |
this.persistSubscribers(); | |
this.readSingleSubscribersImages(newSubscriber); | |
}) | |
.catch(e => { | |
console.log(newSubscriber + ' is no blockstagram user yet'); | |
}) | |
} | |
persistSubscribers() { | |
blockstack.putFile('subscribers.json', JSON.stringify(this.state.subscribers)) | |
.then(() => 'submitted subscribers.json') | |
.catch(e => console.dir(e)) | |
} | |
render () { | |
... | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment