def receive(cls, xml, c_from, u_to): data = cls.as_dict(xml) if DiasporaPost.get_by_guid(data['guid']): return author = DiasporaContact.get_by_username( data['diaspora_handle'], True, False ) assert(author) author = author.contact parent = DiasporaPost.get_by_guid(data['parent_guid']).post assert(parent) if u_to: assert(parent.shared_with(c_from)) assert(parent.shared_with(u_to)) node = xml[0][0] assert(cls.valid_signature(author, data['author_signature'], node)) if 'parent_author_signature' in data: assert( cls.valid_signature( parent.root().author, data['parent_author_signature'], node ) ) p = Post(author=author) p.parent = parent p.add_part(MimePart( type='text/x-markdown', body=data['text'].encode('utf-8'), ), order=0, inline=True) p.tags = cls.find_tags(data['text']) if u_to: p.share_with([p.author, u_to.contact]) else: p.share_with([p.author], show_on_wall=True) if p.author.id != c_from.id: p.share_with([c_from]) p.thread_modified() p.diasp = DiasporaPost( guid=data['guid'], type='limited' if u_to else 'public' ) db.session.add(p) db.session.commit() if not(u_to) or (p.parent.author_id == u_to.contact.id): cls.forward(u_to, p, node)
def receive(cls, xml, c_from, u_to): data = cls.as_dict(xml) if DiasporaPost.get_by_guid(data['guid']): return author = DiasporaContact.get_by_username(data['diaspora_handle'], True, False) assert (author) author = author.contact parent = DiasporaPost.get_by_guid(data['parent_guid']).post assert (parent) if u_to: assert (parent.shared_with(c_from)) assert (parent.shared_with(u_to)) node = xml[0][0] assert (cls.valid_signature(author, data['author_signature'], node)) if 'parent_author_signature' in data: assert (cls.valid_signature(parent.root().author, data['parent_author_signature'], node)) p = Post(author=author) p.parent = parent p.add_part(MimePart( type='text/x-markdown', body=data['text'].encode('utf-8'), ), order=0, inline=True) p.tags = cls.find_tags(data['text']) if u_to: p.share_with([p.author, u_to.contact]) else: p.share_with([p.author], show_on_wall=True) if p.author.id != c_from.id: p.share_with([c_from]) p.thread_modified() p.diasp = DiasporaPost(guid=data['guid'], type='limited' if u_to else 'public') db.session.add(p) db.session.commit() if not (u_to) or (p.parent.author_id == u_to.contact.id): cls.forward(u_to, p, node)
def receive(cls, xml, c_from, u_to): data = cls.as_dict(xml) if DiasporaPost.get_by_guid(data['guid']): return author = DiasporaContact.get_by_username( data['diaspora_handle'], True, False ) assert(author) author = author.contact parent = DiasporaPost.get_by_guid(data['parent_guid']).post assert(parent) assert(parent.shared_with(c_from)) assert(parent.shared_with(u_to)) node = xml[0][0] assert(cls.valid_signature(author, data['author_signature'], node)) if 'parent_author_signature' in data: assert(cls.valid_signature( parent.author, data['parent_author_signature'], node )) created = datetime.strptime(data['created_at'], '%Y-%m-%d %H:%M:%S %Z') p = Post(author=author, created_at=created) p.parent = parent p.add_part(MimePart( type='text/x-markdown', body=data['text'].encode('utf-8'), ), order=0, inline=True) p.tags = cls.find_tags(data['text']) p.share_with([s.contact for s in p.root().shares]) p.thread_modified() p.diasp = DiasporaPost(guid=data['guid'], type='private') db.session.add(p) db.session.commit() if not(u_to) or (p.parent.author_id == u_to.contact.id): cls.forward(u_to, p, node)
def receive(cls, xml, c_from, u_to): data = cls.as_dict(xml) if DiasporaPost.get_by_guid(data['guid']): return author = DiasporaContact.get_by_username(data['diaspora_handle'], True, False) assert (author) author = author.contact parent = DiasporaPost.get_by_guid(data['parent_guid']).post assert (parent) assert (parent.shared_with(c_from)) assert (parent.shared_with(u_to)) node = xml[0][0] assert (cls.valid_signature(author, data['author_signature'], node)) if 'parent_author_signature' in data: assert (cls.valid_signature(parent.author, data['parent_author_signature'], node)) created = datetime.strptime(data['created_at'], '%Y-%m-%d %H:%M:%S %Z') p = Post(author=author, created_at=created) p.parent = parent p.add_part(MimePart( type='text/x-markdown', body=data['text'].encode('utf-8'), ), order=0, inline=True) p.tags = cls.find_tags(data['text']) p.share_with([s.contact for s in p.root().shares]) p.thread_modified() p.diasp = DiasporaPost(guid=data['guid'], type='private') db.session.add(p) db.session.commit() if not (u_to) or (p.parent.author_id == u_to.contact.id): cls.forward(u_to, p, node)
def create(_user): """ Create a new Post and Share it with the selected Contacts. """ body = post_param('body') relationship = { 'type': post_param('relationship_type', optional=True), 'id': post_param('relationship_id', optional=True), } target = { 'type': post_param('target_type'), 'id': post_param('target_id', optional=True), } assert (target['type'] in targets_by_name) # Loathe inflexible HTML forms if target['id'] is None: target['id'] = post_param('target_%s_id' % target['type'], optional=True) if relationship['type']: post = Post.get(relationship['id']) if not post: abort(404, 'No such post', force_status=True) if not post.has_permission_to_view(_user.contact): abort(403, 'Forbidden') relationship['post'] = post shared = None post = Post(author=_user.contact) body_part = MimePart(type='text/x-markdown', body=body.encode('utf-8'), text_preview=None) topics = post_param('tags', optional=True) if topics: post.tags = Tag.parse_line(topics, create=True) if relationship['type'] == 'comment': post.parent = relationship['post'] post.add_part(body_part, order=0, inline=True) elif relationship['type'] == 'share': shared = relationship['post'] share_part = MimePart(type='application/x-pyaspora-share', body=dumps({ 'post': { 'id': shared.id }, 'author': { 'id': shared.author_id, 'name': shared.author.realname, } }).encode('utf-8'), text_preview=u"shared {0}'s post".format( shared.author.realname)) post.add_part(share_part, order=0, inline=True) post.add_part(body_part, order=1, inline=True) order = 1 for part in shared.parts: if part.mime_part.type != 'application/x-pyaspora-share': order += 1 post.add_part(part.mime_part, inline=part.inline, order=order) if not post.tags: post.tags = shared.tags else: # Naked post post.add_part(body_part, order=0, inline=True) attachment = request.files.get('attachment', None) if attachment and attachment.filename: check_attachment_is_safe(attachment) attachment_part = MimePart(type=attachment.mimetype, body=attachment.stream.read(), text_preview=attachment.filename) post.add_part(attachment_part, order=1, inline=bool(renderer_exists(attachment.mimetype))) post.thread_modified() # Sigh, need an ID for the post for making shares db.session.add(post) db.session.commit() targets_by_name[target['type']].make_shares(post, target['id'], reshare_of=shared) db.session.commit() data = json_post(post) return redirect(url_for('feed.view', _external=True), data_structure=data)
def create(_user): """ Create a new Post and Share it with the selected Contacts. """ body = post_param('body') relationship = { 'type': post_param('relationship_type', optional=True), 'id': post_param('relationship_id', optional=True), } target = { 'type': post_param('target_type'), 'id': post_param('target_id', optional=True), } assert(target['type'] in targets_by_name) # Loathe inflexible HTML forms if target['id'] is None: target['id'] = post_param( 'target_%s_id' % target['type'], optional=True) if relationship['type']: post = Post.get(relationship['id']) if not post: abort(404, 'No such post', force_status=True) if not post.has_permission_to_view(_user.contact): abort(403, 'Forbidden') relationship['post'] = post post = Post(author=_user.contact) body_part = MimePart(type='text/x-markdown', body=body.encode('utf-8'), text_preview=None) topics = post_param('tags', optional=True) if topics: post.tags = Tag.parse_line(topics, create=True) if relationship['type'] == 'comment': post.parent = relationship['post'] post.add_part(body_part, order=0, inline=True) elif relationship['type'] == 'share': shared = relationship['post'] share_part = MimePart( type='application/x-pyaspora-share', body=dumps({ 'post': {'id': shared.id}, 'author': { 'id': shared.author_id, 'name': shared.author.realname, } }).encode('utf-8'), text_preview="shared {0}'s post".format(shared.author.realname) ) post.add_part(share_part, order=0, inline=True) post.add_part(body_part, order=1, inline=True) order = 1 for part in shared.parts: if part.mime_part.type != 'application/x-pyaspora-share': order += 1 post.add_part(part.mime_part, inline=part.inline, order=order) if not post.tags: post.tags = shared.tags else: # Naked post post.add_part(body_part, order=0, inline=True) attachment = request.files.get('attachment', None) if attachment and attachment.filename: check_attachment_is_safe(attachment) attachment_part = MimePart( type=attachment.mimetype, body=attachment.stream.read(), text_preview=attachment.filename ) post.add_part(attachment_part, order=1, inline=bool(renderer_exists(attachment.mimetype))) post.thread_modified() # Sigh, need an ID for the post for making shares db.session.add(post) db.session.commit() targets_by_name[target['type']].make_shares(post, target['id']) db.session.commit() data = json_post(post) return redirect(url_for('feed.view', _external=True), data_structure=data)