2019-10-26 03:51:38 +02:00
|
|
|
import logging
|
|
|
|
|
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
|
2019-10-12 15:20:18 +02:00
|
|
|
class SyncToken(object):
|
|
|
|
"""A SyncToken is an instance of a sync token, which is a token retrieved from a matrix
|
|
|
|
homeserver. It is given to the /sync endpoint in order to specify at which point in the
|
|
|
|
event timeline you would like to receive messages after
|
|
|
|
"""
|
|
|
|
|
|
|
|
def __init__(self, store):
|
|
|
|
"""
|
|
|
|
Args:
|
|
|
|
store (Storage): An object to access the storage layer
|
|
|
|
"""
|
|
|
|
self.store = store
|
2019-10-26 03:16:13 +02:00
|
|
|
self.token = None
|
2019-10-12 15:20:18 +02:00
|
|
|
|
|
|
|
# Attempt to load a token from the provided storage layer
|
|
|
|
self._load()
|
|
|
|
|
|
|
|
def _load(self):
|
|
|
|
"""Load the latest sync token from the database"""
|
|
|
|
self.store.cursor.execute("SELECT token FROM sync_token")
|
|
|
|
rows = self.store.cursor.fetchone()
|
|
|
|
|
|
|
|
if rows:
|
|
|
|
self.token = rows[0]
|
|
|
|
|
|
|
|
def update(self, token):
|
2019-10-26 03:51:38 +02:00
|
|
|
"""Update the sync token in the database and the object
|
2019-10-12 15:20:18 +02:00
|
|
|
|
|
|
|
Args:
|
|
|
|
token (str): A sync token from a sync response sent by a matrix homeserver
|
|
|
|
"""
|
2019-10-26 03:51:38 +02:00
|
|
|
self.token = token
|
2019-10-12 15:20:18 +02:00
|
|
|
self.store.cursor.execute("INSERT OR REPLACE INTO sync_token "
|
2019-10-26 03:51:38 +02:00
|
|
|
"(dedupe_id, token) VALUES (1, ?)", (token,))
|
|
|
|
self.store.conn.commit()
|