add common.add to add to repeated properties only if not already there

tried a bunch of other more sophisticated ways to do this in snarfed/webutil@280a93e092 (plus snarfed/webutil@6cb9c1f719), tried a custom repeated property class that acted like a set instead of a list, but couldn't get it to work reliably or compatibly enough.
pull/571/head
Ryan Barrett 2023-06-29 22:15:07 -07:00
rodzic ee52a7b1c6
commit 0caca9243e
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
4 zmienionych plików z 21 dodań i 16 usunięć

Wyświetl plik

@ -223,3 +223,12 @@ def webmention_endpoint_cache_key(url):
def webmention_discover(url, **kwargs):
"""Thin caching wrapper around :func:`web.discover`."""
return webmention.discover(url, **kwargs)
def add(seq, val):
"""Appends val to seq if seq doesn't already contain it.
Useful for treating repeated ndb properties like sets instead of lists.
"""
if val not in seq:
seq.append(val)

Wyświetl plik

@ -19,7 +19,7 @@ from oauth_dropins.webutil.models import ComputedJsonProperty, JsonProperty, Str
from oauth_dropins.webutil.util import json_dumps, json_loads
import common
from common import base64_to_long, long_to_base64, redirect_unwrap
from common import add, base64_to_long, long_to_base64, redirect_unwrap
# maps string label to Protocol subclass. populated by ProtocolUserMeta.
# seed with old and upcoming protocols that don't have their own classes (yet).
@ -431,11 +431,9 @@ class Object(StringIdModel):
assert '^^' not in self.key.id()
if self.as1 and self.as1.get('objectType') == 'activity':
if 'activity' not in self.labels:
self.labels.append('activity')
else:
if 'activity' in self.labels:
self.labels.remove('activity')
add(self.labels, 'activity')
elif 'activity' in self.labels:
self.labels.remove('activity')
def _post_put_hook(self, future):
"""Update :meth:`Protocol.load` cache."""

Wyświetl plik

@ -11,7 +11,7 @@ from granary import as1
import werkzeug.exceptions
import common
from common import error
from common import add, error
from models import Follower, Object, PROTOCOLS, Target
from oauth_dropins.webutil import util
from oauth_dropins.webutil.util import json_dumps, json_loads
@ -456,10 +456,9 @@ class Protocol:
logger.info(f'Delivering to followers of {actor_id}')
for f in Follower.query(Follower.to == from_cls.key_for(actor_id),
Follower.status == 'active'):
if f.from_ not in obj.users:
obj.users.append(f.from_)
if obj.users and 'feed' not in obj.labels:
obj.labels.append('feed')
add(obj.users, f.from_)
if obj.users:
add(obj.labels, 'feed')
obj.put()
return 'OK'
@ -584,8 +583,7 @@ class Protocol:
target = obj.undelivered.pop()
domain = util.domain_from_link(target.uri, minimize=False)
if g.user and domain == g.user.key.id():
if 'notification' not in obj.labels:
obj.labels.append('notification')
add(obj.labels, 'notification')
if (domain == util.domain_from_link(source, minimize=False)
and cls.LABEL != 'fake'):
@ -611,8 +609,7 @@ class Protocol:
try:
if recip.send(obj, target.uri):
obj.delivered.append(target)
if 'notification' not in obj.labels:
obj.labels.append('notification')
add(obj.labels, 'notification')
except BaseException as e:
code, body = util.interpret_http_exception(e)
if not code and not body:

3
web.py
Wyświetl plik

@ -21,6 +21,7 @@ from requests import HTTPError, RequestException, URLRequired
from werkzeug.exceptions import BadGateway, BadRequest, HTTPException, NotFound
import common
from common import add
from flask_app import app, cache
from models import Follower, Object, PROTOCOLS, Target, User
from protocol import Protocol
@ -558,7 +559,7 @@ def webmention_task():
source_protocol='web',
)
if not targets:
obj.labels.append('user')
add(obj.labels, 'user')
obj.status = 'ignored'
obj.put()
return 'No targets', 204