-
-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: show forum votes with voters name
- Loading branch information
Showing
15 changed files
with
325 additions
and
117 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
// Project imports: | ||
import 'user.dart'; | ||
import 'user_dto.dart'; | ||
import 'user_level.dart'; | ||
import 'user_self.dart'; | ||
import 'user_self_dto.dart'; | ||
|
||
User userDtoToUser( | ||
UserDto d, | ||
) { | ||
return User( | ||
id: d.id ?? 0, | ||
level: intToUserLevel(d.level ?? 0), | ||
name: d.name ?? 'User', | ||
joinedDate: d.createdAt ?? DateTime.now(), | ||
uploadCount: d.uploadCount ?? 0, | ||
tagEditCount: d.tagEditCount ?? 0, | ||
noteEditCount: d.noteEditCount ?? 0, | ||
commentCount: d.commentCount ?? 0, | ||
forumPostCount: d.forumPostCount ?? 0, | ||
favoriteGroupCount: d.favoriteGroupCount ?? 0, | ||
); | ||
} | ||
|
||
UserSelf userDtoToUserSelf( | ||
UserSelfDto d, | ||
List<String> defaultBlacklistedTags, | ||
) { | ||
return UserSelf( | ||
id: d.id ?? 0, | ||
level: intToUserLevel(d.level ?? 0), | ||
name: d.name ?? 'User', | ||
blacklistedTags: d.blacklistedTags == null | ||
? defaultBlacklistedTags | ||
: tagStringToListTagString(d.blacklistedTags ?? ''), | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
// Package imports: | ||
import 'package:dio/dio.dart'; | ||
import 'package:hive/hive.dart'; | ||
|
||
// Project imports: | ||
import 'creator.dart'; | ||
import 'user_repository.dart'; | ||
|
||
abstract interface class CreatorRepository { | ||
Future<List<Creator>> getCreatorsByIdStringComma( | ||
String idComma, { | ||
CancelToken? cancelToken, | ||
}); | ||
} | ||
|
||
class CreatorRepositoryFromUserRepo implements CreatorRepository { | ||
CreatorRepositoryFromUserRepo( | ||
this.repo, | ||
this.box, | ||
); | ||
|
||
final UserRepository repo; | ||
final Box box; | ||
|
||
@override | ||
Future<List<Creator>> getCreatorsByIdStringComma( | ||
String idComma, { | ||
CancelToken? cancelToken, | ||
}) async { | ||
final now = DateTime.now(); | ||
final twoDaysAgo = now.subtract(const Duration(days: 2)); | ||
|
||
final ids = idComma.split(','); | ||
|
||
// Identify IDs not in cache | ||
final idsNotInCached = ids.where((e) => !box.containsKey(e)).toList(); | ||
|
||
// Fetch creators from cache | ||
final creatorInCached = ids | ||
.where((e) => box.containsKey(e)) | ||
.map((e) => Creator.fromJson(box.get(e)['creator'])) | ||
.toList(); | ||
|
||
List<Creator> creators = []; | ||
|
||
// Only fetch creators from API when there are uncached IDs | ||
if (idsNotInCached.isNotEmpty) { | ||
try { | ||
creators = await repo | ||
.getUsersByIdStringComma( | ||
idsNotInCached.join(','), | ||
cancelToken: cancelToken, | ||
) | ||
.then((value) => value.map(Creator.fromUser).toList()); | ||
} catch (e) { | ||
// handle the exception | ||
} | ||
|
||
for (var e in creators) { | ||
if (!box.containsKey(e.id.toString()) || | ||
DateTime.parse(box.get(e.id.toString())['time']) | ||
.isBefore(twoDaysAgo)) { | ||
// Update cache with new creators | ||
box.put(e.id.toString(), { | ||
'time': now.toIso8601String(), | ||
'creator': e.toJson(), | ||
}); | ||
} | ||
} | ||
} | ||
|
||
return [...creators, ...creatorInCached]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
// Package imports: | ||
import 'package:flutter_riverpod/flutter_riverpod.dart'; | ||
|
||
// Project imports: | ||
import 'package:boorusama/boorus/danbooru/feats/users/creator_repository.dart'; | ||
import 'package:boorusama/boorus/danbooru/feats/users/users.dart'; | ||
import 'package:boorusama/functional.dart'; | ||
|
||
class CreatorsNotifier extends Notifier<IMap<int, Creator>> { | ||
CreatorRepository get repo => ref.watch(danbooruCreatorRepoProvider); | ||
|
||
@override | ||
IMap<int, Creator> build() { | ||
return <int, Creator>{}.lock; | ||
} | ||
|
||
Future<void> load(List<int> ids) async { | ||
// only load ids that are not already loaded | ||
final notInCached = ids.where((id) => !state.containsKey(id)).toList(); | ||
|
||
final creators = | ||
await repo.getCreatorsByIdStringComma(notInCached.join(',')); | ||
|
||
final map = { | ||
for (final creator in creators) creator.id: creator, | ||
}.lock; | ||
|
||
state = state.addAll(map); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
// Package imports: | ||
import 'package:retrofit/dio.dart'; | ||
|
||
// Project imports: | ||
import 'package:boorusama/foundation/http/http.dart'; | ||
import 'converter.dart'; | ||
import 'user.dart'; | ||
import 'user_dto.dart'; | ||
import 'user_self.dart'; | ||
import 'user_self_dto.dart'; | ||
|
||
List<UserDto> parseUserDtos( | ||
HttpResponse<dynamic> value, | ||
) => | ||
parseResponse( | ||
value: value, | ||
converter: (item) => UserDto.fromJson(item), | ||
); | ||
|
||
List<User> parseUsers(List<UserDto> value) => value.map(userDtoToUser).toList(); | ||
|
||
List<UserSelf> parseUserSelf( | ||
HttpResponse<dynamic> value, | ||
List<String> defaultBlacklistedTags, | ||
) => | ||
parseResponse( | ||
value: value, | ||
converter: (item) => UserSelfDto.fromJson(item), | ||
).map((u) => userDtoToUserSelf(u, defaultBlacklistedTags)).toList(); |
Oops, something went wrong.