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 FlatButton from 'material-ui/FlatButton';
import {fileTypeDetect} from './content' import {fileTypeDetect} from './content'
import {contentIcon} from './torrent' import {contentIcon} from './torrent'
import waitObject from './waitObject'
let buildFilesTree = (filesList) => { let buildFilesTree = (filesList) => {
let rootTree = { 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) => { window.torrentSocket.emit('torrent', this.props.hash, {files: true, peer: this.props.peer}, window.customLoader((data) => {
if(data) { if(data) {
this.torrent = 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') { if(this.torrent.contentCategory == 'xxx') {
this.setMetaTag('robots', 'noindex'); this.setMetaTag('robots', 'noindex');
} }
@ -226,18 +227,18 @@ export default class TorrentPage extends Page {
} }
window.torrentSocket.on('trackerTorrentUpdate', this.trackerUpdate); window.torrentSocket.on('trackerTorrentUpdate', this.trackerUpdate);
this.onVote = ({hash, good, bad}) => { this.onVotes = async ({hash, good, bad}) => {
if(this.props.hash != hash) if(this.props.hash != hash)
return; return;
if(!this.torrent) // in some cases torrent object can be resolve late
return; await waitObject(this, 'torrent')
this.torrent.good = good; this.torrent.good = good;
this.torrent.bad = bad; this.torrent.bad = bad;
this.forceUpdate(); this.forceUpdate();
} }
window.torrentSocket.on('vote', this.onVote); window.torrentSocket.on('votes', this.onVotes);
this.downloading = (hash) => { this.downloading = (hash) => {
if(this.props.hash != hash) if(this.props.hash != hash)
@ -273,8 +274,8 @@ export default class TorrentPage extends Page {
window.torrentSocket.off('filesReady', this.filesUpdated); window.torrentSocket.off('filesReady', this.filesUpdated);
if(this.trackerUpdate) if(this.trackerUpdate)
window.torrentSocket.off('trackerTorrentUpdate', this.trackerUpdate); window.torrentSocket.off('trackerTorrentUpdate', this.trackerUpdate);
if(this.onVote) if(this.onVotes)
window.torrentSocket.off('vote', this.onVote); window.torrentSocket.off('votes', this.onVotes);
if(this.torrent && this.torrent.contentCategory == 'xxx') { if(this.torrent && this.torrent.contentCategory == 'xxx') {
this.removeMetaTag('robots'); 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; 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) { if(!rows || rows.length == 0) {
callback(undefined) callback(undefined)
return; return;
@ -119,11 +119,17 @@ module.exports = ({
if(torrentClientHashMap[hash]) if(torrentClientHashMap[hash])
{ {
const torrent = torrentClient.get(torrentClientHashMap[hash]) const torrent = torrentClient.get(torrentClientHashMap[hash])
if(!torrent) if(torrent)
return {
send('downloading', torrent.infoHash) 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) }, done)
}) })
recive('vote', async (hash, isGood, callback) => const getVotes = async (hash) => {
{
if(hash.length != 40)
return;
if(typeof callback != 'function')
return;
isGood = !!isGood;
const action = isGood ? 'good' : 'bad';
const votes = await p2pStore.find(`vote:${hash}`) const votes = await p2pStore.find(`vote:${hash}`)
let good = 0 let good = 0
let bad = 0 let bad = 0
@ -658,7 +653,22 @@ module.exports = ({
good++ 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) if(!selfVote)
{ {
@ -672,7 +682,7 @@ module.exports = ({
bad += !isGood ? 1 : 0 bad += !isGood ? 1 : 0
} }
send('vote', { send('votes', {
hash, good, bad hash, good, bad
}); });
callback(true) callback(true)