Skip to content

Commit

Permalink
Merge pull request #8 from ethicnology/develop
Browse files Browse the repository at this point in the history
refactor: model and WebSocketChannel init/update
  • Loading branch information
ethicnology committed Feb 5, 2023
2 parents cb97bf2 + e48f532 commit 3db061e
Show file tree
Hide file tree
Showing 8 changed files with 79 additions and 50 deletions.
9 changes: 8 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,11 @@
- refactor: tweet remove pseudonym placeholder

## 1.1.1
- put my own relay as default

- put my own relay as default

## 1.1.2

- refactor: model and WebSocketChannel init/update


12 changes: 0 additions & 12 deletions lib/model/profile.dart
Original file line number Diff line number Diff line change
@@ -1,19 +1,7 @@
import 'package:dispute/main.dart';
import 'package:flutter/material.dart';
import 'package:nostr/nostr.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

class Profile with ChangeNotifier {
Keychain keys = Keychain.generate();
String relay = "wss://relay.dispute.systems";
late WebSocketChannel channel;

init() {
try {
channel = WebSocketChannel.connect(Uri.parse(relay));
logger.i('Connected to WebSocket at $relay');
} catch (e) {
logger.e('Error connecting to WebSocket at $relay: $e');
}
}
}
49 changes: 30 additions & 19 deletions lib/screen/event.dart
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import 'dart:convert';

import 'package:dispute/main.dart';
import 'dart:io';
import 'package:dispute/model/profile.dart';
import 'package:dispute/utils.dart';
import 'package:flutter/material.dart';
Expand All @@ -10,6 +9,25 @@ import 'package:web_socket_channel/web_socket_channel.dart';

import '../constants/constants.dart';

void sendEvent(Uri relay, Event event) {
WebSocketChannel channel = WebSocketChannel.connect(relay);
channel.sink.add(event.serialize());
sleep(const Duration(seconds: 1));
channel.stream.listen((response) {
if (response.isNotEmpty) {
var json = jsonDecode(response);
var msg = Message.deserialize(json);
if (msg.type == "OK") {
} else {
throw Exception(msg);
}
} else {
throw Exception("Empty response");
}
});
channel.sink.close();
}

class EventScreen extends StatefulWidget {
const EventScreen({super.key});

Expand All @@ -19,17 +37,10 @@ class EventScreen extends StatefulWidget {

class EventScreenState extends State<EventScreen> {
final TextEditingController _controller = TextEditingController();
String wsEvent = '';

@override
Widget build(BuildContext context) {
final profil = context.watch<Profile>();
if (wsEvent.isNotEmpty) {
var json = jsonDecode(wsEvent);
if (json[0] == "OK" && json[2] == true) {
logger.w(json);
}
}

return Scaffold(
appBar: AppBar(
Expand Down Expand Up @@ -70,16 +81,16 @@ class EventScreenState extends State<EventScreen> {
content: _controller.text,
privkey: profil.keys.private,
);
final channel = WebSocketChannel.connect(
Uri.parse(profil.relay),
);
channel.sink.add(event.serialize());
await Future.delayed(const Duration(seconds: 1));
channel.sink.close();
displaySnackBar(
context,
"The event is sent, go back to the wall",
);
try {
sendEvent(Uri.parse(profil.relay), event);
displaySnackBar(context, "Event is sent");
Navigator.of(context).pop(false);
} catch (e) {
displaySnackBar(
context,
"Event not sent: $e",
);
}
} else {
displaySnackBar(
context,
Expand Down
18 changes: 5 additions & 13 deletions lib/screen/home.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@ import 'package:dispute/screen/profil.dart';
import 'package:dispute/widget/the_wall.dart';
import 'package:dispute/utils.dart';
import 'package:flutter/material.dart';
import 'package:nostr/nostr.dart';
import 'package:provider/provider.dart';
import 'package:flutter/services.dart';
import 'package:provider/provider.dart';
import 'package:web_socket_channel/web_socket_channel.dart';

import '../constants/constants.dart';

Expand All @@ -17,16 +17,6 @@ class HomeScreen extends StatelessWidget {
Widget build(BuildContext context) {
String projectUrl = 'https://github.com/ethicnology/dispute';
final profil = context.watch<Profile>();
profil.init();
profil.channel.sink.add(
Request(generate64RandomHexChars(), [
Filter(
kinds: [1],
since: currentUnixTimestampSeconds() - 86400,
)
]).serialize(),
);

return Scaffold(
appBar: AppBar(
title: const Text('nostr dispute'),
Expand Down Expand Up @@ -69,7 +59,9 @@ class HomeScreen extends StatelessWidget {
),
),
),
TheWallWidget(channel: profil.channel),
TheWallWidget(
channel: WebSocketChannel.connect(Uri.parse(profil.relay)),
),
],
),
),
Expand Down
4 changes: 3 additions & 1 deletion lib/screen/profil.dart
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import 'package:dispute/main.dart';
import 'package:dispute/model/profile.dart';
import 'package:dispute/screen/home.dart';
import 'package:dispute/utils.dart';
import 'package:flutter/material.dart';
import 'package:nostr/nostr.dart';
Expand Down Expand Up @@ -161,7 +162,8 @@ class ProfilScreenState extends State<ProfilScreen> {
profil.keys = Keychain(privkeyInput.text);
pubkeyInput.text = profil.keys.public; // update UI
profil.relay = relayInput.text;
profil.init();
Navigator.of(context).push(MaterialPageRoute(
builder: (_) => const HomeScreen()));
}
},
child: const Text('Save'),
Expand Down
33 changes: 31 additions & 2 deletions lib/widget/the_wall.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import 'dart:collection';
import 'dart:convert';

import 'package:crypto/crypto.dart';
import 'package:dispute/main.dart';
import 'package:dispute/widget/tweet.dart';
import 'package:flutter/material.dart';
import 'package:nostr/nostr.dart';
Expand All @@ -13,7 +14,7 @@ bool isSpam(Event event) {
var toHash = utf8.encode(event.content);
String hash = sha256.convert(toHash).toString();
if (spams.contains(hash)) {
print("${event.id} is a filtered spam");
logger.i("${event.id} is a filtered spam");
return true;
}
spams.add(hash);
Expand All @@ -23,7 +24,10 @@ bool isSpam(Event event) {
class TheWallWidget extends StatefulWidget {
final WebSocketChannel channel;

const TheWallWidget({super.key, required this.channel});
const TheWallWidget({
super.key,
required this.channel,
});

@override
TheWallState createState() => TheWallState();
Expand All @@ -32,6 +36,31 @@ class TheWallWidget extends StatefulWidget {
class TheWallState extends State<TheWallWidget> {
final List<Event> events = [];

@override
@protected
@mustCallSuper
void initState() {
super.initState();
spams.clear();
logger.i('Connected to WebSocket at ${widget.channel.toString()}');
widget.channel.sink.add(
Request(generate64RandomHexChars(), [
Filter(
kinds: [1],
since: currentUnixTimestampSeconds() - 86400,
)
]).serialize(),
);
}

@override
@protected
@mustCallSuper
void dispose() {
super.dispose();
widget.channel.sink.close();
}

@override
Widget build(BuildContext context) {
return Center(
Expand Down
2 changes: 1 addition & 1 deletion pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ publish_to: 'none' # Remove this line if you wish to publish to pub.dev
# https://developer.apple.com/library/archive/documentation/General/Reference/InfoPlistKeyReference/Articles/CoreFoundationKeys.html
# In Windows, build-name is used as the major, minor, and patch parts
# of the product and file versions while build-number is used as the build suffix.
version: 1.1.1
version: 1.1.2

environment:
sdk: '>=2.18.6 <3.0.0'
Expand Down
2 changes: 1 addition & 1 deletion snap/snapcraft.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name: dispute
version: 1.1.1
version: 1.1.2
summary: A cross-platform client for Nostr
description: A cross-platform client for Nostr

Expand Down

0 comments on commit 3db061e

Please sign in to comment.