Skip to content

Commit

Permalink
Add caching of current user
Browse files Browse the repository at this point in the history
  • Loading branch information
abitofevrything committed Sep 16, 2023
1 parent cf5c17d commit 4be3fb2
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 25 deletions.
54 changes: 36 additions & 18 deletions lib/src/client.dart
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import 'package:nyxx/src/api_options.dart';
import 'package:nyxx/src/models/application.dart';
import 'package:nyxx/src/models/guild/guild.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/models/user/user.dart';
import 'package:nyxx/src/plugin/plugin.dart';
import 'package:nyxx/src/utils/flags.dart';
import 'package:oauth2/oauth2.dart';
Expand Down Expand Up @@ -63,11 +64,9 @@ abstract class Nyxx {
return _doConnect(apiOptions, clientOptions, () async {
final client = NyxxRest._(apiOptions, clientOptions);

if (clientOptions.applicationId != null) {
return client..application = client.applications[clientOptions.applicationId!];
}

return client..application = await client.applications.fetchCurrentApplication();
return client
.._application = await client.applications.fetchCurrentApplication()
.._user = await client.users.fetchCurrentUser();
}, clientOptions.plugins);
}

Expand All @@ -86,11 +85,9 @@ abstract class Nyxx {
return _doConnect(apiOptions, clientOptions, () async {
final client = NyxxOAuth2._(apiOptions, clientOptions);

if (clientOptions.applicationId != null) {
return client..application = client.applications[clientOptions.applicationId!];
}

return client..application = await client.applications.fetchCurrentApplication();
return client
.._application = await client.applications.fetchCurrentApplication()
.._user = await client.users.fetchCurrentUser();
}, clientOptions.plugins);
}

Expand All @@ -116,11 +113,9 @@ abstract class Nyxx {
return _doConnect(apiOptions, clientOptions, () async {
final client = NyxxGateway._(apiOptions, clientOptions);

if (clientOptions.applicationId != null) {
client.application = client.applications[clientOptions.applicationId!];
} else {
client.application = await client.applications.fetchCurrentApplication();
}
client
.._application = await client.applications.fetchCurrentApplication()
.._user = await client.users.fetchCurrentUser();

// We can't use client.gateway as it is not initialized yet
final gatewayManager = GatewayManager(client);
Expand Down Expand Up @@ -148,7 +143,12 @@ class NyxxRest with ManagerMixin implements Nyxx {
late final HttpHandler httpHandler = HttpHandler(this);

/// The application associated with this client.
late final PartialApplication application;
PartialApplication get application => _application;
late final PartialApplication _application;

/// The user associated with this client.
PartialUser get user => _user;
late final PartialUser _user;

@override
Logger get logger => options.logger;
Expand Down Expand Up @@ -194,7 +194,16 @@ class NyxxOAuth2 with ManagerMixin implements NyxxRest {
Logger get logger => options.logger;

@override
late final PartialApplication application;
PartialApplication get application => _application;

@override
late final PartialApplication _application;

@override
PartialUser get user => _user;

@override
late final PartialUser _user;

NyxxOAuth2._(this.apiOptions, this.options);

Expand Down Expand Up @@ -227,7 +236,16 @@ class NyxxGateway with ManagerMixin, EventMixin implements NyxxRest {
late final HttpHandler httpHandler = HttpHandler(this);

@override
late final PartialApplication application;
PartialApplication get application => _application;

@override
late final PartialApplication _application;

@override
PartialUser get user => _user;

@override
late final PartialUser _user;

/// The [Gateway] used by this client to send and receive Gateway events.
// Initialized in connectGateway due to a circular dependency
Expand Down
6 changes: 0 additions & 6 deletions lib/src/client_options.dart
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import 'package:nyxx/src/models/guild/member.dart';
import 'package:nyxx/src/models/guild/scheduled_event.dart';
import 'package:nyxx/src/models/message/message.dart';
import 'package:nyxx/src/models/role.dart';
import 'package:nyxx/src/models/snowflake.dart';
import 'package:nyxx/src/models/sticker/global_sticker.dart';
import 'package:nyxx/src/models/sticker/guild_sticker.dart';
import 'package:nyxx/src/models/user/user.dart';
Expand Down Expand Up @@ -93,9 +92,6 @@ class RestClientOptions extends ClientOptions {
/// The [CacheConfig] to use for the [GuildApplicationCommandManager.permissionsCache] cache.
final CacheConfig<CommandPermissions> commandPermissionsConfig;

/// The ID of the application the client is authenticating for.
final Snowflake? applicationId;

/// Create a new [RestClientOptions].
const RestClientOptions({
super.plugins,
Expand All @@ -118,7 +114,6 @@ class RestClientOptions extends ClientOptions {
this.globalStickerCacheConfig = const CacheConfig(),
this.applicationCommandConfig = const CacheConfig(),
this.commandPermissionsConfig = const CacheConfig(),
this.applicationId,
});
}

Expand Down Expand Up @@ -150,6 +145,5 @@ class GatewayClientOptions extends RestClientOptions {
super.voiceStateConfig,
super.applicationCommandConfig,
super.commandPermissionsConfig,
super.applicationId,
});
}
10 changes: 9 additions & 1 deletion test/integration/rest_integration_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,21 @@ void main() {
// refer to the same variable if we use setUp and tearDown. use the All variants
// to mitigate this.
setUpAll(() async {
client = await Nyxx.connectRest(testToken!, options: RestClientOptions(applicationId: Snowflake.zero));
client = await Nyxx.connectRest(testToken!);
});

tearDownAll(() async {
await client.close();
});

test('client.user', () async {
await expectLater(client.user.get(), completes);
});

test('client.application', () {
expect(client.application.id, isNot(Snowflake.zero));
});

test('applications', () async {
await expectLater(client.applications.fetchCurrentApplication(), completes);
});
Expand Down

0 comments on commit 4be3fb2

Please sign in to comment.