Skip to content
This repository was archived by the owner on Oct 11, 2022. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion analytics/models/channel.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBChannel } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getChannelById = (channelId: string): Promise<DBChannel> => {
return db
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/community.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBCommunity } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getCommunityById = (communityId: string): Promise<DBCommunity> => {
return db
Expand Down
31 changes: 0 additions & 31 deletions analytics/models/db.js

This file was deleted.

2 changes: 1 addition & 1 deletion analytics/models/message.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBMessage } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getMessageById = (messageId: string): Promise<DBMessage> => {
return db
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/notification.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBNotification } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getNotificationById = (
notificationId: string
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/reaction.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBReaction, DBThreadReaction } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getReactionById = (reactionId: string): Promise<DBReaction> => {
return db
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/thread.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBThread } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getThreadById = (threadId: string): Promise<DBThread> => {
return db
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/usersChannels.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBUsersChannels } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

const defaultResult = {
isMember: false,
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/usersCommunities.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBUsersCommunities } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

const defaultResult = {
isMember: false,
Expand Down
2 changes: 1 addition & 1 deletion analytics/models/usersThreads.js
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
// @flow
import type { DBUsersThreads } from 'shared/types';
import { db } from './db';
import { db } from 'shared/db';

export const getThreadNotificationStatusForUser = (
threadId: string,
Expand Down
59 changes: 59 additions & 0 deletions api/migrations/20181126094455-users-channels-roles.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
const branch = (r, field, fallback) => {
return r.branch(r.row(`is${field}`).eq(true), field.toLowerCase(), fallback);
};

exports.up = function(r, conn) {
return Promise.all([
r
.table('usersChannels')
.indexCreate('channelIdAndRole', [
r.row('channelId'),
branch(
r,
'Pending',
branch(
r,
'Blocked',
branch(
r,
'Owner',
branch(r, 'Moderator', branch(r, 'Member', r.literal()))
)
)
),
])
.run(conn),
r
.table('usersChannels')
.indexCreate('userIdAndRole', [
r.row('userId'),
branch(
r,
'Pending',
branch(
r,
'Blocked',
branch(
r,
'Owner',
branch(r, 'Moderator', branch(r, 'Member', r.literal()))
)
)
),
])
.run(conn),
]);
};

exports.down = function(r, conn) {
return Promise.all([
r
.table('usersChannels')
.indexDrop('channelIdAndRole')
.run(conn),
r
.table('usersChannels')
.indexDrop('userIdAndRole')
.run(conn),
]);
};
22 changes: 14 additions & 8 deletions api/models/channel.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,22 +99,28 @@ const getChannelsByUser = (userId: string): Promise<Array<DBChannel>> => {
);
};

// prettier-ignore
const getChannelBySlug = (channelSlug: string, communitySlug: string): Promise<DBChannel> => {
const getChannelBySlug = async (
channelSlug: string,
communitySlug: string
): Promise<?DBChannel> => {
const [communityId] = await db
.table('communities')
.getAll(communitySlug, { index: 'slug' })('id')
.run();

if (!communityId) return null;

return db
.table('channels')
.getAll(communityId, { index: 'communityId' })
.filter(channel =>
channel('slug')
.eq(channelSlug)
.and(db.not(channel.hasFields('deletedAt')))
)
.eqJoin('communityId', db.table('communities'))
.filter({ right: { slug: communitySlug } })
.run()
.then(result => {
if (result && result[0]) {
return result[0].left;
}
.then(res => {
if (Array.isArray(res) && res.length > 0) return res[0];
return null;
});
};
Expand Down
6 changes: 2 additions & 4 deletions api/models/search.js
Original file line number Diff line number Diff line change
Expand Up @@ -89,8 +89,7 @@ export const getPrivateCommunityIdsForUsersThreads = (
export const getUsersJoinedChannels = (userId: string): Promise<Array<string>> => {
return db
.table('usersChannels')
.getAll(userId, { index: 'userId' })
.filter({ isMember: true })
.getAll([userId, "member"], [userId, "moderator"], [userId, "owner"], { index: 'userIdAndRole' })
.eqJoin('channelId', db.table('channels'))
.filter(row => row('right').hasFields('deletedAt').not())
.zip()
Expand All @@ -114,8 +113,7 @@ export const getUsersJoinedCommunities = (userId: string): Promise<Array<string>
export const getUsersJoinedPrivateChannelIds = (userId: string): Promise<Array<string>> => {
return db
.table('usersChannels')
.getAll(userId, { index: 'userId' })
.filter({ isMember: true })
.getAll([userId, "member"], [userId, "moderator"], [userId, "owner"], { index: 'userIdAndRole' })
.eqJoin('channelId', db.table('channels'))
.filter(row => row('right')('isPrivate').eq(true).and(row('right').hasFields('deletedAt').not()))
.without({ left: ['id'] })
Expand Down
20 changes: 16 additions & 4 deletions api/models/thread.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,8 +164,14 @@ export const getViewableThreadsByUser = async (
// get a list of the channelIds the current user is allowed to see threads
const getCurrentUsersChannelIds = db
.table('usersChannels')
.getAll(currentUser, { index: 'userId' })
.filter({ isBlocked: false, isMember: true })
.getAll(
[currentUser, 'member'],
[currentUser, 'moderator'],
[currentUser, 'owner'],
{
index: 'userIdAndRole',
}
)
.map(userChannel => userChannel('channelId'))
.run();

Expand Down Expand Up @@ -273,8 +279,14 @@ export const getViewableParticipantThreadsByUser = async (
// get a list of the channelIds the current user is allowed to see threads for
const getCurrentUsersChannelIds = db
.table('usersChannels')
.getAll(currentUser, { index: 'userId' })
.filter({ isBlocked: false, isMember: true })
.getAll(
[currentUser, 'member'],
[currentUser, 'moderator'],
[currentUser, 'owner'],
{
index: 'userIdAndRole',
}
)
.map(userChannel => userChannel('channelId'))
.run();

Expand Down
Loading