Skip to content

Commit

Permalink
Feat/implement server methods (#199)
Browse files Browse the repository at this point in the history
* feat(Server) create setName() method

* feat(Server) create setDefaultMessageNotifications and setExplicitContentFilter method

* feat(Server) create setAfkTimeout method

* feat(Server) create setAfkChannel method

* feat(Server) create setSytemChannel method

* feat(Server) create setRulesChannel method

* feat(Server) create setPublicUpdatesChannel method

* feat(Server) create enablePremiumProgressBar method

* feat(Server) create setSafetyAlertsChannel method

* feat: rebase and apply new marshaller system

---------

Co-authored-by: Baptiste Parmantier <[email protected]>
  • Loading branch information
DakioCode and LeadcodeDev authored Sep 9, 2024
1 parent 2b5ebda commit 2949c72
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 26 deletions.
6 changes: 0 additions & 6 deletions lib/src/api/common/embed/message_embed_builder.dart
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
import 'package:mineral/api.dart';
import 'package:mineral/src/api/common/embed/message_embed.dart';
import 'package:mineral/src/api/common/embed/message_embed_assets.dart';
import 'package:mineral/src/api/common/embed/message_embed_author.dart';
import 'package:mineral/src/api/common/embed/message_embed_field.dart';
import 'package:mineral/src/api/common/embed/message_embed_footer.dart';
import 'package:mineral/src/api/common/embed/message_embed_image.dart';

final class MessageEmbedBuilder {
String? title;
Expand Down
61 changes: 61 additions & 0 deletions lib/src/api/server/server.dart
Original file line number Diff line number Diff line change
@@ -1,13 +1,20 @@
import 'package:mineral/src/api/common/snowflake.dart';
import 'package:mineral/src/api/server/enums/default_message_notification.dart';
import 'package:mineral/src/api/server/enums/explicit_content_filter.dart';
import 'package:mineral/src/api/server/managers/channel_manager.dart';
import 'package:mineral/src/api/server/managers/member_manager.dart';
import 'package:mineral/src/api/server/managers/role_manager.dart';
import 'package:mineral/src/api/server/managers/threads_manager.dart';
import 'package:mineral/src/api/server/member.dart';
import 'package:mineral/src/api/server/server_assets.dart';
import 'package:mineral/src/api/server/server_settings.dart';
import 'package:mineral/src/infrastructure/internals/container/ioc_container.dart';
import 'package:mineral/src/infrastructure/internals/datastore/data_store.dart';
import 'package:mineral/src/infrastructure/internals/datastore/parts/server_part.dart';

final class Server {
ServerPart get _serverPart => ioc.resolve<DataStoreContract>().server;

final Snowflake id;
final String? applicationId;
final String name;
Expand All @@ -33,4 +40,58 @@ final class Server {
required this.owner,
required this.threads,
});

Future<void> setName(String name, {String? reason}) async {
await _serverPart.updateServer(id, {'name': name}, reason);
}

Future<void> setDescription(String description, {String? reason}) async {
await _serverPart.updateServer(id, {'description': description}, reason);
}

Future<void> setDefaultMessageNotifications(DefaultMessageNotification value,
{String? reason}) async {
await _serverPart.updateServer(
id, {'default_message_notifications': value.value}, reason);
}

Future<void> setExplicitContentFilter(ExplicitContentFilter value,
{String? reason}) async {
await _serverPart.updateServer(
id, {'explicit_content_filter': value.value}, reason);
}

Future<void> setAfkTimeout(int value, {String? reason}) async {
await _serverPart.updateServer(id, {'afk_timeout': value}, reason);
}

Future<void> setAfkChannel(String? channelId, {String? reason}) async {
await _serverPart.updateServer(id, {'afk_channel_id': channelId}, reason);
}

Future<void> setSystemChannel(String? channelId, {String? reason}) async {
await _serverPart.updateServer(
id, {'system_channel_id': channelId}, reason);
}

Future<void> setRulesChannel(String? channelId, {String? reason}) async {
await _serverPart.updateServer(id, {'rules_channel_id': channelId}, reason);
}

Future<void> setPublicUpdatesChannel(String? channelId,
{String? reason}) async {
await _serverPart.updateServer(
id, {'public_updates_channel_id': channelId}, reason);
}

Future<void> enablePremiumProgressBar(bool value, {String? reason}) async {
await _serverPart.updateServer(
id, {'premium_progress_bar_enabled': value}, reason);
}

Future<void> setSafetyAlertsChannel(String? channelId,
{String? reason}) async {
await _serverPart.updateServer(
id, {'safety_alerts_channel_id': channelId}, reason);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,26 @@ final class ServerMessagePart implements DataStorePart {
List<MessageEmbed>? embeds,
List<MessageComponent>? components,
}) async {
final response =
await _kernel.dataStore.client.patch('/channels/$channelId/messages/$id', body: {
final response = await _kernel.dataStore.client
.patch('/channels/$channelId/messages/$id', body: {
'content': content,
'embeds': await Helper.createOrNullAsync(
field: embeds,
fn: () async => embeds?.map(_kernel.marshaller.serializers.embed.deserialize).toList()),
fn: () async => embeds
?.map(_kernel.marshaller.serializers.embed.deserialize)
.toList()),
'components': components?.map((c) => c.toJson()).toList()
});

final server = await _kernel.dataStore.server.getServer(serverId);
final channel = await _kernel.dataStore.channel.getChannel<ServerChannel>(channelId);
final channel =
await _kernel.dataStore.channel.getChannel<ServerChannel>(channelId);

final Map<String, dynamic> serverMessage =
await _kernel.marshaller.serializers.serverMessage.normalize(response.body);
final message = await _kernel.marshaller.serializers.serverMessage.serialize(serverMessage);
final Map<String, dynamic> serverMessage = await _kernel
.marshaller.serializers.serverMessage
.normalize(response.body);
final message = await _kernel.marshaller.serializers.serverMessage
.serialize(serverMessage);

if (channel != null) {
channel.server = server;
Expand All @@ -54,26 +59,31 @@ final class ServerMessagePart implements DataStorePart {
String? content,
List<MessageEmbed>? embeds,
List<MessageComponent>? components}) async {
final response = await _kernel.dataStore.client.post('/channels/$channelId/messages', body: {
final response = await _kernel.dataStore.client
.post('/channels/$channelId/messages', body: {
'content': content,
'embeds': Helper.createOrNull(
field: embeds,
fn: () => embeds?.map(_kernel.marshaller.serializers.embed.deserialize))?.toList(),
field: embeds,
fn: () =>
embeds?.map(_kernel.marshaller.serializers.embed.deserialize))
?.toList(),
'components': components?.map((c) => c.toJson()).toList(),
'message_reference': {'message_id': id, 'channel_id': channelId}
});

final server = await _kernel.dataStore.server.getServer(serverId);
final channel = await _kernel.dataStore.channel.getChannel<ServerChannel>(channelId);
final channel =
await _kernel.dataStore.channel.getChannel<ServerChannel>(channelId);

final Map<String, dynamic> rawMessage =
await _kernel.marshaller.serializers.serverMessage.normalize({
...response.body,
'server_id': serverId,
});

final ServerMessage message =
await _kernel.marshaller.serializers.serverMessage.serialize(rawMessage);
final ServerMessage message = await _kernel
.marshaller.serializers.serverMessage
.serialize(rawMessage);

if (channel != null) {
channel.server = server;
Expand All @@ -83,19 +93,24 @@ final class ServerMessagePart implements DataStorePart {
return message;
}

Future<void> pin({required Snowflake id, required Snowflake channelId}) async {
Future<void> pin(
{required Snowflake id, required Snowflake channelId}) async {
await _kernel.dataStore.client.put('/channels/$channelId/pins/$id');
}

Future<void> unpin({required Snowflake id, required Snowflake channelId}) async {
Future<void> unpin(
{required Snowflake id, required Snowflake channelId}) async {
await _kernel.dataStore.client.delete('/channels/$channelId/pins/$id');
}

Future<void> crosspost({required Snowflake id, required Snowflake channelId}) async {
await _kernel.dataStore.client.post('/channels/$channelId/messages/$id/crosspost');
Future<void> crosspost(
{required Snowflake id, required Snowflake channelId}) async {
await _kernel.dataStore.client
.post('/channels/$channelId/messages/$id/crosspost');
}

Future<void> delete({required Snowflake id, required Snowflake channelId}) async {
Future<void> delete(
{required Snowflake id, required Snowflake channelId}) async {
await _kernel.dataStore.client.delete('/channels/$channelId/messages/$id');
}
}
14 changes: 14 additions & 0 deletions lib/src/infrastructure/internals/datastore/parts/server_part.dart
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,10 @@ import 'package:mineral/src/api/server/channels/server_channel.dart';
import 'package:mineral/src/api/server/role.dart';
import 'package:mineral/src/api/server/server.dart';
import 'package:mineral/src/infrastructure/internals/datastore/data_store_part.dart';
import 'package:mineral/src/infrastructure/internals/http/discord_header.dart';
import 'package:mineral/src/infrastructure/kernel/kernel.dart';
import 'package:mineral/src/infrastructure/services/http/http_client_status.dart';
import 'package:mineral/src/infrastructure/services/http/http_request_option.dart';

final class ServerPart implements DataStorePart {
final KernelContract _kernel;
Expand Down Expand Up @@ -35,6 +37,18 @@ final class ServerPart implements DataStorePart {
return _kernel.marshaller.serializers.server.serialize(payload);
}

Future<Server> updateServer(
Snowflake id, Map<String, dynamic> payload, String? reason) async {
final response = await _kernel.dataStore.client.patch('/guilds/$id',
body: payload,
option: HttpRequestOptionImpl(
headers: {DiscordHeader.auditLogReason(reason)}));

final rawServer =
await _kernel.marshaller.serializers.server.normalize(response.body);
return _kernel.marshaller.serializers.server.serialize(rawServer);
}

Future<List<T>> getChannels<T extends ServerChannel>(Snowflake id) async {
final response = await _kernel.dataStore.client.get('/guilds/$id/channels');
if (status.isError(response.statusCode)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import 'dart:async';
import 'dart:convert';

import 'package:mineral/src/api/common/color.dart';
import 'package:mineral/src/api/common/embed/message_embed.dart';
Expand Down Expand Up @@ -57,7 +56,9 @@ final class EmbedSerializer implements SerializerContract<MessageEmbed> {
fn: () => MessageEmbedProvider.fromJson(json['provider'])),
fields: Helper.createOrNull(
field: json['fields'],
fn: () => List.from(json['fields']).map((element) => MessageEmbedField.fromJson(element)).toList()),
fn: () => List.from(json['fields'])
.map((element) => MessageEmbedField.fromJson(element))
.toList()),
color: Helper.createOrNull(
field: json['color'], fn: () => Color.of(json['color'])),
);
Expand Down

0 comments on commit 2949c72

Please sign in to comment.