This commit is contained in:
AF 2020-08-06 14:20:51 +03:00
parent ad479a615a
commit 0f9f331754
3 changed files with 23 additions and 9 deletions

View File

@ -1,11 +1,11 @@
<?xml version="1.0" encoding="UTF-8"?>
<project version="4">
<component name="DataSourceManagerImpl" format="xml" multifile-model="true">
<data-source source="LOCAL" name="test" uuid="c31e2821-78f4-457d-a96f-a4f0fc76708c">
<data-source source="LOCAL" name="dev" uuid="264a9472-6fd0-4fcc-8354-d6d121fc5bba">
<driver-ref>sqlite.xerial</driver-ref>
<synchronize>true</synchronize>
<jdbc-driver>org.sqlite.JDBC</jdbc-driver>
<jdbc-url>jdbc:sqlite:D:\source\PyCharm\v25\test.db</jdbc-url>
<jdbc-url>jdbc:sqlite:$PROJECT_DIR$\dev.db</jdbc-url>
<libraries>
<library>
<url>file://$APPLICATION_CONFIG_DIR$/jdbc-drivers/Xerial SQLiteJDBC/3.31.1/license.txt</url>

View File

@ -2,7 +2,8 @@
"db": "sqlite:///dev.db",
"subjects": {
"ro6ncuJxA_cGQ51hPKw11Q84of08j7GtOjL0Xr5GaFs=": {
"allowed": null
"allowed": null,
"contacts": null
}
}
}

View File

@ -2,7 +2,8 @@ import json
from typing import Tuple, Optional, Iterable
from nacl.bindings import crypto_sign_PUBLICKEYBYTES
from sqlalchemy import create_engine, LargeBinary, Column, REAL, BLOB, String, or_, and_, ForeignKeyConstraint, Integer
from sqlalchemy import create_engine, LargeBinary, Column, REAL, BLOB, String, or_, and_, ForeignKeyConstraint, \
Integer, UniqueConstraint
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Query
@ -16,7 +17,7 @@ Base = declarative_base()
class SSSJ(Base):
"""Subject Status Storage JSON"""
__tablename__ = 'ssse'
__tablename__ = 'sssj'
subject = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES), primary_key=True)
status = Column(String, nullable=False, default='{}')
@ -34,17 +35,18 @@ class Msg(Base):
__tablename__ = 'msgs'
oid = Column(Integer, autoincrement=True, primary_key=True)
sf = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES))
st = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES))
idn = Column(LargeBinary(NONCE_SIZE))
sf = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES), index=True)
st = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES), index=True)
idn = Column(LargeBinary(NONCE_SIZE), index=True)
ts = Column(REAL, nullable=False, index=True)
en = Column(LargeBinary(NONCE_SIZE), nullable=False)
en = Column(LargeBinary(NONCE_SIZE), nullable=False, index=True)
ec = Column(BLOB, nullable=False)
flags = Column(String, nullable=False)
__table_args__ = (ForeignKeyConstraint((sf, st, idn,),
(MsgSgn.sf, MsgSgn.st, MsgSgn.idn,),
),
UniqueConstraint(sf, st, idn, ),
)
def sgn(self):
@ -78,6 +80,17 @@ class DBStorage(AbstractStorage):
def check(self, subject: Subject) -> dict:
session = self.Session()
status = json.loads(session.query(SSSJ).filter_by(subject=subject.vkey.encode()).one_or_none().status) or {}
if 'contacts' in status:
query: Query = session.query(Msg).filter(or_(
Msg.sf == subject.vkey.encode(),
Msg.st == subject.vkey.encode(),
))
query = query.with_entities(Msg.sf, Msg.st)
contacts = set()
for sf, st in query:
contacts.add(sf)
contacts.add(st)
status['contacts'] = list(map(Encoding.encode, contacts))
session.close()
return status