forked from aboutcircles/circles-envio-indexer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* feat: from & to as Avatar types * feat: remove unused file * feat: fetch v1 garden profiles * feat: refactor load v1 garden profiles * feat: cache.db with v1 profiles * docs: update with from to updated * feat: refactor to use string instead of avatar
- Loading branch information
Showing
10 changed files
with
126 additions
and
68 deletions.
There are no files selected for viewing
Binary file not shown.
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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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 was deleted.
Oops, something went wrong.
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,76 @@ | ||
import { Profile } from "./types"; | ||
import { ProfileCache } from "./cache"; | ||
|
||
type GardenApiResponse = { | ||
status: string; | ||
data: { | ||
id: number; | ||
username: string; | ||
avatarUrl: string | undefined; | ||
}[]; | ||
}; | ||
type GardenProfile = { | ||
name: string; | ||
previewImageUrl: string | undefined; | ||
}; | ||
/** | ||
* This functions calls https://api.circles.garden/api/users?address[]=${address} | ||
* which returns a GardenApiResponse object. | ||
* GardenApiResponse contains an avatarUrl, which is also fetched and it's result is converted into a base 64 image. | ||
* @param {string} address - The address of the user. | ||
* @returns {Promise<{ data: GardenProfile | undefined; timeTaken: number }>} - The GardenProfile object and the time taken to fetch it. | ||
*/ | ||
async function fetchGardenProfile( | ||
address: string | ||
): Promise<{ data: GardenProfile | undefined; timeTaken: number }> { | ||
const startTime = Date.now(); | ||
|
||
try { | ||
const response = await fetch( | ||
`https://api.circles.garden/api/users?address[]=${address}` | ||
); | ||
const json = (await response.json()) as GardenApiResponse; | ||
|
||
if (json.status !== "ok" || json.data.length === 0) { | ||
return { data: undefined, timeTaken: Date.now() - startTime }; | ||
} | ||
|
||
const user = json.data[0]; | ||
|
||
return { | ||
data: { | ||
name: user.username, | ||
previewImageUrl: user.avatarUrl, | ||
}, | ||
timeTaken: Date.now() - startTime, | ||
}; | ||
} catch (error) { | ||
console.error("Error fetching garden profile:", error); | ||
return { data: undefined, timeTaken: Date.now() - startTime }; | ||
} | ||
} | ||
|
||
export async function getProfileMetadataFromGardenApi( | ||
address: string | ||
): Promise<{ data: Profile | null } | null> { | ||
if (!address) { | ||
return null; | ||
} | ||
|
||
const cache = await ProfileCache.init(1); | ||
const cacheResult = await cache.read(address); | ||
|
||
if (cacheResult) { | ||
return { data: cacheResult.data }; | ||
} | ||
|
||
const { data } = await fetchGardenProfile(address); | ||
if (!data) { | ||
return null; | ||
} | ||
|
||
// v1 did not had cidV0 | ||
await cache.add(address, null, data); | ||
|
||
return { data }; | ||
} |
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