-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconnections_mdb.py
More file actions
136 lines (110 loc) · 3.6 KB
/
connections_mdb.py
File metadata and controls
136 lines (110 loc) · 3.6 KB
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
# Don't Remove Credit @VJ_Botz
# Subscribe YouTube Channel For Amazing Bot @Tech_VJ
# Ask Doubt on telegram @KingVJ01
import pymongo
from info import OTHER_DB_URI, DATABASE_NAME
import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.ERROR)
myclient = pymongo.MongoClient(OTHER_DB_URI)
mydb = myclient[DATABASE_NAME]
mycol = mydb['CONNECTION']
async def add_connection(group_id, user_id):
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0, "active_group": 0 }
)
if query is not None:
group_ids = [x["group_id"] for x in query["group_details"]]
if group_id in group_ids:
return False
group_details = {
"group_id" : group_id
}
data = {
'_id': user_id,
'group_details' : [group_details],
'active_group' : group_id,
}
if mycol.count_documents( {"_id": user_id} ) == 0:
try:
mycol.insert_one(data)
return True
except:
logger.exception('Some error occurred!', exc_info=True)
else:
try:
mycol.update_one(
{'_id': user_id},
{
"$push": {"group_details": group_details},
"$set": {"active_group" : group_id}
}
)
return True
except:
logger.exception('Some error occurred!', exc_info=True)
async def active_connection(user_id):
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0, "group_details": 0 }
)
if not query:
return None
group_id = query['active_group']
return int(group_id) if group_id != None else None
async def all_connections(user_id):
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0, "active_group": 0 }
)
if query is not None:
return [x["group_id"] for x in query["group_details"]]
else:
return None
async def if_active(user_id, group_id):
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0, "group_details": 0 }
)
return query is not None and query['active_group'] == group_id
async def make_active(user_id, group_id):
update = mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : group_id}}
)
return update.modified_count != 0
async def make_inactive(user_id):
update = mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : None}}
)
return update.modified_count != 0
async def delete_connection(user_id, group_id):
try:
update = mycol.update_one(
{"_id": user_id},
{"$pull" : { "group_details" : {"group_id":group_id} } }
)
if update.modified_count == 0:
return False
query = mycol.find_one(
{ "_id": user_id },
{ "_id": 0 }
)
if len(query["group_details"]) >= 1:
if query['active_group'] == group_id:
prvs_group_id = query["group_details"][len(query["group_details"]) - 1]["group_id"]
mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : prvs_group_id}}
)
else:
mycol.update_one(
{'_id': user_id},
{"$set": {"active_group" : None}}
)
return True
except Exception as e:
logger.exception(f'Some error occurred! {e}', exc_info=True)
return False