implement channel messages

This commit is contained in:
liamcottle
2025-02-14 00:22:27 +13:00
parent b1cf027f84
commit 4666f89a25
12 changed files with 304 additions and 19 deletions

View File

@@ -198,6 +198,23 @@ class Connection {
}
static async sendChannelMessage(channelIdx, text) {
// send message
await GlobalState.connection.sendChannelTextMessage(channelIdx, text);
// save to database
await Database.ChannelMessage.insert({
channel_idx: channelIdx,
from: GlobalState.selfInfo.publicKey,
path_len: null,
txt_type: Constants.TxtTypes.Plain,
sender_timestamp: Date.now(),
text: text,
});
}
static async syncMessages() {
while(true){
@@ -208,14 +225,18 @@ class Connection {
}
// handle received message
await this.onMessageReceived(message);
if(message.contactMessage){
await this.onContactMessageReceived(message.contactMessage);
} else if(message.channelMessage) {
await this.onChannelMessageReceived(message.channelMessage);
}
}
}
static async onMessageReceived(message) {
static async onContactMessageReceived(message) {
console.log("onMessageReceived", message);
console.log("onContactMessageReceived", message);
// find first contact that matches this public key prefix
// todo, maybe use the most recently updated contact in case of collision? ideally we should be given the full hash by firmware anyway...
@@ -242,7 +263,6 @@ class Connection {
txt_type: message.txtType,
sender_timestamp: message.senderTimestamp,
text: message.text,
timestamp: Date.now(),
expected_ack_crc: null,
error: null,
});
@@ -252,6 +272,22 @@ class Connection {
}
static async onChannelMessageReceived(message) {
console.log("onChannelMessageReceived", message);
// save message to database
await Database.ChannelMessage.insert({
channel_idx: message.channelIdx,
from: null,
path_len: message.pathLen,
txt_type: message.txtType,
sender_timestamp: message.senderTimestamp,
text: message.text,
});
}
}
export default Connection;