Files
chat-server/lib/src/handlers/public_handlers.dart
T

105 lines
2.5 KiB
Dart

import 'package:chat_server/objectbox.g.dart';
import 'package:relic/relic.dart';
import '../../config/config.dart';
import '../state/app_state.dart';
import '../utils/extensions.dart';
/// Public endpoint for cross-server identity verification
/// Called by other servers to verify a user's public key
///
/// GET /.well-known/users/{username}
/// Returns: { userId, publicKey, username, displayName, avatarUrl }
Future<Response> publicUserHandler(Request req) async {
final username = req.rawPathParameters[#username];
if (username == null) {
return Response.badRequest(
body: Body.fromString('Username required'),
);
}
final query = userBox
.query(
ChatUser_.username.equals(username) & ChatUser_.isLocal.equals(true),
)
.build();
final user = query.findFirst();
query.close();
if (user == null) {
return Response.notFound(
body: Body.fromString('User not found'),
);
}
return Response.ok(
body: {
'userId': user.userId,
'username': user.username,
'displayName': user.displayName,
'avatarUrl': user.avatarUrl,
'bio': user.bio,
'publicKey': user.publicKey,
'homeServer': Config.serverHost,
'createdAt': user.createdAt.toIso8601String(),
}.toBody,
);
}
/// Public endpoint to verify a user exists
/// Lightweight check without full profile
///
/// GET /.well-known/verify/{userId}
/// Returns: { exists, userId, publicKey }
Future<Response> publicVerifyHandler(Request req) async {
final userId = req.rawPathParameters[#userId];
if (userId == null) {
return Response.badRequest(
body: Body.fromString('userId required'),
);
}
final query = userBox
.query(
ChatUser_.userId.equals(userId) & ChatUser_.isLocal.equals(true),
)
.build();
final user = query.findFirst();
query.close();
if (user == null) {
return Response.ok(
body: {
'exists': false,
'userId': userId,
}.toBody,
);
}
return Response.ok(
body: {
'exists': true,
'userId': user.userId,
'publicKey': user.publicKey,
'username': user.username,
}.toBody,
);
}
/// Public server info
///
/// GET /.well-known/server
/// Returns: { serverHost, serverName, version }
Future<Response> publicServerInfoHandler(Request req) async {
return Response.ok(
body: {
'serverHost': Config.serverHost,
'serverName': Config.serverName,
'version': '0.1.0',
'features': ['identity', 'chat', 'sticker-packs'],
}.toBody,
);
}