File size: 7,959 Bytes
1e92f2d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
// @flow
import UserError from './UserError';
import type { GraphQLContext } from '../';
import type { DBChannel, DBCommunity, DBUser } from 'shared/types';
import {
  COMMUNITY_SLUG_DENY_LIST,
  CHANNEL_SLUG_DENY_LIST,
} from 'shared/slug-deny-lists';
import { getThreadById } from '../models/thread';

export const isAdmin = (id: string): boolean => {
  const admins = [
    'gVk5mYwccUOEKiN5vtOouqroGKo1',
    '01p2A7kDCWUjGj6zQLlMQUOSQL42',
    'VToKcde16dREgDkXcDl3hhcrFN33',
  ];
  return admins.indexOf(id) > -1;
};

export const communitySlugIsDenyListed = (slug: string): boolean => {
  return COMMUNITY_SLUG_DENY_LIST.indexOf(slug) > -1;
};

export const channelSlugIsDenyListed = (slug: string): boolean => {
  return CHANNEL_SLUG_DENY_LIST.indexOf(slug) > -1;
};

// prettier-ignore
export const isAuthedResolver = (resolver: Function) => async (obj: any, args: any, context: GraphQLContext, info: any) => {
  if (!context.user || !context.user.id) {
    return new UserError('You must be signed in to do this')
  }

  const user = await context.loaders.user.load(context.user.id)

  if (!user || user.bannedAt || user.deletedAt) {
    return new UserError('You must be signed in to do this')
  }

  return resolver(obj, args, context, info)
}

// prettier-ignore
const channelExists = async (channelId: string, loaders: any): Promise<?DBChannel> => {
  const channel = await loaders.channel.load(channelId);
  if (!channel || channel.deletedAt) return null;
  return channel;
};

// prettier-ignore
const communityExists = async (communityId: string, loaders: any): Promise<?DBCommunity> => {
  const community = await loaders.community.load(communityId);
  if (!community || community.deletedAt) return null;
  return community;
};

// prettier-ignore
export const canAdministerChannel = async (userId: string, channelId: string, loaders: any) => {
  if (!userId || !channelId) return false;

  const channel = await channelExists(channelId, loaders);
  if (!channel) return false;

  const [communityPermissions, channelPermissions] = await Promise.all([
    loaders.userPermissionsInCommunity.load([userId, channel.communityId]),
    loaders.userPermissionsInChannel.load([userId, channelId]),
  ]);

  if (!communityPermissions) return false;
  if (communityPermissions.isOwner || communityPermissions.isModerator)
    return true;
  if (!channelPermissions) return false;
  if (channelPermissions.isOwner) return true;

  return false;
};

// prettier-ignore
export const canModerateChannel = async (userId: string, channelId: string, loaders: any) => {
  if (!userId || !channelId) return false;

  const channel = await channelExists(channelId, loaders);
  if (!channel) return false;

  const [communityPermissions, channelPermissions] = await Promise.all([
    loaders.userPermissionsInCommunity.load([userId, channel.communityId]),
    loaders.userPermissionsInChannel.load([userId, channelId]),
  ]);

  if (!communityPermissions) return false;
  if (communityPermissions.isBlocked) return false
  if (communityPermissions.isOwner || communityPermissions.isModerator) return true

  if (!channelPermissions) return false;
  if (channelPermissions.isBlocked) return false
  return channelPermissions.isOwner || channelPermissions.isModerator
};

// prettier-ignore
export const canAdministerCommunity = async (userId: string, communityId: string, loaders: any) => {
  if (!userId || !communityId) return false;

  const community = await communityExists(communityId, loaders);
  if (!community) return false;

  const communityPermissions = await loaders.userPermissionsInCommunity.load([
    userId,
    communityId,
  ]);

  if (!communityPermissions) return false
  if (communityPermissions.isBlocked) return false
  return communityPermissions.isOwner
};

// prettier-igore
export const canModerateCommunity = async (
  userId: string,
  communityId: string,
  loaders: any
) => {
  if (!userId || !communityId) return false;

  const community = await communityExists(communityId, loaders);
  if (!community) return false;

  const communityPermissions = await loaders.userPermissionsInCommunity.load([
    userId,
    communityId,
  ]);

  if (!communityPermissions) return false;
  if (communityPermissions.isBlocked) return false;
  return communityPermissions.isOwner || communityPermissions.isModerator;
};

// prettier-ignore
export const canViewCommunity = async (user: DBUser, communityId: string, loaders: any) => {
  if (!communityId) return false;

  const community = await communityExists(communityId, loaders);
  if (!community) return false;

  if (!user) {
    if (community.isPrivate) return false
    return true
  }

  const communityPermissions = await loaders.userPermissionsInCommunity.load([
    user.id,
    communityId,
  ]);

  if (community.isPrivate && !communityPermissions) return false;
  if (communityPermissions && communityPermissions.isBlocked) return false
  if (community.isPrivate && !communityPermissions.isMember) return false
  
  return true;
}

export const canViewThread = async (
  userId: string,
  threadId: string,
  loaders: any
) => {
  const thread = await getThreadById(threadId);

  if (!thread || thread.deletedAt) return false;

  const [
    channel,
    community,
    channelPermissions,
    communityPermissions,
  ] = await Promise.all([
    loaders.channel.load(thread.channelId),
    loaders.community.load(thread.communityId),
    loaders.userPermissionsInChannel.load([userId, thread.channelId]),
    loaders.userPermissionsInCommunity.load([userId, thread.communityId]),
  ]);

  if (!channel || !community) return false;
  if (channel.deletedAt || community.deletedAt) return false;

  if (userId) {
    if (channel.isPrivate) {
      if (
        !channelPermissions ||
        channelPermissions.isBlocked ||
        !channelPermissions.isMember
      ) {
        return false;
      }

      return true;
    }

    if (community.isPrivate) {
      if (
        !communityPermissions ||
        communityPermissions.isBlocked ||
        !communityPermissions.isMember
      ) {
        return false;
      }

      return true;
    }

    if (communityPermissions) {
      return !communityPermissions.isBlocked;
    }

    if (channelPermissions) {
      return !channelPermissions.isBlocked;
    }
  }

  return !channel.isPrivate && !community.isPrivate;
};

export const canViewDMThread = async (
  userId: string,
  threadId: string,
  loaders: any
) => {
  if (!userId) return false;

  const thread = await loaders.directMessageParticipants.load(threadId);

  if (!thread || !thread.reduction || thread.reduction.length === 0)
    return false;

  const participants = thread.reduction;

  const ids = participants.map(({ userId }) => userId);

  if (ids.indexOf(userId) === -1) return false;

  return true;
};

// prettier-ignore
export const canViewChannel = async (user: DBUser, channelId: string, loaders: any) => {
  if (!channelId) return false;

  const channel = await channelExists(channelId, loaders);
  if (!channel) return false;

  const community = await communityExists(channel.communityId, loaders);
  if (!community) return false
  
  if (!user) {
    if (!community.isPrivate && !channel.isPrivate) return true
    return false
  }
  
  const [
    communityPermissions,
    channelPermissions
  ] = await Promise.all([
    loaders.userPermissionsInCommunity.load([
      user.id,
      community.id,
    ]),
    loaders.userPermissionsInChannel.load([
      user.id,
      channel.id,
    ])
  ])
  
  if (channel.isPrivate && !channelPermissions) return false
  if (community.isPrivate && !communityPermissions) return false
  if (channel.isPrivate && !channelPermissions.isMember) return false
  if (community.isPrivate && !communityPermissions.isMember) return false
  if (channelPermissions && channelPermissions.isBlocked) return false
  if (communityPermissions && communityPermissions.isBlocked) return false
  
  return true
}