diff sat_pubsub/pgsql_storage.py @ 323:8496af26be45

mam: added filtering by category
author Goffi <goffi@goffi.org>
date Thu, 07 Jan 2016 16:20:43 +0100
parents 54d90c73b8b5
children e73e42b4f6ff
line wrap: on
line diff
--- a/sat_pubsub/pgsql_storage.py	Tue Jan 05 23:13:13 2016 +0100
+++ b/sat_pubsub/pgsql_storage.py	Thu Jan 07 16:20:43 2016 +0100
@@ -57,6 +57,7 @@
 
 from twisted.internet import defer
 from twisted.words.protocols.jabber import jid
+from twisted.python import log
 
 from wokkel import generic
 from wokkel.pubsub import Subscription
@@ -701,12 +702,13 @@
         _getItems
         """
         # SOURCES
+        query.append("FROM nodes INNER JOIN items USING (node_id)")
+
         if unrestricted:
-            query.append("""FROM nodes
-                INNER JOIN items USING (node_id)
-                WHERE node_id=%s""")
+            query_filters = ["WHERE node_id=%s"]
             args.append(self.nodeDbId)
         else:
+            query.append("LEFT JOIN item_groups_authorized USING (item_id)")
             args.append(self.nodeDbId)
             if authorized_groups:
                 get_groups = " or (items.access_model='roster' and groupname in %s)"
@@ -714,29 +716,33 @@
             else:
                 get_groups = ""
 
-            query.append("""FROM nodes
-                INNER JOIN items USING (node_id)
-                LEFT JOIN item_groups_authorized USING (item_id)
-                WHERE node_id=%s AND
-                (items.access_model='open'""" + get_groups + ")")
+            query_filters = ["WHERE node_id=%s AND (items.access_model='open'" + get_groups + ")"]
 
         # FILTERS
         if 'filters' in ext_data:  # MAM filters
             for filter_ in ext_data['filters']:
                 if filter_.var == 'start':
-                    query.append("AND date>=%s")
+                    query_filters.append("AND date>=%s")
                     args.append(filter_.value)
-                if filter_.var == 'end':
-                    query.append("AND date<=%s")
+                elif filter_.var == 'end':
+                    query_filters.append("AND date<=%s")
                     args.append(filter_.value)
-                if filter_.var == 'with':
+                elif filter_.var == 'with':
                     jid_s = filter_.value
                     if '/' in jid_s:
-                        query.append("AND publisher=%s")
+                        query_filters.append("AND publisher=%s")
                         args.append(filter_.value)
                     else:
-                        query.append("AND publisher LIKE %s")
+                        query_filters.append("AND publisher LIKE %s")
                         args.append(u"{}%".format(filter_.value))
+                elif filter_.var == const.MAM_FILTER_CATEGORY:
+                    query.append("LEFT JOIN item_categories USING (item_id)")
+                    query_filters.append("AND category=%s")
+                    args.append(filter_.value)
+                else:
+                    log.msg("WARNING: unknown filter: {}".format(filter_.encode('utf-8')))
+
+        query.extend(query_filters)
 
         return "ORDER BY item_id DESC"