-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdb_utils.py
175 lines (152 loc) · 5.61 KB
/
db_utils.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
''' DB Utilities '''
from pymongo import MongoClient
from bson.objectid import ObjectId
import datetime
import os
import urllib.parse
mongo = MongoClient(
os.environ["DB_URL"],
username=urllib.parse.quote_plus(os.environ["MONGODB_USER"]) if "MONGODB_USER" in os.environ else None,
password=urllib.parse.quote_plus(os.environ["MONGODB_PASSWORD"]) if "MONGODB_PASSWORD" in os.environ else None,
authSource="admin"
)
db = mongo.mediator
def get_connection(remote_did):
''' Get existing connection '''
return db.connections.find_one({"remote_did": remote_did})
def create_connection(remote_did, local_did):
''' Create connection'''
db.connections.insert_one({
"remote_did": remote_did,
"local_did": local_did,
"creation_time": int(datetime.datetime.now().timestamp())
})
def update_connection(remote_old_did, remote_new_did):
''' Update connection '''
#TODO history of rotations
db.connections.update_one(
{"remote_did": remote_old_did}, {
"$set": {"remote_did": remote_new_did, "update_time": int(datetime.datetime.now().timestamp())}
}
)
def get_oob_did():
return db.oobs.find_one(sort=[('date', -1)])
def store_oob_did(did):
db.oobs.insert_one(did)
def get_issuer_did():
issuer_did = db.issuers.find_one(sort=[('date', -1)])
return issuer_did["did"] if issuer_did is not None else None
def store_issuer_did(did):
db.issuers.insert_one(did)
def get_prism_did(did):
return db.prism.find_one({"did": did})
def store_prism_did(did,seed):
db.prism.insert_one({
"did": did,
"seed": seed
})
def add_mediation(remote_did, routing_did, endpoint):
''' Add mediation info to connection '''
db.connections.update_one(
{"remote_did": remote_did}, {
"$set": {
"isMediation": True,
"routing_did": routing_did,
"endpoint": endpoint
}
}
)
def update_keys(remote_did, updates):
''' Add mediation keys '''
connection = get_connection(remote_did)
current_keys = connection["keylist"] if "keylist" in connection else []
updated = []
for update in updates:
if update["action"] == "add":
if update["recipient_did"] in current_keys:
updated.append(
{
"recipient_did": update["recipient_did"],
"action": "add",
"result": "no_change"
})
else:
current_keys.append(update["recipient_did"])
updated.append(
{
"recipient_did": update["recipient_did"],
"action": "add",
"result": "success"
})
elif update["action"] == "remove":
if update["recipient_did"] in current_keys:
current_keys.remove(update["recipient_did"])
updated.append(
{
"recipient_did": update["recipient_did"],
"action": "remove",
"result": "success"
})
else:
updated.append(
{
"recipient_did": update["recipient_did"],
"action": "remove",
"result": "no_change"
})
else:
updated.append(
{
"recipient_did": update["recipient_did"],
"action": update["action"],
"result": "client_error"
})
db.connections.update_one(
{"remote_did": remote_did}, {
"$set": {
"keylist": current_keys,
}
}
)
return updated
def get_message_status(remote_did, recipient_key):
if recipient_key:
count = db.messages.count_documents({"recipient_key": recipient_key},{"attachment":1})
else:
recipient_keys = db.connections.find_one({"remote_did":remote_did})["keylist"]
count = db.messages.count_documents({"recipient_key": {"$in":recipient_keys}})
return count
def get_messages(remote_did, recipient_key,limit):
if recipient_key:
return db.messages.find({"recipient_key": recipient_key},{"attachment":1}).limit(limit)
else:
recipient_keys = db.connections.find_one({"remote_did":remote_did})["keylist"]
return list(db.messages.find({"recipient_key": {"$in":recipient_keys}}).limit(limit))
def add_message(recipient_key, attachment):
# TODO verify that recipient_key belong to a registered peer
db.messages.insert_one(
{
"recipient_key": recipient_key,
"attachment": attachment,
"datetime": int(datetime.datetime.now().timestamp())
}
)
def remove_messages(remote_did, message_id_list):
#TODO verify that recipient_key belongs to remote_did
for id in message_id_list:
db.messages.delete_one({"_id": ObjectId(id)})
return get_message_status(remote_did, None)
def get_short_url(oobid):
short_url = db.shortUrls.find_one({"oobid": oobid},sort=[('date', -1)])
return short_url
def store_short_url(short_url):
db.shortUrls.insert_one(short_url)
def expire_short_url(oobid):
# TODO order by date
db.shortUrls.update_one(
{"oobid": oobid}, {
"$set": {
"expires_time": int(0)
}
}
)