Skip to content

Commit

Permalink
Brave Today: content scores
Browse files Browse the repository at this point in the history
  • Loading branch information
petemill committed Jul 30, 2020
1 parent dfadfaa commit 97188e5
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 15 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -2,35 +2,41 @@
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this file,
// you can obtain one at http://mozilla.org/MPL/2.0/.
import { formatDistance, formatDistanceToNow } from 'date-fns'
import { formatDistanceToNow } from 'date-fns'


function shuffleArray(array: Array<any>) {
for (let i = array.length - 1; i > 0; i--) {
const j = Math.floor(Math.random() * (i + 1));
[array[i], array[j]] = [array[j], array[i]];
}
}

export function generateRelativeTimeFormat (publishTime: string) {
if (!publishTime) {
return
}
// TODO(petemill): proper internationalisation syntax
return formatDistanceToNow(
new Date(publishTime)
) + ' ago'
}

function cleanFeedItem(item: BraveToday.IContentItem): BraveToday.IContentItem {
function convertFeedItem(item: BraveToday.IContentItem): BraveToday.IContentItem {
const publishTime = item.publish_time + ' UTC'
return {
...item,
// img: '',
seen: false,
publish_time: formatDistance(
new Date(item.publish_time),
new Date(Date.now())
),
relative_time: generateRelativeTimeFormat(item.publish_time)
publish_time: publishTime,
relative_time: generateRelativeTimeFormat(publishTime),
seen: false
}
}

function generateFeedProperties (
content: BraveToday.IContentItem,
points?: number
): BraveToday.IContentItem {
content = cleanFeedItem(content)
content = convertFeedItem(content)
if ('partner_id' in content) {
return content
}
Expand All @@ -40,14 +46,14 @@ function generateFeedProperties (
} as BraveToday.Article
}

export default function getBraveTodayData (
export default async function getBraveTodayData (
feedContent: BraveToday.ContentFromFeed[]
): BraveToday.Feed {
): Promise<BraveToday.Feed> {
// const BraveTodayWhitelist = getBraveTodayWhitelist(topSites)
const sponsors: (BraveToday.Article)[] = []
const deals: (BraveToday.Deal)[] = []
const media: (BraveToday.Media)[] = []
const articles: (BraveToday.Article)[] = []
let articles: (BraveToday.Article)[] = []

for (const feedItem of feedContent) {
if ('partner_id' in feedItem) {
Expand All @@ -65,6 +71,8 @@ export default function getBraveTodayData (
}
}

articles = await weightArticles(articles)

const featuredDeals = []
if (deals.length > 0) {
featuredDeals.push(deals[0])
Expand All @@ -85,3 +93,31 @@ export default function getBraveTodayData (
}
}
}

function domainFromUrlString(urlString: string): string {
return new window.URL(urlString).host
}

async function weightArticles(articles: BraveToday.Article[]): Promise<BraveToday.Article[]> {
// hosts from latest max-200 history items
const historyItems: chrome.history.HistoryItem[] = await new Promise(resolve => chrome.history.search({ text: '', maxResults: 200 }, resolve))
const historyHosts: string[] = historyItems.map(h => h.url).filter(i => i).map((u: string) => domainFromUrlString(u))
for (const article of articles) {
const secondsSincePublish = Math.abs((new Date().getTime() - new Date(article.publish_time).getTime()) / 1000)
console.log('secondsSince Publish', article.publish_time, secondsSincePublish)
let score = Math.log(secondsSincePublish)
const hasDomainHistoryMatch = historyHosts.includes(domainFromUrlString(article.url))
if (hasDomainHistoryMatch) {
score -= 5
}
article.points = score
}
articles = articles.sort((a, b) => (a.points || 1000) - (b.points || 1000))

const topArticles = articles.splice(0, 25)
shuffleArray(topArticles)
return [
...topArticles,
...articles
]
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function updateFeed() {
if (feedResponse.ok) {
const feedContents: RemoteData = await feedResponse.json()
console.log('Got feed', feedContents)
memoryTodayData = feedToData(feedContents)
memoryTodayData = await feedToData(feedContents)
} else {
console.error(`Not ok when fetching feed. Status ${feedResponse.status} (${feedResponse.statusText})`)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
"all_frames": true
}
],
"permissions": [ "contentSettings", "settingsPrivate", "management", "tabs", "storage", "webNavigation", "contextMenus", "cookies", "*://*/*", "chrome://favicon/*", "https://pcdn.brave.software/*" ],
"permissions": [ "contentSettings", "settingsPrivate", "management", "tabs", "history", "storage", "webNavigation", "contextMenus", "cookies", "*://*/*", "chrome://favicon/*", "https://pcdn.brave.software/*" ],
"content_security_policy": "default-src 'self' https://pcdn.brave.software; font-src 'self' data:; script-src 'self'; style-src 'unsafe-inline'; img-src 'self' data: chrome://favicon/ https://pcdn.brave.software; connect-src https://pcdn.brave.software;",
"incognito": "split",
"key": "MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAupOLMy5Fd4dCSOtjcApsAQOnuBdTs+OvBVt/3P93noIrf068x0xXkvxbn+fpigcqfNamiJ5CjGyfx9zAIs7zcHwbxjOw0Uih4SllfgtK+svNTeE0r5atMWE0xR489BvsqNuPSxYJUmW28JqhaSZ4SabYrRx114KcU6ko7hkjyPkjQa3P+chStJjIKYgu5tWBiMJp5QVLelKoM+xkY6S7efvJ8AfajxCViLGyDQPDviGr2D0VvIBob0D1ZmAoTvYOWafcNCaqaejPDybFtuLFX3pZBqfyOCyyzGhucyCmfBXJALKbhjRAqN5glNsUmGhhPK87TuGATQfVuZtenMvXMQIDAQAB"
Expand Down

0 comments on commit 97188e5

Please sign in to comment.