Skip to content

Commit

Permalink
refactor: external PR #39
Browse files Browse the repository at this point in the history
  • Loading branch information
ethicnology committed Nov 21, 2023
1 parent 323bd6c commit e8fee85
Showing 1 changed file with 10 additions and 12 deletions.
22 changes: 10 additions & 12 deletions lib/src/message.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,20 @@ import 'package:nostr/nostr.dart';

// Used to deserialize any kind of message that a nostr client or relay can transmit.
class Message {
late String type;
late MessageType concreteType;
late dynamic message;
late MessageType messageType;

String get type => messageType.name;

// nostr message deserializer
Message.deserialize(String payload) {
dynamic data = jsonDecode(payload);
if (MessageType.values.map<String>((e) => e.rawType).contains(data[0]) ==
false) {
if (!MessageType.values.map((e) => e.name).contains(data[0])) {
throw 'Unsupported payload (or NIP)';
}

type = data[0];
concreteType = MessageType.byRawType(data[0]);
switch (concreteType) {
messageType = MessageType.fromName(data[0]);
switch (messageType) {
case MessageType.event:
message = Event.deserialize(data);
// ignore: deprecated_member_use_from_same_package
Expand Down Expand Up @@ -49,9 +48,8 @@ enum MessageType {
ok("OK"),
auth("AUTH");

const MessageType(this.rawType);
final String rawType;
static MessageType byRawType(String name) {
return MessageType.values.byName(name.toLowerCase());
}
final String name;
const MessageType(this.name);

static fromName(String name) => MessageType.values.byName(name.toLowerCase());
}

0 comments on commit e8fee85

Please sign in to comment.