File size: 6,195 Bytes
bc96bf6
 
 
 
 
 
94c8e6e
bc96bf6
 
 
 
 
 
94c8e6e
 
 
 
bc96bf6
 
 
 
 
 
 
94c8e6e
bc96bf6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
94c8e6e
bc96bf6
94c8e6e
 
bc96bf6
 
 
 
 
 
 
 
 
94c8e6e
bc96bf6
94c8e6e
 
bc96bf6
 
94c8e6e
 
 
 
bc96bf6
 
 
 
 
94c8e6e
bc96bf6
 
 
94c8e6e
 
bc96bf6
 
94c8e6e
bc96bf6
 
 
94c8e6e
 
 
 
bc96bf6
 
 
 
 
94c8e6e
 
 
bc96bf6
94c8e6e
bc96bf6
 
 
94c8e6e
 
 
 
bc96bf6
 
 
 
94c8e6e
 
 
 
 
 
bc96bf6
 
94c8e6e
 
 
 
bc96bf6
 
 
 
94c8e6e
 
 
 
bc96bf6
 
 
94c8e6e
 
 
 
bc96bf6
 
 
 
94c8e6e
 
 
 
bc96bf6
 
 
94c8e6e
 
 
 
bc96bf6
 
 
 
 
 
94c8e6e
bc96bf6
 
 
 
 
 
 
 
 
 
 
 
94c8e6e
bc96bf6
 
 
 
 
94c8e6e
 
bc96bf6
 
94c8e6e
 
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
176
177
178
import pymongo

from info import DATABASE_URL, DATABASE_NAME

import logging
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)

myclient = pymongo.MongoClient(DATABASE_URL)
mydb = myclient[DATABASE_NAME]
mycol = mydb['CONNECTION']   

async def add_connection(group_id, user_id):
    """
    Add a connection between a user and a group.
    """
    logger.info(f"Adding connection for user {user_id} to group {group_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:
            logger.info(f"Connection already exists for user {user_id} to group {group_id}.")
            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)
            logger.info(f"New connection inserted for user {user_id} to group {group_id}.")
            return True
        except Exception as e:
            logger.exception(f"Error inserting new connection for user {user_id} to group {group_id}: {e}")
    else:
        try:
            mycol.update_one(
                {'_id': user_id},
                {
                    "$push": {"group_details": group_details},
                    "$set": {"active_group" : group_id}
                }
            )
            logger.info(f"Existing connection updated for user {user_id} to group {group_id}.")
            return True
        except Exception as e:
            logger.exception(f"Error updating existing connection for user {user_id} to group {group_id}: {e}")

async def active_connection(user_id):
    """
    Get the active connection for a user.
    """
    logger.info(f"Getting active connection for user {user_id}.")
    query = mycol.find_one(
        { "_id": user_id },
        { "_id": 0, "group_details": 0 }
    )
    if not query:
        logger.info(f"No active connection found for user {user_id}.")
        return None

    group_id = query['active_group']
    if group_id is not None:
        logger.info(f"Active connection found for user {user_id}: group {group_id}.")
        return int(group_id)
    else:
        logger.info(f"No active group set for user {user_id}.")
        return None

async def all_connections(user_id):
    """
    Get all connections for a user.
    """
    logger.info(f"Getting all connections for user {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"]]
        logger.info(f"All connections for user {user_id}: {group_ids}.")
        return group_ids
    else:
        logger.info(f"No connections found for user {user_id}.")
        return None

async def if_active(user_id, group_id):
    """
    Check if a specific group is the active connection for a user.
    """
    logger.info(f"Checking if group {group_id} is active for user {user_id}.")
    query = mycol.find_one(
        { "_id": user_id },
        { "_id": 0, "group_details": 0 }
    )
    is_active = query is not None and query['active_group'] == group_id
    if is_active:
        logger.info(f"Group {group_id} is active for user {user_id}.")
    else:
        logger.info(f"Group {group_id} is not active for user {user_id}.")
    return is_active

async def make_active(user_id, group_id):
    """
    Set a specific group as the active connection for a user.
    """
    logger.info(f"Making group {group_id} active for user {user_id}.")
    update = mycol.update_one(
        {'_id': user_id},
        {"$set": {"active_group" : group_id}}
    )
    if update.modified_count != 0:
        logger.info(f"Group {group_id} set as active for user {user_id}.")
    else:
        logger.info(f"No active group modified for user {user_id}.")
    return update.modified_count != 0

async def make_inactive(user_id):
    """
    Make a user's connection inactive.
    """
    logger.info(f"Making connection inactive for user {user_id}.")
    update = mycol.update_one(
        {'_id': user_id},
        {"$set": {"active_group" : None}}
    )
    if update.modified_count != 0:
        logger.info(f"Connection made inactive for user {user_id}.")
    else:
        logger.info(f"No active group modified for user {user_id}.")
    return update.modified_count != 0

async def delete_connection(user_id, group_id):
    """
    Delete a specific connection between a user and a group.
    """
    logger.info(f"Deleting connection for user {user_id} from group {group_id}.")
    try:
        update = mycol.update_one(
            {"_id": user_id},
            {"$pull" : { "group_details" : {"group_id":group_id} } }
        )
        if update.modified_count == 0:
            logger.info(f"No connection found to delete for user {user_id} from group {group_id}.")
            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}}
                )
                logger.info(f"Active group changed to previous group {prvs_group_id} for user {user_id}.")
        else:
            mycol.update_one(
                {'_id': user_id},
                {"$set": {"active_group" : None}}
            )
            logger.info(f"No group details left, active group set to None for user {user_id}.")
        logger.info(f"Connection deleted for user {user_id} from group {group_id}.")
        return True
    except Exception as e:
        logger.exception(f"Error deleting connection for user {user_id} from group {group_id}: {e}")
        return False