Skip to content

Commit

Permalink
feat: Added more shared codes!
Browse files Browse the repository at this point in the history
  • Loading branch information
amindadgar committed Nov 20, 2024
1 parent 3ab6239 commit b4e4f37
Show file tree
Hide file tree
Showing 2 changed files with 253 additions and 0 deletions.
132 changes: 132 additions & 0 deletions tc_hivemind_backend/db/modules_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
from bson import ObjectId
from .mongo import MongoSingleton


class ModulesBase:
def __init__(self) -> None:
pass

def query(self, platform: str, **kwargs) -> list[dict]:
"""
query the modules database for to get platforms' metadata
Parameters
-----------
platform : str
the platform to choose
it can be `github`, `discourse`, `discord` or etc
**kwargs : dict
projection : dict[str, int]
feature projection on query
Returns
---------
modules_docs : list[dict]
all the module documents that have the `platform` within them
"""
client = MongoSingleton.get_instance().client
projection = kwargs.get("projection", {})

cursor = client["Core"]["modules"].find(
{
"options.platforms.name": platform,
"name": "hivemind",
},
projection,
)
modules_docs = list(cursor)
return modules_docs

def get_platform_community_ids(self, platform_name: str) -> list[str]:
"""
get all community ids that a platform has
Parameters
------------
platform_name : str
the platform having community id and available for hivemind module
Returns
--------
community_ids : list[str]
id of communities that has discord platform and hivemind module enabled
"""
modules = self.query(platform=platform_name, projection={"community"})
community_ids = list(map(lambda x: str(x["community"]), modules))

return community_ids

def get_token(self, platform_id: ObjectId, token_type: str) -> str:
"""
get a specific type of token for a platform
This method is called when we needed a token for modules to extract its data
Parameters
------------
platform_id : ObjectId
the platform id that we want their token
token_type : str
the type of token. i.e. `google_refresh`
Returns
--------
token : str
the token that was required for module's ETL process
"""
client = MongoSingleton.get_instance().client

user_id = self.get_platform_metadata(platform_id, "userId")
user_id = ObjectId(user_id)
token_doc = client["Core"]["tokens"].find_one(
{
"user": user_id,
"type": token_type,
},
{
"token": 1,
},
sort=[("createdAt", -1)],
)
if token_doc is None:
raise ValueError(
f"No Token for the given user {user_id} "
"in tokens collection of the Core database!"
)
token = token_doc["token"]
return token

def get_platform_metadata(
self, platform_id: ObjectId, metadata_name: str
) -> str | dict | list:
"""
get the userid that belongs to a platform
Parameters
-----------
platform_id : bson.ObjectId
the platform id we need their owner user id
metadata_name : str
a specific field of metadata that we want
Returns
---------
metadata_value : Any
the values that the metadata belongs to
"""
client = MongoSingleton.get_instance().get_client()

platform = client["Core"]["platforms"].find_one(
{
"_id": platform_id,
"disconnectedAt": None,
},
{
f"metadata.{metadata_name}": 1,
},
)
if platform is None:
raise ValueError(f"No platform available given platform id: {platform_id}")

metadata_field = platform["metadata"][metadata_name]
return metadata_field
121 changes: 121 additions & 0 deletions tc_hivemind_backend/tests/integration/test_modules_base_query_token.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,121 @@
from datetime import datetime, timedelta
from unittest import TestCase

from bson import ObjectId
from tc_hivemind_backend.db.modules_base import ModulesBase
from tc_hivemind_backend.db.mongo import MongoSingleton


class TestModulesBaseQueryToken(TestCase):
def setUp(self) -> None:
self.client = MongoSingleton.get_instance().get_client()
self.client["Core"].drop_collection("tokens")
self.client["Core"].drop_collection("platforms")

def test_one_token(self):
sample_user = ObjectId("5d7baf326c8a2e2400000000")
community_id = ObjectId("6579c364f1120850414e0dc5")
sample_token_type = "type1"
sample_token_value = "tokenid12345"
platform_id = ObjectId("6579c364f1120850414e0dc6")

self.client["Core"]["platforms"].insert_one(
{
"_id": platform_id,
"name": "platform_name",
"metadata": {
"id": "113445975232201081511",
"userId": str(sample_user),
},
"community": community_id,
"disconnectedAt": None,
"connectedAt": datetime.now(),
"createdAt": datetime.now(),
"updatedAt": datetime.now(),
}
)

sample_token_doc = {
"token": sample_token_value,
"user": sample_user,
"type": sample_token_type,
"expires": datetime.now() + timedelta(days=1),
"blacklisted": False,
"createdAt": datetime.now() - timedelta(days=1),
"updatedAt": datetime.now() - timedelta(days=1),
}
self.client["Core"]["tokens"].insert_one(sample_token_doc)
token = ModulesBase().get_token(
platform_id=platform_id, token_type=sample_token_type
)

self.assertEqual(token, sample_token_value)

def test_empty_tokens_collection(self):
platform_id = ObjectId("6579c364f1120850414e0dc6")
sample_token_type = "type1"
with self.assertRaises(ValueError):
_ = ModulesBase().get_token(
platform_id=platform_id, token_type=sample_token_type
)

def test_no_platform(self):
sample_user = ObjectId("5d7baf326c8a2e2400000000")
platform_id = ObjectId("6579c364f1120850414e0dc6")
sample_token_type = "type1"
sample_token_value = "tokenid12345"

sample_token_doc = {
"token": sample_token_value,
"user": sample_user,
"type": sample_token_type,
"expires": datetime.now() + timedelta(days=1),
"blacklisted": False,
"createdAt": datetime.now() - timedelta(days=1),
"updatedAt": datetime.now() - timedelta(days=1),
}
self.client["Core"]["tokens"].insert_one(sample_token_doc)
with self.assertRaises(ValueError):
_ = ModulesBase().get_token(
platform_id=platform_id, token_type=sample_token_type
)

def test_no_token(self):
sample_user = ObjectId("5d7baf326c8a2e2400000000")
sample_user_with_no_token = ObjectId("5d7baf326c8a2e2400000001")

platform_id = ObjectId("6579c364f1120850414e0dc6")
sample_token_type = "type1"
sample_token_value = "tokenid12345"
community_id = ObjectId("6579c364f1120850414e0dc5")

self.client["Core"]["platforms"].insert_one(
{
"_id": platform_id,
"name": "platform_name",
"metadata": {
"id": "113445975232201081511",
"userId": str(sample_user_with_no_token),
},
"community": community_id,
"disconnectedAt": None,
"connectedAt": datetime.now(),
"createdAt": datetime.now(),
"updatedAt": datetime.now(),
}
)

sample_token_doc = {
"token": sample_token_value,
"user": sample_user,
"type": sample_token_type,
"expires": datetime.now() + timedelta(days=1),
"blacklisted": False,
"createdAt": datetime.now() - timedelta(days=1),
"updatedAt": datetime.now() - timedelta(days=1),
}
self.client["Core"]["tokens"].insert_one(sample_token_doc)
with self.assertRaises(ValueError):
_ = ModulesBase().get_token(
platform_id=platform_id, token_type=sample_token_type
)

0 comments on commit b4e4f37

Please sign in to comment.