Skip to content

Commit

Permalink
update clean_topics with MQTTMatcher for +/#
Browse files Browse the repository at this point in the history
  • Loading branch information
mwfarb committed Aug 13, 2024
1 parent 85c5112 commit d40ac85
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 9 deletions.
13 changes: 6 additions & 7 deletions users/mqtt.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
from .models import (SCENE_ANON_USERS_DEF, SCENE_PUBLIC_READ_DEF,
SCENE_PUBLIC_WRITE_DEF, SCENE_USERS_DEF,
SCENE_VIDEO_CONF_DEF, Scene)
from .mqtt_match import topic_matches_sub
from .topics import ADMIN_TOPICS, PUBLISH_TOPICS, SUBSCRIBE_TOPICS

PUBLIC_NAMESPACE = "public"
Expand Down Expand Up @@ -328,19 +329,17 @@ def pubsub_api_v2(

def clean_topics(topics):
"""
Sort and remove list duplicates.
Sort and remove list redundancies.
"""
topics = list(dict.fromkeys(topics))
topics.sort()
# after sort, collapse overlapping topic levels to reduce size
_topics = []
high_topic = ""
for i, topic in enumerate(topics):
for topic in topics:
add = True
if i > 0 and high_topic.endswith("/#"):
if topic.startswith(high_topic[0:-1]):
add = False # higher topic level already granted
for have in _topics:
if topic_matches_sub(have, topic):
add = False
if add:
high_topic = topic
_topics.append(topic)
return _topics
4 changes: 2 additions & 2 deletions users/topics.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
from types import SimpleNamespace
from string import Template
from types import SimpleNamespace

TOPIC_TYPES = SimpleNamespace(**{
'SCENE': 's',
Expand Down Expand Up @@ -72,4 +72,4 @@
ADMIN_TOPICS = SimpleNamespace(**{
"SCENE_ALL": Template('${realm}/s/#'),
"DEVICE_ALL": Template('${realm}/d/#'),
})
})

0 comments on commit d40ac85

Please sign in to comment.