mirror of
https://github.com/ChronosX88/wind.git
synced 2024-12-05 02:22:17 +00:00
Implement posting messages in thread
This commit is contained in:
parent
18d4f2dc0b
commit
987c48d530
@ -1,18 +1,28 @@
|
|||||||
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:wind/thread_model.dart';
|
import 'package:wind/thread_model.dart';
|
||||||
|
|
||||||
class MessageItemView extends StatelessWidget {
|
class MessageItemView extends StatelessWidget {
|
||||||
const MessageItemView({Key? key, required this.item, required this.isOpPost})
|
const MessageItemView(
|
||||||
|
{Key? key,
|
||||||
|
required this.item,
|
||||||
|
required this.isOpPost,
|
||||||
|
required this.isLast})
|
||||||
: super(key: key);
|
: super(key: key);
|
||||||
|
|
||||||
final MessageItem item;
|
final MessageItem item;
|
||||||
final bool isOpPost;
|
final bool isOpPost;
|
||||||
|
final bool isLast;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
return Container(
|
return Container(
|
||||||
margin: this.isOpPost ? EdgeInsets.only(bottom: 10) : EdgeInsets.all(0),
|
margin: this.isOpPost
|
||||||
|
? EdgeInsets.only(bottom: 10, left: 10, right: 10, top: 16)
|
||||||
|
: isLast
|
||||||
|
? EdgeInsets.only(left: 10, right: 10, bottom: 16)
|
||||||
|
: EdgeInsets.only(left: 10, right: 10),
|
||||||
child: Card(
|
child: Card(
|
||||||
elevation: 5,
|
elevation: 5,
|
||||||
child: Column(
|
child: Column(
|
||||||
@ -81,6 +91,8 @@ class MessageItem {
|
|||||||
final String date;
|
final String date;
|
||||||
final String body;
|
final String body;
|
||||||
|
|
||||||
|
MimeMessage? originalMessage;
|
||||||
|
|
||||||
MessageItem(
|
MessageItem(
|
||||||
this.id, this.number, this.subject, this.author, this.date, this.body);
|
this.id, this.number, this.subject, this.author, this.date, this.body);
|
||||||
}
|
}
|
||||||
|
@ -6,8 +6,8 @@ import 'package:tuple/tuple.dart';
|
|||||||
|
|
||||||
class NNTPClient {
|
class NNTPClient {
|
||||||
late WebSocketChannel _channel;
|
late WebSocketChannel _channel;
|
||||||
final Queue<_NNTPCommand> commandQueue = new Queue();
|
final Queue<_NNTPCommand> _commandQueue = new Queue();
|
||||||
final List<String> tempBuffer = [];
|
final List<String> _tempBuffer = [];
|
||||||
|
|
||||||
String? currentGroup;
|
String? currentGroup;
|
||||||
|
|
||||||
@ -25,22 +25,22 @@ class NNTPClient {
|
|||||||
var respLines = resp.split("\r\n");
|
var respLines = resp.split("\r\n");
|
||||||
if (respLines.last == "") respLines.removeLast(); // trailing empty line
|
if (respLines.last == "") respLines.removeLast(); // trailing empty line
|
||||||
|
|
||||||
if ((respLines.length > 1 || tempBuffer.isNotEmpty) &&
|
if ((respLines.length > 1 || _tempBuffer.isNotEmpty) &&
|
||||||
respLines.last.codeUnits.last != ".".codeUnits.first) {
|
respLines.last.codeUnits.last != ".".codeUnits.first) {
|
||||||
// if it's multiline response and it doesn't contain dot in the end
|
// if it's multiline response and it doesn't contain dot in the end
|
||||||
// then looks like we need to wait for next message to concatenate with current msg
|
// then looks like we need to wait for next message to concatenate with current msg
|
||||||
tempBuffer.add(resp);
|
_tempBuffer.add(resp);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
if (tempBuffer.isNotEmpty) {
|
if (_tempBuffer.isNotEmpty) {
|
||||||
tempBuffer.add(resp);
|
_tempBuffer.add(resp);
|
||||||
resp = tempBuffer.join();
|
resp = _tempBuffer.join();
|
||||||
respLines = resp.split("\r\n");
|
respLines = resp.split("\r\n");
|
||||||
respLines.removeLast(); // trailing empty line
|
respLines.removeLast(); // trailing empty line
|
||||||
tempBuffer.clear();
|
_tempBuffer.clear();
|
||||||
}
|
}
|
||||||
var command = commandQueue.removeFirst();
|
var command = _commandQueue.removeFirst();
|
||||||
var respCode = int.parse(respLines[0].split(" ")[0]);
|
var respCode = int.parse(respLines[0].split(" ")[0]);
|
||||||
command.responseCompleter.complete(_CommandResponse(respCode, respLines));
|
command.responseCompleter.complete(_CommandResponse(respCode, respLines));
|
||||||
});
|
});
|
||||||
@ -49,7 +49,7 @@ class NNTPClient {
|
|||||||
Future<_CommandResponse> _sendCommand(
|
Future<_CommandResponse> _sendCommand(
|
||||||
String command, List<String> args) async {
|
String command, List<String> args) async {
|
||||||
var cmd = _NNTPCommand(_CommandRequest(command, args));
|
var cmd = _NNTPCommand(_CommandRequest(command, args));
|
||||||
commandQueue.add(cmd);
|
_commandQueue.add(cmd);
|
||||||
if (args.length > 0) {
|
if (args.length > 0) {
|
||||||
_channel.sink.add("$command ${args.join(" ")}\r\n");
|
_channel.sink.add("$command ${args.join(" ")}\r\n");
|
||||||
} else {
|
} else {
|
||||||
@ -155,6 +155,18 @@ class NNTPClient {
|
|||||||
|
|
||||||
return threads;
|
return threads;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<int> postArticle(MimeMessage message) async {
|
||||||
|
var resp = await _sendCommand("POST", []);
|
||||||
|
if (resp.responseCode != 340) return resp.responseCode;
|
||||||
|
var rawMessage = message.renderMessage() + "\r\n.\r\n";
|
||||||
|
|
||||||
|
_channel.sink.add(rawMessage);
|
||||||
|
var cmd = _NNTPCommand(_CommandRequest("POST", []));
|
||||||
|
_commandQueue.add(cmd);
|
||||||
|
resp = await cmd.response;
|
||||||
|
return resp.responseCode;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class _NNTPCommand {
|
class _NNTPCommand {
|
||||||
|
@ -1,3 +1,4 @@
|
|||||||
|
import 'package:enough_mail/enough_mail.dart';
|
||||||
import 'package:flutter/cupertino.dart';
|
import 'package:flutter/cupertino.dart';
|
||||||
import 'package:wind/message_item_view.dart';
|
import 'package:wind/message_item_view.dart';
|
||||||
import 'package:wind/nntp_client.dart';
|
import 'package:wind/nntp_client.dart';
|
||||||
@ -9,13 +10,15 @@ class ThreadModel extends ChangeNotifier {
|
|||||||
|
|
||||||
Future<MessageItem> getPost(int number) async {
|
Future<MessageItem> getPost(int number) async {
|
||||||
var msg = await client!.getPost(number);
|
var msg = await client!.getPost(number);
|
||||||
return MessageItem(
|
var mi = MessageItem(
|
||||||
msg.getHeaderValue("Message-Id")!,
|
msg.getHeaderValue("Message-Id")!,
|
||||||
number,
|
number,
|
||||||
msg.getHeaderValue("Subject")!,
|
msg.getHeaderValue("Subject")!,
|
||||||
msg.getHeaderValue("From")!,
|
msg.getHeaderValue("From")!,
|
||||||
msg.getHeaderValue("Date")!,
|
msg.getHeaderValue("Date")!,
|
||||||
msg.decodeTextPlainPart()!);
|
msg.decodeTextPlainPart()!);
|
||||||
|
mi.originalMessage = msg;
|
||||||
|
return mi;
|
||||||
}
|
}
|
||||||
|
|
||||||
Future<List<MessageItem>> getThread(int threadNumber) async {
|
Future<List<MessageItem>> getThread(int threadNumber) async {
|
||||||
@ -39,4 +42,15 @@ class ThreadModel extends ChangeNotifier {
|
|||||||
|
|
||||||
return items;
|
return items;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Future<int> postMessage(MimeMessage opPost, String text) async {
|
||||||
|
var msg = MessageBuilder.buildSimpleTextMessage(
|
||||||
|
MailAddress.empty(), [], text,
|
||||||
|
subject: "Re: " + opPost.decodeSubject()!);
|
||||||
|
msg.setHeader("From", "anonymous");
|
||||||
|
msg.addHeader("In-Reply-To", opPost.getHeaderValue("Message-Id"));
|
||||||
|
msg.addHeader("References", opPost.getHeaderValue("Message-Id"));
|
||||||
|
msg.addHeader("Newsgroups", client!.currentGroup!);
|
||||||
|
return await client!.postArticle(msg);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
|
import 'package:enough_mail/mime_message.dart';
|
||||||
import 'package:flutter/material.dart';
|
import 'package:flutter/material.dart';
|
||||||
import 'package:provider/provider.dart';
|
import 'package:provider/provider.dart';
|
||||||
import 'package:wind/message_item_view.dart';
|
import 'package:wind/message_item_view.dart';
|
||||||
import 'package:wind/nntp_client.dart';
|
|
||||||
import 'package:wind/thread_list_view.dart';
|
import 'package:wind/thread_list_view.dart';
|
||||||
import 'package:wind/thread_model.dart';
|
import 'package:wind/thread_model.dart';
|
||||||
|
|
||||||
@ -22,21 +22,17 @@ class ThreadScreen extends StatefulWidget {
|
|||||||
|
|
||||||
class ThreadScreenState extends State<ThreadScreen> {
|
class ThreadScreenState extends State<ThreadScreen> {
|
||||||
ThreadScreenState(this.threadNumber);
|
ThreadScreenState(this.threadNumber);
|
||||||
|
|
||||||
late NNTPClient client;
|
|
||||||
late int threadNumber;
|
late int threadNumber;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
Widget build(BuildContext context) {
|
Widget build(BuildContext context) {
|
||||||
client = context.read<NNTPClient>();
|
|
||||||
|
|
||||||
return Scaffold(
|
return Scaffold(
|
||||||
appBar: AppBar(
|
appBar: AppBar(
|
||||||
title: Text("Thread #${this.threadNumber}"),
|
title: Text("Thread #${this.threadNumber}"),
|
||||||
),
|
),
|
||||||
body: Center(
|
body: Center(
|
||||||
child: Container(
|
child: Container(
|
||||||
width: 640,
|
width: 650,
|
||||||
child: FutureBuilder<List<MessageItem>>(
|
child: FutureBuilder<List<MessageItem>>(
|
||||||
future: _fetch(context),
|
future: _fetch(context),
|
||||||
builder: (context, snapshot) {
|
builder: (context, snapshot) {
|
||||||
@ -71,25 +67,34 @@ class ThreadScreenState extends State<ThreadScreen> {
|
|||||||
itemCount: data.length,
|
itemCount: data.length,
|
||||||
itemBuilder: (context, index) {
|
itemBuilder: (context, index) {
|
||||||
if (index == 1) {
|
if (index == 1) {
|
||||||
return SendMessageForm();
|
return SendMessageForm(opPost: data[0].originalMessage!);
|
||||||
}
|
}
|
||||||
return MessageItemView(item: data[index], isOpPost: index == 0);
|
return MessageItemView(
|
||||||
|
item: data[index],
|
||||||
|
isOpPost: index == 0,
|
||||||
|
isLast: index == data.length - 1);
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
class SendMessageForm extends StatefulWidget {
|
class SendMessageForm extends StatefulWidget {
|
||||||
const SendMessageForm({Key? key}) : super(key: key);
|
const SendMessageForm({Key? key, required this.opPost}) : super(key: key);
|
||||||
|
|
||||||
|
final MimeMessage opPost;
|
||||||
|
|
||||||
@override
|
@override
|
||||||
SendMessageFormState createState() {
|
SendMessageFormState createState() {
|
||||||
return SendMessageFormState();
|
return SendMessageFormState(opPost);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Create a corresponding State class.
|
// Create a corresponding State class.
|
||||||
// This class holds data related to the form.
|
// This class holds data related to the form.
|
||||||
class SendMessageFormState extends State<SendMessageForm> {
|
class SendMessageFormState extends State<SendMessageForm> {
|
||||||
|
SendMessageFormState(this.opPost);
|
||||||
|
|
||||||
|
final MimeMessage opPost;
|
||||||
|
|
||||||
// Create a global key that uniquely identifies the Form widget
|
// Create a global key that uniquely identifies the Form widget
|
||||||
// and allows validation of the form.
|
// and allows validation of the form.
|
||||||
//
|
//
|
||||||
@ -102,50 +107,64 @@ class SendMessageFormState extends State<SendMessageForm> {
|
|||||||
// Build a Form widget using the _formKey created above.
|
// Build a Form widget using the _formKey created above.
|
||||||
return Form(
|
return Form(
|
||||||
key: _formKey,
|
key: _formKey,
|
||||||
child: Column(
|
child: Container(
|
||||||
crossAxisAlignment: CrossAxisAlignment.start,
|
margin: EdgeInsets.only(left: 16, right: 16),
|
||||||
children: [
|
child: Column(
|
||||||
Center(
|
crossAxisAlignment: CrossAxisAlignment.start,
|
||||||
child: Column(children: [
|
children: [
|
||||||
Consumer<ThreadModel>(
|
Center(
|
||||||
builder: ((context, value, child) => TextFormField(
|
child: Column(children: [
|
||||||
controller: value.commentTextController,
|
Consumer<ThreadModel>(
|
||||||
minLines: 5,
|
builder: ((context, value, child) => TextFormField(
|
||||||
keyboardType: TextInputType.multiline,
|
controller: value.commentTextController,
|
||||||
maxLines: null,
|
minLines: 5,
|
||||||
decoration: InputDecoration(
|
keyboardType: TextInputType.multiline,
|
||||||
border: OutlineInputBorder(), labelText: "Comment"),
|
maxLines: null,
|
||||||
// The validator receives the text that the user has entered.
|
decoration: InputDecoration(
|
||||||
validator: (value) {
|
border: OutlineInputBorder(),
|
||||||
if (value == null || value.isEmpty) {
|
labelText: "Комментарий"),
|
||||||
return 'Please enter some text';
|
// The validator receives the text that the user has entered.
|
||||||
}
|
validator: (value) {
|
||||||
return null;
|
if (value == null || value.isEmpty) {
|
||||||
},
|
return 'Пожалуйста, введите текст';
|
||||||
))),
|
|
||||||
Padding(
|
|
||||||
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
|
||||||
child: Row(
|
|
||||||
mainAxisAlignment: MainAxisAlignment.start,
|
|
||||||
children: [
|
|
||||||
ElevatedButton(
|
|
||||||
onPressed: () {
|
|
||||||
// Validate returns true if the form is valid, or false otherwise.
|
|
||||||
if (_formKey.currentState!.validate()) {
|
|
||||||
// If the form is valid, display a snackbar. In the real world,
|
|
||||||
// you'd often call a server or save the information in a database.
|
|
||||||
ScaffoldMessenger.of(context).showSnackBar(
|
|
||||||
const SnackBar(
|
|
||||||
content: Text('Processing Data')),
|
|
||||||
);
|
|
||||||
}
|
}
|
||||||
|
return null;
|
||||||
},
|
},
|
||||||
child: const Text('Send'),
|
))),
|
||||||
)
|
Consumer<ThreadModel>(
|
||||||
]))
|
builder: (((context, value, child) => Padding(
|
||||||
]),
|
padding: const EdgeInsets.symmetric(vertical: 16.0),
|
||||||
)
|
child: Row(
|
||||||
],
|
mainAxisAlignment: MainAxisAlignment.start,
|
||||||
|
children: [
|
||||||
|
ElevatedButton(
|
||||||
|
onPressed: () {
|
||||||
|
// Validate returns true if the form is valid, or false otherwise.
|
||||||
|
if (_formKey.currentState!.validate()) {
|
||||||
|
Provider.of<ThreadModel>(context,
|
||||||
|
listen: false)
|
||||||
|
.postMessage(opPost,
|
||||||
|
value.commentTextController.text)
|
||||||
|
.then((value) {
|
||||||
|
if (value == 240) {
|
||||||
|
ScaffoldMessenger.of(context)
|
||||||
|
.showSnackBar(
|
||||||
|
const SnackBar(
|
||||||
|
content: Text('Пост отправлен!')),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
});
|
||||||
|
value.commentTextController.text = "";
|
||||||
|
}
|
||||||
|
},
|
||||||
|
child: const Text('Отправить'),
|
||||||
|
)
|
||||||
|
])))),
|
||||||
|
)
|
||||||
|
]),
|
||||||
|
)
|
||||||
|
],
|
||||||
|
),
|
||||||
),
|
),
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user