-
Notifications
You must be signed in to change notification settings - Fork 2
Feat: add profile fetching from pubky #824
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
ben-kaufman
wants to merge
11
commits into
master
Choose a base branch
from
feat/pubky-profile
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
5983a73
feat: add profile fetching from pubky
ben-kaufman b5f211c
Merge branch 'master' into feat/pubky-profile
ben-kaufman 8d5bac7
fix build
ben-kaufman 1bdd233
Merge branch 'feat/pubky-profile' of https://github.com/synonymdev/bi…
ben-kaufman 1484f26
fix paykit version
ben-kaufman 06a618d
fixes
ben-kaufman 7ff4b1e
fixes
ben-kaufman 13bd697
fixes
ben-kaufman 359eddc
fixes
ben-kaufman ec754c1
detekt fix
ben-kaufman 9ede70f
fixes
ben-kaufman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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 hidden or 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,52 @@ | ||
| package to.bitkit.data | ||
|
|
||
| import android.content.Context | ||
| import android.graphics.Bitmap | ||
| import android.graphics.BitmapFactory | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import java.io.File | ||
| import java.security.MessageDigest | ||
| import java.util.concurrent.ConcurrentHashMap | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| @Singleton | ||
| class PubkyImageCache @Inject constructor( | ||
| @ApplicationContext context: Context, | ||
| ) { | ||
| private val memoryCache = ConcurrentHashMap<String, Bitmap>() | ||
| private val diskDir: File = File(context.cacheDir, "pubky-images").also { it.mkdirs() } | ||
|
|
||
| fun memoryImage(uri: String): Bitmap? = memoryCache[uri] | ||
|
|
||
| fun image(uri: String): Bitmap? { | ||
| memoryCache[uri]?.let { return it } | ||
|
|
||
| val file = diskPath(uri) | ||
| if (file.exists()) { | ||
| val bitmap = BitmapFactory.decodeFile(file.absolutePath) ?: return null | ||
| memoryCache[uri] = bitmap | ||
| return bitmap | ||
| } | ||
| return null | ||
| } | ||
|
|
||
| fun store(bitmap: Bitmap, data: ByteArray, uri: String) { | ||
| memoryCache[uri] = bitmap | ||
| runCatching { diskPath(uri).writeBytes(data) } | ||
| } | ||
|
|
||
| fun clear() { | ||
| memoryCache.clear() | ||
| runCatching { | ||
| diskDir.deleteRecursively() | ||
| diskDir.mkdirs() | ||
| } | ||
| } | ||
|
|
||
| private fun diskPath(uri: String): File { | ||
| val digest = MessageDigest.getInstance("SHA-256") | ||
| val hash = digest.digest(uri.toByteArray()).joinToString("") { "%02x".format(it) } | ||
| return File(diskDir, hash) | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,34 @@ | ||
| package to.bitkit.models | ||
|
|
||
| import com.synonym.paykit.FfiProfile | ||
|
|
||
| data class PubkyProfileLink(val label: String, val url: String) | ||
|
|
||
| data class PubkyProfile( | ||
| val publicKey: String, | ||
| val name: String, | ||
| val bio: String, | ||
| val imageUrl: String?, | ||
| val links: List<PubkyProfileLink>, | ||
| val status: String?, | ||
| ) { | ||
| companion object { | ||
| fun fromFfi(publicKey: String, ffiProfile: FfiProfile): PubkyProfile { | ||
| return PubkyProfile( | ||
| publicKey = publicKey, | ||
| name = ffiProfile.name, | ||
| bio = ffiProfile.bio ?: "", | ||
| imageUrl = ffiProfile.image, | ||
| links = ffiProfile.links?.map { PubkyProfileLink(label = it.title, url = it.url) } ?: emptyList(), | ||
| status = ffiProfile.status, | ||
| ) | ||
| } | ||
| } | ||
|
|
||
| val truncatedPublicKey: String | ||
| get() = if (publicKey.length > 10) { | ||
| "${publicKey.take(4)}...${publicKey.takeLast(4)}" | ||
| } else { | ||
| publicKey | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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,238 @@ | ||
| package to.bitkit.repositories | ||
|
|
||
| import android.content.Context | ||
| import android.content.SharedPreferences | ||
| import android.graphics.Bitmap | ||
| import android.graphics.BitmapFactory | ||
| import dagger.hilt.android.qualifiers.ApplicationContext | ||
| import kotlinx.coroutines.CoroutineDispatcher | ||
| import kotlinx.coroutines.CoroutineScope | ||
| import kotlinx.coroutines.SupervisorJob | ||
| import kotlinx.coroutines.flow.MutableStateFlow | ||
| import kotlinx.coroutines.flow.SharingStarted | ||
| import kotlinx.coroutines.flow.StateFlow | ||
| import kotlinx.coroutines.flow.asStateFlow | ||
| import kotlinx.coroutines.flow.map | ||
| import kotlinx.coroutines.flow.stateIn | ||
| import kotlinx.coroutines.flow.update | ||
| import kotlinx.coroutines.launch | ||
| import kotlinx.coroutines.sync.Mutex | ||
| import kotlinx.coroutines.withContext | ||
| import org.json.JSONObject | ||
| import to.bitkit.data.PubkyImageCache | ||
| import to.bitkit.data.keychain.Keychain | ||
| import to.bitkit.di.BgDispatcher | ||
| import to.bitkit.models.PubkyProfile | ||
| import to.bitkit.services.PubkyService | ||
| import to.bitkit.utils.Logger | ||
| import javax.inject.Inject | ||
| import javax.inject.Singleton | ||
|
|
||
| enum class PubkyAuthState { Idle, Authenticating, Authenticated } | ||
|
|
||
| @Singleton | ||
| class PubkyRepo @Inject constructor( | ||
| @ApplicationContext private val context: Context, | ||
| @BgDispatcher private val bgDispatcher: CoroutineDispatcher, | ||
| private val pubkyService: PubkyService, | ||
| private val keychain: Keychain, | ||
| private val imageCache: PubkyImageCache, | ||
| ) { | ||
| companion object { | ||
| private const val TAG = "PubkyRepo" | ||
| private const val PREFS_NAME = "pubky_profile_cache" | ||
| private const val KEY_CACHED_NAME = "cached_name" | ||
| private const val KEY_CACHED_IMAGE_URI = "cached_image_uri" | ||
| private const val PUBKY_SCHEME = "pubky://" | ||
| } | ||
|
|
||
| private val scope = CoroutineScope(bgDispatcher + SupervisorJob()) | ||
| private val prefs: SharedPreferences = context.getSharedPreferences(PREFS_NAME, Context.MODE_PRIVATE) | ||
| private val loadProfileMutex = Mutex() | ||
|
|
||
| private val _authState = MutableStateFlow(PubkyAuthState.Idle) | ||
|
|
||
| private val _profile = MutableStateFlow<PubkyProfile?>(null) | ||
| val profile: StateFlow<PubkyProfile?> = _profile.asStateFlow() | ||
|
|
||
| private val _publicKey = MutableStateFlow<String?>(null) | ||
| val publicKey: StateFlow<String?> = _publicKey.asStateFlow() | ||
|
|
||
| private val _isLoadingProfile = MutableStateFlow(false) | ||
| val isLoadingProfile: StateFlow<Boolean> = _isLoadingProfile.asStateFlow() | ||
|
|
||
| val isAuthenticated: StateFlow<Boolean> = _authState.map { it == PubkyAuthState.Authenticated } | ||
| .stateIn(scope, SharingStarted.Eagerly, false) | ||
|
|
||
| val displayName: StateFlow<String?> = _profile.map { it?.name ?: prefs.getString(KEY_CACHED_NAME, null) } | ||
| .stateIn(scope, SharingStarted.Eagerly, prefs.getString(KEY_CACHED_NAME, null)) | ||
|
|
||
| val displayImageUri: StateFlow<String?> = _profile | ||
| .map { it?.imageUrl ?: prefs.getString(KEY_CACHED_IMAGE_URI, null) } | ||
| .stateIn(scope, SharingStarted.Eagerly, prefs.getString(KEY_CACHED_IMAGE_URI, null)) | ||
|
|
||
| private sealed interface InitResult { | ||
| data object NoSession : InitResult | ||
| data class Restored(val publicKey: String) : InitResult | ||
| data object RestorationFailed : InitResult | ||
| } | ||
|
|
||
| suspend fun initialize() { | ||
| val result = runCatching { | ||
| withContext(bgDispatcher) { | ||
| pubkyService.initialize() | ||
|
|
||
| val savedSecret = runCatching { | ||
| keychain.loadString(Keychain.Key.PAYKIT_SESSION.name) | ||
| }.getOrNull() | ||
|
|
||
| if (savedSecret.isNullOrEmpty()) { | ||
| return@withContext InitResult.NoSession | ||
| } | ||
|
|
||
| runCatching { | ||
| val pk = pubkyService.importSession(savedSecret) | ||
| InitResult.Restored(pk) | ||
| }.getOrElse { | ||
| Logger.warn("Failed to restore paykit session", it, context = TAG) | ||
| InitResult.RestorationFailed | ||
| } | ||
| } | ||
| }.onFailure { | ||
| Logger.error("Failed to initialize paykit", it, context = TAG) | ||
| }.getOrNull() ?: return | ||
|
|
||
| when (result) { | ||
| is InitResult.NoSession -> Logger.debug("No saved paykit session found", context = TAG) | ||
| is InitResult.Restored -> { | ||
| _publicKey.update { result.publicKey } | ||
| _authState.update { PubkyAuthState.Authenticated } | ||
| Logger.info("Paykit session restored for ${result.publicKey}", context = TAG) | ||
| loadProfile() | ||
| } | ||
| is InitResult.RestorationFailed -> { | ||
| runCatching { keychain.delete(Keychain.Key.PAYKIT_SESSION.name) } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| suspend fun startAuthentication(): Result<String> { | ||
| _authState.update { PubkyAuthState.Authenticating } | ||
| return runCatching { | ||
| withContext(bgDispatcher) { pubkyService.startAuth() } | ||
| }.onFailure { | ||
| _authState.update { PubkyAuthState.Idle } | ||
| } | ||
| } | ||
|
|
||
| suspend fun completeAuthentication(): Result<Unit> { | ||
| return runCatching { | ||
| withContext(bgDispatcher) { | ||
| val sessionSecret = pubkyService.completeAuth() | ||
| val pk = pubkyService.importSession(sessionSecret) | ||
|
|
||
| runCatching { keychain.delete(Keychain.Key.PAYKIT_SESSION.name) } | ||
| keychain.saveString(Keychain.Key.PAYKIT_SESSION.name, sessionSecret) | ||
|
|
||
| pk | ||
| } | ||
| }.onFailure { | ||
| _authState.update { PubkyAuthState.Idle } | ||
| }.onSuccess { pk -> | ||
| _publicKey.update { pk } | ||
| _authState.update { PubkyAuthState.Authenticated } | ||
| Logger.info("Pubky auth completed for $pk", context = TAG) | ||
| loadProfile() | ||
| }.map {} | ||
| } | ||
|
|
||
| suspend fun cancelAuthentication() { | ||
| runCatching { | ||
| withContext(bgDispatcher) { pubkyService.cancelAuth() } | ||
| }.onFailure { Logger.warn("Cancel auth failed", it, context = TAG) } | ||
| _authState.update { PubkyAuthState.Idle } | ||
| } | ||
|
|
||
| fun cancelAuthenticationSync() { | ||
| scope.launch { cancelAuthentication() } | ||
| } | ||
|
|
||
| suspend fun loadProfile() { | ||
| val pk = _publicKey.value ?: return | ||
| if (!loadProfileMutex.tryLock()) return | ||
|
|
||
| _isLoadingProfile.update { true } | ||
ben-kaufman marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| runCatching { | ||
| withContext(bgDispatcher) { | ||
| val ffiProfile = pubkyService.getProfile(pk) | ||
| Logger.debug("Profile loaded — name: ${ffiProfile.name}, image: ${ffiProfile.image}", context = TAG) | ||
| PubkyProfile.fromFfi(pk, ffiProfile) | ||
| } | ||
| }.onSuccess { loadedProfile -> | ||
| _profile.update { loadedProfile } | ||
| cacheMetadata(loadedProfile) | ||
| }.onFailure { | ||
| Logger.error("Failed to load profile", it, context = TAG) | ||
| } | ||
| } finally { | ||
| _isLoadingProfile.update { false } | ||
| loadProfileMutex.unlock() | ||
| } | ||
| } | ||
|
|
||
| suspend fun signOut(): Result<Unit> = runCatching { | ||
| withContext(bgDispatcher) { | ||
| runCatching { pubkyService.signOut() } | ||
| .onFailure { | ||
| Logger.warn("Server sign out failed, forcing local sign out", it, context = TAG) | ||
| runCatching { pubkyService.forceSignOut() } | ||
| } | ||
| runCatching { keychain.delete(Keychain.Key.PAYKIT_SESSION.name) } | ||
| runCatching { imageCache.clear() } | ||
| } | ||
| clearCachedMetadata() | ||
| _publicKey.update { null } | ||
| _profile.update { null } | ||
| _authState.update { PubkyAuthState.Idle } | ||
| } | ||
|
|
||
| fun cachedImage(uri: String): Bitmap? = imageCache.memoryImage(uri) | ||
|
|
||
| suspend fun fetchImage(uri: String): Result<Bitmap> = runCatching { | ||
| withContext(bgDispatcher) { | ||
| imageCache.image(uri)?.let { return@withContext it } | ||
|
|
||
| val data = pubkyService.fetchFile(uri) | ||
| val blobData = resolveImageData(data) | ||
| val image = BitmapFactory.decodeByteArray(blobData, 0, blobData.size) | ||
| ?: error("Could not decode image blob (${blobData.size} bytes)") | ||
| imageCache.store(image, blobData, uri) | ||
| image | ||
| } | ||
| } | ||
|
|
||
| private suspend fun resolveImageData(data: ByteArray): ByteArray { | ||
| return runCatching { | ||
| val json = JSONObject(String(data)) | ||
| val src = json.optString("src", "") | ||
| if (src.isNotEmpty() && src.startsWith(PUBKY_SCHEME)) { | ||
| Logger.debug("File descriptor found, fetching blob from: $src", context = TAG) | ||
| pubkyService.fetchFile(src) | ||
| } else { | ||
| data | ||
| } | ||
| }.getOrDefault(data) | ||
| } | ||
|
|
||
| private fun cacheMetadata(profile: PubkyProfile) { | ||
| prefs.edit() | ||
| .putString(KEY_CACHED_NAME, profile.name) | ||
| .putString(KEY_CACHED_IMAGE_URI, profile.imageUrl) | ||
| .apply() | ||
| } | ||
|
|
||
| private fun clearCachedMetadata() { | ||
| prefs.edit().clear().apply() | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.