improved code related to public channel

was crashing on entering channel based on channel id
This commit is contained in:
Vishal 2022-11-27 17:49:51 +05:30
parent 83d05c2721
commit 494380b5e9
3 changed files with 53 additions and 102 deletions

View File

@ -590,15 +590,9 @@ class EventData {
String? decryptEncryptedChannelMessage(List<String> secretMessageIds, Map<String, Tree> tempChildEventsMap) {
if( id == "865c9352de11a3959c06fce5350c5a1b9fa0475d3234078a1bb45d152b370f0b") { // known issue
//print("\n\ngoing to decrypt b1ab66ac50f00f3c3bbc91e5b9e03fc8e79e3fdb9f6d5c9ae9777aa6ca3020a2");
//print(channel.participants);
return null;
}
//printWarning("Going to decrypt 14x event id: $id");
//print("In decryptEncryptedChannelMessage: for event of kind 142 with event id = $id");
int ivIndex = content.indexOf("?iv=");
if( ivIndex == -1) {
return null;
@ -607,7 +601,6 @@ class EventData {
var enc_str = content.substring(0, ivIndex);
String channelId = getChannelIdForMessage();
//print("In decryptEncryptedChannelMessage: got channel id $channelId");
List<String> keys = [];
keys = getEncryptedChannelKeys(secretMessageIds, tempChildEventsMap, channelId);
@ -616,14 +609,10 @@ class EventData {
return null;
}
//print("\nevent id: $id");
//print(keys);
String priKey = keys[0];
String pubKey = "02" + keys[1];
var decrypted = myPrivateDecrypt( priKey, pubKey, enc_str, iv); // use bob's privatekey and alic's publickey means bob can read message from alic
//print("decrypted = |$decrypted|");
return decrypted;
}
@ -632,12 +621,12 @@ class EventData {
if( kind != 42 && kind != 142 && kind!=141) {
return "";
}
//print("in getChannelIdForMessage tag length = ${tags.length}");
// get first e tag, which should be the channel of which this is part of
for( int i = 0; i < tags.length; i++) {
List tag = tags[i];
if( tag[0] == 'e') {
return tag[1];
for( int i = 0; i < eTags.length; i++) {
List tag = eTags[i];
if( tag.length >= 1) {
return tag[0];
}
}
return '';

View File

@ -180,23 +180,14 @@ class Relays {
e = Event.fromJson(d, relay);
if( gDebug > 0) log.info("In listener: Event size before adding: ${rEvents.length}");
if( rEvents.add(e) ) {
uniqueIdsRecieved.add(id);
String receivedSubscription = json[1];
if( gDebug > 3) e.eventData.printEventData(0, true, null, null, null);
if( gDebug > 2) print("");
if( gDebug > 1) log.info("In relay listener for relay url $relay: after adding element, rEvents Size = ${rEvents.length} numReceived = ${newRelay.numReceived} for subscription $receivedSubscription");
if( gDebug > 1) print("\n");
} else {
}
} on FormatException {
if( gDebug > 0) print( 'in Relay::sendRequset. FormatException in sendRequest for event');
return;
} catch(err) {
//dynamic json = jsonDecode(d);
if( gDebug > 0) print('---\nin Relay::sendRequset. exception = "$err" ; for relay $relay d = \n${d}');
return;
}
},

View File

@ -674,25 +674,6 @@ class Store {
int eKind = ce.eventData.kind;
switch(eKind) {
case 42:
{
if( gCheckEventId == ce.eventData.id) print("In handleChannelEvents: processing $gCheckEventId ");
String channelId = ce.eventData.getChannelIdForMessage();
if( channelId != "") { // sometimes people may forget to give e tags or give wrong tags like #e
Channel? channel = getChannel(rooms, channelId);
if( channel != null) {
if( gDebug > 0) print("chat room already exists = $channelId adding event to it" );
if( gCheckEventId == ce.eventData.id) print("Adding new message $eId to a chat room $channelId. ");
channel.addMessageToRoom(eId, tempChildEventsMap);
} else {
Channel newChannel = Channel(channelId, "", "", "", [eId], {}, 0);
rooms.add( newChannel);
}
}
}
break;
case 40:
{
String chatRoomId = eId;
@ -712,9 +693,12 @@ class Store {
if( json.containsKey('about')) {
roomAbout = json['about'];
}
List<String> emptyMessageList = [];
assert(chatRoomId.length == 64);
Channel room = Channel(chatRoomId, roomName, roomAbout, "", emptyMessageList, {}, ce.eventData.createdAt);
//print("created room with id $chatRoomId name ${roomName}");
rooms.add( room);
}
} on Exception catch(e) {
@ -722,6 +706,30 @@ class Store {
}
}
break;
case 42:
{
String channelId = ce.eventData.getChannelIdForMessage();
if( channelId.length != 64) {
break;
}
//print( "for event id ${ce.eventData.id} getting channel id of ${channelId} ");
assert(channelId.length == 64);
if( channelId != "") { // sometimes people may forget to give e tags or give wrong tags like #e
Channel? channel = getChannel(rooms, channelId);
if( channel != null) {
channel.addMessageToRoom(eId, tempChildEventsMap);
} else {
Channel newChannel = Channel(channelId, "", "", "", [eId], {}, 0);
// message added in above line
rooms.add( newChannel);
}
}
}
break;
default:
break;
} // end switch
@ -792,7 +800,7 @@ class Store {
}
} // end if 140
else {
printWarning("could not find event 140 from event $gSecretMessageKind ${eventSecretMessage.eventData.id}");
//printWarning("could not find event 140 from event $gSecretMessageKind ${eventSecretMessage.eventData.id}");
}
}
@ -808,21 +816,16 @@ class Store {
switch(eKind) {
case 142:
{
if( gCheckEventId == ce.eventData.id) print("In handleEncryptedChannelEvents: processing $gCheckEventId ");
String channelId = ce.eventData.getChannelIdForMessage();
if( channelId != "") { // sometimes people may forget to give e tags or give wrong tags like #e
Channel? channel = getChannel(encryptedChannels, channelId);
if( channelId.length == 64) { // sometimes people may forget to give e tags or give wrong tags like #e
Channel? channel = getChannel(encryptedChannels, channelId);
if( channel != null) {
if( gDebug > 0) print("encrypted chat room already exists = $channelId adding event to it" );
if( gCheckEventId == ce.eventData.id) print("Adding new message $eId to a chat room $channelId. ");
channel.addMessageToRoom(eId, tempChildEventsMap);
} else {
//Channel newChannel = Channel(channelId, "", "", "", [eId], {}, 0);
//encryptedChannels.add( newChannel);
}
}
} else {
// could not get channel id of message.
printWarning("---Could not get encryptd channel for message id ${ce.eventData.id} got channelId : ${channelId} its len ${channelId.length}");
}
}
break;
@ -831,26 +834,20 @@ class Store {
Set<String> participants = {};
ce.eventData.pTags.forEach((element) { participants.add(element);});
if( ce.eventData.id == "21779b82caf3628c83f382ad45a78ca0958e5edae7643d3fb222c03732c299d0") {
//printInColor("handling 141 : 21779b82caf3628c83f382ad45a78ca0958e5edae7643d3fb222c03732c299d0\n", redColor);
String chatRoomId = ce.eventData.getChannelIdForMessage();
if( chatRoomId.length != 64) {
break;
}
String chatRoomId = ce.eventData.getChannelIdForMessage();
//print("--------\nIn handleEncryptedChannelEvents: processing kind 141 id with ${ce.eventData.id} with participants = $participants");
//print("for original channel id: $chatRoomId");
try {
dynamic json = jsonDecode(ce.eventData.content);
Channel? channel = getChannel(encryptedChannels, chatRoomId);
if( channel != null) {
//print("got 141, and channel structure already exists");
// as channel entry already exists, then update its participants info, and name info
if( channel.chatRoomName == "" && json.containsKey('name')) {
channel.chatRoomName = json['name'];
//print("renamed channel to ${channel.chatRoomName}");
}
if( ce.eventData.id == "21779b82caf3628c83f382ad45a78ca0958e5edae7643d3fb222c03732c299d0") {
//printInColor("original: ${channel.participants}\n new participants: $participants \n chatRoomId:${chatRoomId}", redColor);
}
if( channel.lastUpdated < ce.eventData.createdAt) {
if( participants.contains(userPublicKey) && !channel.participants.contains(userPublicKey) ) {
@ -862,26 +859,13 @@ class Store {
for(int i = 0; i < channel.messageIds.length; i++) {
Event ?e = tempChildEventsMap[channel.messageIds[i]]?.event;
if( e != null) {
//print("num directRooms = ${directRooms.length}");
e.eventData.translateAndDecrypt14x(secretMessageIds, encryptedChannels, tempChildEventsMap);
}
}
}
} else {
//print("In handleEncryptedChannelEvents: got 141 when 140 is not yet found");
String roomName = "", roomAbout = "";
if( json.containsKey('name') ) {
roomName = json['name']??"";
}
if( json.containsKey('about')) {
roomAbout = json['about'];
}
List<String> emptyMessageList = [];
//Channel room = Channel(chatRoomId, roomName, roomAbout, "", emptyMessageList, participants, ce.eventData.createdAt);
//print("created encrypted room with id $chatRoomId and name $roomName");
//encryptedChannels.add( room);
// encrypted channel is only created on getting invite through kind 104, not here
}
} on Exception catch(e) {
if( gDebug > 0) print("In From Event. Event type 140. Json Decode error for event id ${ce.eventData.id}. error = $e");
@ -1328,29 +1312,11 @@ class Store {
break;
case 40:
case 42:
//print("calling handleChannelEvents for kind 40");
handleChannelEvents(channels, allChildEventsMap, newTree.event);
break;
case 42:
newTree.event.eventData.isNotification = true; // highlight it too in next printing
// add 42 chat message event id to its chat room
String channelId = newTree.event.eventData.getChannelIdForMessage();
if( channelId != "") {
Channel? channel = getChannel(channels, channelId);
if( channel != null) {
if( gDebug > 0) print("added event to chat room in insert event");
channel.addMessageToRoom(newTree.event.eventData.id, allChildEventsMap); // adds in order
break;
} else {
Channel newChannel = Channel(channelId, "", "", "", [], {}, 0);
newChannel.addMessageToRoom(newTree.event.eventData.id, allChildEventsMap);
channels.add(newChannel);
}
}
break;
case 141:
case 142:
//print("calling handleEncryptedChannelEvents for kind ${newTree.event.eventData.kind} from processIncoming");
@ -1603,7 +1569,7 @@ class Store {
String name = "";
if( channelstoPrint[j].chatRoomName == "") {
//print("channel has no name");
// channel has no name, print part of its id
name = channelstoPrint[j].channelId.substring(0, 6);
} else {
name = "${channelstoPrint[j].chatRoomName} ( ${channelstoPrint[j].channelId.substring(0, 6)})";
@ -1674,16 +1640,21 @@ class Store {
return "";
}
// first check channelsId's, in case user has sent a channelId itself
Set<String> fullChannelId = {};
for(int i = 0; i < listChannels.length; i++) {
if( listChannels[i].channelId.length != 64) {
//printWarning("For index i = $i channel id len is ${listChannels[i].channelId.length} and listChannels[i].channelId.length = ${listChannels[i].channelId.length}");
continue;
}
//print("listChannels[i].channelId = ${listChannels[i].channelId} channelId = ${channelId}");
if( listChannels[i].channelId.substring(0, channelId.length) == channelId ) {
fullChannelId.add(listChannels[i].channelId);
}
}
if(fullChannelId.length != 1) {
if(fullChannelId.length < 1) {
// lookup in channel room name
for(int i = 0; i < listChannels.length; i++) {
Channel room = listChannels[i];