Skip to content
This repository has been archived by the owner on Oct 11, 2022. It is now read-only.

Commit

Permalink
Merge pull request #5088 from withspectrum/3.1.11
Browse files Browse the repository at this point in the history
3.1.11
  • Loading branch information
brianlovin authored May 6, 2019
2 parents 98fae62 + 4c222e0 commit ecd60ed
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 29 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
exports.up = async function(r, conn) {
// get all private channels that haven't been deleted
const privateChannels = await r
.db('spectrum')
.table('channels')
.filter({ isPrivate: true })
.filter(row => row.hasFields('deletedAt').not())
.run(conn)
.then(cursor => cursor.toArray());

// for each channel, remove all members except the community owner
return Promise.all(
privateChannels.map(async channel => {
const community = await r
.db('spectrum')
.table('communities')
.get(channel.communityId)
.run(conn);

// ensure that the community owner also owns the channel
// to account for situations where a moderator created the channel
const communityOwnerChannelRecord = await r
.db('spectrum')
.table('usersChannels')
.getAll([community.creatorId, channel.id], {
index: 'userIdAndChannelId',
})
.run(conn)
.then(cursor => cursor.toArray());

if (
!communityOwnerChannelRecord ||
communityOwnerChannelRecord.length === 0
) {
await r
.db('spectrum')
.table('usersChannels')
.insert({
channelId: channel.id,
userId: community.creatorId,
createdAt: new Date(),
isOwner: true,
isMember: true,
isModerator: false,
isBlocked: false,
isPending: false,
receiveNotifications: false,
})
.run(conn);
} else {
await r
.db('spectrum')
.table('usersChannels')
.getAll([community.creatorId, channel.id], {
index: 'userIdAndChannelId',
})
.update({ isOwner: true })
.run(conn);
}

return await r
.db('spectrum')
.table('usersChannels')
.getAll(channel.id, { index: 'channelId' })
.filter({ isMember: true })
.filter(row => row('userId').ne(community.creatorId))
.update({ isMember: false })
.run(conn);
})
);
};

exports.down = function(r, conn) {
return Promise.resolve();
};
17 changes: 15 additions & 2 deletions api/models/usersChannels.js
Original file line number Diff line number Diff line change
Expand Up @@ -466,6 +466,11 @@ const toggleUserChannelNotifications = async (userId: string, channelId: string,
.getAll([userId, channelId], { index: 'userIdAndChannelId' })
.run();

const channel = await db
.table('channels')
.get(channelId)
.run()

// permissions exist, this user is trying to toggle notifications for a channel where they
// are already a member
if (permissions && permissions.length > 0) {
Expand All @@ -476,8 +481,16 @@ const toggleUserChannelNotifications = async (userId: string, channelId: string,
.run();
}

// if permissions don't exist, create a usersChannel relationship with notifications on
return createMemberInChannel(channelId, userId, false)
// if the channel isn't private, it means the user is enabling notifications
// in a public channel that they have not yet joined - for example, if a user
// joins a community, then some time later the community creates a new channel,
// then again some time later the user wants notifications about that channel
if (!channel.isPrivate) {
// if permissions don't exist, create a usersChannel relationship with notifications on
return createMemberInChannel(channelId, userId, false)
}

return
};

const removeUsersChannelMemberships = async (userId: string) => {
Expand Down
2 changes: 1 addition & 1 deletion api/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@
"ms": "^2.1.1",
"node-env-file": "^0.1.8",
"node-localstorage": "^1.3.1",
"now-env": "^3.1.0",
"now-env": "^3.2.0",
"offline-plugin": "^4.9.1",
"passport": "^0.3.2",
"passport-facebook": "^2.1.1",
Expand Down
42 changes: 26 additions & 16 deletions api/routes/api/email.js
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
// @flow
require('now-env');
const IS_PROD = process.env.NODE_ENV === 'production';
const IS_TESTING = process.env.TEST_DB;
import { BRIAN_ID } from '../../migrations/seed/default/constants';
import { Router } from 'express';
const jwt = require('jsonwebtoken');
const emailRouter = Router();
import { updateUserEmail } from 'shared/db/queries/user';
import { unsubscribeUserFromEmailNotification } from '../../models/usersSettings';
import { updateThreadNotificationStatusForUser } from '../../models/usersThreads';
import { updateDirectMessageThreadNotificationStatusForUser } from '../../models/usersDirectMessageThreads';
import { toggleUserChannelNotifications } from '../../models/usersChannels';
import {
toggleUserChannelNotifications,
getUsersPermissionsInChannels,
} from '../../models/usersChannels';
import {
updateCommunityAdministratorEmail,
resetCommunityAdministratorEmail,
getCommunityById,
} from '../../models/community';
import { getChannelsByCommunity, getChannelById } from '../../models/channel';
Expand Down Expand Up @@ -89,18 +89,28 @@ emailRouter.get('/unsubscribe', async (req, res) => {
}
case 'muteCommunity': {
const community = await getCommunityById(dataId);
return getChannelsByCommunity(dataId)
.then(channels => channels.map(c => c.id))
.then(channels =>
channels.map(c => toggleUserChannelNotifications(userId, c, false))
)
.then(() =>
res.redirect(
`${rootRedirect}/${
community.slug
}?toastType=success&toastMessage=You will no longer receive new thread emails from this community.`
)
);
const channels = await getChannelsByCommunity(dataId);
const channelIds = channels.map(channel => channel.id);
const usersChannels = await getUsersPermissionsInChannels(
channelIds.map(id => [userId, id])
);
const usersChannelsWithNotifications = usersChannels.filter(
usersChannel => usersChannel && usersChannel.receiveNotifications
);
const channelIdsWithNotifications = usersChannelsWithNotifications.map(
usersChannel => usersChannel.channelId
);

await channelIdsWithNotifications.map(
async channelId =>
await toggleUserChannelNotifications(userId, channelId, false)
);

return res.redirect(
`${rootRedirect}/${
community.slug
}?toastType=success&toastMessage=You will no longer receive new thread emails from this community.`
);
}
case 'muteThread':
return updateThreadNotificationStatusForUser(
Expand Down
8 changes: 4 additions & 4 deletions api/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -7100,10 +7100,10 @@ normalize-path@^2.0.1, normalize-path@^2.1.1:
dependencies:
remove-trailing-separator "^1.0.1"

now-env@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/now-env/-/now-env-3.1.0.tgz#e0198b67279d387229cfd4b25de4c2fc7156943f"
integrity sha512-f+jXC+UkoxD/g9Nlig99Bxswoh7UUuQxw0EsPfuueHnVpVE0LfgQ4el5dxY4TSXwrL9mEF9GGm0gb7r3K8r/ug==
now-env@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/now-env/-/now-env-3.2.0.tgz#33223dd000d7966e66c0d00779c28c7aa0f111c6"
integrity sha512-zCWAwdX1KTa2ZoEg6Rc8efym5V4I36sC52OXo9F3O4IaNrvd/o1z7ZjuwnevoQqLloIdQWHo3JYAcL12m+KsUQ==

npm-bundled@^1.0.1:
version "1.0.5"
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "Spectrum",
"version": "3.1.10",
"version": "3.1.11",
"license": "BSD-3-Clause",
"devDependencies": {
"@babel/preset-flow": "^7.0.0",
Expand Down
2 changes: 1 addition & 1 deletion vulcan/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
"lodash": "^4.17.11",
"lodash.intersection": "^4.4.0",
"node-env-file": "^0.1.8",
"now-env": "^3.1.0",
"now-env": "^3.2.0",
"performance-now": "^2.1.0",
"raven": "^2.6.4",
"redis-tag-cache": "^1.2.1",
Expand Down
8 changes: 4 additions & 4 deletions vulcan/yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -667,10 +667,10 @@ node-fetch@^1.0.1:
encoding "^0.1.11"
is-stream "^1.0.1"

now-env@^3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/now-env/-/now-env-3.1.0.tgz#e0198b67279d387229cfd4b25de4c2fc7156943f"
integrity sha512-f+jXC+UkoxD/g9Nlig99Bxswoh7UUuQxw0EsPfuueHnVpVE0LfgQ4el5dxY4TSXwrL9mEF9GGm0gb7r3K8r/ug==
now-env@^3.2.0:
version "3.2.0"
resolved "https://registry.yarnpkg.com/now-env/-/now-env-3.2.0.tgz#33223dd000d7966e66c0d00779c28c7aa0f111c6"
integrity sha512-zCWAwdX1KTa2ZoEg6Rc8efym5V4I36sC52OXo9F3O4IaNrvd/o1z7ZjuwnevoQqLloIdQWHo3JYAcL12m+KsUQ==

object-assign@^4.1.0:
version "4.1.1"
Expand Down

0 comments on commit ecd60ed

Please sign in to comment.