switch User.sent_dms from Target to new DM type

pull/1263/head
Ryan Barrett 2024-08-14 17:26:00 -07:00
rodzic c7b1bb1f45
commit 17371a31cb
Nie znaleziono w bazie danych klucza dla tego podpisu
ID klucza GPG: 6BE31FDF4776E9D4
2 zmienionych plików z 22 dodań i 11 usunięć

Wyświetl plik

@ -84,7 +84,7 @@ OBJECT_EXPIRE_TYPES = (
OBJECT_EXPIRE_AGE = timedelta(days=90)
# Types of DMs that we send. Used in User.sent_dms
DMS = (
DM_TYPES = (
'follow_request_from_bridged_user',
'replied_to_bridged_user',
'welcome',
@ -118,8 +118,6 @@ class Target(ndb.Model):
https://googleapis.dev/python/python-ndb/latest/model.html#google.cloud.ndb.model.StructuredProperty
"""
uri = ndb.StringProperty(required=True)
# choices is populated in app via reset_protocol_properties, after all User
# subclasses are created, so that PROTOCOLS is fully populated
protocol = ndb.StringProperty(choices=list(PROTOCOLS.keys()), required=True)
def __eq__(self, other):
@ -131,6 +129,21 @@ class Target(ndb.Model):
return hash((self.protocol, self.uri))
class DM(ndb.Model):
""":class:`protocol.Protocol` + type pairs for identifying sent DMs.
Used in :attr:`User.sent_dms`.
https://googleapis.dev/python/python-ndb/latest/model.html#google.cloud.ndb.model.StructuredProperty
"""
type = ndb.StringProperty(choices=DM_TYPES, required=True)
protocol = ndb.StringProperty(choices=list(PROTOCOLS.keys()), required=True)
def __eq__(self, other):
"""Equality excludes Targets' :class:`Key`."""
return self.type == other.type and self.protocol == other.protocol
class ProtocolUserMeta(type(ndb.Model)):
""":class:`User` metaclass. Registers all subclasses in the ``PROTOCOLS`` global."""
def __new__(meta, name, bases, class_dict):
@ -197,10 +210,8 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
# reset_protocol_properties.
enabled_protocols = ndb.StringProperty(repeated=True, choices=list(PROTOCOLS.keys()))
# DMs that we've attempted to send to this user. Target.protocol is the
# protocol we sent the DM about (and the bot account it came from),
# Target.uri is the type of DM.
sent_dms = ndb.StructuredProperty(Target, repeated=True, choices=DMS)
# DMs that we've attempted to send to this user
sent_dms = ndb.StructuredProperty(DM, repeated=True)
created = ndb.DateTimeProperty(auto_now_add=True)
updated = ndb.DateTimeProperty(auto_now=True)
@ -507,7 +518,7 @@ class User(StringIdModel, metaclass=ProtocolUserMeta):
user = self.key.get()
if to_proto.LABEL not in user.enabled_protocols:
user.enabled_protocols.append(to_proto.LABEL)
add(user.sent_dms, Target(protocol=to_proto.LABEL, uri='welcome'))
add(user.sent_dms, DM(protocol=to_proto.LABEL, type='welcome'))
user.put()
nonlocal added
added = True

Wyświetl plik

@ -18,7 +18,7 @@ import app
from atproto import ATProto, Cursor
from atproto_firehose import handle, new_commits, Op
import common
from models import Follower, Object, Target
from models import DM, Follower, Object, Target
from web import Web
from .testutil import ATPROTO_KEY, TestCase
@ -380,7 +380,7 @@ class IntegrationTests(TestCase):
# check results
user = ActivityPub.get_by_id('https://inst/alice')
self.assertTrue(user.is_enabled(ATProto))
self.assertEqual([Target(protocol='atproto', uri='welcome')], user.sent_dms)
self.assertEqual([DM(protocol='atproto', type='welcome')], user.sent_dms)
self.assertEqual(1, len(user.copies))
self.assertEqual('atproto', user.copies[0].protocol)
@ -505,7 +505,7 @@ class IntegrationTests(TestCase):
user = ATProto.get_by_id('did:plc:alice')
self.assertTrue(user.is_enabled(ActivityPub))
self.assertEqual([Target(protocol='activitypub', uri='welcome')],
self.assertEqual([DM(protocol='activitypub', type='welcome')],
user.sent_dms)
headers = {