-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Drop old database maskbook-avatar-store
Signed-off-by: Jack Works <[email protected]>
- Loading branch information
1 parent
8b2d118
commit 85ec537
Showing
38 changed files
with
528 additions
and
325 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
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
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 |
---|---|---|
@@ -1,17 +1,17 @@ | ||
import React from 'react' | ||
import { Person } from '../../extension/background-script/PeopleService' | ||
import { ValueRef } from '@holoflows/kit/es' | ||
import { MessageCenter } from '../../utils/messages' | ||
import Services from '../../extension/service' | ||
import { Person } from '../../database' | ||
|
||
const ref = new ValueRef<Person[]>([]) | ||
Services.People.queryPeople().then(p => (ref.value = p)) | ||
Services.People.queryPeople('facebook.com').then(p => (ref.value = p)) | ||
MessageCenter.on('newPerson', p => { | ||
const old = ref.value.filter(x => x.username !== p.username) | ||
const old = ref.value.filter(x => x.identifier.toString() !== p.identifier.toString()) | ||
ref.value = [...old, p] | ||
}) | ||
export function usePeople() { | ||
const [people, setPeople] = React.useState<Person[]>(ref.value) | ||
React.useEffect(() => ref.addListener(val => setPeople(val.filter(x => x.username !== '$self'))), []) | ||
React.useEffect(() => ref.addListener(val => setPeople(val)), []) | ||
return people | ||
} |
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
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
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
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
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
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
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,9 @@ | ||
declare module 'idb/with-async-ittr' { | ||
export * from 'idb' | ||
} | ||
/** | ||
* IDBFactory v3 | ||
*/ | ||
interface IDBFactory { | ||
databases?(): Promise<Array<{ name: string; version: number }>> | ||
} |
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,47 @@ | ||
import { PersonIdentifier, GroupIdentifier } from '../type' | ||
import { queryAvatarDB, isAvatarOutdatedDB, storeAvatarDB } from '../avatar' | ||
|
||
const avatarCache = new Map<string, string>() | ||
|
||
/** | ||
* Get a (cached) blob url for an identifier. | ||
*/ | ||
export async function getAvatarBlobURL(identifier: PersonIdentifier | GroupIdentifier): Promise<string | undefined> { | ||
const id = identifier.toString() | ||
if (avatarCache.has(id)) return avatarCache.get(id)! | ||
const buffer = await queryAvatarDB(identifier) | ||
if (!buffer) return undefined | ||
const blob = new Blob([buffer], { type: 'image/png' }) | ||
const url = URL.createObjectURL(blob) | ||
avatarCache.set(id, url) | ||
return url | ||
} | ||
|
||
/** | ||
* Store an avatar with a url for an identifier. | ||
* @param avatar - Avatar to store. If it is a string, will try to fetch it. | ||
* @param identifier - This avatar belongs to. | ||
*/ | ||
|
||
export async function storeAvatar(identifier: PersonIdentifier | GroupIdentifier, avatar: ArrayBuffer | string) { | ||
try { | ||
if (typeof avatar === 'string') { | ||
if (await isAvatarOutdatedDB(identifier, 'lastUpdateTime')) { | ||
await storeAvatarDB(identifier, await downloadAvatar(avatar)) | ||
} | ||
// else do nothing | ||
} else { | ||
await storeAvatarDB(identifier, avatar) | ||
} | ||
} catch (e) { | ||
console.error('Store avatar failed', e) | ||
} | ||
} | ||
/** | ||
* Download avatar from url | ||
*/ | ||
async function downloadAvatar(url: string): Promise<ArrayBuffer> { | ||
const res = await fetch(url) | ||
if (!res.ok) throw new Error('Fetch avatar failed.') | ||
return res.arrayBuffer() | ||
} |
Oops, something went wrong.