def testConversationIds(self): '''Test the increment of conversation ids''' db = supersimpledb.MurmeliDb() DbI.setDb(db) # If we start with an empty db, the id should start at 1 for i in range(100): self.assertEqual(DbI.getNewConversationId(), i+1, "id should be 1 more than i") # Add profiles for us and a messages sender DbI.updateProfile("F055", {"keyid":"ZYXW987", "status":"self", "name":"That's me"}) DbI.updateProfile("ABC312", {"keyid":"ZYXW987", "status":"trusted", "name":"Best friend"}) # Add an inbox message with a conversation id DbI.addToInbox({"messageBody":"Gorgonzola and Camembert", "timestamp":"early 2017", "fromId":"ABC312"}) # Get this message again, and check the conversation id, should be 101 msg0 = DbI.getInboxMessages()[0] self.assertEqual(msg0.get("conversationid", 0), 101, "Id should now be 101") # Add another message with the parent hash referring to the first one DbI.addToInbox({"messageBody":"Fried egg sandwich", "timestamp":"middle 2017", "fromId":"ABC312", "parentHash":'40e98bae7a811c23b59b89bd0f11b0a0'}) msg1 = DbI.getInboxMessages()[0] self.assertTrue("Fried egg" in msg1.get("messageBody", ""), "Fried egg should be first") self.assertEqual(msg1.get("conversationid", 0), 101, "Id should now also be 101") # Add another message with an unrecognised parent hash DbI.addToInbox({"messageBody":"Red wine and chocolate", "timestamp":"late 2017", "fromId":"ABC312", "parentHash":'ff3'}) msg2 = DbI.getInboxMessages()[0] self.assertTrue("Red wine" in msg2.get("messageBody", ""), "Red wine should be first") self.assertEqual(msg2.get("conversationid", 0), 102, "Id should take 102") # done DbI.releaseDb()
def testSearching(self): '''Test the searching of the inbox''' db = supersimpledb.MurmeliDb() DbI.setDb(db) # Add some inbox messages with different text DbI.addToInbox({"messageBody":"There were some children playing in the park", "timestamp":"", "fromId":"ABC312"}) DbI.addToInbox({"messageBody":"There was a child playing in the castle", "timestamp":"", "fromId":"ABC312"}) DbI.addToInbox({"messageBody":"Children were making far too much noise", "timestamp":"", "fromId":"ABC312"}) # Get this message again, and check the conversation id, should be 101 self.assertEqual(len(DbI.getInboxMessages()), 3, "Inbox has 3") search1 = DbI.searchInboxMessages("child") self.assertEqual(len(search1), 2, "Only 2 match 'child'") search2 = DbI.searchInboxMessages("children") self.assertEqual(len(search2), 1, "Only 1 matches 'children'") search3 = DbI.searchInboxMessages("") self.assertEqual(len(search3), 3, "All match empty search string") search4 = DbI.searchInboxMessages("hild") self.assertEqual(len(search4), 3, "All match 'hild'") search5 = DbI.searchInboxMessages("noisy") self.assertEqual(len(search5), 0, "None are noisy") search6 = DbI.searchInboxMessages("Child") self.assertEqual(len(search6), 1, "Only 1 matches 'Child'") # done DbI.releaseDb()
def getContactRequestDetails(torId): '''Use all the received contact requests for the given id, and summarize the name and public key''' # Set up empty name / publicKey nameList = set() keyList = set() directRequest = False # Loop through all contact requests and contact refers for the given torid for m in DbI.getInboxMessages(): if m["messageType"] == "contactrequest" and m["fromId"] == torId: nameList.add(m.get("fromName", None)) keyList.add(m.get("publicKey", None)) directRequest = True elif m["messageType"] == "contactrefer" and m["friendId"] == torId: nameList.add(m.get("friendName", None)) keyList.add(m.get("publicKey", None)) if len(keyList) != 1: return (None, None, directRequest) # no keys or more than one key! suppliedKey = keyList.pop() if suppliedKey is None or len(suppliedKey) < 80: return (None, None, directRequest ) # one key supplied but it's missing or too short suppliedName = nameList.pop() if len(nameList) == 1 else torId return (suppliedName, suppliedKey, directRequest)
def _getInboxMessage(torId, messageType): for m in DbI.getInboxMessages(): if m["fromId"] == torId and m["messageType"] == messageType: return m
def checkInbox(self): '''Look in the inbox for messages''' messagesFound = len(DbI.getInboxMessages()) self.somethingInInbox = (messagesFound > 0) self.parent.postmanKnock() # only once
def servePage(self, view, url, params): self.requirePageResources( ['button-compose.png', 'default.css', 'jquery-3.1.1.js']) DbI.exportAllAvatars(Config.getWebCacheDir()) messageList = None if url == "/send": print("send message of type '%(messageType)s' to id '%(sendTo)s'" % params) if params['messageType'] == "contactresponse": torId = params['sendTo'] if params.get("accept", "0") == "1": ContactMaker.handleAccept(torId) # Make sure this new contact has an empty avatar DbI.exportAllAvatars(Config.getWebCacheDir()) outmsg = message.ContactResponseMessage( message=params['messageBody']) else: ContactMaker.handleDeny(torId) outmsg = message.ContactDenyMessage() # Construct a ContactResponse message object for sending outmsg.recipients = [params['sendTo']] DbI.addToOutbox(outmsg) elif url.startswith("/delete/"): DbI.deleteFromInbox(params.get("msgId", "")) elif url in ["/search", "/search/"]: messageList = DbI.searchInboxMessages(params.get("searchTerm")) # Make dictionary to convert ids to names contactNames = { c['torid']: c['displayName'] for c in DbI.getProfiles() } unknownSender = I18nManager.getText("messages.sender.unknown") unknownRecpt = I18nManager.getText("messages.recpt.unknown") # Get contact requests, responses and mails from inbox conreqs = [] conresps = [] mailTree = MessageTree() if messageList is None: messageList = DbI.getInboxMessages() # TODO: Paging options? for m in messageList: if not m: continue m['msgId'] = str(m.get("_id", "")) if m['messageType'] == "contactrequest": conreqs.append(m) elif m['messageType'] == "contactrefer": senderId = m.get('fromId', None) m['senderName'] = contactNames.get(senderId, unknownSender) conreqs.append(m) elif m['messageType'] == "contactresponse": if not m.get('accepted', False): m['messageBody'] = I18nManager.getText( "messages.contactrequest.refused") m['fromName'] = DbI.getProfile(m['fromId'])["displayName"] elif not m.get('messageBody', False): m['messageBody'] = I18nManager.getText( "messages.contactrequest.accepted") conresps.append(m) else: senderId = m.get('fromId', None) if not senderId and m.get('signatureKeyId', None): senderId = DbI.findUserIdFromKeyId(m['signatureKeyId']) m['senderName'] = contactNames.get(senderId, unknownSender) m['sentTimeStr'] = self.makeLocalTimeString(m['timestamp']) # Split m['recipients'] by commas, and look up each id with contactNames recpts = m.get('recipients', '') if recpts: replyAll = recpts.split(",") m['recipients'] = ", ".join( [contactNames.get(i, unknownRecpt) for i in replyAll]) replyAll.append(senderId) m['replyAll'] = ",".join(replyAll) else: m['recipients'] = unknownRecpt m['replyAll'] = "" mailTree.addMsg(m) mails = mailTree.build() bodytext = self.messagestemplate.getHtml({ "contactrequests": conreqs, "contactresponses": conresps, "mails": mails, "nummessages": len(conreqs) + len(conresps) + len(mails), "webcachedir": Config.getWebCacheDir() }) contents = self.buildPage({ 'pageTitle': I18nManager.getText("messages.title"), 'pageBody': bodytext, 'pageFooter': "<p>Footer</p>" }) view.setHtml(contents)