def getOriginalMessage(self, messageid): env = Envelope() # First, pull the referenced message. if env.loadmongo(mongo_id=messageid): if env.dict['envelope']['payload']['class'] == 'message': return env.dict['envelope']['local']['payload_sha512'] else: return env.dict['envelope']['payload']['regarding'] return None
def writetopic(topic, since=0, limit=0, skip=0, directory=None): """Write a topic out to .7z files.""" e = Envelope() if topic == 'all': envelopes = server.db.safe.find('envelopes') else: envelopes = server.db.safe.find('envelopes', { 'envelope.local.time_added': { '$gt': since }, 'envelope.payload.topic': topic }, limit=limit, skip=skip) for envelope in envelopes: if args.verbose: print(envelope) id = envelope['envelope']['local']['payload_sha512'] e.loadmongo(id) e.validate() if 'topic' in e.payload.dict: topic = e.payload.dict['topic'] else: topic = 'none' if directory is None: topicdir = msgsdir + topic else: topicdir = directory # Make a dir if nec. if not os.path.isdir(topicdir): os.makedirs(topicdir) if not os.path.exists(topicdir + "/" + e.payload.hash() + ".7zTavernEnvelope"): e.savefile(topicdir)
def writetopic(topic, since=0, limit=0, skip=0, directory=None): """Write a topic out to .7z files.""" e = Envelope() if topic == 'all': envelopes = server.db.safe.find('envelopes') else: envelopes = server.db.safe.find( 'envelopes', {'envelope.local.time_added': {'$gt': since}, 'envelope.payload.topic': topic}, limit=limit, skip=skip) for envelope in envelopes: if args.verbose: print(envelope) id = envelope['envelope']['local']['payload_sha512'] e.loadmongo(id) e.validate() if 'topic' in e.payload.dict: topic = e.payload.dict['topic'] else: topic = 'none' if directory is None: topicdir = msgsdir + topic else: topicdir = directory # Make a dir if nec. if not os.path.isdir(topicdir): os.makedirs(topicdir) if not os.path.exists(topicdir + "/" + e.payload.hash() + ".7zTavernEnvelope"): e.savefile(topicdir)
def getTopMessage(self, messageid): # Find the top level of a post that we currently have. env = Envelope() # First, pull the referenced message. if env.loadmongo(mongo_id=messageid): # If we have no references, congrats, we're the top. if not 'regarding' in env.dict['envelope']['payload']: return env.dict['envelope']['local']['payload_sha512'] if env.dict['envelope']['payload']['regarding'] is None: return env.dict['envelope']['local']['payload_sha512'] # If we're here, it means we have a parent. # Recurse upstream result = self.getTopMessage( env.dict['envelope']['payload']['regarding']) # Don't blindly return it, since it might be None in broken chains. # In that case, return yourself. if result is not None: return result else: return env.dict['envelope']['local']['payload_sha512'] else: return None
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']