def testGetProfile(self): # Delete whole profiles table DbClient._getProfileTable().remove({}) self.assertEqual(DbClient._getProfileTable().count(), 0, "Profiles table should be empty") # Add own profile myTorId = "ABC123DEF456GH78" myprofile = {"name" : "Constantin Taylor", "keyid" : "someKeyId", "displayName" : "Me", "status" : "self", "ownprofile" : True} DbClient.updateContact(myTorId, myprofile) self.assertEqual(DbClient._getProfileTable().count(), 1, "Profiles table should have my profile in it") profileFromDb = DbClient.getProfile(None) self.assertIsNotNone(profileFromDb, "Couldn't retrieve own profile") profileFromDb = DbClient.getProfile(myTorId) self.assertIsNotNone(profileFromDb, "Couldn't retrieve profile using own id") # Initiate contact with a new person otherTorId = "PQR123STU456VWX78" otherName = "Olivia Barnacles" DbClient.updateContact(otherTorId, {"status" : "untrusted", "keyid" : "donotknow", "name" : otherName}) self.assertEqual(DbClient._getProfileTable().count(), 2, "Profiles table should have 2 profiles") self.assertEqual(DbClient.getMessageableContacts().count(), 1, "Profiles table should have 1 messageable") self.assertEqual(DbClient.getTrustedContacts().count(), 0, "Profiles table should have 0 trusted") profileFromDb = DbClient.getProfile(otherTorId) self.assertIsNotNone(profileFromDb, "Couldn't retrieve profile using other id") self.assertEqual(profileFromDb.get("name", None), otherName, "Profile name doesn't match what was stored") self.assertEqual(profileFromDb.get("status", None), "untrusted", "Profile status doesn't match what was stored") # Update existing record, change status DbClient.updateContact(otherTorId, {"status" : "trusted"}) self.assertEqual(DbClient._getProfileTable().count(), 2, "Profiles table should still have 2 profiles") profileFromDb = DbClient.getProfile(otherTorId) self.assertIsNotNone(profileFromDb, "Couldn't retrieve profile using other id") self.assertEqual(profileFromDb.get("status", None), "trusted", "Profile status should have been updated") self.assertEqual(DbClient.getMessageableContacts().count(), 1, "Profiles table should have 1 messageable") self.assertEqual(DbClient.getTrustedContacts().count(), 1, "Profiles table should have 1 trusted") # Delete other contact DbClient.updateContact(otherTorId, {"status" : "deleted"}) self.assertEqual(DbClient.getMessageableContacts().count(), 0, "Profiles table should have 0 messageable") self.assertEqual(DbClient.getTrustedContacts().count(), 0, "Profiles table should have 0 trusted") self.assertFalse(DbClient.hasFriends(), "Shouldn't have any friends any more")
def checkAllContactsKeys(): for c in DbClient.getMessageableContacts(): torId = c.get("torid", None) if c else None if torId: keyId = c.get("keyid", None) if not keyId: print("No keyid found for torid", torId) elif not CryptoClient.getPublicKey(keyId): print("CryptoClient hasn't got a public key for torid", torId) if not keyId or not CryptoClient.getPublicKey(keyId): # We haven't got their key in our keyring! DbClient.updateContact(torId, {"status":"requested"})
def getSharedAndPossibleContacts(torid): '''Check which contacts we share with the given torid and which ones we could recommend to each other''' nameMap = {} ourContactIds = set() trustedContactIds = set() theirContactIds = set() # Get our id so we can exclude it from the sets myTorId = DbClient.getOwnTorId() if torid == myTorId: return (None, None, None, None) # Find the contacts of the specified person selectedProfile = DbClient.getProfile(torid, False) selectedContacts = selectedProfile.get('contactlist', None) if selectedProfile else None if selectedContacts: for s in selectedContacts.split(","): if s and len(s) >= 16: foundid = s[0:16] if foundid != myTorId: foundName = s[16:] theirContactIds.add(foundid) nameMap[foundid] = foundName foundTheirContacts = len(theirContactIds) > 0 # Now get information about our contacts for c in DbClient.getMessageableContacts(): foundid = c['torid'] ourContactIds.add(foundid) if c['status'] == 'trusted' and foundid != torid: trustedContactIds.add(foundid) nameMap[foundid] = c.get('displayName', c.get('name', None)) # Should we check the contact information too? if not foundTheirContacts: foundContacts = c.get('contactlist', None) if foundContacts: for s in foundContacts.split(","): if s[0:16] == torid: theirContactIds.add(foundid) # Now we have three sets of torids: our contacts, our trusted contacts, and their contacts. sharedContactIds = ourContactIds.intersection(theirContactIds) # might be empty suggestionsForThem = trustedContactIds.difference(theirContactIds) possibleForMe = theirContactIds.difference(ourContactIds) # Some or all of these sets may be empty, but we still return the map so we can look up names return (sharedContactIds, suggestionsForThem, possibleForMe, nameMap)
def servePage(self, view, url, params): print("Compose: %s, params %s" % (url, ",".join(params))) if url == "/start": self.requirePageResources(['default.css']) parentHash = params.get("reply", None) recpts = params.get("sendto", None) # Build list of contacts to whom we can send userboxes = [] for p in DbClient.getMessageableContacts(): box = Bean() box.dispName = p['displayName'] box.torid = p['torid'] userboxes.append(box) pageParams = {"contactlist":userboxes, "parenthash" : parentHash if parentHash else "", "webcachedir":Config.getWebCacheDir(), "recipientids":recpts} contents = self.buildPage({'pageTitle' : I18nManager.getText("composemessage.title"), 'pageBody' : self.composetemplate.getHtml(pageParams), 'pageFooter' : "<p>Footer</p>"}) view.setHtml(contents) # If we've got no friends, then warn, can't send to anyone if not DbClient.hasFriends(): view.page().mainFrame().evaluateJavaScript("window.alert('No friends :(');") elif url == "/send": print("Submit new message with params:", params) msgBody = params['messagebody'] # TODO: check body isn't empty, throw an exception? parentHash = params.get("parenthash", None) recpts = params['sendto'] # Make a corresponding message object and pass it on msg = message.RegularMessage(sendTo=recpts, messageBody=msgBody, replyToHash=parentHash) msg.recipients = recpts.split(",") DbClient.addMessageToOutbox(msg) # Save a copy of the sent message sentMessage = {"messageType":"normal", "fromId":DbClient.getOwnTorId(), "messageBody":msgBody, "timestamp":msg.timestamp, "messageRead":True, "messageReplied":False, "recipients":recpts, "parentHash":parentHash} DbClient.addMessageToInbox(sentMessage) # Close window after successful send contents = self.buildPage({'pageTitle' : I18nManager.getText("messages.title"), 'pageBody' : self.closingtemplate.getHtml(), 'pageFooter' : "<p>Footer</p>"}) view.setHtml(contents)