From 4c21d2fee6bd1e97b354dd9aec9c7931d1a2707c Mon Sep 17 00:00:00 2001 From: David Rotermund <54365609+davrot@users.noreply.github.com> Date: Tue, 9 Apr 2024 01:26:59 +0200 Subject: [PATCH] Add files via upload --- user_tools/config_mail.json | 1 + user_tools/get_activation_requests.py | 87 +++++++++++++++++++++++++++ user_tools/prepare.sh | 4 ++ 3 files changed, 92 insertions(+) create mode 100644 user_tools/config_mail.json create mode 100644 user_tools/get_activation_requests.py create mode 100644 user_tools/prepare.sh diff --git a/user_tools/config_mail.json b/user_tools/config_mail.json new file mode 100644 index 0000000..9115e69 --- /dev/null +++ b/user_tools/config_mail.json @@ -0,0 +1 @@ +{"overleafdomain": "https://overleaf.neuro.uni-bremen.de"} \ No newline at end of file diff --git a/user_tools/get_activation_requests.py b/user_tools/get_activation_requests.py new file mode 100644 index 0000000..4fb7002 --- /dev/null +++ b/user_tools/get_activation_requests.py @@ -0,0 +1,87 @@ +import pymongo +import datetime +import os +import pickle +import json + + +def get_activation_requests(remove_old_entries: bool = False) -> list[dict]: + results: list[dict] = [] + + filename = "last_run.pkl" + + with open("config_mail.json", "r") as file: + config = json.load(file) + + now = datetime.datetime.now() + + client = pymongo.MongoClient("overleafmongo", 27017) + db = client.sharelatex + tokens = db.tokens + + continue_at_time = None + if remove_old_entries: + if os.path.exists(filename): + with open(filename, "rb") as file: + continue_at_time = pickle.load(file) + + query = {"expiresAt": {"$gt": now}} + if continue_at_time is not None: + query["createdAt"] = {"$gt": continue_at_time} + + newest = None + for token in tokens.find(query): + if newest is None: + newest = token["createdAt"] + elif token["createdAt"] > newest: + newest = token["createdAt"] + + # Freeze time. We don't want to misshandle item that are newer than the last check + if newest is not None: + if "createdAt" in query: + query["createdAt"] = {"$gt": continue_at_time, "$lte": newest} + else: + query["createdAt"] = {"$lte": newest} + + # Find unique user ids + user_id_set = set() + + for token in tokens.find(query): + user_id_set.add(token["data"]["user_id"]) + + user_ids = list(user_id_set) + + # Store the time stamp for newest + with open(filename, "wb") as file: + pickle.dump(newest, file) + + for user_id in user_ids: + new_query = query.copy() + new_query["data.user_id"] = user_id + + newest_entry = None + object_id = None + + for token in tokens.find(new_query): + + if newest_entry is None: + newest_entry = token["createdAt"] + object_id = token["_id"] + elif token["createdAt"] > newest_entry: + newest_entry = token["createdAt"] + object_id = token["_id"] + + dataset_found = None + + profile = dict() + if object_id is not None: + dataset_found = tokens.find_one({"_id": object_id}) + extracted_user_id = dataset_found["data"]["user_id"] + profile["email"] = dataset_found["data"]["email"] + extracted_token = dataset_found["token"] + profile["url_string"] = ( + f"{config['overleafdomain']}/user/activate?token={extracted_token}&user_id={extracted_user_id}" + ) + results.append(profile) + + return results diff --git a/user_tools/prepare.sh b/user_tools/prepare.sh new file mode 100644 index 0000000..f8bece1 --- /dev/null +++ b/user_tools/prepare.sh @@ -0,0 +1,4 @@ +apk add mc +pip install docker +pip install pymongo +pip install email_validator