hedgedoc/src/components/editor/document-bar/revisions/utils.ts
Erik Michelson 091b225672
Add caching of user-data (#568)
* Add caching of user-data for 600 seconds

* Make cache-entry interface commonly usable

* Extract revision types

* Remove revision-cache rule

* Use seconds as cache-time interval (Date.now uses milliseconds)

* Fix import error

* Extract cache logic into common cache-class

* Add cache class that was forgotten to commit in last commit

* Start adding unit tests

* Fix bug detected during unit-testing

* Add unit tests for cache

* Made entry-limit test more explicit

* Renamed files to lower-case starting letter
2020-09-30 23:37:57 +02:00

31 lines
977 B
TypeScript

import { Revision } from '../../../../api/revisions/types'
import { getUserById } from '../../../../api/users'
import { UserResponse } from '../../../../api/users/types'
export const downloadRevision = (noteId: string, revision: Revision | null): void => {
if (!revision) {
return
}
const encoded = Buffer.from(revision.content).toString('base64')
const wrapper = document.createElement('a')
wrapper.download = `${noteId}-${revision.timestamp}.md`
wrapper.href = `data:text/markdown;charset=utf-8;base64,${encoded}`
document.body.appendChild(wrapper)
wrapper.click()
document.body.removeChild(wrapper)
}
export const getUserDataForRevision = (authors: string[]): UserResponse[] => {
const users: UserResponse[] = []
authors.forEach((author, index) => {
if (index > 9) {
return
}
getUserById(author)
.then(userData => {
users.push(userData)
})
.catch((error) => console.error(error))
})
return users
}