You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

104 lines
3.0 KiB

import '../../nostr/models/nostr_profile.dart';
/// Data model representing a user session.
///
/// This model stores user identification and authentication information
/// for session management and data isolation.
class User {
/// Unique identifier for the user.
final String id;
/// Display name or username for the user.
final String username;
/// Optional authentication token or session token.
final String? token;
/// Timestamp when the session was created (milliseconds since epoch).
final int createdAt;
/// Optional Nostr profile data (if logged in via Nostr).
final NostrProfile? nostrProfile;
/// Optional Nostr private key (nsec) if logged in with nsec.
/// This is stored to enable event publishing. Only set if user logged in with nsec.
final String? nostrPrivateKey;
/// Creates a [User] instance.
///
/// [id] - Unique identifier for the user.
/// [username] - Display name or username.
/// [token] - Optional authentication token.
/// [createdAt] - Session creation timestamp (defaults to current time).
/// [nostrProfile] - Optional Nostr profile data.
/// [nostrPrivateKey] - Optional Nostr private key (nsec) for event publishing.
User({
required this.id,
required this.username,
this.token,
int? createdAt,
this.nostrProfile,
this.nostrPrivateKey,
}) : createdAt = createdAt ?? DateTime.now().millisecondsSinceEpoch;
/// Creates a [User] from a Map (e.g., from database or JSON).
factory User.fromMap(Map<String, dynamic> map) {
return User(
id: map['id'] as String,
username: map['username'] as String,
token: map['token'] as String?,
createdAt: map['created_at'] as int?,
nostrProfile: map['nostr_profile'] != null
? NostrProfile.fromJson(map['nostr_profile'] as Map<String, dynamic>)
: null,
nostrPrivateKey: map['nostr_private_key'] as String?,
);
}
/// Converts the [User] to a Map for storage.
Map<String, dynamic> toMap() {
return {
'id': id,
'username': username,
'token': token,
'created_at': createdAt,
'nostr_profile': nostrProfile?.toJson(),
'nostr_private_key': nostrPrivateKey,
};
}
/// Creates a copy of this [User] with updated fields.
User copyWith({
String? id,
String? username,
String? token,
int? createdAt,
NostrProfile? nostrProfile,
String? nostrPrivateKey,
}) {
return User(
id: id ?? this.id,
username: username ?? this.username,
token: token ?? this.token,
createdAt: createdAt ?? this.createdAt,
nostrProfile: nostrProfile ?? this.nostrProfile,
nostrPrivateKey: nostrPrivateKey ?? this.nostrPrivateKey,
);
}
@override
String toString() {
return 'User(id: $id, username: $username, createdAt: $createdAt)';
}
@override
bool operator ==(Object other) {
if (identical(this, other)) return true;
return other is User && other.id == id;
}
@override
int get hashCode => id.hashCode;
}

Powered by TurnKey Linux.