forked from Foxyz/sync_replay_project
The shell (lib/src/shell/) replaces the Home and session screens:
- Party strip: profile, Library / Schedule / Settings tabs, Join and
Host cards (hosting no longer takes media), status-ring avatar stack
with the member list, chat & activity panel, Leave.
- The real session player is mounted once and shown full, as a
draggable corner-snapping mini player, or fullscreen (window_manager,
party and toasts overlaid); the Video is never mounted twice.
- Profile popover with inline rename, colour and a crop picker (256 px
PNG), pushed live to the session; avatar toasts; waiting pill.
- Motion kit (fastEaseInToSlowEaseOut, fade-through, popovers,
mouse-drag/wheel horizontal scrolling).
- Library = the existing picker for now (restyled next round); Schedule
= the new SchedulePage; Settings = left nav over the existing panels.
Protocol 9 (packages/core, packages/server, sync client):
- {"t":"profile"} renames / changes the picture mid-session ("rename",
"avatar" feed actions); names are cleaned the same way at hello.
- state.members carry avatarHash; pictures are served once from
/a/<sid>/<token>/<hash> (PNG/JPEG/WebP/GIF by magic bytes only) and
cached by hash in the app (AvatarCache).
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
97 lines
2.8 KiB
Dart
97 lines
2.8 KiB
Dart
import 'dart:io';
|
|
import 'dart:math';
|
|
|
|
import 'package:sync_relay_core/core.dart';
|
|
|
|
enum MediaGate { ok, gone, unauthorized, forbidden, offline }
|
|
|
|
class ViewerToken {
|
|
final String token;
|
|
/// Cleaned ([cleanMemberName]); follows `profile` renames.
|
|
String name;
|
|
bool kicked = false;
|
|
/// Sent the server's host key in hello: the hosting app's own client.
|
|
bool isHost = false;
|
|
int bytesServed = 0;
|
|
DateTime lastSeen;
|
|
Future<void> Function(String reason)? onKick;
|
|
ViewerToken({required this.token, required this.name})
|
|
: lastSeen = DateTime.now().toUtc();
|
|
|
|
bool isStale(Duration timeout, [DateTime? now]) =>
|
|
(now ?? DateTime.now().toUtc()).difference(lastSeen) > timeout;
|
|
|
|
void touch([DateTime? now]) => lastSeen = now ?? DateTime.now().toUtc();
|
|
}
|
|
|
|
class SessionMedia {
|
|
final String name;
|
|
final int size;
|
|
final double duration;
|
|
final String? url;
|
|
final File? file;
|
|
/// Jellyfin media by reference; never together with [url] or [file].
|
|
final JellyfinRef? jellyfin;
|
|
final int mediaId;
|
|
const SessionMedia({
|
|
required this.name,
|
|
required this.size,
|
|
required this.duration,
|
|
this.url,
|
|
this.file,
|
|
this.jellyfin,
|
|
this.mediaId = 1,
|
|
});
|
|
}
|
|
|
|
class Session {
|
|
final String id;
|
|
final String secret;
|
|
final Map<String, ViewerToken> tokens = {};
|
|
SessionMedia? media;
|
|
final _random = Random.secure();
|
|
|
|
Session({required this.id, required this.secret});
|
|
|
|
/// The avatar is only checked here; the engine keeps the picture.
|
|
String issueToken(String name, String avatar) {
|
|
if (avatar.length > RelayConstants.maxAvatarBytes) {
|
|
throw const AuthException('avatar too large');
|
|
}
|
|
final bytes = List<int>.generate(16, (_) => _random.nextInt(256));
|
|
final token = bytes.map((b) => b.toRadixString(16).padLeft(2, '0')).join();
|
|
tokens[token] = ViewerToken(token: token, name: cleanMemberName(name));
|
|
return token;
|
|
}
|
|
|
|
MediaGate gate(String? token) {
|
|
if (token == null || !tokens.containsKey(token)) return MediaGate.unauthorized;
|
|
if (tokens[token]!.kicked) return MediaGate.forbidden;
|
|
return MediaGate.ok;
|
|
}
|
|
|
|
Future<void> kick(String token, {String reason = ''}) async {
|
|
final viewer = tokens[token];
|
|
if (viewer == null) throw const AuthException('unknown token');
|
|
viewer.kicked = true;
|
|
await viewer.onKick?.call(reason);
|
|
}
|
|
|
|
void addBytes(String? token, int n) {
|
|
if (token != null) tokens[token]?.bytesServed += n;
|
|
}
|
|
|
|
int get totalBytesServed => tokens.values.fold(0, (a, v) => a + v.bytesServed);
|
|
}
|
|
|
|
class SessionMap {
|
|
final Map<String, Session> _sessions = {};
|
|
|
|
Session getOrCreate(String id, String secret) =>
|
|
_sessions.putIfAbsent(id, () => Session(id: id, secret: secret));
|
|
|
|
Session? get(String id) => _sessions[id];
|
|
|
|
void drop(String id) => _sessions.remove(id);
|
|
}
|