implemented avatar management for user profiles and chat rooms, including 1:1 cropping, integrated OCS and Spreed avatar APIs, added cache invalidation logic, and updated the account settings view to display user info and profile pictures.

This commit is contained in:
2026-05-31 18:42:30 +02:00
parent f966cf302b
commit 5ebf5bccdb
9 changed files with 777 additions and 38 deletions
+74 -8
View File
@@ -13,10 +13,18 @@ class UserAvatar extends StatefulWidget {
final String id;
final bool isGroup;
final int size;
/// Server-side pixel size requested for user avatars. `null` lets the
/// widget pick `(size * 4).clamp(64, 1024)` — enough headroom for typical
/// device pixel ratios. Group avatars ignore this (Spreed serves one
/// fixed-size image per token).
final int? requestSize;
const UserAvatar({
required this.id,
this.isGroup = false,
this.size = 20,
this.requestSize,
super.key,
});
@@ -46,6 +54,48 @@ final LinkedHashMap<String, _AvatarCacheEntry> _resolvedAvatars =
LinkedHashMap<String, _AvatarCacheEntry>();
final Map<String, Future<_AvatarPayload?>> _pendingAvatars = {};
// Bumped by invalidateAvatarCache so *already mounted* avatars re-resolve.
// Clearing the cache map alone only affects future mounts — a UserAvatar
// elsewhere on screen (chat list, chat header) holds its bytes in State and
// would keep showing the stale image until rebuilt. Each state listens here
// and re-attaches: invalidated urls miss the cache and re-fetch, the rest
// hit the cache and cost nothing.
final ValueNotifier<int> _avatarCacheGeneration = ValueNotifier<int>(0);
String avatarUrl({required String id, required bool isGroup, int size = 512}) {
final host = EndpointData().nextcloud().full();
if (isGroup) {
return 'https://$host/ocs/v2.php/apps/spreed/api/v1/room/$id/avatar';
}
return 'https://$host/avatar/$id/$size';
}
/// Drops cached avatar bytes so the next mount re-fetches from the server.
/// Call after the app uploaded or removed an avatar — without this the
/// 30-min TTL would mask the change for the rest of the session.
///
/// With [id]+[isGroup], invalidates every cached size for that subject
/// (user avatars cache per-size URL). Without arguments, clears everything.
/// Pending fetches are also discarded so a stale in-flight response can't
/// repopulate the cache.
void invalidateAvatarCache({String? id, bool? isGroup}) {
if (id == null) {
_resolvedAvatars.clear();
_pendingAvatars.clear();
} else if (isGroup == true) {
final url = avatarUrl(id: id, isGroup: true);
_resolvedAvatars.remove(url);
_pendingAvatars.remove(url);
} else {
// User avatars include the rendered size in the URL — drop every variant.
final host = EndpointData().nextcloud().full();
final prefix = 'https://$host/avatar/$id/';
_resolvedAvatars.removeWhere((url, _) => url.startsWith(prefix));
_pendingAvatars.removeWhere((url, _) => url.startsWith(prefix));
}
_avatarCacheGeneration.value++;
}
_AvatarCacheEntry? _readAvatarCache(String url) {
final entry = _resolvedAvatars.remove(url);
if (entry == null) return null;
@@ -72,6 +122,20 @@ class _UserAvatarState extends State<UserAvatar> {
void initState() {
super.initState();
_attach();
_avatarCacheGeneration.addListener(_onCacheInvalidated);
}
@override
void dispose() {
_avatarCacheGeneration.removeListener(_onCacheInvalidated);
super.dispose();
}
// Re-resolve when the cache generation changes. Cache hit → no network and
// the visible bytes stay; cache miss (our url was invalidated) → re-fetch.
void _onCacheInvalidated() {
if (!mounted) return;
setState(_attach);
}
@override
@@ -79,18 +143,20 @@ class _UserAvatarState extends State<UserAvatar> {
super.didUpdateWidget(oldWidget);
if (oldWidget.id != widget.id ||
oldWidget.isGroup != widget.isGroup ||
oldWidget.size != widget.size) {
oldWidget.size != widget.size ||
oldWidget.requestSize != widget.requestSize) {
_attach();
}
}
String _url() {
final host = EndpointData().nextcloud().full();
if (widget.isGroup) {
return 'https://$host/ocs/v2.php/apps/spreed/api/v1/room/${widget.id}/avatar';
}
return 'https://$host/avatar/${widget.id}/${widget.size}';
}
int _resolvedRequestSize() =>
widget.requestSize ?? (widget.size * 4).clamp(64, 1024);
String _url() => avatarUrl(
id: widget.id,
isGroup: widget.isGroup,
size: _resolvedRequestSize(),
);
void _attach() {
final url = _url();