Skip to content

Commit

Permalink
feat(feed): feed widget support
Browse files Browse the repository at this point in the history
  • Loading branch information
DEgITx committed Jun 12, 2018
1 parent 81c471b commit 3aefdc8
Show file tree
Hide file tree
Showing 4 changed files with 109 additions and 1 deletion.
2 changes: 2 additions & 0 deletions src/app/index-page.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import SearchResults from './search-results'
import Search from './search'
import SelectField from 'material-ui/SelectField';
import MenuItem from 'material-ui/MenuItem';
import Feed from './feed';

export default class IndexPage extends Page {
constructor(props) {
Expand Down Expand Up @@ -88,6 +89,7 @@ export default class IndexPage extends Page {
/>
</div>
<div className='column center w100p pad0-75'>
<Feed />
<RecentTorrents />
</div>
</div>
Expand Down
39 changes: 38 additions & 1 deletion src/background/api.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,9 @@ const forBigTable = require('./forBigTable')
const compareVersions = require('compare-versions');
const getTorrent = require('./gettorrent')
const _ = require('lodash')
const Feed = require('./feed')

module.exports = ({
module.exports = async ({
sphinx,
send,
recive,
Expand Down Expand Up @@ -735,4 +736,40 @@ module.exports = ({
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 changes: 60 additions & 0 deletions src/background/feed.js
Original file line number Diff line number Diff line change
@@ -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
}
}
9 changes: 9 additions & 0 deletions src/background/sphinx.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,15 @@ const writeSphinxConfig = (path, dbPath) => {
rt_attr_string = peerId
}
index feed
{
type = rt
path = ${dbPath}/database/feed
rt_field = feedIndex
rt_attr_json = data
}
searchd
{
listen = 9312
Expand Down

0 comments on commit 3aefdc8

Please sign in to comment.