Skip to content

Commit

Permalink
Refactor session handling; Implement main page statistics
Browse files Browse the repository at this point in the history
  • Loading branch information
l7ssha committed Nov 18, 2024
1 parent 843e30e commit cb6b076
Show file tree
Hide file tree
Showing 3 changed files with 108 additions and 22 deletions.
23 changes: 4 additions & 19 deletions lib/src/web_app/api_server.dart
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ class WebServer {
...data.toJson(),
'clientId': clientId,
'redirectUri': clientRedirectUri,
'user_data': Session.getSession(request)?.data['user_data'] ?? false,
'user_data': getUserDataFromSession(request) ?? false,
});
}

Expand All @@ -47,34 +47,19 @@ class WebServer {
final tokenBodyJson = jsonDecode(tokenResponse.body);
final token = tokenBodyJson['access_token'];

print(tokenResponse.body);
print(tokenResponse.statusCode);

final userData = await http.get(Uri.https('discord.com', '/api/oauth2/@me'), headers: {
"Accept": "application/json",
"Authorization": "Bearer $token",
});

final userDataJson = jsonDecode(userData.body);
print(userData.body);
print(userData.statusCode);

var session = Session.getSession(request);
session ??= Session.createSession(request);
session.data['user_data'] = {
'id': userDataJson['user']['id'],
'name': userDataJson['user']['global_name'] ?? userDataJson['user']['username'],
'avatar': userDataJson['user']['avatar'],
'expires_at': userDataJson['expires'],
'token': token,
};
session.expires = DateTime.now().add(Duration(seconds: tokenBodyJson['expires_in']));

initSession(request, userDataJson, tokenBodyJson);

return shelf.Response.seeOther("/");
}

Future<shelf.Response> _handleLogOut(shelf.Request request) async {
Session.deleteSession(request);
deleteSession(request);

return shelf.Response.seeOther("/");
}
Expand Down
21 changes: 21 additions & 0 deletions lib/src/web_app/utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'dart:io';

import 'package:mustachex/mustachex.dart';
import 'package:shelf/shelf.dart' as shelf;
import 'package:shelf_session/session_middleware.dart';

shelf.Response createJsonErrorResponse(int errorCode, String errorMessage) {
return shelf.Response(errorCode,
Expand All @@ -23,3 +24,23 @@ Future<shelf.Response> createTwigResponse(String name, {Map<String, dynamic>? pa
shelf.Response createUnauthorizedResponse(String errorMessage) => createJsonErrorResponse(400, errorMessage);

shelf.Response createForbiddenResponse() => shelf.Response.forbidden(null);

void initSession(shelf.Request request, Map<String, dynamic> userDataJson, Map<String, dynamic> tokenDataJson) {
var session = Session.getSession(request);
session ??= Session.createSession(request);
session.data['user_data'] = {
'id': userDataJson['user']['id'],
'name': userDataJson['user']['global_name'] ?? userDataJson['user']['username'],
'avatar': userDataJson['user']['avatar'],
'expires_at': userDataJson['expires'],
'token': tokenDataJson['access_token'],
};
session.expires = DateTime.now().add(Duration(seconds: tokenDataJson['expires_in']));
}

void deleteSession(shelf.Request request) {
Session.deleteSession(request);
}

Map<String, dynamic>? getUserDataFromSession(shelf.Request request) =>
Session.getSession(request)?.data['user_data'] as Map<String, dynamic>?;
86 changes: 83 additions & 3 deletions templates/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -6,20 +6,23 @@
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/[email protected]/css/bulma.min.css">
</head>
<body>
<nav class="navbar" role="navigation" aria-label="main navigation">
<nav class="navbar is-primary" role="navigation" aria-label="main navigation">
<div class="navbar-brand">
<a class="navbar-item" href="/">Running on Dart</a>
</div>

<div id="navbarBasicExample" class="navbar-menu">
<div class="navbar-start">
<a class="navbar-item" href="/guild">Guild</a>
<a class="navbar-item" href="/guilds">Guilds</a>
</div>

<div class="navbar-end">
<div class="navbar-item">
<div class="buttons">
{{^user_data}}
<a class="button is-light" href="https://discord.com/oauth2/authorize?response_type=code&client_id={{clientId}}&redirect_uri={{redirectUri}}&scope=identify">Log in</a>
{{/user_data}}

{{#user_data}}
<a class="button is-light" href="/logout">Log out</a>
{{/user_data}}
Expand All @@ -29,6 +32,83 @@
</div>
</nav>

<p>Is logged in: {{user_data}}</p>
<div class="container block mt-4">
<div class="notification is-danger">
Experimental version
</div>
</div>

<div class="container block">
<h1 class="title">Cache Info</h1>
<div class="grid">
<div class="hero-body cell">
<p class="title">{{cached_channels}}</p>
<p class="subtitle">Cached channels</p>
</div>
<div class="hero-body cell">
<p class="title">{{cached_messages}}</p>
<p class="subtitle">Cached messages</p>
</div>
<div class="hero-body cell">
<p class="title">{{cached_guilds}}</p>
<p class="subtitle">Cached guilds</p>
</div>
<div class="hero-body cell">
<p class="title">{{cached_users}}</p>
<p class="subtitle">Cached Users</p>
</div>
<div class="hero-body cell">
<p class="title">{{cached_voice_states}}</p>
<p class="subtitle">Cached voice states</p>
</div>
</div>
</div>

<div class="container block">
<h1 class="title">Modules Info</h1>
<div class="grid">
<div class="hero-body cell">
<p class="title">{{total_tags_count}}</p>
<p class="subtitle">Total tags</p>
</div>
<div class="hero-body cell">
<p class="title">{{total_reminder_count}}</p>
<p class="subtitle">Total reminders</p>
</div>
<div class="hero-body cell">
<p class="title">n/a</p>
<!-- <p class="title">{{docs_update}}</p>-->
<p class="subtitle">Last docs update</p>
</div>
</div>
</div>

<div class="container block">
<h1 class="title">Program Info</h1>
<div class="grid">
<div class="hero-body cell">
<p class="title">{{nyxx_version}}</p>
<p class="subtitle">Nyxx Version</p>
</div>
<div class="hero-body cell">
<p class="title">{{version}}</p>
<p class="subtitle">Bot Version</p>
</div>
<div class="hero-body cell">
<p class="title">{{platform}}</p>
<p class="subtitle">Dart Version</p>
</div>
<div class="hero-body cell">
<p class="title">{{memory_usage_string}}</p>
<p class="subtitle">Memory usage</p>
</div>
<div class="hero-body cell is-col-start-1">
<p class="title">n/a</p>
<!-- <p class="title">{{uptime}}</p>-->
<p class="subtitle">Uptime</p>
</div>
</div>
</div>

</body>
</html>

0 comments on commit cb6b076

Please sign in to comment.