|
1 | 1 | // @flow |
2 | 2 | require('now-env'); |
3 | 3 | const IS_PROD = process.env.NODE_ENV === 'production'; |
4 | | -const IS_TESTING = process.env.TEST_DB; |
5 | | -import { BRIAN_ID } from '../../migrations/seed/default/constants'; |
6 | 4 | import { Router } from 'express'; |
7 | 5 | const jwt = require('jsonwebtoken'); |
8 | 6 | const emailRouter = Router(); |
9 | 7 | import { updateUserEmail } from 'shared/db/queries/user'; |
10 | 8 | import { unsubscribeUserFromEmailNotification } from '../../models/usersSettings'; |
11 | 9 | import { updateThreadNotificationStatusForUser } from '../../models/usersThreads'; |
12 | 10 | import { updateDirectMessageThreadNotificationStatusForUser } from '../../models/usersDirectMessageThreads'; |
13 | | -import { toggleUserChannelNotifications } from '../../models/usersChannels'; |
| 11 | +import { |
| 12 | + toggleUserChannelNotifications, |
| 13 | + getUsersPermissionsInChannels, |
| 14 | +} from '../../models/usersChannels'; |
14 | 15 | import { |
15 | 16 | updateCommunityAdministratorEmail, |
16 | | - resetCommunityAdministratorEmail, |
17 | 17 | getCommunityById, |
18 | 18 | } from '../../models/community'; |
19 | 19 | import { getChannelsByCommunity, getChannelById } from '../../models/channel'; |
@@ -89,18 +89,28 @@ emailRouter.get('/unsubscribe', async (req, res) => { |
89 | 89 | } |
90 | 90 | case 'muteCommunity': { |
91 | 91 | const community = await getCommunityById(dataId); |
92 | | - return getChannelsByCommunity(dataId) |
93 | | - .then(channels => channels.map(c => c.id)) |
94 | | - .then(channels => |
95 | | - channels.map(c => toggleUserChannelNotifications(userId, c, false)) |
96 | | - ) |
97 | | - .then(() => |
98 | | - res.redirect( |
99 | | - `${rootRedirect}/${ |
100 | | - community.slug |
101 | | - }?toastType=success&toastMessage=You will no longer receive new thread emails from this community.` |
102 | | - ) |
103 | | - ); |
| 92 | + const channels = await getChannelsByCommunity(dataId); |
| 93 | + const channelIds = channels.map(channel => channel.id); |
| 94 | + const usersChannels = await getUsersPermissionsInChannels( |
| 95 | + channelIds.map(id => [userId, id]) |
| 96 | + ); |
| 97 | + const usersChannelsWithNotifications = usersChannels.filter( |
| 98 | + usersChannel => usersChannel && usersChannel.receiveNotifications |
| 99 | + ); |
| 100 | + const channelIdsWithNotifications = usersChannelsWithNotifications.map( |
| 101 | + usersChannel => usersChannel.channelId |
| 102 | + ); |
| 103 | + |
| 104 | + await channelIdsWithNotifications.map( |
| 105 | + async channelId => |
| 106 | + await toggleUserChannelNotifications(userId, channelId, false) |
| 107 | + ); |
| 108 | + |
| 109 | + return res.redirect( |
| 110 | + `${rootRedirect}/${ |
| 111 | + community.slug |
| 112 | + }?toastType=success&toastMessage=You will no longer receive new thread emails from this community.` |
| 113 | + ); |
104 | 114 | } |
105 | 115 | case 'muteThread': |
106 | 116 | return updateThreadNotificationStatusForUser( |
|
0 commit comments