Skip to content

Commit

Permalink
Add sourcecred functions and db fetch functions
Browse files Browse the repository at this point in the history
  • Loading branch information
hernandoagf committed May 21, 2021
1 parent a7e0961 commit 66508fd
Showing 1 changed file with 72 additions and 0 deletions.
72 changes: 72 additions & 0 deletions src/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,72 @@
import { sourcecred } from 'sourcecred'
import { config } from 'dotenv'

import User, { IUser } from './models/User'
import LedgerUpdate from './models/LedgerUpdate'

config()

export const log = (message) => console.log(`${Date.now()}: ${message}`)

const fetchLastLedgerUpdate = async (): Promise<number> => {
const lastLedgerUpdateEntry = await LedgerUpdate.find({}).sort('-modifiedAt').limit(1)

return lastLedgerUpdateEntry.length ? lastLedgerUpdateEntry[0].modifiedAt : 0
}

export const fetchModifiedUsers = async (): Promise<IUser[]> => {
const lastLedgerUpdate = await fetchLastLedgerUpdate()
const foundUsers = await User.find({
modifiedAt: { $gte: lastLedgerUpdate }
})

return foundUsers
}

const storage = new sourcecred.ledger.storage.GithubStorage({
apiToken: process.env.GITHUB_API_TOKEN,
repo: process.env.REPO,
branch: process.env.BRANCH
})

export const manager = new sourcecred.ledger.manager.LedgerManager({ storage })

export const loadLedger = async () => {
const ledger = await manager.reloadLedger()

return ledger
}

export const createDiscourseIdentity = (discourse, ledger) => {
const newDiscourseIdentityId = ledger.createIdentity(
'USER',
ledger.nameAvailable(discourse) ? discourse : `${discourse}-discourse`
)

ledger.addAlias(
newDiscourseIdentityId,
{
description: `discourse/[@${discourse}](https://forum.1hive.org/u/${discourse}/)`,
address: `N\u0000sourcecred\u0000discourse\u0000user\u0000https://forum.1hive.org\u0000${discourse}\u0000`
}
)

return newDiscourseIdentityId
}

export const createGithubIdentity = (github, ledger) => {
const newGithubIdentityId = ledger.createIdentity(
'USER',
ledger.nameAvailable(github) ? github : `${github}-github`
)

ledger.addAlias(
newGithubIdentityId,
{
description: `github/[@${github}](https://github.com/${github})`,
address: `N\u0000sourcecred\u0000github\u0000USERLIKE\u0000USER\u0000${github}\u0000`
}
)

return newGithubIdentityId
}

0 comments on commit 66508fd

Please sign in to comment.