Compare commits

...

1 Commits

Author SHA1 Message Date
06212a0d72 fix auth for user with no activity. (#191) 2023-04-01 10:06:32 -07:00
5 changed files with 28 additions and 9 deletions

View File

@ -41,7 +41,12 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
await _authRepository.loggedIn.then((bool loggedIn) async { await _authRepository.loggedIn.then((bool loggedIn) async {
if (loggedIn) { if (loggedIn) {
final String? username = await _authRepository.username; final String? username = await _authRepository.username;
final User user = await _storiesRepository.fetchUser(id: username!); User? user = await _storiesRepository.fetchUser(id: username!);
/// According to Hacker News' API documentation,
/// if user has no public activity (posting a comment or story),
/// then it will not be available from the API.
user ??= User.emptyWithId(username);
emit( emit(
state.copyWith( state.copyWith(
@ -84,10 +89,10 @@ class AuthBloc extends Bloc<AuthEvent, AuthState> {
); );
if (successful) { if (successful) {
final User user = await _storiesRepository.fetchUser(id: event.username); final User? user = await _storiesRepository.fetchUser(id: event.username);
emit( emit(
state.copyWith( state.copyWith(
user: user, user: user ?? User.emptyWithId(event.username),
isLoggedIn: true, isLoggedIn: true,
status: AuthStatus.loaded, status: AuthStatus.loaded,
), ),

View File

@ -16,8 +16,13 @@ class UserCubit extends Cubit<UserState> {
void init({required String userId}) { void init({required String userId}) {
emit(state.copyWith(status: UserStatus.loading)); emit(state.copyWith(status: UserStatus.loading));
_storiesRepository.fetchUser(id: userId).then((User user) { _storiesRepository.fetchUser(id: userId).then((User? user) {
emit(state.copyWith(user: user, status: UserStatus.loaded)); emit(
state.copyWith(
user: user ?? User.emptyWithId(userId),
status: UserStatus.loaded,
),
);
}).onError((_, __) { }).onError((_, __) {
emit(state.copyWith(status: UserStatus.failure)); emit(state.copyWith(status: UserStatus.failure));
return; return;

View File

@ -17,6 +17,12 @@ class User extends Equatable {
id = '', id = '',
karma = 0; karma = 0;
const User.emptyWithId(this.id)
: about = '',
created = 0,
delay = 0,
karma = 0;
User.fromJson(Map<String, dynamic> json) User.fromJson(Map<String, dynamic> json)
: about = json['about'] as String? ?? '', : about = json['about'] as String? ?? '',
created = json['created'] as int? ?? 0, created = json['created'] as int? ?? 0,

View File

@ -74,11 +74,14 @@ class StoriesRepository {
/// Fetch a [User] by its [id]. /// Fetch a [User] by its [id].
/// Hacker News uses user's username as [id]. /// Hacker News uses user's username as [id].
Future<User> fetchUser({required String id}) async { Future<User?> fetchUser({required String id}) async {
final User user = await _firebaseClient final User? user = await _firebaseClient
.get('${_baseUrl}user/$id.json') .get('${_baseUrl}user/$id.json')
.then((dynamic val) { .then((dynamic val) {
final Map<String, dynamic> json = val as Map<String, dynamic>; final Map<String, dynamic>? json = val as Map<String, dynamic>?;
if (json == null) return null;
final User user = User.fromJson(json); final User user = User.fromJson(json);
return user; return user;
}); });

View File

@ -1,6 +1,6 @@
name: hacki name: hacki
description: A Hacker News reader. description: A Hacker News reader.
version: 1.4.0+104 version: 1.4.1+105
publish_to: none publish_to: none
environment: environment: