feat(feed): feed widget support

This commit is contained in:
Alexey Kasyanchuk 2018-06-12 23:39:14 +03:00
parent 77721293e4
commit 027be8c3f3
4 changed files with 109 additions and 1 deletions

View File

@ -6,6 +6,7 @@ import SearchResults from './search-results'
import Search from './search' import Search from './search'
import SelectField from 'material-ui/SelectField'; import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem'; import MenuItem from 'material-ui/MenuItem';
import Feed from './feed';
export default class IndexPage extends Page { export default class IndexPage extends Page {
constructor(props) { constructor(props) {
@ -88,6 +89,7 @@ export default class IndexPage extends Page {
/> />
</div> </div>
<div className='column center w100p pad0-75'> <div className='column center w100p pad0-75'>
<Feed />
<RecentTorrents /> <RecentTorrents />
</div> </div>
</div> </div>

View File

@ -3,8 +3,9 @@ const forBigTable = require('./forBigTable')
const compareVersions = require('compare-versions'); const compareVersions = require('compare-versions');
const getTorrent = require('./gettorrent') const getTorrent = require('./gettorrent')
const _ = require('lodash') const _ = require('lodash')
const Feed = require('./feed')
module.exports = ({ module.exports = async ({
sphinx, sphinx,
send, send,
recive, recive,
@ -735,4 +736,40 @@ module.exports = ({
callback(true) callback(true)
}); });
const feed = new Feed({sphinx})
await feed.load()
feed.clear()
setInterval(() => feed.save(), 10000)
// store torrent to feed
p2pStore.on('store', async ({data: record, temp}) => {
if(!temp || !temp.torrent)
return
const { torrent } = temp
if(record.type !== 'vote')
return
if(record.vote !== 'good')
return
if(!torrent)
return
if(torrent.hash !== record.torrentHash)
return
feed.add(torrent)
send('feedUpdate', {
feed: feed.feed
});
})
recive('feed', (callback) =>
{
callback(feed.feed)
});
} }

60
src/background/feed.js Normal file
View File

@ -0,0 +1,60 @@
module.exports = class Feed {
constructor({sphinx})
{
this.feed = []
this.sphinx = sphinx
}
async save() {
console.log('saving feed')
await this.sphinx.query('delete from feed where id > 0')
let id = 0
return Promise.all(
this.feed.map(
async record => await this.sphinx.query('insert into feed(id, data) values(?, ?)', [++id, JSON.stringify(record)])
)
)
}
async load() {
this.feed = await this.sphinx.query('select * from feed limit 1000')
if(this.feed && this.feed.length > 0)
this.feed = this.feed.map(f => JSON.parse(f.data))
else
this.feed = []
this._order()
}
clear()
{
this.feed = []
}
add(data) {
if(typeof data == 'object')
{
data.feedDate = Math.floor(Date.now() / 1000)
}
this.feed.push(data)
this._order()
}
_order() {
this.feed.sort((a, b) => this._compare(b) - this._compare(a))
}
_compare(x)
{
const rating = 0
const comments = 0
const time = Math.floor(Date.now() / 1000) - x.feedDate
const maxTime = 600000
if(time > maxTime)
time = maxTime
const relativeTime = (maxTime - time) / maxTime
return relativeTime * relativeTime + rating * 1.5 * relativeTime + comments * 4 * relativeTime
}
}

View File

@ -66,6 +66,15 @@ const writeSphinxConfig = (path, dbPath) => {
rt_attr_string = peerId rt_attr_string = peerId
} }
index feed
{
type = rt
path = ${dbPath}/database/feed
rt_field = feedIndex
rt_attr_json = data
}
searchd searchd
{ {
listen = 9312 listen = 9312