File size: 6,900 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
// $FlowFixMe
const { v4: uuid } = require('uuid');
// $FlowFixMe
const faker = require('faker');
// $FlowFixMe
const slugify = require('slugg');
// $FlowFixMe
const casual = require('casual').functions();
const { fromPlainText, toJSON } = require('../../../shared/draft-utils');

const randomAmount = ({ max, min }, cb) => {
  if (!max) throw new Error('randomAmount({ max }): max has to be defined!');
  const n = faker.random.number({ min: min || 0, max });
  let result = [];
  for (var i = 0; i < n; i++) {
    result.push(cb(i));
  }
  return result;
};

const generateUser = () => {
  const createdAt = faker.date.past(2);
  const name = faker.name.findName();
  return {
    id: uuid(),
    name,
    description: casual.short_description(),
    website: faker.internet.url(),
    username: faker.internet.userName(name),
    profilePhoto: faker.internet.avatar(),
    coverPhoto: faker.image.image(),
    email: faker.internet.email(name),
    providerId: uuid(),
    createdAt,
    // Make sure lastSeen is > createdAt
    lastSeen: faker.date.between(createdAt, new Date()),
  };
};

const generateUsersSettings = userId => {
  return {
    id: uuid(),
    userId,
    notifications: {
      types: {
        newMessageInThreads: {
          email: true,
        },
        newThreadCreated: {
          email: true,
        },
        dailyDigest: {
          email: true,
        },
        weeklyDigest: {
          email: true,
        },
      },
    },
  };
};

const generateCommunity = () => {
  const name = faker.company.companyName();

  return {
    id: uuid(),
    createdAt: faker.date.past(2),
    name,
    description: casual.short_description(),
    website: faker.internet.url(),
    profilePhoto: faker.image.business(),
    coverPhoto: faker.image.image(),
    slug: slugify(name),
  };
};

const generateChannel = communityId => {
  const name = faker.commerce.department();

  return {
    id: uuid(),
    communityId,
    createdAt: faker.date.past(2),
    name,
    description: casual.short_description(),
    slug: slugify(name),
    isPrivate: faker.random.boolean(),
    isDefault: false,
  };
};

const generateUsersCommunities = (communityId, userId) => {
  const isOwner = faker.random.boolean();
  // for ease of use, set to false
  const isModerator = false;
  // if user is either an admin or moderator, they have to be a member
  // otherwise random chance
  const isMember = isOwner || isModerator ? true : faker.random.boolean();
  // might be blocked as long as they aren't an admin, mod, or member
  const isBlocked =
    isOwner || isModerator || isMember ? false : faker.random.boolean();

  return {
    id: uuid(),
    createdAt: faker.date.past(2),
    communityId,
    userId,
    isOwner,
    isModerator,
    isMember,
    isBlocked,
    receiveNotifications: true,
    reputation: 1,
  };
};

const generateUsersChannels = (channels, usersCommunities, userId) => {
  // figure out which communities the user being evaulated is a member of
  let possibleCommunities = usersCommunities.filter(
    elem => elem.userId === userId
  );
  // make sure they are a member of the community
  possibleCommunities = possibleCommunities
    .filter(elem => elem.isMember || elem.isOwner || elem.isModerator)
    .map(elem => elem.communityId);
  let possibleChannels = channels.filter(
    channel => possibleCommunities.indexOf(channel.communityId) > -1
  );

  // for each of those communities, construct an array of channels that are in
  // that community
  // const possibleChannels = possibleCommunities.map(community => channels.filter(channel => channel.communityId === community.id))

  // for each of hte possible channels, generate a new usersChannels object
  const foo = possibleChannels.map(channel => {
    const isOwner = faker.random.boolean();
    // for ease of use, set to false
    const isModerator = false;
    // if user is either an admin or moderator, they have to be a member
    // otherwise random chance
    const isMember = isOwner || isModerator ? true : faker.random.boolean();
    // if a user is admin, mod, or member, they can't be pending, otherwise random
    const isPending =
      isOwner || isModerator || isMember ? false : faker.random.boolean();
    // might be blocked as long as they aren't an admin, mod, pending, or member
    const isBlocked =
      isOwner || isModerator || isMember || isPending
        ? false
        : faker.random.boolean();

    return {
      id: uuid(),
      createdAt: new Date(),
      channelId: channel.id,
      userId,
      isOwner,
      isModerator,
      isMember,
      isBlocked,
      isPending,
      receiveNotifications: true,
    };
  });
  return foo;
};

const generateThread = (communityId, channelId, creatorId) => {
  const content = {
    title: casual.title(),
    body: JSON.stringify(toJSON(fromPlainText(casual.text()))),
  };

  const createdAt = faker.date.past(2);

  return {
    id: uuid(),
    createdAt,
    creatorId,
    channelId,
    communityId,
    isPublished: faker.random.boolean(),
    content,
    type: 'DRAFTJS',
    lastActive: faker.date.between(createdAt, new Date()),
    edits: [
      {
        timestamp: createdAt,
        content,
      },
    ],
    modifiedAt: faker.date.between(createdAt, new Date()),
  };
};

const generateUsersThreads = (threadId, userId) => {
  const createdAt = faker.date.past(2);

  return {
    id: uuid(),
    createdAt,
    threadId,
    userId,
    isParticipant: true,
    receiveNotifications: true,
  };
};

const generateDirectMessageThread = users => {
  const createdAt = faker.date.past(2);
  const threadLastActive = faker.date.between(createdAt, faker.date.recent());

  return {
    id: uuid(),
    name: null,
    createdAt,
    threadLastActive,
  };
};

const generateUsersDirectMessageThreads = (threadId, userId) => {
  const createdAt = faker.date.past(2);
  const lastActive = faker.date.between(createdAt, faker.date.recent());
  const lastSeen = faker.date.between(createdAt, new Date());

  return {
    id: uuid(),
    createdAt,
    threadId,
    userId,
    lastActive,
    lastSeen,
    receiveNotifications: true,
  };
};

const generateMessage = (senderId, threadId, threadType) => {
  return {
    id: uuid(),
    threadType,
    threadId,
    senderId,
    content: {
      body: casual.text(),
    },
    messageType: 'text',
    timestamp: faker.date.past(2),
  };
};

const generateReaction = (userId, messageId) => {
  return {
    id: uuid(),
    messageId,
    userId,
    timestamp: faker.date.past(2),
    type: 'like',
  };
};

module.exports = {
  randomAmount,
  generateUser,
  generateUsersSettings,
  generateCommunity,
  generateChannel,
  generateUsersCommunities,
  generateUsersChannels,
  generateUsersDirectMessageThreads,
  generateThread,
  generateUsersThreads,
  generateMessage,
  generateReaction,
  generateDirectMessageThread,
};