forked from hozzjss/pollen-onboarding
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add sourcecred functions and db fetch functions
- Loading branch information
1 parent
a7e0961
commit 66508fd
Showing
1 changed file
with
72 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |