54
54
IBugNotificationSet,
56
56
from lp.bugs.model.bugactivity import BugActivity
57
from lp.bugs.model.bugsubscriptionfilter import BugSubscriptionFilter
57
from lp.bugs.model.bugsubscriptionfilter import (
58
BugSubscriptionFilter,
59
BugSubscriptionFilterMute,
58
61
from lp.bugs.model.structuralsubscription import StructuralSubscription
59
62
from lp.registry.interfaces.person import IPersonSet
60
63
from lp.services.database.stormbase import StormBase
191
194
if not notifications or not recipient_to_sources:
192
195
# This is a shortcut that will remove some error conditions.
194
# This makes one call to the database to get all the information
195
# we need. We get the filter ids and descriptions for each
196
# source, and then we divide up the information per recipient.
197
# This makes two calls to the database to get all the
198
# information we need. The first call gets the filter ids and
199
# descriptions for each recipient, and then we divide up the
200
# information per recipient.
197
201
# First we get some intermediate data structures set up.
198
202
source_person_id_map = {}
199
203
recipient_id_map = {}
241
245
source_person_id_map[source_person_id]['filters'][filter_id] = (
242
246
filter_description)
243
247
filter_ids.append(filter_id)
248
no_filter_marker = -1 # This is only necessary while production and
249
# sample data have structural subscriptions without filters.
244
250
# Assign the filters to each recipient.
245
251
for recipient_data in recipient_id_map.values():
246
252
for source_person_id in recipient_data['source person ids']:
247
253
recipient_data['filters'].update(
248
source_person_id_map[source_person_id]['filters'])
254
source_person_id_map[source_person_id]['filters']
255
or {no_filter_marker: None})
257
# Now we get the information about subscriptions that might be
258
# filtered and take that into account.
259
mute_data = store.find(
260
(BugSubscriptionFilterMute.person_id,
261
BugSubscriptionFilterMute.filter_id),
262
In(BugSubscriptionFilterMute.person_id, recipient_id_map.keys()),
263
In(BugSubscriptionFilterMute.filter_id, filter_ids))
264
for person_id, filter_id in mute_data:
265
del recipient_id_map[person_id]['filters'][filter_id]
249
266
# Now recipient_id_map has all the information we need. Let's
250
267
# build the final result and return it.
252
269
for recipient_data in recipient_id_map.values():
253
filter_descriptions = [
254
description for description
255
in recipient_data['filters'].values() if description]
256
filter_descriptions.sort() # This is good for tests.
257
result[recipient_data['principal']] = {
258
'sources': recipient_data['sources'],
259
'filter descriptions': filter_descriptions}
270
if recipient_data['filters']:
271
filter_descriptions = [
272
description for description
273
in recipient_data['filters'].values() if description]
274
filter_descriptions.sort() # This is good for tests.
275
result[recipient_data['principal']] = {
276
'sources': recipient_data['sources'],
277
'filter descriptions': filter_descriptions}