public static FriendMessage addMessageWithType(byte[] id, FriendMessageType type, byte[] wallet_address, string message, bool local_sender = false, byte[] sender_address = null, long timestamp = 0, bool fire_local_notification = true) { Friend friend = getFriend(wallet_address); if (friend == null) { // No matching contact found in friendlist // Add the contact, then issue the message again? // TODO: need to fetch the stage 1 public key somehow here // Ignoring such messages for now //addFriend(wallet_address, "pubkey", "Unknown"); //addMessage(wallet_address, message); Logging.warn("Received message but contact isn't in our contact list."); return(null); } Node.shouldRefreshContacts = true; if (!friend.online) { using (MemoryStream mw = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(mw)) { writer.Write(wallet_address.Length); writer.Write(wallet_address); CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M' }, ProtocolMessageCode.getPresence, mw.ToArray(), null); } } } string sender_nick = ""; if (friend.bot && sender_address != null) { if (IxianHandler.getWalletStorage().isMyAddress(sender_address)) { local_sender = true; } if (!local_sender) { if (friend.contacts.ContainsKey(sender_address) && friend.contacts[sender_address].nick != "") { sender_nick = friend.contacts[sender_address].nick; } else { if (!friend.contacts.ContainsKey(sender_address) || friend.contacts[sender_address].publicKey == null) { StreamProcessor.requestPubKey(friend, sender_address); } StreamProcessor.requestNickname(friend, sender_address); } } } else { sender_nick = friend.nickname; } if (timestamp == 0) { timestamp = Clock.getTimestamp(); } FriendMessage friend_message = new FriendMessage(id, message, timestamp, local_sender, type, sender_address, sender_nick); if (friend.bot) { if (local_sender) { friend_message.read = true; } else { friend.lastReceivedMessageId = id; saveToStorage(); } } lock (friend.messages) { // TODO should be optimized if (id != null && friend.messages.Find(x => x.id != null && x.id.SequenceEqual(id)) != null) { Logging.warn("Message with id {0} was already in message list.", Crypto.hashToString(id)); return(null); } friend.messages.Add(friend_message); } // If a chat page is visible, insert the message directly if (friend.chat_page != null) { friend.chat_page.insertMessage(friend_message); } // Send a local push notification if Spixi is not in the foreground if (fire_local_notification && !local_sender) { if (App.isInForeground == false || friend.chat_page == null) { // don't fire notification for nickname and avatar if (!friend_message.id.SequenceEqual(new byte[] { 4 }) && !friend_message.id.SequenceEqual(new byte[] { 5 })) { DependencyService.Get <IPushService>().showLocalNotification("Spixi", "New Message", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress)); } } } ISystemAlert alert = DependencyService.Get <ISystemAlert>(); if (alert != null) { alert.flash(); } // Write to chat history Node.localStorage.writeMessages(wallet_address, friend.messages); return(friend_message); }
public static FriendMessage addMessageWithType(byte[] id, FriendMessageType type, byte[] wallet_address, int channel, string message, bool local_sender = false, byte[] sender_address = null, long timestamp = 0, bool fire_local_notification = true, int payable_data_len = 0) { Friend friend = getFriend(wallet_address); if (friend == null) { // No matching contact found in friendlist // Add the contact, then issue the message again? // TODO: need to fetch the stage 1 public key somehow here // Ignoring such messages for now //addFriend(wallet_address, "pubkey", "Unknown"); //addMessage(wallet_address, message); Logging.warn("Received message but contact isn't in our contact list."); return(null); } if (!friend.online) { using (MemoryStream mw = new MemoryStream()) { using (BinaryWriter writer = new BinaryWriter(mw)) { writer.WriteIxiVarInt(wallet_address.Length); writer.Write(wallet_address); CoreProtocolMessage.broadcastProtocolMessage(new char[] { 'M', 'H' }, ProtocolMessageCode.getPresence2, mw.ToArray(), null); } } } bool set_read = false; string sender_nick = ""; if (friend.bot && sender_address != null) { if (IxianHandler.getWalletStorage().isMyAddress(sender_address)) { if (!local_sender) { set_read = true; } local_sender = true; } if (!local_sender) { if (friend.users.hasUser(sender_address) && friend.users.getUser(sender_address).getNick() != "") { sender_nick = friend.users.getUser(sender_address).getNick(); } else { if (!friend.users.hasUser(sender_address) || friend.users.getUser(sender_address).publicKey == null) { StreamProcessor.requestPubKey(friend, sender_address); } StreamProcessor.requestNickname(friend, sender_address); } } } else { sender_nick = friend.nickname; } if (timestamp == 0) { timestamp = Clock.getTimestamp(); } FriendMessage friend_message = new FriendMessage(id, message, timestamp, local_sender, type, sender_address, sender_nick); friend_message.payableDataLen = payable_data_len; List <FriendMessage> messages = friend.getMessages(channel); if (messages == null) { Logging.warn("Message with id {0} was sent to invalid channel {1}.", Crypto.hashToString(id), channel); return(null); } lock (messages) { // TODO should be optimized if (id != null) { FriendMessage tmp_msg = messages.Find(x => x.id != null && x.id.SequenceEqual(id)); if (tmp_msg != null) { if (!tmp_msg.localSender) { Logging.warn("Message with id {0} was already in message list.", Crypto.hashToString(id)); } else { friend.setMessageRead(channel, id); } if (messages.Last() == tmp_msg) { friend.metaData.setLastMessage(tmp_msg, channel); friend.metaData.setLastReceivedMessageIds(tmp_msg.id, channel); friend.saveMetaData(); } return(null); } else { friend.metaData.setLastReceivedMessageIds(friend_message.id, channel); } } else if (!local_sender) { Logging.error("Message id sent by {0} is null!", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress)); return(null); } messages.Add(friend_message); } if (set_read) { friend_message.confirmed = true; friend_message.read = true; } friend.metaData.setLastMessage(friend_message, channel); friend.saveMetaData(); // If a chat page is visible, insert the message directly if (friend.chat_page != null) { friend.chat_page.insertMessage(friend_message, channel); } else if (!set_read) { friend.metaData.unreadMessageCount++; friend.saveMetaData(); } UIHelpers.setContactStatus(friend.walletAddress, friend.online, friend.getUnreadMessageCount(), message, timestamp); // Send a local push notification if Spixi is not in the foreground if (fire_local_notification && !local_sender) { if (App.isInForeground == false || friend.chat_page == null) { // don't fire notification for nickname and avatar if (!friend_message.id.SequenceEqual(new byte[] { 4 }) && !friend_message.id.SequenceEqual(new byte[] { 5 })) { if (friend.bot == false || (friend.metaData.botInfo != null && friend.metaData.botInfo.sendNotification)) { DependencyService.Get <IPushService>().showLocalNotification("Spixi", "New Message", Base58Check.Base58CheckEncoding.EncodePlain(friend.walletAddress)); } } } } ISystemAlert alert = DependencyService.Get <ISystemAlert>(); if (alert != null) { alert.flash(); } // Write to chat history Node.localStorage.requestWriteMessages(wallet_address, channel); return(friend_message); }