mirror of
https://github.com/block/buzz.git
synced 2026-11-28 04:01:45 +00:00
Follow-ups to the mobile diagnostic reporting work in
docs/mobile-diagnostic-reporting.md.
## Changes
- **Install ID repair:** if the install ID file holds something that
isn't text, the app now replaces it with a new ID, as it already does
for an empty file. Before, every report went out without an install ID.
A read that fails (for example, no file descriptor free) still keeps the
file, so a passing error never replaces a good ID.
- **Clock-window refusals are not reported as errors:** the spec treats
relay refusals for a timestamp outside the relay's clock window as
expected (counting them is still pending). The app only recognized one
of the relay's wordings ("timestamp too far from server time"), so
refusals for canvas events, relay-admin commands, leave requests and
observer frames were reported as invalid requests. The five wordings are
matched from the start of the reason, so unrelated `invalid:` reasons
that mention a timestamp, such as the feedback timestamp overflow or one
that echoes a value, are still reported.
- **Diagnostics card:** rows get subtitles, and the divider indent
matches the neighbouring Settings cards.
- **Comments:** explain why push stop gates its current-identity request
on the community key, and correct a test comment about who drops a
repeated error object.
## Validation
New tests cover:
- member attribution on relay sessions, including a missing key and an
answer that arrives after the member changed;
- push stop reports while signed out;
- reporter wiring for hosted admission and the self-hosted probe;
- frames the socket can't decode;
- a table of relay refusal reasons, each either reported or not, for
both `OK` and `CLOSED`;
- an install ID file whose reads fail keeps its ID.
Full-app run on an iPhone 17 Pro simulator at 61b04f6: startup, channel
list, sending and reacting, Settings, the Diagnostics card and Send test
report, and install ID repair (a file that isn't valid UTF-8 and an
empty file each got a new ID that stayed the same on the next launch).
Not covered: a relay clock-window refusal, which the live relay would
not produce.
## Diagnostics card
| Before | After |
| --- | --- |
| 
| 
|
---------
Signed-off-by: Tom Brow <tomb@block.xyz>
Co-authored-by: studio <dc2fc481343672df31a689840c09299972e704dac592e0da6f18486646a7ee3e@localhost>
238 lines
6.9 KiB
Dart
238 lines
6.9 KiB
Dart
import 'dart:convert';
|
|
import 'dart:async';
|
|
|
|
import 'package:buzz/shared/deeplink/deep_link.dart';
|
|
import 'package:buzz/shared/hosted/account_identity_store.dart';
|
|
import 'package:buzz/shared/hosted/browser_auth.dart';
|
|
import 'package:buzz/shared/hosted/builderlab_account.dart';
|
|
import 'package:buzz/shared/hosted/hosted_flow.dart';
|
|
import 'package:buzz/shared/hosted/hosted_identity_service.dart';
|
|
import 'package:flutter_secure_storage/flutter_secure_storage.dart';
|
|
import 'package:http/http.dart' as http;
|
|
import 'package:http/testing.dart';
|
|
import 'package:nostr/nostr.dart' as nostr;
|
|
|
|
class DiscoveryBrowser extends BrowserAuth {
|
|
final callbacks = StreamController<BrowserAuthCode>.broadcast();
|
|
Map<String, String>? startedIntent;
|
|
@override
|
|
Future<void> start(Map<String, String> intent) async {
|
|
startedIntent = intent;
|
|
}
|
|
|
|
@override
|
|
Stream<BrowserAuthCode> get codes => callbacks.stream;
|
|
@override
|
|
Future<void> dispose() async {
|
|
await callbacks.close();
|
|
await super.dispose();
|
|
}
|
|
|
|
@override
|
|
Future<void> initialize() async {}
|
|
|
|
/// Thrown by [clearIntent] while set, as unavailable storage would.
|
|
Object? clearIntentError;
|
|
@override
|
|
Future<void> clearIntent() async {
|
|
if (clearIntentError case final error?) throw error;
|
|
}
|
|
|
|
@override
|
|
Future<void> cancel() async {}
|
|
}
|
|
|
|
class WidgetDiscoveryAccount extends BuilderlabAccountClient {
|
|
WidgetDiscoveryAccount(this.fixture);
|
|
final HostedCommunitiesFixture fixture;
|
|
@override
|
|
BuilderlabAccount? get account => fixture.expired
|
|
? null
|
|
: BuilderlabAccount(
|
|
subject: fixture.subject,
|
|
expiresAt: DateTime.now().add(const Duration(hours: 1)),
|
|
);
|
|
@override
|
|
Future<BuilderlabAccount?> restore() async => account;
|
|
@override
|
|
Future<void> rememberCommunity(
|
|
String relay, {
|
|
required String expectedSubject,
|
|
}) async {
|
|
if (account?.subject != expectedSubject) throw AccountFailure.denied;
|
|
fixture.remembered.add(relay);
|
|
}
|
|
|
|
@override
|
|
Future<Map<String, dynamic>> post(
|
|
String path,
|
|
Map<String, dynamic> body, {
|
|
String? expectedSubject,
|
|
String? signer,
|
|
}) async {
|
|
fixture.requests.add(path);
|
|
if (path.endsWith('/current')) {
|
|
if (fixture.rejectCurrent) throw AccountFailure.denied;
|
|
return {
|
|
'identity': {'pubkey_hex': fixture.bound},
|
|
};
|
|
}
|
|
if (path.endsWith('/known-communities/list')) {
|
|
return {'communities': fixture.rows};
|
|
}
|
|
throw AccountFailure.unavailable;
|
|
}
|
|
}
|
|
|
|
class HostedCommunitiesFixture {
|
|
final keys = nostr.Keys('1'.padLeft(64, '0'));
|
|
AccountIdentityStore get store => AccountIdentityStore(subject: subject);
|
|
final browser = DiscoveryBrowser();
|
|
late BuilderlabAccountClient account;
|
|
late final HostedFlow flow;
|
|
final admitted = <String>[];
|
|
String? openedCommunity;
|
|
Future<void> Function()? beforeOpen;
|
|
final remembered = <String>[];
|
|
final requests = <String>[];
|
|
List<Map<String, dynamic>> rows = [];
|
|
String? bound;
|
|
bool rejectList = false;
|
|
bool rejectCurrent = false;
|
|
bool expired = false;
|
|
bool unavailable = false;
|
|
String subject = 'owner';
|
|
Future<void> Function(String)? beforeAdmission;
|
|
Future<void> Function()? beforeList;
|
|
Future<void> Function()? beforeMe;
|
|
Uri? opened;
|
|
|
|
Map<String, dynamic> row(String name) => {
|
|
'community_url': 'wss://$name.communities.buzz.xyz',
|
|
};
|
|
|
|
Future<void> initialize({
|
|
bool backup = true,
|
|
bool widget = false,
|
|
bool withIdentity = true,
|
|
FlutterSecureStorage? accountStorage,
|
|
bool coldAccount = false,
|
|
}) async {
|
|
bound = withIdentity ? keys.public : null;
|
|
final expiry = DateTime.now()
|
|
.toUtc()
|
|
.add(const Duration(hours: 1))
|
|
.toIso8601String();
|
|
final client = MockClient((request) async {
|
|
final path = request.url.path;
|
|
requests.add(path);
|
|
if (path.endsWith('/exchange')) {
|
|
return http.Response(
|
|
jsonEncode({'session_credential': 'fixture', 'expires_at': expiry}),
|
|
200,
|
|
);
|
|
}
|
|
if (path.endsWith('/me')) {
|
|
await beforeMe?.call();
|
|
if (expired) return http.Response('{}', 401);
|
|
if (unavailable) return http.Response('{}', 503);
|
|
return http.Response(
|
|
jsonEncode({
|
|
'subject': subject,
|
|
'email': 'owner@example.com',
|
|
'expires_at': expiry,
|
|
}),
|
|
200,
|
|
);
|
|
}
|
|
if (path.endsWith('/current')) {
|
|
return http.Response(
|
|
jsonEncode({
|
|
'identity': bound == null ? null : {'pubkey_hex': bound},
|
|
}),
|
|
200,
|
|
);
|
|
}
|
|
if (path.endsWith('/nsec-backups/status')) {
|
|
return http.Response(
|
|
jsonEncode({
|
|
'backup_nostr_pubkeys': <String>[],
|
|
'current_nostr_pubkey': bound,
|
|
}),
|
|
200,
|
|
);
|
|
}
|
|
if (path.endsWith('/known-communities/list')) {
|
|
await beforeList?.call();
|
|
if (rejectList) return http.Response('{}', 503);
|
|
return http.Response(jsonEncode({'communities': rows}), 200);
|
|
}
|
|
if (path.endsWith('/known-communities/add')) {
|
|
final body = jsonDecode(request.body) as Map<String, dynamic>;
|
|
return http.Response(
|
|
jsonEncode({
|
|
'community': {'community_url': body['community_url']},
|
|
}),
|
|
200,
|
|
);
|
|
}
|
|
return http.Response('{}', 503);
|
|
});
|
|
account = BuilderlabAccountClient(
|
|
storage: accountStorage,
|
|
httpClient: client,
|
|
);
|
|
if (widget) {
|
|
account.dispose();
|
|
account = WidgetDiscoveryAccount(this);
|
|
} else {
|
|
await account.exchange(code: 'code', verifier: 'a' * 43);
|
|
if (coldAccount) {
|
|
// A later process: the session is saved but not restored yet.
|
|
account.dispose();
|
|
account = BuilderlabAccountClient(
|
|
storage: accountStorage,
|
|
httpClient: client,
|
|
);
|
|
}
|
|
}
|
|
if (withIdentity) await store.create(keys.nsec);
|
|
if (backup && withIdentity) {
|
|
await store.confirmBackup(
|
|
BackupReceipt(
|
|
backend: BuilderlabAccountClient.origin.toString(),
|
|
subject: 'owner',
|
|
pubkey: keys.public,
|
|
backupId: 'fixture-backup',
|
|
),
|
|
);
|
|
}
|
|
flow = HostedFlow(
|
|
account: account,
|
|
browser: browser,
|
|
store: store,
|
|
identity: HostedIdentityService(account: account, store: store),
|
|
openWeb: (uri) async {
|
|
opened = uri;
|
|
return true;
|
|
},
|
|
openLandingCommunity: (link, subject, pubkey, current) async {
|
|
await beforeOpen?.call();
|
|
if (current()) openedCommunity = link.relayUrl;
|
|
},
|
|
enterCommunity: (link, subject, pubkey, current) async {
|
|
// Record the relay host: names are display labels.
|
|
final host = Uri.parse((link as AddCommunityDeepLink).relayUrl).host;
|
|
await beforeAdmission?.call(host);
|
|
if (current()) admitted.add(host);
|
|
},
|
|
);
|
|
}
|
|
|
|
Future<void> dispose() async {
|
|
flow.dispose();
|
|
await browser.dispose();
|
|
account.dispose();
|
|
}
|
|
}
|