comparison src/plugins/plugin_misc_groupblog.py @ 1454:4e2fab4de195

plugin XEP-0277: added mBGetFromManyWithComments to have items + comments in one method call
author Goffi <goffi@goffi.org>
date Sat, 15 Aug 2015 22:24:38 +0200
parents 5116d70ddd1c
children 4c4f88d7b156
comparison
equal deleted inserted replaced
1453:d5e72362ee91 1454:4e2fab4de195
106 106
107 host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='ssa{ss}s', out_sign='', 107 host.bridge.addMethod("sendGroupBlogComment", ".plugin", in_sign='ssa{ss}s', out_sign='',
108 method=self.sendGroupBlogComment, 108 method=self.sendGroupBlogComment,
109 async=True) 109 async=True)
110 110
111 host.bridge.addMethod("getGroupBlogs", ".plugin", 111 # host.bridge.addMethod("getGroupBlogs", ".plugin",
112 in_sign='sasa{ss}bs', out_sign='(aa{ss}a{ss})', 112 # in_sign='sasa{ss}bs', out_sign='(aa{ss}a{ss})',
113 method=self.getGroupBlogs, 113 # method=self.getGroupBlogs,
114 async=True) 114 # async=True)
115 115
116 host.bridge.addMethod("getGroupBlogsWithComments", ".plugin", 116 host.bridge.addMethod("getGroupBlogsWithComments", ".plugin",
117 in_sign='sasa{ss}is', out_sign='(a(a{ss}(aa{ss}a{ss}))a{ss})', 117 in_sign='sasa{ss}is', out_sign='(a(a{ss}(aa{ss}a{ss}))a{ss})',
118 method=self.getGroupBlogsWithComments, 118 method=self.getGroupBlogsWithComments,
119 async=True) 119 async=True)
121 host.bridge.addMethod("getGroupBlogsAtom", ".plugin", 121 host.bridge.addMethod("getGroupBlogsAtom", ".plugin",
122 in_sign='sa{ss}s', out_sign='s', 122 in_sign='sa{ss}s', out_sign='s',
123 method=self.getGroupBlogsAtom, 123 method=self.getGroupBlogsAtom,
124 async=True) 124 async=True)
125 125
126 host.bridge.addMethod("getMassiveGroupBlogs", ".plugin", 126 # host.bridge.addMethod("getMassiveGroupBlogs", ".plugin",
127 in_sign='sasa{ss}s', out_sign='a{s(aa{ss}a{ss})}', 127 # in_sign='sasa{ss}s', out_sign='a{s(aa{ss}a{ss})}',
128 method=self._getMassiveGroupBlogs, 128 # method=self._getMassiveGroupBlogs,
129 async=True) 129 # async=True)
130 130
131 host.bridge.addMethod("getGroupBlogComments", ".plugin", 131 # host.bridge.addMethod("getGroupBlogComments", ".plugin",
132 in_sign='ssa{ss}s', out_sign='(aa{ss}a{ss})', 132 # in_sign='ssa{ss}s', out_sign='(aa{ss}a{ss})',
133 method=self.getGroupBlogComments, 133 # method=self.getGroupBlogComments,
134 async=True) 134 # async=True)
135 135
136 host.bridge.addMethod("subscribeGroupBlog", ".plugin", in_sign='ss', out_sign='', 136 # host.bridge.addMethod("subscribeGroupBlog", ".plugin", in_sign='ss', out_sign='',
137 method=self.subscribeGroupBlog, 137 # method=self.subscribeGroupBlog,
138 async=True) 138 # async=True)
139 139
140 host.trigger.add("PubSubItemsReceived", self.pubSubItemsReceivedTrigger) 140 host.trigger.add("PubSubItemsReceived", self.pubSubItemsReceivedTrigger)
141 141
142 ## plugin management methods ## 142 ## plugin management methods ##
143 143
243 243
244 if (form.formNamespace == NS_PUBSUB_ITEM_CONFIG): 244 if (form.formNamespace == NS_PUBSUB_ITEM_CONFIG):
245 access_model = form.get(P.OPT_ACCESS_MODEL, 'open') 245 access_model = form.get(P.OPT_ACCESS_MODEL, 'open')
246 if access_model == "roster": 246 if access_model == "roster":
247 try: 247 try:
248 # FIXME: groups are xs:string, so they can contain "\n" ! This code is bugged
248 microblog_data["groups"] = '\n'.join(form.fields[P.OPT_ROSTER_GROUPS_ALLOWED].values) 249 microblog_data["groups"] = '\n'.join(form.fields[P.OPT_ROSTER_GROUPS_ALLOWED].values)
249 except KeyError: 250 except KeyError:
250 log.warning("No group found for roster access-model") 251 log.warning("No group found for roster access-model")
251 microblog_data["groups"] = '' 252 microblog_data["groups"] = ''
252 253
551 return d 552 return d
552 return d.addCallback(self._getOrCountComments, max_comments, profile_key) 553 return d.addCallback(self._getOrCountComments, max_comments, profile_key)
553 554
554 return DeferredItems(self, cb, None, profile_key).get(self.getNodeName(pub_jid), item_ids, rsm_data=rsm_data) 555 return DeferredItems(self, cb, None, profile_key).get(self.getNodeName(pub_jid), item_ids, rsm_data=rsm_data)
555 556
556 def getGroupBlogs(self, pub_jid_s, item_ids=None, rsm_data=None, count_comments=True, profile_key=C.PROF_KEY_NONE): 557 # def getGroupBlogs(self, pub_jid_s, item_ids=None, rsm_data=None, count_comments=True, profile_key=C.PROF_KEY_NONE):
557 """Get the published microblogs of the specified IDs. If item_ids is 558 # """Get the published microblogs of the specified IDs. If item_ids is
558 None, the result would be the same than calling getGroupBlogs 559 # None, the result would be the same than calling getGroupBlogs
559 with the default value for the attribute max_items. 560 # with the default value for the attribute max_items.
560 561
561 @param pub_jid_s: jid of the publisher 562 # @param pub_jid_s: jid of the publisher
562 @param item_ids: list of microblogs items IDs 563 # @param item_ids: list of microblogs items IDs
563 @param rsm_data (dict): RSM request data 564 # @param rsm_data (dict): RSM request data
564 @param count_comments (bool): also count the comments if True 565 # @param count_comments (bool): also count the comments if True
565 @param profile_key (str): %(doc_profile_key)s 566 # @param profile_key (str): %(doc_profile_key)s
566 @return: a deferred couple (list, dict) containing: 567 # @return: a deferred couple (list, dict) containing:
567 - list of microblog data 568 # - list of microblog data
568 - RSM response data 569 # - RSM response data
569 """ 570 # """
570 max_comments = 0 if count_comments else DO_NOT_COUNT_COMMENTS 571 # max_comments = 0 if count_comments else DO_NOT_COUNT_COMMENTS
571 return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm_data=rsm_data, max_comments=max_comments, profile_key=profile_key) 572 # return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm_data=rsm_data, max_comments=max_comments, profile_key=profile_key)
572 573
573 def getGroupBlogsWithComments(self, pub_jid_s, item_ids=None, rsm_data=None, max_comments=None, profile_key=C.PROF_KEY_NONE): 574 def getGroupBlogsWithComments(self, pub_jid_s, item_ids=None, rsm_data=None, max_comments=None, profile_key=C.PROF_KEY_NONE):
574 """Get the published microblogs of the specified IDs and their comments. If 575 """Get the published microblogs of the specified IDs and their comments. If
575 item_ids is None, returns the last published microblogs and their comments. 576 item_ids is None, returns the last published microblogs and their comments.
576 577
629 return items2feed(items, pub_jid, client) 630 return items2feed(items, pub_jid, client)
630 631
631 d = DeferredItems(self, cb, lambda dummy: [''], profile_key).get(self.getNodeName(pub_jid), rsm_data=rsm_data) 632 d = DeferredItems(self, cb, lambda dummy: [''], profile_key).get(self.getNodeName(pub_jid), rsm_data=rsm_data)
632 return d.addCallback(lambda res: res[0]) 633 return d.addCallback(lambda res: res[0])
633 634
634 def getGroupBlogComments(self, service_s, node, rsm_data=None, profile_key=C.PROF_KEY_NONE): 635 # def _getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE):
635 """Get all comments of given node 636 # if publishers_type == 'JID':
636 @param service_s: service hosting the node 637 # publishers_jids = [jid.JID(publisher) for publisher in publishers]
637 @param node: comments node 638 # else:
638 @param profile_key: profile key 639 # publishers_jids = publishers
639 @return: a deferred couple (list, dict) containing: 640 # return self.getMassiveGroupBlogs(publishers_type, publishers_jids, rsm_data, profile_key)
640 - list of microblog data
641 - RSM response data
642 """
643 service = jid.JID(service_s)
644
645 def cb(items, client):
646 return self._handleCommentsItems(items, service, node)
647
648 return DeferredItems(self, cb, None, profile_key).get(node, rsm_data=rsm_data)
649
650 def _getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE):
651 if publishers_type == 'JID':
652 publishers_jids = [jid.JID(publisher) for publisher in publishers]
653 else:
654 publishers_jids = publishers
655 return self.getMassiveGroupBlogs(publishers_type, publishers_jids, rsm_data, profile_key)
656 641
657 # def _getPublishersJIDs(self, publishers_type, publishers, client): 642 # def _getPublishersJIDs(self, publishers_type, publishers, client):
658 # #TODO: custom exception 643 # #TODO: custom exception
659 # if publishers_type not in ["GROUP", "JID", "ALL"]: 644 # if publishers_type not in ["GROUP", "JID", "ALL"]:
660 # raise Exception("Bad call, unknown publishers_type") 645 # raise Exception("Bad call, unknown publishers_type")
672 # jids = publishers 657 # jids = publishers
673 # else: 658 # else:
674 # raise UnknownType 659 # raise UnknownType
675 # return jids 660 # return jids
676 661
677 def getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE): 662 # def getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE):
678 """Get the last published microblogs for a list of groups or jids 663 # """Get the last published microblogs for a list of groups or jids
679 @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") 664 # @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL")
680 @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) 665 # @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids)
681 @param rsm_data (dict): RSM request data, common to all publishers 666 # @param rsm_data (dict): RSM request data, common to all publishers
682 @param profile_key: profile key 667 # @param profile_key: profile key
683 @return: a deferred dict with: 668 # @return: a deferred dict with:
684 - key: publisher (unicode) 669 # - key: publisher (unicode)
685 - value: couple (list[dict], dict) with: 670 # - value: couple (list[dict], dict) with:
686 - the microblogs data 671 # - the microblogs data
687 - RSM response data 672 # - RSM response data
688 """ 673 # """
689 def cb(items, publisher, client): 674 # def cb(items, publisher, client):
690 d = self._itemsConstruction(items, publisher, client) 675 # d = self._itemsConstruction(items, publisher, client)
691 return d.addCallback(self._getOrCountComments, False, profile_key) 676 # return d.addCallback(self._getOrCountComments, False, profile_key)
692 677
693 #TODO: we need to use the server corresponding to the host of the jid 678 # #TODO: we need to use the server corresponding to the host of the jid
694 return DeferredItemsFromMany(self, cb, profile_key).get(publishers_type, publishers, rsm_data=rsm_data) 679 # return DeferredItemsFromMany(self, cb, profile_key).get(publishers_type, publishers, rsm_data=rsm_data)
695 680
696 ## subscribe ## 681 ## subscribe ##
697 682
698 def subscribeGroupBlog(self, pub_jid, profile_key=C.PROF_KEY_NONE): 683 # def subscribeGroupBlog(self, pub_jid, profile_key=C.PROF_KEY_NONE):
699 def initialised(result): 684 # def initialised(result):
700 profile, client = result 685 # profile, client = result
701 d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)), 686 # d = self.host.plugins["XEP-0060"].subscribe(client.item_access_pubsub, self.getNodeName(jid.JID(pub_jid)),
702 profile_key=profile_key) 687 # profile_key=profile_key)
703 return d 688 # return d
704 689
705 #TODO: we need to use the server corresponding the the host of the jid 690 # #TODO: we need to use the server corresponding the the host of the jid
706 return self._initialise(profile_key).addCallback(initialised) 691 # return self._initialise(profile_key).addCallback(initialised)
707 692
708 693
709 ## delete ## 694 ## delete ##
710 695
711 def deleteGroupBlog(self, pub_data, comments, profile_key=C.PROF_KEY_NONE): 696 def deleteGroupBlog(self, pub_data, comments, profile_key=C.PROF_KEY_NONE):