changeset 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
files sat_pubsub/const.py sat_pubsub/pgsql_storage.py sat_pubsub/tap.py
diffstat 3 files changed, 26 insertions(+), 15 deletions(-) [+]
line wrap: on
line diff
--- a/sat_pubsub/const.py	Tue Jan 05 23:13:13 2016 +0100
+++ b/sat_pubsub/const.py	Thu Jan 07 16:20:43 2016 +0100
@@ -73,3 +73,4 @@
 VAL_RSM_MAX_DEFAULT = 10 # None for no limit
 FLAG_ENABLE_RSM = True
 FLAG_ENABLE_MAM = True
+MAM_FILTER_CATEGORY = 'http://salut-a-toi.org/protocols/mam_filter_category'
--- 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"
 
--- a/sat_pubsub/tap.py	Tue Jan 05 23:13:13 2016 +0100
+++ b/sat_pubsub/tap.py	Thu Jan 07 16:20:43 2016 +0100
@@ -59,7 +59,10 @@
 from wokkel.disco import DiscoHandler
 from wokkel.generic import FallbackHandler, VersionHandler
 from wokkel.iwokkel import IPubSubResource
-from wokkel import pubsub, rsm, mam
+from wokkel import data_form
+from wokkel import pubsub
+from wokkel import rsm
+from wokkel import mam
 
 from sat_pubsub import __version__
 from sat_pubsub import const
@@ -155,6 +158,7 @@
     if const.FLAG_ENABLE_MAM:
         mam_resource = pubsub_mam.MAMResource(bs)
         mam_s = mam.MAMService(mam_resource)
+        mam_s.addFilter(data_form.Field(var=const.MAM_FILTER_CATEGORY))
         mam_s.setHandlerParent(cs)
 
     # XXX: delegation must be instancied at the end,