kopia lustrzana https://github.com/snarfed/bridgy-fed
				
				
				
			Object.get_or_create: merge repeated fields instead of overwriting
							rodzic
							
								
									525a26f7b6
								
							
						
					
					
						commit
						8796b9edcd
					
				|  | @ -1132,6 +1132,11 @@ class Object(StringIdModel): | |||
| 
 | ||||
|         if set(props.keys()) & set(('as2', 'bsky', 'mf2', 'raw')): | ||||
|             obj.clear() | ||||
| 
 | ||||
|         # merge repeated fields | ||||
|         for field in 'feed', 'copies', 'labels', 'notify', 'users': | ||||
|             getattr(obj, field).extend(props.pop(field, [])) | ||||
| 
 | ||||
|         obj.populate(**{ | ||||
|             k: v for k, v in props.items() | ||||
|             if v and not isinstance(getattr(Object, k), ndb.ComputedProperty) | ||||
|  |  | |||
|  | @ -933,7 +933,11 @@ class Protocol: | |||
|             error(f'Actor {actor} is opted out or blocked', status=204) | ||||
| 
 | ||||
|         # write Object to datastore | ||||
|         orig_props = obj.to_dict() | ||||
|         orig_props = { | ||||
|             **obj.to_dict(), | ||||
|             # structured properties | ||||
|             'copies': obj.copies, | ||||
|         } | ||||
|         obj = Object.get_or_create(id, new=obj.new, changed=obj.changed, | ||||
|                                    authed_as=actor, **orig_props) | ||||
| 
 | ||||
|  |  | |||
|  | @ -637,11 +637,23 @@ class ObjectTest(TestCase): | |||
|         obj7 = Object.get_or_create('http://b.ee/ff', as2={'a': 'b'}, mf2={'c': 'd'}, | ||||
|                                     source_protocol='web') | ||||
|         Object.get_or_create('http://b.ee/ff', authed_as='http://b.ee/ff', | ||||
|                              users=[ndb.Key(Web, 'me')]) | ||||
|                              users=[ndb.Key(Web, 'me')], labels=['feed'], | ||||
|                              copies=[Target(protocol='ui', uri='http://foo')]) | ||||
|         self.assert_object('http://b.ee/ff', as2={'a': 'b'}, mf2={'c': 'd'}, | ||||
|                            users=[ndb.Key(Web, 'me')], | ||||
|                            users=[ndb.Key(Web, 'me')], labels=['feed'], | ||||
|                            copies=[Target(protocol='ui', uri='http://foo')], | ||||
|                            source_protocol='web') | ||||
| 
 | ||||
|         # repeated properties should merge, not overwrite | ||||
|         Object.get_or_create('http://b.ee/ff', authed_as='http://b.ee/ff', | ||||
|                              users=[ndb.Key(Web, 'you')], labels=['user'], | ||||
|                              copies=[Target(protocol='ui', uri='http://bar')]) | ||||
|         self.assert_object('http://b.ee/ff', as2={'a': 'b'}, mf2={'c': 'd'}, | ||||
|                            users=[ndb.Key(Web, 'me'), ndb.Key(Web, 'you')], | ||||
|                            labels=['feed', 'user'], source_protocol='web', | ||||
|                            copies=[Target(protocol='ui', uri='http://foo'), | ||||
|                                    Target(protocol='ui', uri='http://bar')]) | ||||
| 
 | ||||
|     def test_get_or_create_auth_check(self): | ||||
|         Object(id='fake:foo', our_as1={'author': 'fake:alice'}, | ||||
|                source_protocol='fake').put() | ||||
|  |  | |||
		Ładowanie…
	
		Reference in New Issue
	
	 Ryan Barrett
						Ryan Barrett