def servePage(self, view, url, params): DbClient.exportAvatars(Config.getWebCacheDir()) if url == "/send": print("send message of type '%(messageType)s' to id '%(sendTo)s'" % params) elif url.startswith("/delete/"): DbClient.deleteMessageFromInbox(params.get("msgId", "")) # Make dictionary to convert ids to names contactNames = {c['torid']:c['displayName'] for c in DbClient.getContactList()} unknownSender = I18nManager.getText("messages.sender.unknown") unknownRecpt = I18nManager.getText("messages.recpt.unknown") # Get contact requests, responses and mails from inbox conreqs = [] conresps = [] mails = [] for m in DbClient.getInboxMessages(): 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'] = DbClient.getProfile(m['fromId'], True).get("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 = DbClient.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: m['recipients'] = ", ".join([contactNames.get(i, unknownRecpt) for i in recpts.split(",")]) else: m['recipients'] = unknownRecpt mails.append(m) 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)
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() # Loop through all contact requests and contact refers for the given torid for m in DbClient.getInboxMessages(): if m["messageType"] == "contactrequest" and m["fromId"] == torId: nameList.add(m.get("fromName", None)) keyList.add(m.get("publicKey", None)) 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) # no keys or more than one key! suppliedKey = keyList.pop() if suppliedKey is None or len(suppliedKey) < 80: return (None, None) # one key supplied but it's missing or too short suppliedName = nameList.pop() if len(nameList) == 1 else torId return (suppliedName, suppliedKey)
def checkInbox(self): '''Look in the inbox for messages''' messagesFound = DbClient.getInboxMessages().count() self.somethingInInbox = (messagesFound > 0) self.parent.postmanKnock() # only once
def _getInboxMessage(torId, messageType): for m in DbClient.getInboxMessages(): if m["fromId"] == torId and m["messageType"] == messageType: return m