def expire_notifications(self, act_ids: List[str]) -> None: """ Expires notifications by changing their expiration time to now. """ now = epoch_ms() coll = get_feeds_collection() coll.update_many({'id': {'$in': act_ids}}, {'$set': {'expires': now}})
def get_timeline(self, count: int=10, include_seen: int=False, level: Level=None, verb: Verb=None, reverse: bool=False) -> List[dict]: """ :param count: int > 0 :param include_seen: boolean :param level: Level or None :param verb: Verb or None """ coll = get_feeds_collection() now = epoch_ms() query = { "users": self._user_doc(), "expires": {"$gt": now} } if not include_seen: query['unseen'] = self._user_doc() if level is not None: query['level'] = level.id if verb is not None: query['verb'] = verb.id order = pymongo.DESCENDING if reverse: order = pymongo.ASCENDING timeline = coll.find(query).sort("created", order).limit(count) serial_notes = [note for note in timeline] return serial_notes
def get_unseen_count(self) -> int: coll = get_feeds_collection() now = epoch_ms() query = { "users": self._user_doc(), "unseen": self._user_doc(), "expires": {"$gt": now} } return coll.find(query).count()
def test_add_global_notification_custom_expiration(client, mock_valid_admin_token, mongo): expiration = epoch_ms() + 20000 note = {"verb": 1, "object": "2", "level": 1, "expires": expiration} mock_valid_admin_token("kbase_admin", "KBase Admin") response = client.post("/admin/api/V1/notification/global", headers={"Authorization": "token-" + str(uuid4())}, json=note) data = json.loads(response.data) assert "id" in data response = client.get("/api/V1/notification/" + data["id"], headers={"Authorization": "token-" + str(uuid4())}) data = json.loads(response.data)["notification"] assert "expires" in data assert "created" in data assert data["expires"] == expiration
# some dummy "good" inputs for testing actor_d = {"id": "test_actor", "type": "user"} actor = Entity.from_dict(actor_d) verb_inf = "invite" verb_past = "invited" verb_id = 1 object_d = {"id": "foo", "type": "workspace"} note_object = Entity.from_dict(object_d) source = "groups" level_name = "warning" level_id = 2 target_d = {"id": "target_actor", "type": "user"} target = [Entity.from_dict(target_d)] context = {"some": "context"} expires = epoch_ms() + (10 * 24 * 60 * 60 * 1000) # 10 days external_key = "an_external_key" user_d = {"id": "user_actor", "type": "user"} users = [Entity.from_dict(user_d)] def assert_note_ok(note, **kwargs): keys = [ 'actor', 'actor_type', 'object', 'source', 'target', 'context', 'expires', 'external_key', 'users' ] for k in keys: if k in kwargs: assert getattr(note, k) == kwargs[k] if 'verb_id' in kwargs: assert note.verb.id == int(kwargs['verb_id']) if 'verb_inf' in kwargs:
def test_epoch_ms(): ''' Just make sure it's an int with 13 digits. Be more rigorous later. At least, before year 2287. ''' t = epoch_ms() assert len(str(t)) == 13