def messages(self, topic, maxposts, before=None): """Get all messages in a topic, no later than `before`""" if topic != 'all': if isinstance(topic, str): topics = [] topics.append(server.sorttopic(topic)) elif isinstance(topic, list): topics = [] for t in topic: topics.append(server.sorttopic(t)) sorttopic = {'envelope.local.sorttopic': {'$in': topics}} else: sorttopic = {'envelope.local.sorttopic': {'$exists': True}} # Don't do this in the def, so that our cache is respected. if before is None: before = TavernUtils.inttime() # Append our search topic query. subjects = [] search = {'envelope.payload.class': 'message', 'envelope.payload.regarding': {'$exists': False}, 'envelope.local.time_added': {'$lt': before}} search.update(sorttopic) for envelope in server.db.unsafe.find('envelopes', search, limit=maxposts, sortkey='envelope.local.time_added', sortdirection='descending'): e = Envelope() e.loaddict(envelope) subjects.append(e) return subjects
def receiveEnvelope(self, envstr=None, env=None): """Receive an envelope for processing in the server. Can take either a string, or an envelope obj. """ if envstr is None and env is None: raise Exception( 'receiveEnvelope MUST receive an envelope. Really! ;)') if env is not None: # If we get an envelope, flatten it - The caller may not have. c = env.flatten() else: c = Envelope() c.loadstring(importstring=envstr) # Fill-out the message's local fields. c.munge() # Make sure the message is valid, meets our standards, and is good to # accept and save. if not c.validate(): self.logger.info( "Received an Envelope which does not validate- " + c.payload.hash()) self.logger.debug(c.text()) return False utctime = time.time() existing = self.db.unsafe.find_one( 'envelopes', {'envelope.local.payload_sha512': c.dict['envelope']['local']['payload_sha512']}) if existing is not None: self.logger.debug("We already have that msg.") return c.dict['envelope']['local']['payload_sha512'] # Sign the message to saw we saw it. if self.serversettings.settings['mark-seen']: c.addStamp( stampclass='server', keys=self.ServerKeys, hostname=defaultsettings.settings['hostname']) # Store the time, in full UTC (with precision). This is used to skip # pages in the viewer later. c.dict['envelope']['local']['time_added'] = utctime if c.dict['envelope']['payload']['class'] == "message": # If the message referenes anyone, mark the original, for ease of finding it later. # Do this in the 'local' block, so we don't waste bits passing this on to others. # Partners can calculate this when they receive it. if 'regarding' in c.dict['envelope']['payload']: repliedTo = Envelope() if repliedTo.loadmongo(mongo_id=c.dict['envelope']['payload']['regarding']): self.logger.debug( " I am :: " + c.dict['envelope']['local']['payload_sha512']) self.logger.debug( " Adding a cite on my parent :: " + repliedTo.dict[ 'envelope'][ 'local'][ 'payload_sha512']) repliedTo.addcite( c.dict[ 'envelope'][ 'local'][ 'payload_sha512']) c.addAncestor(c.dict['envelope']['payload']['regarding']) print("id is :" + c.dict['envelope']['local']['payload_sha512']) # It could also be that this message is cited BY others we already have! # Sometimes we received them out of order. Better check. for citedict in self.db.unsafe.find('envelopes', {'envelope.payload.regarding': c.dict['envelope']['local']['payload_sha512']}): self.logger.debug('found existing cite, bad order. ') self.logger.debug( " I am :: " + c.dict['envelope']['local']['payload_sha512']) self.logger.debug(" Found pre-existing cite at :: " + citedict['envelope']['local']['payload_sha512']) # If it's a message, write that in the reply, and in me. if citedict['envelope']['payload']['class'] == 'message': citedme = Envelope() citedme.loaddict(citedict) c.addcite( citedme.dict[ 'envelope'][ 'local'][ 'payload_sha512']) citedme.addAncestor( c.dict[ 'envelope'][ 'local'][ 'payload_sha512']) citedme.saveMongo() # If it's an edit, write that in me. elif citedict['envelope']['payload']['class'] == 'messagerevision': c.addEdit(citedict['envelope']['local']['payload_sha512']) elif citedict['envelope']['payload']['class'] == 'messagerating': citedme = Envelope() citedme.loaddict(citedict) citedme.dict[ 'envelope'][ 'local'][ 'regardingAuthor'] = c.dict[ 'envelope'][ 'payload'][ 'author'] citedme.saveMongo() elif c.dict['envelope']['payload']['class'] == "messagerating": # If this is a rating, cache the AUTHOR of the rated message. regardingPost = self.db.unsafe.find_one( 'envelopes', {'envelope.local.payload_sha512': c.dict['envelope']['payload']['regarding']}) if regardingPost is not None: c.dict[ 'envelope'][ 'local'][ 'regardingAuthor'] = regardingPost[ 'envelope'][ 'payload'][ 'author'] elif c.dict['envelope']['payload']['class'] == "messagerevision": # This is an edit to an existing message. regardingPost = self.db.unsafe.find_one( 'envelopes', {'envelope.local.payload_sha512': c.dict['envelope']['payload']['regarding']}) if regardingPost is not None: if 'priority' in c.dict['envelope']['payload']: c.dict[ 'envelope'][ 'local'][ 'priority'] = c.dict[ 'envelope'][ 'payload'][ 'priority'] else: c.dict['envelope']['local']['priority'] = 0 # Store this edit. # Save this message out to mongo, so we can then retrieve it in # addEdit(). c.saveMongo() # Modify the original message. r = Envelope() r.loaddict(regardingPost) r.addEdit(c.dict['envelope']['local']['payload_sha512']) # Ensure we have the freshest version in memory. c.reloadmongo() else: self.logger.debug("Received an edit without the original") # Store our Envelope c.saveMongo() return c.dict['envelope']['local']['payload_sha512']
def receiveEnvelope(self, envstr=None, env=None): """Receive an envelope for processing in the server. Can take either a string, or an envelope obj. """ if envstr is None and env is None: raise Exception( 'receiveEnvelope MUST receive an envelope. Really! ;)') if env is not None: # If we get an envelope, flatten it - The caller may not have. c = env.flatten() else: c = Envelope() c.loadstring(importstring=envstr) # Fill-out the message's local fields. c.munge() # Make sure the message is valid, meets our standards, and is good to # accept and save. if not c.validate(): self.logger.info( "Received an Envelope which does not validate- " + c.payload.hash()) self.logger.debug(c.text()) return False utctime = time.time() existing = self.db.unsafe.find_one( 'envelopes', { 'envelope.local.payload_sha512': c.dict['envelope']['local']['payload_sha512'] }) if existing is not None: self.logger.debug("We already have that msg.") return c.dict['envelope']['local']['payload_sha512'] # Sign the message to saw we saw it. if self.serversettings.settings['mark-seen']: c.addStamp(stampclass='server', keys=self.ServerKeys, hostname=defaultsettings.settings['hostname']) # Store the time, in full UTC (with precision). This is used to skip # pages in the viewer later. c.dict['envelope']['local']['time_added'] = utctime if c.dict['envelope']['payload']['class'] == "message": # If the message referenes anyone, mark the original, for ease of finding it later. # Do this in the 'local' block, so we don't waste bits passing this on to others. # Partners can calculate this when they receive it. if 'regarding' in c.dict['envelope']['payload']: repliedTo = Envelope() if repliedTo.loadmongo( mongo_id=c.dict['envelope']['payload']['regarding']): self.logger.debug( " I am :: " + c.dict['envelope']['local']['payload_sha512']) self.logger.debug( " Adding a cite on my parent :: " + repliedTo.dict['envelope']['local']['payload_sha512']) repliedTo.addcite( c.dict['envelope']['local']['payload_sha512']) c.addAncestor(c.dict['envelope']['payload']['regarding']) print("id is :" + c.dict['envelope']['local']['payload_sha512']) # It could also be that this message is cited BY others we already have! # Sometimes we received them out of order. Better check. for citedict in self.db.unsafe.find( 'envelopes', { 'envelope.payload.regarding': c.dict['envelope']['local']['payload_sha512'] }): self.logger.debug('found existing cite, bad order. ') self.logger.debug( " I am :: " + c.dict['envelope']['local']['payload_sha512']) self.logger.debug( " Found pre-existing cite at :: " + citedict['envelope']['local']['payload_sha512']) # If it's a message, write that in the reply, and in me. if citedict['envelope']['payload']['class'] == 'message': citedme = Envelope() citedme.loaddict(citedict) c.addcite( citedme.dict['envelope']['local']['payload_sha512']) citedme.addAncestor( c.dict['envelope']['local']['payload_sha512']) citedme.saveMongo() # If it's an edit, write that in me. elif citedict['envelope']['payload'][ 'class'] == 'messagerevision': c.addEdit(citedict['envelope']['local']['payload_sha512']) elif citedict['envelope']['payload'][ 'class'] == 'messagerating': citedme = Envelope() citedme.loaddict(citedict) citedme.dict['envelope']['local'][ 'regardingAuthor'] = c.dict['envelope']['payload'][ 'author'] citedme.saveMongo() elif c.dict['envelope']['payload']['class'] == "messagerating": # If this is a rating, cache the AUTHOR of the rated message. regardingPost = self.db.unsafe.find_one( 'envelopes', { 'envelope.local.payload_sha512': c.dict['envelope']['payload']['regarding'] }) if regardingPost is not None: c.dict['envelope']['local']['regardingAuthor'] = regardingPost[ 'envelope']['payload']['author'] elif c.dict['envelope']['payload']['class'] == "messagerevision": # This is an edit to an existing message. regardingPost = self.db.unsafe.find_one( 'envelopes', { 'envelope.local.payload_sha512': c.dict['envelope']['payload']['regarding'] }) if regardingPost is not None: if 'priority' in c.dict['envelope']['payload']: c.dict['envelope']['local']['priority'] = c.dict[ 'envelope']['payload']['priority'] else: c.dict['envelope']['local']['priority'] = 0 # Store this edit. # Save this message out to mongo, so we can then retrieve it in # addEdit(). c.saveMongo() # Modify the original message. r = Envelope() r.loaddict(regardingPost) r.addEdit(c.dict['envelope']['local']['payload_sha512']) # Ensure we have the freshest version in memory. c.reloadmongo() else: self.logger.debug("Received an edit without the original") # Store our Envelope c.saveMongo() return c.dict['envelope']['local']['payload_sha512']