Fix text/plain NIP-05 response (& auto-prepend _@)

Show errors in toast

Closes #90
This commit is contained in:
franzap
2024-06-13 20:22:07 -03:00
parent ddc43a964c
commit 6bd1affa34
2 changed files with 30 additions and 11 deletions
+22 -8
View File
@@ -120,6 +120,11 @@ mixin UserAdapter on NostrAdapter<User> {
return data.models;
}
@override
bool isOfflineError(Object? error) {
return false;
}
@override
Future<User?> findOne(Object id,
{bool remote = true,
@@ -131,24 +136,33 @@ mixin UserAdapter on NostrAdapter<User> {
DataRequestLabel? label}) async {
if (id.toString().isEmpty) return null;
String? publicKey = id.toString();
var publicKey = id.toString();
if (publicKey.startsWith('npub')) {
publicKey = publicKey.hexKey;
} else if (publicKey.contains('@')) {
final [username, domain] = id.toString().split('@');
} else {
// If it's not an npub we treat the string as NIP-05
if (!publicKey.contains('@')) {
// If it does not have a @, we treat the string as a domain name
publicKey = '_@$publicKey';
}
final [username, domain] = publicKey.split('@');
publicKey = await sendRequest<dynamic>(
Uri.parse('https://$domain/.well-known/nostr.json?name=$username'),
onSuccess: (response, label) {
return (response.body as Map)['names']?[username];
var body = response.body;
if (body is String) {
body = jsonDecode(body);
}
return (body as Map)['names']?[username];
},
onError: (e, _) {
throw e;
},
);
}
if (publicKey == null) {
return null;
}
final req = RelayRequest(
authors: {publicKey},
kinds: {kind, if (params?['contacts'] != null) 3},
+8 -3
View File
@@ -5,6 +5,7 @@ import 'package:gap/gap.dart';
import 'package:hooks_riverpod/hooks_riverpod.dart';
import 'package:zapstore/main.data.dart';
import 'package:zapstore/models/settings.dart';
import 'package:zapstore/utils/extensions.dart';
import 'package:zapstore/widgets/rounded_image.dart';
class LoginContainer extends HookConsumerWidget {
@@ -76,9 +77,13 @@ class LoginContainer extends HookConsumerWidget {
child: CircularProgressIndicator(),
),
onPressed: () async {
final user =
await ref.users.findOne(controller.text.trim());
ref.settings.findOneLocalById('_')!.user.value = user;
return ref.users
.findOne(controller.text.trim())
.then((user) {
ref.settings.findOneLocalById('_')!.user.value = user;
}).catchError((e) {
context.showError(e.toString());
});
},
builder: (context, child, callback, buttonState) {
return Padding(