Mercurial > libervia-backend
comparison src/plugins/plugin_misc_groupblog.py @ 1425:7f8af1e69812
core (tests): fixed bad use of reserved name
author | Goffi <goffi@goffi.org> |
---|---|
date | Thu, 23 Apr 2015 15:16:06 +0200 |
parents | 882e5fabf68c |
children | 226bbe758b1a |
comparison
equal
deleted
inserted
replaced
1424:2d8fccec84e8 | 1425:7f8af1e69812 |
---|---|
24 from twisted.internet import defer | 24 from twisted.internet import defer |
25 from twisted.words.protocols.jabber import jid | 25 from twisted.words.protocols.jabber import jid |
26 from twisted.words.xish.domish import Element, generateElementsNamed | 26 from twisted.words.xish.domish import Element, generateElementsNamed |
27 from sat.core import exceptions | 27 from sat.core import exceptions |
28 from wokkel import disco, data_form, iwokkel | 28 from wokkel import disco, data_form, iwokkel |
29 from wokkel import rsm as wokkel_rsm | 29 from wokkel import rsm |
30 from zope.interface import implements | 30 from zope.interface import implements |
31 from feed import date | 31 from feed import date |
32 import uuid | 32 import uuid |
33 import urllib | 33 import urllib |
34 | 34 |
513 | 513 |
514 assert max_ >= 0 | 514 assert max_ >= 0 |
515 d_list = [] | 515 d_list = [] |
516 for entry in items: | 516 for entry in items: |
517 if entry.get('comments', False): | 517 if entry.get('comments', False): |
518 comments_rsm = {'max': max_} | 518 comments_rsm = {'max_': max_} |
519 d = self.getGroupBlogComments(entry['comments_service'], entry['comments_node'], rsm=comments_rsm, profile_key=profile_key) | 519 d = self.getGroupBlogComments(entry['comments_service'], entry['comments_node'], rsm_data=comments_rsm, profile_key=profile_key) |
520 d.addCallback(comments_cb, entry) | 520 d.addCallback(comments_cb, entry) |
521 d_list.append(d) | 521 d_list.append(d) |
522 else: | 522 else: |
523 if max_ > 0: | 523 if max_ > 0: |
524 d_list.append(defer.succeed((entry, ([], {})))) | 524 d_list.append(defer.succeed((entry, ([], {})))) |
526 d_list.append(defer.succeed(entry)) | 526 d_list.append(defer.succeed(entry)) |
527 deferred_list = defer.DeferredList(d_list) | 527 deferred_list = defer.DeferredList(d_list) |
528 deferred_list.addCallback(lambda result: [value for (success, value) in result if success]) | 528 deferred_list.addCallback(lambda result: [value for (success, value) in result if success]) |
529 return deferred_list | 529 return deferred_list |
530 | 530 |
531 def _getGroupBlogs(self, pub_jid_s, item_ids=None, rsm=None, max_comments=0, profile_key=C.PROF_KEY_NONE): | 531 def _getGroupBlogs(self, pub_jid_s, item_ids=None, rsm_data=None, max_comments=0, profile_key=C.PROF_KEY_NONE): |
532 """Retrieve previously published items from a publish subscribe node. | 532 """Retrieve previously published items from a publish subscribe node. |
533 | 533 |
534 @param pub_jid_s: jid of the publisher | 534 @param pub_jid_s: jid of the publisher |
535 @param item_ids: list of microblogs items IDs | 535 @param item_ids: list of microblogs items IDs |
536 @param rsm (dict): RSM request data | 536 @param rsm_data (dict): RSM request data |
537 @param max_comments (int): maximum number of comments to retrieve | 537 @param max_comments (int): maximum number of comments to retrieve |
538 @param profile_key (str): %(doc_profile_key)s | 538 @param profile_key (str): %(doc_profile_key)s |
539 @return: a deferred couple (list, dict) containing: | 539 @return: a deferred couple (list, dict) containing: |
540 - list of: | 540 - list of: |
541 - if max_comments == 0: microblog data | 541 - if max_comments == 0: microblog data |
550 d = self._itemsConstruction(items, pub_jid, client) | 550 d = self._itemsConstruction(items, pub_jid, client) |
551 if max_comments == DO_NOT_COUNT_COMMENTS: | 551 if max_comments == DO_NOT_COUNT_COMMENTS: |
552 return d | 552 return d |
553 return d.addCallback(self._getOrCountComments, max_comments, profile_key) | 553 return d.addCallback(self._getOrCountComments, max_comments, profile_key) |
554 | 554 |
555 return DeferredItems(self, cb, None, profile_key).get(self.getNodeName(pub_jid), item_ids, rsm=rsm) | 555 return DeferredItems(self, cb, None, profile_key).get(self.getNodeName(pub_jid), item_ids, rsm_data=rsm_data) |
556 | 556 |
557 def getGroupBlogs(self, pub_jid_s, item_ids=None, rsm=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): |
558 """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 |
559 None, the result would be the same than calling getGroupBlogs | 559 None, the result would be the same than calling getGroupBlogs |
560 with the default value for the attribute max_items. | 560 with the default value for the attribute max_items. |
561 | 561 |
562 @param pub_jid_s: jid of the publisher | 562 @param pub_jid_s: jid of the publisher |
563 @param item_ids: list of microblogs items IDs | 563 @param item_ids: list of microblogs items IDs |
564 @param rsm (dict): RSM request data | 564 @param rsm_data (dict): RSM request data |
565 @param count_comments (bool): also count the comments if True | 565 @param count_comments (bool): also count the comments if True |
566 @param profile_key (str): %(doc_profile_key)s | 566 @param profile_key (str): %(doc_profile_key)s |
567 @return: a deferred couple (list, dict) containing: | 567 @return: a deferred couple (list, dict) containing: |
568 - list of microblog data | 568 - list of microblog data |
569 - RSM response data | 569 - RSM response data |
570 """ | 570 """ |
571 max_comments = 0 if count_comments else DO_NOT_COUNT_COMMENTS | 571 max_comments = 0 if count_comments else DO_NOT_COUNT_COMMENTS |
572 return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm=rsm, 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) |
573 | 573 |
574 def getGroupBlogsWithComments(self, pub_jid_s, item_ids=None, rsm=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): |
575 """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 |
576 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. |
577 | 577 |
578 @param pub_jid_s: jid of the publisher | 578 @param pub_jid_s: jid of the publisher |
579 @param item_ids: list of microblogs items IDs | 579 @param item_ids: list of microblogs items IDs |
587 - RSM response data | 587 - RSM response data |
588 """ | 588 """ |
589 if max_comments is None: | 589 if max_comments is None: |
590 max_comments = MAX_COMMENTS | 590 max_comments = MAX_COMMENTS |
591 assert max_comments > 0 # otherwise the return signature is not the same | 591 assert max_comments > 0 # otherwise the return signature is not the same |
592 return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm=rsm, max_comments=max_comments, profile_key=profile_key) | 592 return self._getGroupBlogs(pub_jid_s, item_ids=item_ids, rsm_data=rsm_data, max_comments=max_comments, profile_key=profile_key) |
593 | 593 |
594 def getGroupBlogsAtom(self, pub_jid_s, rsm=None, profile_key=C.PROF_KEY_NONE): | 594 def getGroupBlogsAtom(self, pub_jid_s, rsm_data=None, profile_key=C.PROF_KEY_NONE): |
595 """Get the atom feed of the last published microblogs | 595 """Get the atom feed of the last published microblogs |
596 @param pub_jid: jid of the publisher | 596 @param pub_jid: jid of the publisher |
597 @param profile_key: profile key | 597 @param profile_key: profile key |
598 @return: a deferred unicode (atom XML feed) | 598 @return: a deferred unicode (atom XML feed) |
599 """ | 599 """ |
627 return feed + "</feed>" | 627 return feed + "</feed>" |
628 | 628 |
629 def cb(items, client): | 629 def cb(items, client): |
630 return items2feed(items, pub_jid, client) | 630 return items2feed(items, pub_jid, client) |
631 | 631 |
632 d = DeferredItems(self, cb, lambda dummy: [''], profile_key).get(self.getNodeName(pub_jid), rsm=rsm) | 632 d = DeferredItems(self, cb, lambda dummy: [''], profile_key).get(self.getNodeName(pub_jid), rsm_data=rsm_data) |
633 return d.addCallback(lambda res: res[0]) | 633 return d.addCallback(lambda res: res[0]) |
634 | 634 |
635 def getGroupBlogComments(self, service_s, node, rsm=None, profile_key=C.PROF_KEY_NONE): | 635 def getGroupBlogComments(self, service_s, node, rsm_data=None, profile_key=C.PROF_KEY_NONE): |
636 """Get all comments of given node | 636 """Get all comments of given node |
637 @param service_s: service hosting the node | 637 @param service_s: service hosting the node |
638 @param node: comments node | 638 @param node: comments node |
639 @param profile_key: profile key | 639 @param profile_key: profile key |
640 @return: a deferred couple (list, dict) containing: | 640 @return: a deferred couple (list, dict) containing: |
644 service = jid.JID(service_s) | 644 service = jid.JID(service_s) |
645 | 645 |
646 def cb(items, client): | 646 def cb(items, client): |
647 return self._handleCommentsItems(items, service, node) | 647 return self._handleCommentsItems(items, service, node) |
648 | 648 |
649 return DeferredItems(self, cb, None, profile_key).get(node, rsm=rsm) | 649 return DeferredItems(self, cb, None, profile_key).get(node, rsm_data=rsm_data) |
650 | 650 |
651 def _getMassiveGroupBlogs(self, publishers_type, publishers, rsm=None, profile_key=C.PROF_KEY_NONE): | 651 def _getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE): |
652 if publishers_type == 'JID': | 652 if publishers_type == 'JID': |
653 publishers_jids = [jid.JID(publisher) for publisher in publishers] | 653 publishers_jids = [jid.JID(publisher) for publisher in publishers] |
654 else: | 654 else: |
655 publishers_jids = publishers | 655 publishers_jids = publishers |
656 return self.getMassiveGroupBlogs(publishers_type, publishers_jids, rsm, profile_key) | 656 return self.getMassiveGroupBlogs(publishers_type, publishers_jids, rsm_data, profile_key) |
657 | 657 |
658 def _getPublishersJIDs(self, publishers_type, publishers, client): | 658 def _getPublishersJIDs(self, publishers_type, publishers, client): |
659 #TODO: custom exception | 659 #TODO: custom exception |
660 if publishers_type not in ["GROUP", "JID", "ALL"]: | 660 if publishers_type not in ["GROUP", "JID", "ALL"]: |
661 raise Exception("Bad call, unknown publishers_type") | 661 raise Exception("Bad call, unknown publishers_type") |
673 jids = publishers | 673 jids = publishers |
674 else: | 674 else: |
675 raise UnknownType | 675 raise UnknownType |
676 return jids | 676 return jids |
677 | 677 |
678 def getMassiveGroupBlogs(self, publishers_type, publishers, rsm=None, profile_key=C.PROF_KEY_NONE): | 678 def getMassiveGroupBlogs(self, publishers_type, publishers, rsm_data=None, profile_key=C.PROF_KEY_NONE): |
679 """Get the last published microblogs for a list of groups or jids | 679 """Get the last published microblogs for a list of groups or jids |
680 @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") | 680 @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") |
681 @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) | 681 @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) |
682 @param rsm (dict): RSM request data, common to all publishers | 682 @param rsm_data (dict): RSM request data, common to all publishers |
683 @param profile_key: profile key | 683 @param profile_key: profile key |
684 @return: a deferred dict with: | 684 @return: a deferred dict with: |
685 - key: publisher (unicode) | 685 - key: publisher (unicode) |
686 - value: couple (list[dict], dict) with: | 686 - value: couple (list[dict], dict) with: |
687 - the microblogs data | 687 - the microblogs data |
690 def cb(items, publisher, client): | 690 def cb(items, publisher, client): |
691 d = self._itemsConstruction(items, publisher, client) | 691 d = self._itemsConstruction(items, publisher, client) |
692 return d.addCallback(self._getOrCountComments, False, profile_key) | 692 return d.addCallback(self._getOrCountComments, False, profile_key) |
693 | 693 |
694 #TODO: we need to use the server corresponding to the host of the jid | 694 #TODO: we need to use the server corresponding to the host of the jid |
695 return DeferredItemsFromMany(self, cb, profile_key).get(publishers_type, publishers, rsm=rsm) | 695 return DeferredItemsFromMany(self, cb, profile_key).get(publishers_type, publishers, rsm_data=rsm_data) |
696 | 696 |
697 ## subscribe ## | 697 ## subscribe ## |
698 | 698 |
699 def subscribeGroupBlog(self, pub_jid, profile_key=C.PROF_KEY_NONE): | 699 def subscribeGroupBlog(self, pub_jid, profile_key=C.PROF_KEY_NONE): |
700 def initialised(result): | 700 def initialised(result): |
897 self.parent = parent | 897 self.parent = parent |
898 self.cb = cb | 898 self.cb = cb |
899 self.eb = (lambda dummy: ([], {})) if eb is None else eb | 899 self.eb = (lambda dummy: ([], {})) if eb is None else eb |
900 self.profile_key = profile_key | 900 self.profile_key = profile_key |
901 | 901 |
902 def get(self, node, item_ids=None, sub_id=None, rsm=None): | 902 def get(self, node, item_ids=None, sub_id=None, rsm_data=None): |
903 """Retrieve and process a page of pubsub items | 903 """Retrieve and process a page of pubsub items |
904 | 904 |
905 @param node (str): node identifier. | 905 @param node (str): node identifier. |
906 @param item_ids (list[str]): list of items identifiers. | 906 @param item_ids (list[str]): list of items identifiers. |
907 @param sub_id (str): optional subscription identifier. | 907 @param sub_id (str): optional subscription identifier. |
908 @param rsm (dict): RSM request data | 908 @param rsm_data (dict): RSM request data |
909 @return: a deferred couple (list, dict) containing: | 909 @return: a deferred couple (list, dict) containing: |
910 - list of microblog data | 910 - list of microblog data |
911 - RSM response data | 911 - RSM response data |
912 """ | 912 """ |
913 if rsm is None: | 913 if rsm_data is None: |
914 rsm = {'max': (len(item_ids) if item_ids else MAX_ITEMS)} | 914 rsm_data = {'max_': (len(item_ids) if item_ids else MAX_ITEMS)} |
915 | 915 |
916 def initialised(result): | 916 def initialised(result): |
917 profile, client = result | 917 profile, client = result |
918 rsm_ = wokkel_rsm.RSMRequest(**rsm) | 918 rsm_request = rsm.RSMRequest(**rsm_data) |
919 d = self.parent.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, | 919 d = self.parent.host.plugins["XEP-0060"].getItems(client.item_access_pubsub, |
920 node, rsm_.max, | 920 node, rsm_request.max, |
921 item_ids, sub_id, rsm_, | 921 item_ids, sub_id, rsm_request, |
922 profile_key=profile) | 922 profile_key=profile) |
923 | 923 |
924 def cb(result): | 924 def cb(result): |
925 d = defer.maybeDeferred(self.cb, result[0], client) | 925 d = defer.maybeDeferred(self.cb, result[0], client) |
926 return d.addCallback(lambda items: (items, result[1])) | 926 return d.addCallback(lambda items: (items, result[1])) |
945 | 945 |
946 def _buildData(self, publishers_type, publishers, client): | 946 def _buildData(self, publishers_type, publishers, client): |
947 jids = self.parent._getPublishersJIDs(publishers_type, publishers, client) | 947 jids = self.parent._getPublishersJIDs(publishers_type, publishers, client) |
948 return {publisher: self.parent.getNodeName(publisher) for publisher in jids} | 948 return {publisher: self.parent.getNodeName(publisher) for publisher in jids} |
949 | 949 |
950 def get(self, publishers_type, publishers, sub_id=None, rsm=None): | 950 def get(self, publishers_type, publishers, sub_id=None, rsm_data=None): |
951 """Retrieve and process a page of pubsub items | 951 """Retrieve and process a page of pubsub items |
952 | 952 |
953 @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") | 953 @param publishers_type (str): type of the list of publishers (one of "GROUP" or "JID" or "ALL") |
954 @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) | 954 @param publishers (list): list of publishers, according to publishers_type (list of groups or list of jids) |
955 @param sub_id (str): optional subscription identifier. | 955 @param sub_id (str): optional subscription identifier. |
956 @param rsm (dict): RSM request data | 956 @param rsm_data (dict): RSM request data |
957 @return: a deferred dict with: | 957 @return: a deferred dict with: |
958 - key: publisher (unicode) | 958 - key: publisher (unicode) |
959 - value: couple (list[dict], dict) with: | 959 - value: couple (list[dict], dict) with: |
960 - the microblogs data | 960 - the microblogs data |
961 - RSM response data | 961 - RSM response data |
962 """ | 962 """ |
963 if rsm is None: | 963 if rsm_data is None: |
964 rsm = {'max': MAX_ITEMS} | 964 rsm_data = {'max_': MAX_ITEMS} |
965 | 965 |
966 def initialised(result): | 966 def initialised(result): |
967 profile, client = result | 967 profile, client = result |
968 | 968 |
969 data = self._buildData(publishers_type, publishers, client) | 969 data = self._buildData(publishers_type, publishers, client) |
970 rsm_ = wokkel_rsm.RSMRequest(**rsm) | 970 rsm_request = rsm.RSMRequest(**rsm_data) |
971 d = self.parent.host.plugins["XEP-0060"].getItemsFromMany(client.item_access_pubsub, | 971 d = self.parent.host.plugins["XEP-0060"].getItemsFromMany(client.item_access_pubsub, |
972 data, rsm_.max, sub_id, | 972 data, rsm_request.max, sub_id, |
973 rsm_, profile_key=profile) | 973 rsm_request, profile_key=profile) |
974 | 974 |
975 def cb(publisher): | 975 def cb(publisher): |
976 def callback(result): | 976 def callback(result): |
977 d = defer.maybeDeferred(self.cb, result[0], publisher, client) | 977 d = defer.maybeDeferred(self.cb, result[0], publisher, client) |
978 d.addCallback(lambda items: (publisher.full(), (items, result[1]))) | 978 d.addCallback(lambda items: (publisher.full(), (items, result[1]))) |