First set of embellish() additions for Notes

2019-08-17
Marnanel Thurman 2019-04-25 19:47:44 +01:00
rodzic e73a34f10b
commit d201903750
3 zmienionych plików z 102 dodań i 4 usunięć

Wyświetl plik

@ -1,2 +1,79 @@
def embellish(thing):
import logging
from django.conf import settings
logger = logging.getLogger(name='django_kepi')
def _embellish_Note(thing, user=None):
if 'summary' not in thing:
thing['summary'] = None
if 'sensitive' not in thing:
thing['sensitive'] = False
if 'attachment' not in thing:
thing['attachment'] = []
if 'tag' not in thing:
thing['tag'] = []
if 'to' not in thing:
thing['to'] = ['https://www.w3.org/ns/activitystreams#Public']
if 'cc' not in thing:
thing['cc'] = [user.activity_followers]
if 'attributedTo' not in thing:
thing['attributedTo'] = user.activity_id
## Conversation structure
if 'inReplyTo' not in thing:
thing['inReplyTo'] = None
## Content map
if 'contentMap' not in thing and 'content' in thing:
thing['contentMap'] = {
settings.LANGUAGE_CODE: thing['content'],
}
elif 'content' not in thing and 'contentMap' in thing:
# just pick one
thing['content'] = thing['contentMap'].values()[0]
## Atom feeds
if 'atomUri' not in thing:
thing['atomUri'] = thing['url']
if 'inReplyToUri' not in thing:
thing['inReplyToAtomUri'] = thing['inReplyTo']
## All done
return thing
def embellish(thing, user=None):
if 'type' not in thing:
logger.debug('embellish: object does not contain a type: %s',
str(thing))
raise ValueError('object does not contain a type!')
f_type = thing['type']
logger.debug('embellish: received thing of type %s', f_type)
###### general embellishments
if 'id' not in thing:
logger.debug('embellish: object does not contain a type: %s',
str(thing))
raise ValueError('object does not contain an id!')
if 'url' not in thing:
thing['url'] = thing['id']
# XXX 'published' date: format?
###### special embellishments per "type"
if f_type=='Note':
thing = _embellish_Note(thing, user)
else:
logger.warn('don\'t know how to embellish things of type %s',
f_type)
return thing

Wyświetl plik

@ -1,5 +1,9 @@
from django.test import TestCase
from django_kepi.embellish import embellish
from things_for_testing.models import ThingUser
import logging
logger = logging.getLogger(name='django_kepi')
class TestEmbellish(TestCase):
def test_embellish(self):
@ -23,14 +27,23 @@ class TestEmbellish(TestCase):
"sensitive": False,
"inReplyTo": None,
"inReplyToAtomUri": None,
"contentMap":{"en": 'Hello world',},
"contentMap":{"en-us": 'Hello world',},
"attachment":[],
"tag":[],
}
# (plus "published")
# (plus "conversation", wtf?)
self.assertEqual(
embellish(SOURCE),
user = ThingUser(
name = 'Fred',
url = 'https://example.com/users/fred',
)
result = embellish(SOURCE,
user=user)
self.maxDiff = None
self.assertDictContainsSubset(
result,
EXPECTING)

Wyświetl plik

@ -36,6 +36,14 @@ class ThingUser(models.Model):
def activity_id(self):
return self.url
@property
def activity_followers(self):
return self.url+'/followers'
@property
def activity_following(self):
return self.url+'/following'
@classmethod
def activity_find(cls, url):
PREFIX = "https://example.com/user/"