fix(votes): actual votes display works now

This commit is contained in:
Alexey Kasyanchuk
2018-04-18 17:12:25 +03:00
parent 57de180bfd
commit 5e02b0df73
3 changed files with 52 additions and 26 deletions

View File

@ -20,6 +20,7 @@ import LinearProgress from 'material-ui/LinearProgress';
import FlatButton from 'material-ui/FlatButton';
import {fileTypeDetect} from './content'
import {contentIcon} from './torrent'
import waitObject from './waitObject'
let buildFilesTree = (filesList) => {
let rootTree = {
@ -182,7 +183,7 @@ export default class TorrentPage extends Page {
window.torrentSocket.emit('torrent', this.props.hash, {files: true, peer: this.props.peer}, window.customLoader((data) => {
if(data) {
this.torrent = data
this.setTitle(this.torrent.name + ' - Rats On TheBoat');
this.setTitle(this.torrent.name + ' - Rats On The Boat');
if(this.torrent.contentCategory == 'xxx') {
this.setMetaTag('robots', 'noindex');
}
@ -226,18 +227,18 @@ export default class TorrentPage extends Page {
}
window.torrentSocket.on('trackerTorrentUpdate', this.trackerUpdate);
this.onVote = ({hash, good, bad}) => {
this.onVotes = async ({hash, good, bad}) => {
if(this.props.hash != hash)
return;
if(!this.torrent)
return;
// in some cases torrent object can be resolve late
await waitObject(this, 'torrent')
this.torrent.good = good;
this.torrent.bad = bad;
this.forceUpdate();
}
window.torrentSocket.on('vote', this.onVote);
window.torrentSocket.on('votes', this.onVotes);
this.downloading = (hash) => {
if(this.props.hash != hash)
@ -273,8 +274,8 @@ export default class TorrentPage extends Page {
window.torrentSocket.off('filesReady', this.filesUpdated);
if(this.trackerUpdate)
window.torrentSocket.off('trackerTorrentUpdate', this.trackerUpdate);
if(this.onVote)
window.torrentSocket.off('vote', this.onVote);
if(this.onVotes)
window.torrentSocket.off('votes', this.onVotes);
if(this.torrent && this.torrent.contentCategory == 'xxx') {
this.removeMetaTag('robots');
}

15
src/app/waitObject.js Normal file
View File

@ -0,0 +1,15 @@
export default (obj, val) => new Promise((resolve) => {
if(typeof obj[val] === 'object')
{
resolve()
return
}
const w = setInterval(() => {
if(typeof obj[val] === 'object')
{
clearInterval(w)
resolve()
}
}, 3)
})

View File

@ -97,7 +97,7 @@ module.exports = ({
return;
}
sphinx.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, function (error, rows, fields) {
sphinx.query('SELECT * FROM `torrents` WHERE `hash` = ?', hash, async function (error, rows, fields) {
if(!rows || rows.length == 0) {
callback(undefined)
return;
@ -119,11 +119,17 @@ module.exports = ({
if(torrentClientHashMap[hash])
{
const torrent = torrentClient.get(torrentClientHashMap[hash])
if(!torrent)
return
send('downloading', torrent.infoHash)
if(torrent)
{
send('downloading', torrent.infoHash)
}
}
// get votes
const {good, bad, selfVote} = await getVotes(hash)
send('votes', {
hash, good, bad
});
});
}
@ -630,18 +636,7 @@ module.exports = ({
}, done)
})
recive('vote', async (hash, isGood, callback) =>
{
if(hash.length != 40)
return;
if(typeof callback != 'function')
return;
isGood = !!isGood;
const action = isGood ? 'good' : 'bad';
const getVotes = async (hash) => {
const votes = await p2pStore.find(`vote:${hash}`)
let good = 0
let bad = 0
@ -658,7 +653,22 @@ module.exports = ({
good++
})
}
console.log('votes before', bad, good)
return {good, bad, selfVote}
}
recive('vote', async (hash, isGood, callback) =>
{
if(hash.length != 40)
return;
if(typeof callback != 'function')
return;
isGood = !!isGood;
const action = isGood ? 'good' : 'bad';
let {good, bad, selfVote} = await getVotes(hash)
if(!selfVote)
{
@ -672,7 +682,7 @@ module.exports = ({
bad += !isGood ? 1 : 0
}
send('vote', {
send('votes', {
hash, good, bad
});
callback(true)