comparison sat_pubsub/pgsql_storage.py @ 278:8a71486c3e95

implements RSM (XEP-0059)
author souliane <souliane@mailoo.org>
date Mon, 13 Oct 2014 14:53:42 +0200
parents b757c29b20d7
children 7d54ff2eeaf2
comparison
equal deleted inserted replaced
277:e749401be529 278:8a71486c3e95
606 deleted.append(itemIdentifier) 606 deleted.append(itemIdentifier)
607 607
608 return deleted 608 return deleted
609 609
610 610
611 def getItems(self, authorized_groups, unrestricted, maxItems=None): 611 def getItems(self, authorized_groups, unrestricted, maxItems=None, ext_data=None):
612 """ Get all authorised items 612 """ Get all authorised items
613 @param authorized_groups: we want to get items that these groups can access 613 @param authorized_groups: we want to get items that these groups can access
614 @param unrestricted: if true, don't check permissions (i.e.: get all items) 614 @param unrestricted: if true, don't check permissions (i.e.: get all items)
615 @param maxItems: nb of items we want to tget 615 @param maxItems: nb of items we want to tget
616 @param rsm_data: options for RSM feature handling (XEP-0059) as a
617 dictionnary of C{unicode} to C{unicode}.
618
616 @return: list of (item, access_model, id) if unrestricted is True, else list of items 619 @return: list of (item, access_model, id) if unrestricted is True, else list of items
617 """ 620 """
618 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems) 621 if ext_data is None:
619 622 ext_data = {}
620 623 return self.dbpool.runInteraction(self._getItems, authorized_groups, unrestricted, maxItems, ext_data)
621 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems): 624
622 self._checkNodeExists(cursor) 625 def _getItems(self, cursor, authorized_groups, unrestricted, maxItems, ext_data):
626 self._checkNodeExists(cursor)
627
623 if unrestricted: 628 if unrestricted:
624 query = ["""SELECT data,items.access_model,item_id FROM nodes 629 query = ["SELECT data,items.access_model,item_id"]
630 source = """FROM nodes
625 INNER JOIN items USING (node_id) 631 INNER JOIN items USING (node_id)
626 WHERE node=%s ORDER BY date DESC"""] 632 WHERE node=%s"""
627 args = [self.nodeIdentifier] 633 args = [self.nodeIdentifier]
628 else: 634 else:
629 query = ["""SELECT data FROM nodes 635 query = ["SELECT data"]
630 INNER JOIN items USING (node_id) 636 groups = " or (items.access_model='roster' and groupname in %s)" if authorized_groups else ""
637 source = """FROM nodes
638 INNER JOIN items USING (node_id)
631 LEFT JOIN item_groups_authorized USING (item_id) 639 LEFT JOIN item_groups_authorized USING (item_id)
632 WHERE node=%s AND 640 WHERE node=%s AND
633 (items.access_model='open' """ + 641 (items.access_model='open'""" + groups + ")"
634 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') + 642
635 """)
636 ORDER BY date DESC"""]
637 args = [self.nodeIdentifier] 643 args = [self.nodeIdentifier]
638 if authorized_groups: 644 if authorized_groups:
639 args.append(authorized_groups) 645 args.append(authorized_groups)
646
647 query.append(source)
648 order = "DESC"
649
650 if 'rsm' in ext_data:
651 rsm = ext_data['rsm']
652 maxItems = rsm.max
653 if rsm.index is not None:
654 query.append("AND date<=(SELECT date " + source + " ORDER BY date DESC LIMIT 1 OFFSET %s)")
655 args.append(self.nodeIdentifier)
656 if authorized_groups:
657 args.append(authorized_groups)
658 args.append(rsm.index)
659 elif rsm.before is not None:
660 order = "ASC"
661 if rsm.before != '':
662 query.append("AND date>(SELECT date FROM items WHERE item=%s LIMIT 1)")
663 args.append(rsm.before)
664 elif rsm.after:
665 query.append("AND date<(SELECT date FROM items WHERE item=%s LIMIT 1)")
666 args.append(rsm.after)
667
668 query.append("ORDER BY date %s" % order)
640 669
641 if maxItems: 670 if maxItems:
642 query.append("LIMIT %s") 671 query.append("LIMIT %s")
643 args.append(maxItems) 672 args.append(maxItems)
644 673
660 ret.append((item, access_model, access_list)) 689 ret.append((item, access_model, access_list))
661 return ret 690 return ret
662 items = [generic.stripNamespace(parseXml(r[0])) for r in result] 691 items = [generic.stripNamespace(parseXml(r[0])) for r in result]
663 return items 692 return items
664 693
694 def countItems(self, authorized_groups, unrestricted):
695 """ Count the accessible items.
696
697 @param authorized_groups: we want to get items that these groups can access.
698 @param unrestricted: if true, don't check permissions (i.e.: get all items).
699 @return: deferred that fires a C{int}.
700 """
701 return self.dbpool.runInteraction(self._countItems, authorized_groups, unrestricted)
702
703 def _countItems(self, cursor, authorized_groups, unrestricted):
704 self._checkNodeExists(cursor)
705
706 if unrestricted:
707 query = ["""SELECT count(item_id) FROM nodes
708 INNER JOIN items USING (node_id)
709 WHERE node=%s"""]
710 args = [self.nodeIdentifier]
711 else:
712 query = ["""SELECT count(item_id) FROM nodes
713 INNER JOIN items USING (node_id)
714 LEFT JOIN item_groups_authorized USING (item_id)
715 WHERE node=%s AND
716 (items.access_model='open' """ +
717 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') +
718 ")"]
719
720 args = [self.nodeIdentifier]
721 if authorized_groups:
722 args.append(authorized_groups)
723
724 cursor.execute(' '.join(query), args)
725 return cursor.fetchall()[0][0]
726
727 def getIndex(self, authorized_groups, unrestricted, item):
728 """ Retrieve the index of the given item within the accessible window.
729
730 @param authorized_groups: we want to get items that these groups can access.
731 @param unrestricted: if true, don't check permissions (i.e.: get all items).
732 @param item: item identifier.
733 @return: deferred that fires a C{int}.
734 """
735 return self.dbpool.runInteraction(self._getIndex, authorized_groups, unrestricted, item)
736
737 def _getIndex(self, cursor, authorized_groups, unrestricted, item):
738 self._checkNodeExists(cursor)
739
740 if unrestricted:
741 query = ["""SELECT row_number FROM (
742 SELECT row_number() OVER (ORDER BY date DESC), item
743 FROM nodes INNER JOIN items USING (node_id)
744 WHERE node=%s
745 ) as x
746 WHERE item=%s LIMIT 1"""]
747 args = [self.nodeIdentifier]
748 else:
749 query = ["""SELECT row_number FROM (
750 SELECT row_number() OVER (ORDER BY date DESC), item
751 FROM nodes INNER JOIN items USING (node_id)
752 LEFT JOIN item_groups_authorized USING (item_id)
753 WHERE node=%s AND
754 (items.access_model='open' """ +
755 ("or (items.access_model='roster' and groupname in %s)" if authorized_groups else '') +
756 """)) as x
757 WHERE item=%s LIMIT 1"""]
758
759 args = [self.nodeIdentifier]
760 if authorized_groups:
761 args.append(authorized_groups)
762
763 args.append(item)
764 cursor.execute(' '.join(query), args)
765
766 return cursor.fetchall()[0][0]
665 767
666 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers): 768 def getItemsById(self, authorized_groups, unrestricted, itemIdentifiers):
667 """ Get items which are in the given list 769 """ Get items which are in the given list
668 @param authorized_groups: we want to get items that these groups can access 770 @param authorized_groups: we want to get items that these groups can access
669 @param unrestricted: if true, don't check permissions 771 @param unrestricted: if true, don't check permissions