mirror of
https://github.com/blakeblackshear/frigate.git
synced 2026-11-24 22:37:08 +03:00
Compare commits
2
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
89fbdd7207 | ||
|
|
758b2c96e1 |
@@ -180,6 +180,8 @@ class WebPushClient(Communicator):
|
|||||||
self.refresh: int = 0
|
self.refresh: int = 0
|
||||||
self.web_pushers: dict[str, list[WebPusher]] = {}
|
self.web_pushers: dict[str, list[WebPusher]] = {}
|
||||||
self.expired_subs: dict[str, list[str]] = {}
|
self.expired_subs: dict[str, list[str]] = {}
|
||||||
|
self.expired_subs_lock = threading.Lock()
|
||||||
|
self.cleanup_lock = threading.Lock()
|
||||||
self.suspended_cameras: dict[str, int] = {
|
self.suspended_cameras: dict[str, int] = {
|
||||||
c.name: 0 # type: ignore[misc]
|
c.name: 0 # type: ignore[misc]
|
||||||
for c in self.config.cameras.values()
|
for c in self.config.cameras.values()
|
||||||
@@ -260,15 +262,23 @@ class WebPushClient(Communicator):
|
|||||||
self.claim_headers[endpoint] = self.vapid.sign(claim)
|
self.claim_headers[endpoint] = self.vapid.sign(claim)
|
||||||
|
|
||||||
def cleanup_registrations(self) -> None:
|
def cleanup_registrations(self) -> None:
|
||||||
# delete any expired subs
|
# concurrent cleanups would each restore what the other removed
|
||||||
if len(self.expired_subs) > 0:
|
with self.cleanup_lock:
|
||||||
for user, expired in self.expired_subs.items():
|
with self.expired_subs_lock:
|
||||||
|
expired_subs, self.expired_subs = self.expired_subs, {}
|
||||||
|
|
||||||
|
for user, expired in expired_subs.items():
|
||||||
user_subs = []
|
user_subs = []
|
||||||
|
|
||||||
# get all subscriptions, removing ones that are expired
|
# get all subscriptions, removing ones that are expired
|
||||||
stored_user: User = User.get_by_id(user)
|
stored_user: User | None = User.get_or_none(User.username == user)
|
||||||
|
|
||||||
|
# the user was deleted after the endpoint expired
|
||||||
|
if stored_user is None:
|
||||||
|
continue
|
||||||
|
|
||||||
for token in stored_user.notification_tokens:
|
for token in stored_user.notification_tokens:
|
||||||
if token["endpoint"] in expired:
|
if isinstance(token, dict) and token.get("endpoint") in expired:
|
||||||
continue
|
continue
|
||||||
|
|
||||||
user_subs.append(token)
|
user_subs.append(token)
|
||||||
@@ -284,8 +294,6 @@ class WebPushClient(Communicator):
|
|||||||
f"Cleaned up {len(expired)} notification subscriptions for {user}"
|
f"Cleaned up {len(expired)} notification subscriptions for {user}"
|
||||||
)
|
)
|
||||||
|
|
||||||
self.expired_subs = {}
|
|
||||||
|
|
||||||
def suspend_notifications(self, camera: str, minutes: int) -> None:
|
def suspend_notifications(self, camera: str, minutes: int) -> None:
|
||||||
"""Suspend notifications for a specific camera."""
|
"""Suspend notifications for a specific camera."""
|
||||||
suspend_until = int(
|
suspend_until = int(
|
||||||
@@ -460,6 +468,7 @@ class WebPushClient(Communicator):
|
|||||||
)
|
)
|
||||||
|
|
||||||
if resp.status_code in (404, 410):
|
if resp.status_code in (404, 410):
|
||||||
|
with self.expired_subs_lock:
|
||||||
self.expired_subs.setdefault(notification.user, []).append(
|
self.expired_subs.setdefault(notification.user, []).append(
|
||||||
endpoint
|
endpoint
|
||||||
)
|
)
|
||||||
|
|||||||
@@ -0,0 +1,58 @@
|
|||||||
|
"""Tests for expired push subscription cleanup."""
|
||||||
|
|
||||||
|
import threading
|
||||||
|
import unittest
|
||||||
|
from unittest.mock import patch
|
||||||
|
|
||||||
|
from peewee import SqliteDatabase
|
||||||
|
|
||||||
|
from frigate.comms.webpush import WebPushClient
|
||||||
|
from frigate.models import User
|
||||||
|
|
||||||
|
LIVE = {"endpoint": "https://push.example.com/live", "keys": {}}
|
||||||
|
DEAD = {"endpoint": "https://push.example.com/dead", "keys": {}}
|
||||||
|
|
||||||
|
|
||||||
|
class TestCleanupRegistrations(unittest.TestCase):
|
||||||
|
def setUp(self):
|
||||||
|
self.db = SqliteDatabase(":memory:")
|
||||||
|
self.enterContext(self.db.bind_ctx([User]))
|
||||||
|
self.db.create_tables([User])
|
||||||
|
|
||||||
|
User.create(
|
||||||
|
username="alice", password_hash="x", notification_tokens=[LIVE, DEAD]
|
||||||
|
)
|
||||||
|
|
||||||
|
self.client = WebPushClient.__new__(WebPushClient)
|
||||||
|
self.client.web_pushers = {}
|
||||||
|
self.client.expired_subs = {}
|
||||||
|
self.client.expired_subs_lock = threading.Lock()
|
||||||
|
self.client.cleanup_lock = threading.Lock()
|
||||||
|
|
||||||
|
def tearDown(self):
|
||||||
|
self.db.close()
|
||||||
|
|
||||||
|
@patch("frigate.comms.webpush._build_web_pushers", return_value=[])
|
||||||
|
def test_skips_deleted_user_and_prunes_the_rest(self, _build):
|
||||||
|
self.client.expired_subs = {
|
||||||
|
"deleted": ["https://push.example.com/gone"],
|
||||||
|
"alice": [DEAD["endpoint"]],
|
||||||
|
}
|
||||||
|
|
||||||
|
self.client.cleanup_registrations()
|
||||||
|
|
||||||
|
self.assertEqual(User.get_by_id("alice").notification_tokens, [LIVE])
|
||||||
|
self.assertEqual(self.client.expired_subs, {})
|
||||||
|
_build.assert_called_once_with("alice", [LIVE])
|
||||||
|
|
||||||
|
@patch("frigate.comms.webpush._build_web_pushers", return_value=[])
|
||||||
|
def test_keeps_malformed_tokens_and_prunes_expired(self, _build):
|
||||||
|
malformed = [None, {"keys": {}}]
|
||||||
|
User.update(notification_tokens=[LIVE, *malformed, DEAD]).execute()
|
||||||
|
self.client.expired_subs = {"alice": [DEAD["endpoint"]]}
|
||||||
|
|
||||||
|
self.client.cleanup_registrations()
|
||||||
|
|
||||||
|
self.assertEqual(
|
||||||
|
User.get_by_id("alice").notification_tokens, [LIVE, *malformed]
|
||||||
|
)
|
||||||
Reference in New Issue
Block a user