component pubsub: Implement "max_items":
A new `max_items` field has been added to `PubsubNode`. `0` means `max`. If any >0 value is used, items will be automatically deleted by `cache_pubsub_items`. Items are currently discarded (i.e. no notification is sent), this is the same behaviour as in legacy Libervia Pubsub (formerly "SàT Pubsub"). rel 476
This commit is contained in:
parent
c10c34304a
commit
da2c2c49ea
1 changed files with 122 additions and 17 deletions
|
|
@ -142,6 +142,11 @@ class PubsubNodeConfig:
|
|||
"label": label,
|
||||
"options": {value.value: None for value in enum_type},
|
||||
}
|
||||
case "max_items":
|
||||
definitions[pubsub_var] = {
|
||||
"type": "text-single",
|
||||
"label": label,
|
||||
}
|
||||
case _:
|
||||
definitions[pubsub_var] = {
|
||||
"type": "text-single",
|
||||
|
|
@ -168,6 +173,7 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
"delete-nodes",
|
||||
"item-ids",
|
||||
"meta-data",
|
||||
"multi-items",
|
||||
"publish",
|
||||
"purge-nodes",
|
||||
"retract-items",
|
||||
|
|
@ -292,7 +298,8 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
client,
|
||||
client.jid,
|
||||
request.nodeIdentifier,
|
||||
with_affiliations=True, with_parent=True,
|
||||
with_affiliations=True,
|
||||
with_parent=True,
|
||||
with_subscriptions=True,
|
||||
)
|
||||
|
||||
|
|
@ -574,6 +581,22 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
except ValueError:
|
||||
pass
|
||||
|
||||
# Handle max_items: "max" or any value <= 0 means unlimited (stored as 0),
|
||||
# otherwise parse as int
|
||||
max_items_value = request.options.get("pubsub#max_items")
|
||||
if max_items_value is not None:
|
||||
if max_items_value.lower() == "max":
|
||||
config.max_items = "max"
|
||||
else:
|
||||
try:
|
||||
parsed_max_items = int(max_items_value)
|
||||
# Convert any value <= 0 to "max"
|
||||
config.max_items = (
|
||||
"max" if parsed_max_items <= 0 else parsed_max_items
|
||||
)
|
||||
except ValueError:
|
||||
pass
|
||||
|
||||
# Store relationship fields and normalize empty strings to None
|
||||
config.parent = request.options.get(PARENT_VAR) or None
|
||||
config.link = request.options.get(LINK_VAR) or None
|
||||
|
|
@ -584,6 +607,9 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
)
|
||||
|
||||
try:
|
||||
# "max" becomes 0 (unlimited), int stays as-is
|
||||
db_max_items = 0 if config.max_items == "max" else config.max_items
|
||||
|
||||
node = await G.storage.set_pubsub_node(
|
||||
client=client,
|
||||
service=client.jid,
|
||||
|
|
@ -591,6 +617,7 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
access_model=config.access_model,
|
||||
publish_model=config.publish_model,
|
||||
affiliations=[owner_affiliation],
|
||||
max_items=db_max_items,
|
||||
)
|
||||
except exceptions.ConflictError:
|
||||
raise error.StanzaError("conflict", text=f"node {node_id!r} already exists.")
|
||||
|
|
@ -680,7 +707,8 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
client,
|
||||
client.jid,
|
||||
request.nodeIdentifier,
|
||||
with_affiliations=True, with_parent=True,
|
||||
with_affiliations=True,
|
||||
with_parent=True,
|
||||
with_link=True,
|
||||
)
|
||||
|
||||
|
|
@ -716,11 +744,10 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
if node.publish_model:
|
||||
config_values["pubsub#publish_model"] = str(node.publish_model)
|
||||
|
||||
# TODO: Add max_items when it's stored in the database
|
||||
# if node.max_items is not None:
|
||||
# config_values["pubsub#max_items"] = (
|
||||
# "max" if node.max_items == -1 else str(node.max_items)
|
||||
# )
|
||||
if node.max_items is not None:
|
||||
config_values["pubsub#max_items"] = (
|
||||
"max" if node.max_items == 0 else str(node.max_items)
|
||||
)
|
||||
|
||||
if node.linked_node is not None:
|
||||
config_values[LINK_VAR] = node.linked_node.name
|
||||
|
|
@ -750,7 +777,8 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
client,
|
||||
client.jid,
|
||||
request.nodeIdentifier,
|
||||
with_affiliations=True, with_parent=True,
|
||||
with_affiliations=True,
|
||||
with_parent=True,
|
||||
with_link=True,
|
||||
)
|
||||
|
||||
|
|
@ -783,6 +811,7 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
return
|
||||
|
||||
config = PubsubNodeConfig()
|
||||
update_values: dict = {}
|
||||
|
||||
access_model_value = request.options.get(XEP_0060.OPT_ACCESS_MODEL)
|
||||
if access_model_value:
|
||||
|
|
@ -804,6 +833,46 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
text=f"Invalid publish_model value: {publish_model_value!r}",
|
||||
)
|
||||
|
||||
# Max_items
|
||||
max_items_value = request.options.get("pubsub#max_items")
|
||||
if max_items_value is not None:
|
||||
max_items_value = max_items_value.strip().lower()
|
||||
if max_items_value == "max":
|
||||
# "max" means unlimited, stored as 0 in database
|
||||
db_max_items = 0
|
||||
elif max_items_value == "":
|
||||
# Empty value, we ignore
|
||||
db_max_items = None
|
||||
else:
|
||||
try:
|
||||
db_max_items = int(max_items_value)
|
||||
# Convert any value <= 0 to "max" (0)
|
||||
if db_max_items <= 0:
|
||||
db_max_items = 0
|
||||
except ValueError:
|
||||
raise error.StanzaError(
|
||||
"not-acceptable",
|
||||
text=f"Invalid max_items value: {max_items_value!r}",
|
||||
)
|
||||
|
||||
# max_items can't be lower than current item count. This is not a standard
|
||||
# validation, but it may avoid catastrophic accidental item deletion.
|
||||
if db_max_items is not None and db_max_items > 0:
|
||||
items_count = await G.storage.get_pubsub_item_count(node)
|
||||
if db_max_items < items_count:
|
||||
raise error.StanzaError(
|
||||
"not-acceptable",
|
||||
text=(
|
||||
f"max_items can't be set to {db_max_items} which is lower"
|
||||
f" than the current number of items ({items_count}). Please"
|
||||
" increase max_items or delete items from this node first."
|
||||
),
|
||||
)
|
||||
|
||||
# Only update if value changed
|
||||
if db_max_items is not None and db_max_items != node.max_items:
|
||||
update_values["max_items"] = db_max_items
|
||||
|
||||
title_value = request.options.get("pubsub#title")
|
||||
description_value = request.options.get("pubsub#description")
|
||||
|
||||
|
|
@ -816,7 +885,6 @@ class PubsubResource(pubsub.PubSubResource):
|
|||
extra["description"] = description_value
|
||||
update_extra = True
|
||||
|
||||
update_values = {}
|
||||
if config.access_model != node.access_model:
|
||||
update_values["access_model"] = config.access_model
|
||||
if config.publish_model != node.publish_model:
|
||||
|
|
|
|||
Loading…
Reference in a new issue