diff --git a/changedetectionio/api/Tags.py b/changedetectionio/api/Tags.py index b001edc21..6c8a9b81c 100644 --- a/changedetectionio/api/Tags.py +++ b/changedetectionio/api/Tags.py @@ -1,5 +1,8 @@ +from changedetectionio import queuedWatchMetaData +from changedetectionio import worker_handler from flask_expects_json import expects_json from flask_restful import abort, Resource + from flask import request from . import auth @@ -11,21 +14,24 @@ class Tag(Resource): def __init__(self, **kwargs): # datastore is a black box dependency self.datastore = kwargs['datastore'] + self.update_q = kwargs['update_q'] # Get information about a single tag # curl http://localhost:5000/api/v1/tag/ @auth.check_token def get(self, uuid): """ - @api {get} /api/v1/tag/:uuid Single tag - get data or toggle notification muting. - @apiDescription Retrieve tag information and set notification_muted status + @api {get} /api/v1/tag/:uuid Single tag - Get data, toggle notification muting, recheck all. + @apiDescription Retrieve tag information, set notification_muted status, recheck all in tag. @apiExample {curl} Example usage: curl http://localhost:5000/api/v1/tag/cc0cfffa-f449-477b-83ea-0caafd1dc091 -H"x-api-key:813031b16330fe25e3780cf0325daa45" curl "http://localhost:5000/api/v1/tag/cc0cfffa-f449-477b-83ea-0caafd1dc091?muted=muted" -H"x-api-key:813031b16330fe25e3780cf0325daa45" + curl "http://localhost:5000/api/v1/tag/cc0cfffa-f449-477b-83ea-0caafd1dc091?recheck=true" -H"x-api-key:813031b16330fe25e3780cf0325daa45" @apiName Tag @apiGroup Tag @apiParam {uuid} uuid Tag unique ID. @apiQuery {String} [muted] =`muted` or =`unmuted` , Sets the MUTE NOTIFICATIONS state + @apiQuery {String} [recheck] = True, Queue all watches with this tag for recheck @apiSuccess (200) {String} OK When muted operation OR full JSON object of the tag @apiSuccess (200) {JSON} TagJSON JSON Full JSON object of the tag """ @@ -34,6 +40,20 @@ class Tag(Resource): if not tag: abort(404, message=f'No tag exists with the UUID of {uuid}') + if request.args.get('recheck'): + # Recheck all, including muted + # Get most overdue first + i=0 + for k in sorted(self.datastore.data['watching'].items(), key=lambda item: item[1].get('last_checked', 0)): + watch_uuid = k[0] + watch = k[1] + if not watch['paused'] and tag['uuid'] not in watch['tags']: + continue + worker_handler.queue_item_async_safe(self.update_q, queuedWatchMetaData.PrioritizedItem(priority=1, item={'uuid': watch_uuid})) + i+=1 + + return f"OK, {i} watches queued", 200 + if request.args.get('muted', '') == 'muted': self.datastore.data['settings']['application']['tags'][uuid]['notification_muted'] = True return "OK", 200 diff --git a/changedetectionio/flask_app.py b/changedetectionio/flask_app.py index b4d4f0761..6e52d4ed9 100644 --- a/changedetectionio/flask_app.py +++ b/changedetectionio/flask_app.py @@ -329,7 +329,7 @@ def changedetection_app(config=None, datastore_o=None): resource_class_kwargs={'datastore': datastore}) watch_api.add_resource(Tag, '/api/v1/tag', '/api/v1/tag/', - resource_class_kwargs={'datastore': datastore}) + resource_class_kwargs={'datastore': datastore, 'update_q': update_q}) watch_api.add_resource(Search, '/api/v1/search', resource_class_kwargs={'datastore': datastore}) diff --git a/changedetectionio/tests/test_api_tags.py b/changedetectionio/tests/test_api_tags.py index 831d052e8..fcae15466 100644 --- a/changedetectionio/tests/test_api_tags.py +++ b/changedetectionio/tests/test_api_tags.py @@ -1,15 +1,18 @@ #!/usr/bin/env python3 from flask import url_for -from .util import live_server_setup, wait_for_all_checks +from .util import live_server_setup, wait_for_all_checks, set_original_response import json +import time def test_api_tags_listing(client, live_server, measure_memory_usage): # live_server_setup(live_server) # Setup on conftest per function api_key = live_server.app.config['DATASTORE'].data['settings']['application'].get('api_access_token') tag_title = 'Test Tag' - # Get a listing + + set_original_response() + res = client.get( url_for("tags"), headers={'x-api-key': api_key} @@ -104,6 +107,8 @@ def test_api_tags_listing(client, live_server, measure_memory_usage): assert res.status_code == 201 watch_uuid = res.json.get('uuid') + + wait_for_all_checks() # Verify tag is associated with watch by name if need be res = client.get( url_for("watch", uuid=watch_uuid), @@ -112,6 +117,21 @@ def test_api_tags_listing(client, live_server, measure_memory_usage): assert res.status_code == 200 assert new_tag_uuid in res.json.get('tags', []) + # Check recheck by tag + before_check_time = live_server.app.config['DATASTORE'].data['watching'][watch_uuid].get('last_checked') + time.sleep(1) + res = client.get( + url_for("tag", uuid=new_tag_uuid) + "?recheck=true", + headers={'x-api-key': api_key} + ) + wait_for_all_checks() + assert res.status_code == 200 + assert b'OK, 1 watches' in res.data + + after_check_time = live_server.app.config['DATASTORE'].data['watching'][watch_uuid].get('last_checked') + + assert before_check_time != after_check_time + # Delete tag res = client.delete( url_for("tag", uuid=new_tag_uuid), @@ -141,3 +161,6 @@ def test_api_tags_listing(client, live_server, measure_memory_usage): headers={'x-api-key': api_key}, ) assert res.status_code == 204 + + +