Пример #1
0
	def servePage(self, view, url, params):
		print("Special function:", url)
		if url == "/selectprofilepic":
			# Get home directory for file dialog
			homedir = os.path.expanduser("~/")
			fname = QtGui.QFileDialog.getOpenFileName(view, I18nManager.getText("gui.dialogtitle.openimage"),
				homedir, I18nManager.getText("gui.fileselection.filetypes.jpg"))
			if fname:
				view.page().mainFrame().evaluateJavaScript("updateProfilePic('" + fname + "');")
		elif url == "/friendstorm":
			if not DbClient.hasFriends():
				view.page().mainFrame().evaluateJavaScript("window.alert('No friends :(');")
				return
			# Launch a storm
			self.bs = Brainstorm(I18nManager.getText("contacts.storm.title"))
			self.bs.show()
			storm = Storm()
			# Build up Nodes and Edges using our contact list and if possible our friends' contact lists
			myTorId = DbClient.getOwnTorId()
			friends = {}
			friendsOfFriends = {}
			for c in DbClient.getContactList():
				#print("Contact:", c['torid'], "'", c['displayName'], "'")
				nodeid = storm.getUnusedNodeId()
				torid = c['torid']
				friends[torid] = nodeid
				storm.addNode(Node(None, nodeid, c['displayName']))
				friendsOfFriends[torid] = c.get('contactlist', "")
			for torid in friends:
				if torid != myTorId:
					storm.addEdge(friends[torid], friends[myTorId])
			for torid in friendsOfFriends:
				if torid != myTorId:
					ffList = friendsOfFriends[torid]
					if ffList:
						for ff in ffList.split(","):
							if ff and len(ff) > 16:
								ffTorid = ff[:16]
								ffName = ff[16:]
								if ffTorid != myTorId:
									if not friends.get(ffTorid, None):
										# Friend's friend is not in the list yet - add it
										nodeid = storm.getUnusedNodeId()
										friends[ffTorid] = nodeid
										storm.addNode(Node(None, nodeid, ffName))
									# Add edge from torid to ffTorid
									storm.addEdge(friends[torid], friends[ffTorid])

			self.bs.setStorm(storm)
Пример #2
0
	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")
Пример #3
0
	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)