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