xmpp: ALLOW SENDING MESSAGES
This commit is contained in:
parent
ca11e31876
commit
2ee72c0345
@ -9,6 +9,9 @@ class DBMessage {
|
||||
@Index(caseSensitive: false)
|
||||
late String from;
|
||||
|
||||
@Index(caseSensitive: false)
|
||||
late String conversationJid;
|
||||
|
||||
late int timestamp;
|
||||
|
||||
late String body;
|
||||
|
@ -2,10 +2,11 @@ class Message {
|
||||
final String body;
|
||||
final int timestamp; // NOTE: Milliseconds since Epoch
|
||||
final String from;
|
||||
final String conversationJid;
|
||||
final bool sent;
|
||||
final int id; // Database ID
|
||||
|
||||
const Message({ required this.from, required this.body, required this.timestamp, required this.sent, required this.id });
|
||||
const Message({ required this.from, required this.body, required this.timestamp, required this.sent, required this.id, required this.conversationJid });
|
||||
|
||||
Message copyWith({ String? from, String? body, int? timestamp }) {
|
||||
return Message(
|
||||
@ -13,6 +14,7 @@ class Message {
|
||||
body: body ?? this.body,
|
||||
timestamp: timestamp ?? this.timestamp,
|
||||
sent: this.sent,
|
||||
conversationJid: this.conversationJid,
|
||||
id: this.id
|
||||
);
|
||||
}
|
||||
|
@ -16,11 +16,9 @@ class SetShowScrollToEndButtonAction {
|
||||
class SendMessageAction {
|
||||
final String body;
|
||||
final int timestamp;
|
||||
final String from;
|
||||
final String jid;
|
||||
final int cid;
|
||||
|
||||
SendMessageAction({ required this.from, required this.body, required this.timestamp, required this.jid, required this.cid });
|
||||
SendMessageAction({ required this.body, required this.timestamp, required this.jid });
|
||||
}
|
||||
|
||||
class ReceiveMessageAction {
|
||||
|
@ -6,10 +6,10 @@ import "package:moxxyv2/redux/conversation/actions.dart";
|
||||
|
||||
HashMap<String, List<Message>> messageReducer(HashMap<String, List<Message>> state, dynamic action) {
|
||||
if (action is AddMessageAction) {
|
||||
if (!state.containsKey(action.message.from)) {
|
||||
state[action.message.from] = List.from([ action.message ]);
|
||||
if (!state.containsKey(action.message.conversationJid)) {
|
||||
state[action.message.conversationJid] = List.from([ action.message ]);
|
||||
} else {
|
||||
state[action.message.from] = state[action.message.from]!..add(action.message);
|
||||
state[action.message.conversationJid] = state[action.message.conversationJid]!..add(action.message);
|
||||
}
|
||||
|
||||
return state;
|
||||
|
@ -5,6 +5,7 @@ import "package:moxxyv2/repositories/conversation.dart";
|
||||
import "package:moxxyv2/models/conversation.dart";
|
||||
import "package:moxxyv2/models/message.dart";
|
||||
import "package:moxxyv2/redux/messages/actions.dart";
|
||||
import "package:moxxyv2/xmpp/connection.dart";
|
||||
import "package:moxxyv2/helpers.dart";
|
||||
|
||||
import "package:redux/redux.dart";
|
||||
@ -12,9 +13,8 @@ import "package:flutter_redux_navigation/flutter_redux_navigation.dart";
|
||||
import "package:get_it/get_it.dart";
|
||||
|
||||
void messageMiddleware(Store<MoxxyState> store, action, NextDispatcher next) async {
|
||||
final repo = GetIt.I.get<DatabaseRepository>();
|
||||
if (action is ReceiveMessageAction) {
|
||||
// TODO: Check if the conversation already exists
|
||||
final repo = GetIt.I.get<DatabaseRepository>();
|
||||
final now = DateTime.now().millisecondsSinceEpoch;
|
||||
final bareJidString = action.from.toBare().toString();
|
||||
|
||||
@ -22,6 +22,7 @@ void messageMiddleware(Store<MoxxyState> store, action, NextDispatcher next) asy
|
||||
action.body,
|
||||
now,
|
||||
bareJidString,
|
||||
bareJidString,
|
||||
false
|
||||
);
|
||||
|
||||
@ -30,7 +31,7 @@ void messageMiddleware(Store<MoxxyState> store, action, NextDispatcher next) asy
|
||||
final conversation = await repo.addConversationFromData(
|
||||
action.from.local,
|
||||
action.body,
|
||||
"",
|
||||
"", // TODO
|
||||
bareJidString,
|
||||
1,
|
||||
now,
|
||||
@ -56,6 +57,17 @@ void messageMiddleware(Store<MoxxyState> store, action, NextDispatcher next) asy
|
||||
));
|
||||
}
|
||||
|
||||
store.dispatch(AddMessageAction(message: message));
|
||||
} else if (action is SendMessageAction) {
|
||||
final message = await repo.addMessageFromData(
|
||||
action.body,
|
||||
action.timestamp,
|
||||
"", // TODO
|
||||
action.jid,
|
||||
true
|
||||
);
|
||||
GetIt.I.get<XmppConnection>().sendMessage(action.body, action.jid);
|
||||
|
||||
store.dispatch(AddMessageAction(message: message));
|
||||
} else if (action is LoadMessagesAction) {
|
||||
GetIt.I.get<DatabaseRepository>().loadMessagesForJid(action.jid);
|
||||
|
@ -47,11 +47,12 @@ class DatabaseRepository {
|
||||
}
|
||||
|
||||
Future<void> loadMessagesForJid(String jid) async {
|
||||
final messages = await this.isar.dBMessages.where().fromEqualTo(jid).findAll();
|
||||
final messages = await this.isar.dBMessages.where().conversationJidEqualTo(jid).findAll();
|
||||
this.loadedConversations.add(jid);
|
||||
|
||||
messages.forEach((m) => this.store.dispatch(AddMessageAction(message: Message(
|
||||
from: m.from,
|
||||
conversationJid: m.conversationJid,
|
||||
body: m.body,
|
||||
timestamp: m.timestamp,
|
||||
sent: m.sent,
|
||||
@ -116,10 +117,11 @@ class DatabaseRepository {
|
||||
);
|
||||
}
|
||||
|
||||
Future<Message> addMessageFromData(String body, int timestamp, String from, bool sent) async {
|
||||
Future<Message> addMessageFromData(String body, int timestamp, String from, String conversationJid, bool sent) async {
|
||||
print("addMessageFromData");
|
||||
final m = DBMessage()
|
||||
..from = from
|
||||
..conversationJid = conversationJid
|
||||
..timestamp = timestamp
|
||||
..body = body
|
||||
..sent = sent;
|
||||
@ -132,6 +134,7 @@ class DatabaseRepository {
|
||||
return Message(
|
||||
body: body,
|
||||
from: from,
|
||||
conversationJid: conversationJid,
|
||||
timestamp: timestamp,
|
||||
sent: sent,
|
||||
id: m.id!
|
||||
|
@ -133,11 +133,9 @@ class ConversationPage extends StatelessWidget {
|
||||
sendMessage: (body) => store.dispatch(
|
||||
// TODO
|
||||
SendMessageAction(
|
||||
from: "UwU",
|
||||
timestamp: DateTime.now().millisecondsSinceEpoch,
|
||||
body: body,
|
||||
jid: jid,
|
||||
cid: conversation.id
|
||||
)
|
||||
)
|
||||
);
|
||||
|
@ -129,6 +129,16 @@ class XmppConnection {
|
||||
void sendRawXML(XMLNode node) {
|
||||
this._socket.write(node.toXml());
|
||||
}
|
||||
|
||||
void sendMessage(String body, String to) async {
|
||||
await this.sendStanza(Stanza.message(
|
||||
to: to,
|
||||
type: "normal",
|
||||
children: [
|
||||
XMLNode(tag: "body", text: body)
|
||||
]
|
||||
));
|
||||
}
|
||||
|
||||
Future<XMLNode> sendStanza(Stanza stanza, { bool addFrom = true, bool addId = true }) {
|
||||
// Add extra data in case it was not set
|
||||
|
Loading…
Reference in New Issue
Block a user