Exemplo n.º 1
0
    def addStamp(self, stampclass, keys, passkey=None, **kwargs):
        """Adds a stamp of type `class` to the current envelope."""

        if passkey is not None:
            keys.unlock(passkey)
        signature = keys.signstring(self.payload.text())

        # Generate the full stamp obj we will insert.
        fullstamp = {}
        fullstamp['class'] = stampclass
        fullstamp['keyformat'] = keys.keydetails['format']
        fullstamp['pubkey'] = keys.pubkey
        fullstamp['signature'] = signature
        fullstamp['time_added'] = TavernUtils.inttime()

        # Copy in any passed values
        for key in kwargs.keys():
            # Remove the kwargs we know we already added
            if key not in ["stampclass", "keys", "passkey"]:
                fullstamp[key] = kwargs[key]

        proof = {}
        proof['class'] = 'sha256'
        proof[
            'difficulty'] = self.server.serversettings.settings[
            'proof-of-work-difficulty']
        proof['proof'] = TavernUtils.proveWork(
            self.payload.hash(),
            proof['difficulty'])
        fullstamp['proof-of-work'] = proof

        self.dict['envelope']['stamps'].append(fullstamp)
Exemplo n.º 2
0
    def getbackdate(self, topic, maxposts, after):
        """Get the earliest dated message, before `after`"""
        sorttopic = server.sorttopic(topic)
        subjects = []
        if topic != "all":
            for envelope in server.db.unsafe.find('envelopes', {'envelope.local.sorttopic': sorttopic, 'envelope.payload.class': 'message', 'envelope.payload.regarding': {'$exists': False}, 'envelope.local.time_added': {'$gt': after}}, limit=maxposts + 1, sortkey='envelope.local.time_added', sortdirection='ascending'):
                subjects.append(envelope)
        else:
            for envelope in server.db.unsafe.find('envelopes', {'envelope.payload.class': 'message', 'envelope.payload.regarding': {'$exists': False}, 'envelope.local.time_added': {'$gt': after}}, limit=maxposts + 1, sortkey='envelope.local.time_added', sortdirection='ascending'):
                subjects.append(envelope)

        # Adding 1 to maxposts above, because we're going to use this to get the 10 posts AFTER the date we return from this function.
        # This is also the reason why, if we don't have maxposts posts, we
        # subtract 1 below. This ensures that we get ALL the posts in the
        # range.
        if len(subjects) > 0:
            if len(subjects) <= maxposts:
                ret = subjects[-1]['envelope']['local']['time_added'] + 1
                print("ret")
            else:
                ret = subjects[-1]['envelope']['local']['time_added']
        else:
            ret = TavernUtils.inttime()

        return ret
Exemplo n.º 3
0
    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
Exemplo n.º 4
0
    def addStamp(self, stampclass, keys, passkey=None, **kwargs):
        """Adds a stamp of type `class` to the current envelope."""

        if passkey is not None:
            keys.unlock(passkey)
        signature = keys.signstring(self.payload.text())

        # Generate the full stamp obj we will insert.
        fullstamp = {}
        fullstamp['class'] = stampclass
        fullstamp['keyformat'] = keys.keydetails['format']
        fullstamp['pubkey'] = keys.pubkey
        fullstamp['signature'] = signature
        fullstamp['time_added'] = TavernUtils.inttime()

        # Copy in any passed values
        for key in kwargs.keys():
            # Remove the kwargs we know we already added
            if key not in ["stampclass", "keys", "passkey"]:
                fullstamp[key] = kwargs[key]

        proof = {}
        proof['class'] = 'sha256'
        proof['difficulty'] = self.server.serversettings.settings[
            'proof-of-work-difficulty']
        proof['proof'] = TavernUtils.proveWork(self.payload.hash(),
                                               proof['difficulty'])
        fullstamp['proof-of-work'] = proof

        self.dict['envelope']['stamps'].append(fullstamp)
Exemplo n.º 5
0
    def topicCount(self, topic, after=0, before=None, toponly=True):

        # Don't do this in the def, so that our cache is respected.
        if before is None:
            before = TavernUtils.inttime()

        sorttopic = server.sorttopic(topic)
        if toponly:
            count = server.db.unsafe.count(
                'envelopes',
                {'envelope.local.time_added': {'$lt': before},
                 'envelope.local.time_added': {'$gt': after},
                 'envelope.payload.regarding': {'$exists': False},
                 'envelope.local.sorttopic': sorttopic})
        else:
            count = server.db.unsafe.count(
                'envelopes',
                {'envelope.local.time_added': {'$lt': before},
                 'envelope.local.time_added': {'$gt': after},
                 'envelope.local.sorttopic': sorttopic})
        return count