-
-
Notifications
You must be signed in to change notification settings - Fork 9
Expand file tree
/
Copy pathindex.js
More file actions
489 lines (431 loc) · 15.3 KB
/
index.js
File metadata and controls
489 lines (431 loc) · 15.3 KB
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
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
/* eslint-disable no-fallthrough */
import * as sessionReducers from './sessionReducers'
import {
ACCEPT_GROUP_RELATIONSHIP_INVITE,
ADD_MODERATOR_PENDING,
CANCEL_GROUP_RELATIONSHIP_INVITE,
CREATE_COMMENT,
CREATE_COMMENT_PENDING,
CREATE_JOIN_REQUEST,
CREATE_MESSAGE,
CREATE_MESSAGE_PENDING,
DELETE_COMMENT_PENDING,
DELETE_GROUP_RELATIONSHIP,
FETCH_GROUP_DETAILS_PENDING,
FETCH_MESSAGES_PENDING,
FETCH_POSTS_PENDING,
INVITE_CHILD_TO_JOIN_PARENT_GROUP,
JOIN_PROJECT_PENDING,
LEAVE_GROUP,
LEAVE_PROJECT_PENDING,
PROCESS_STRIPE_TOKEN_PENDING,
REMOVE_MODERATOR_PENDING,
REJECT_GROUP_RELATIONSHIP_INVITE,
REQUEST_FOR_CHILD_TO_JOIN_PARENT_GROUP,
RESET_NEW_POST_COUNT_PENDING,
RESPOND_TO_EVENT_PENDING,
TOGGLE_GROUP_TOPIC_SUBSCRIBE_PENDING,
UPDATE_COMMENT_PENDING,
UPDATE_GROUP_TOPIC_PENDING,
UPDATE_POST_PENDING,
UPDATE_POST,
UPDATE_THREAD_READ_TIME,
UPDATE_USER_SETTINGS_PENDING as UPDATE_USER_SETTINGS_GLOBAL_PENDING,
UPDATE_WIDGET,
VOTE_ON_POST_PENDING
} from 'store/constants'
import {
UPDATE_MEMBERSHIP_SETTINGS_PENDING,
UPDATE_USER_SETTINGS_PENDING,
UPDATE_ALL_MEMBERSHIP_SETTINGS_PENDING
} from 'routes/UserSettings/UserSettings.store'
// FIXME these should not be using different constants and getting handled in
// different places -- they're doing the same thing!
import {
REMOVE_SKILL_PENDING, ADD_SKILL, ADD_SKILL_TO_GROUP, REMOVE_SKILL_FROM_GROUP_PENDING
} from 'components/SkillsSection/SkillsSection.store'
import {
REMOVE_SKILL_PENDING as REMOVE_SKILL_TO_LEARN_PENDING, ADD_SKILL as ADD_SKILL_TO_LEARN
} from 'components/SkillsToLearnSection/SkillsToLearnSection.store'
import {
UPDATE_GROUP_SETTINGS,
UPDATE_GROUP_SETTINGS_PENDING
} from 'routes/GroupSettings/GroupSettings.store'
import {
CREATE_GROUP
} from 'components/CreateGroup/CreateGroup.store'
import {
USE_INVITATION
} from 'routes/JoinGroup/JoinGroup.store'
import { FETCH_GROUP_WELCOME_DATA } from 'routes/GroupWelcomeModal/GroupWelcomeModal.store'
import {
DELETE_GROUP_TOPIC_PENDING
} from 'routes/AllTopics/AllTopics.store'
import {
INVITE_PEOPLE_TO_EVENT_PENDING
} from 'components/EventInviteDialog/EventInviteDialog.store'
import { FETCH_GROUP_TO_GROUP_JOIN_QUESTIONS } from 'routes/GroupSettings/RelatedGroupsTab/RelatedGroupsTab.store'
import orm from 'store/models'
import clearCacheFor from './clearCacheFor'
import { find, get, values } from 'lodash/fp'
import extractModelsFromAction from '../ModelExtractor/extractModelsFromAction'
import { isPromise } from 'util/index'
export default function ormReducer (state = {}, action) {
const session = orm.session(state)
const { payload, type, meta, error } = action
if (error) return state
const {
Comment,
Group,
GroupRelationship,
GroupRelationshipInvite,
GroupTopic,
EventInvitation,
Invitation,
JoinRequest,
Me,
Membership,
Message,
MessageThread,
Person,
Post,
PostCommenter,
ProjectMember,
Skill
} = session
if (payload && !isPromise(payload) && meta && meta.extractModel) {
extractModelsFromAction(action, session)
}
let me, membership, group, person, post, comment, groupTopic, childGroup
switch (type) {
case ACCEPT_GROUP_RELATIONSHIP_INVITE:
const newGroupRelationship = payload.data.acceptGroupRelationshipInvite.groupRelationship
if (newGroupRelationship) {
childGroup = Group.withId(newGroupRelationship.childGroup.id)
Group.withId(newGroupRelationship.parentGroup.id).updateAppending({ childGroups: [childGroup] })
clearCacheFor(Group, childGroup.id)
}
case ADD_MODERATOR_PENDING:
person = Person.withId(meta.personId)
Group.withId(meta.groupId).updateAppending({ moderators: [person] })
break
case ADD_SKILL: {
const skill = payload.data.addSkill
person = Person.withId(Me.first().id)
person.updateAppending({ skills: [Skill.create(skill)] })
break
}
case ADD_SKILL_TO_LEARN:
const skillToLearn = payload.data.addSkillToLearn
person = Person.withId(Me.first().id)
person.updateAppending({ skillsToLearn: [Skill.create(skillToLearn)] })
break
case ADD_SKILL_TO_GROUP: {
const skill = payload.data.addSuggestedSkillToGroup
group = Group.withId(meta.groupId)
group.updateAppending({ suggestedSkills: [Skill.create(skill)] })
clearCacheFor(Group, meta.groupId)
break
}
case CANCEL_GROUP_RELATIONSHIP_INVITE:
case REJECT_GROUP_RELATIONSHIP_INVITE:
const invite = GroupRelationshipInvite.withId(meta.id)
invite.delete()
break
case CREATE_COMMENT_PENDING:
Comment.create({
id: meta.tempId,
post: meta.postId,
text: meta.text,
creator: Me.first().id })
break
case CREATE_COMMENT:
Comment.withId(meta.tempId).delete()
if (!PostCommenter.safeGet({ post: meta.postId, commenter: Me.first().id })) {
PostCommenter.create({ post: meta.postId, commenter: Me.first().id })
// we can assume the following because the backend returns the results pre-sorted
// with the currentUser at the beginning
const p = Post.withId(meta.postId)
p.update({ commentersTotal: p.commentersTotal + 1 })
}
break
case CREATE_GROUP:
me = Me.withId(Me.first().id)
me.updateAppending({ memberships: [payload.data.createGroup.id] })
break
case CREATE_JOIN_REQUEST:
if (payload.data.createJoinRequest.request) {
me = Me.first()
const jr = JoinRequest.create({ group: meta.groupId, user: me.id, status: payload.data.createJoinRequest.request.status })
me.updateAppending({ joinRequests: [jr] })
}
break
case CREATE_MESSAGE_PENDING:
Message.create({
id: meta.tempId,
messageThread: meta.messageThreadId,
text: meta.text,
createdAt: new Date().toString(),
creator: Me.first().id })
break
case CREATE_MESSAGE:
Message.withId(meta.tempId).delete()
const message = payload.data.createMessage
MessageThread.withId(message.messageThread.id).newMessageReceived()
break
case DELETE_COMMENT_PENDING:
comment = Comment.withId(meta.id)
comment.delete()
break
case DELETE_GROUP_RELATIONSHIP: {
if (payload.data.deleteGroupRelationship.success) {
const gr = GroupRelationship.safeGet({ parentGroup: meta.parentId, childGroup: meta.childId })
if (gr) {
gr.delete()
clearCacheFor(Group, meta.parentId)
clearCacheFor(Group, meta.childId)
}
}
break
}
case DELETE_GROUP_TOPIC_PENDING:
groupTopic = GroupTopic.withId(meta.id)
groupTopic.delete()
break
case FETCH_GROUP_DETAILS_PENDING: {
// Clear out prerequisite groups so they correclty update with latest data
group = Group.safeGet({ slug: meta.slug })
if (group) {
group.update({ prerequisiteGroups: [] })
}
break
}
case FETCH_GROUP_WELCOME_DATA:
clearCacheFor(Group, meta.id)
break
case FETCH_GROUP_TO_GROUP_JOIN_QUESTIONS: {
const memberships = get('data.me.memberships', payload)
if (memberships) {
memberships.forEach(m => clearCacheFor(Membership, m.id))
}
break
}
case FETCH_MESSAGES_PENDING:
if (meta.reset) {
// this is so that after websocket reconnect events, pagination
// of messages works as expected
Message.filter({ messageThread: meta.id }).delete()
}
break
case FETCH_POSTS_PENDING:
// When looking at group for first time, immediately set lastViewedAt so we know first view has happened
// This is so that we can go to /explore page on first view then every time after go to regular home page
if (meta.slug) {
group = Group.safeGet({ slug: meta.slug })
me = Me.first()
membership = Membership.safeGet({ group: group.id, person: me.id })
membership.update({ lastViewedAt: (new Date()).toISOString() })
}
break
case INVITE_CHILD_TO_JOIN_PARENT_GROUP: {
const newGroupRelationship = payload.data.inviteGroupToJoinParent.groupRelationship
if (newGroupRelationship) {
clearCacheFor(Group, newGroupRelationship.parentGroup.id)
clearCacheFor(Group, newGroupRelationship.childGroup.id)
} else {
const newGroupRelationshipInvite = payload.data.inviteGroupToJoinParent.groupRelationshipInvite
if (newGroupRelationshipInvite) {
clearCacheFor(Group, newGroupRelationshipInvite.toGroup.id)
clearCacheFor(Group, newGroupRelationshipInvite.fromGroup.id)
}
}
break
}
case INVITE_PEOPLE_TO_EVENT_PENDING:
meta.inviteeIds.map(inviteeId => {
EventInvitation.create({
event: meta.eventId,
person: inviteeId
})
})
clearCacheFor(Post, meta.eventId)
break
case JOIN_PROJECT_PENDING:
me = Me.first()
ProjectMember.create({ post: meta.id, member: me.id })
clearCacheFor(Post, meta.id)
break
case LEAVE_GROUP:
me = Me.first()
membership = find(m => m.group.id === meta.id, me.memberships.toModelArray())
if (membership) membership.delete()
membership = Membership.safeGet({ group: meta.id, person: me.id })
if (membership) membership.delete()
break
case LEAVE_PROJECT_PENDING:
me = Me.first()
const projectMember = find(
m => String(m.member.id) === String(me.id) && String(m.post.id) === String(meta.id),
ProjectMember.all().toModelArray()
)
if (projectMember) {
projectMember.delete()
clearCacheFor(Post, meta.id)
}
break
case PROCESS_STRIPE_TOKEN_PENDING:
post = Post.withId(meta.postId)
const totalContributions = Number(post.totalContributions) + Number(meta.amount)
post.update({
totalContributions
})
break
case REMOVE_MODERATOR_PENDING:
group = Group.withId(meta.groupId)
const moderators = group.moderators.filter(m =>
m.id !== meta.personId)
.toModelArray()
group.update({ moderators })
break
case RESET_NEW_POST_COUNT_PENDING:
if (meta.type === 'GroupTopic') {
session.GroupTopic.withId(meta.id).update({ newPostCount: 0 })
} else if (meta.type === 'Membership') {
me = Me.first()
const membership = Membership.safeGet({ group: meta.id, person: me.id })
membership && membership.update({ newPostCount: 0 })
}
break
case REMOVE_SKILL_PENDING:
person = Person.withId(Me.first().id)
person.skills.remove(meta.skillId)
break
case REMOVE_SKILL_TO_LEARN_PENDING:
person = Person.withId(Me.first().id)
person.skillsToLearn.remove(meta.skillId)
break
case REMOVE_SKILL_FROM_GROUP_PENDING:
group = Group.withId(meta.groupId)
group.suggestedSkills.remove(meta.skillId)
clearCacheFor(Group, meta.groupId)
break
case REQUEST_FOR_CHILD_TO_JOIN_PARENT_GROUP: {
const newGroupRelationship = payload.data.requestToAddGroupToParent.groupRelationship
if (newGroupRelationship) {
clearCacheFor(Group, newGroupRelationship.parentGroup.id)
clearCacheFor(Group, newGroupRelationship.childGroup.id)
} else {
const newGroupRelationshipInvite = payload.data.requestToAddGroupToParent.groupRelationshipInvite
if (newGroupRelationshipInvite) {
clearCacheFor(Group, newGroupRelationshipInvite.toGroup.id)
clearCacheFor(Group, newGroupRelationshipInvite.fromGroup.id)
}
}
break
}
case RESPOND_TO_EVENT_PENDING:
const event = Post.withId(meta.id)
event.update({ myEventResponse: meta.response })
break
case TOGGLE_GROUP_TOPIC_SUBSCRIBE_PENDING:
groupTopic = GroupTopic.get({ topic: meta.topicId, group: meta.groupId })
groupTopic.update({
followersTotal: groupTopic.followersTotal + (meta.isSubscribing ? 1 : -1),
isSubscribed: !!meta.isSubscribing
})
break
case UPDATE_ALL_MEMBERSHIP_SETTINGS_PENDING:
const memberships = session.Membership.all()
memberships.toModelArray().map(membership => {
membership.update({
settings: {
...membership.settings,
...meta.settings
}
})
})
break
case UPDATE_COMMENT_PENDING:
comment = Comment.withId(meta.id)
comment.update(meta.data)
break
case UPDATE_GROUP_SETTINGS_PENDING:
group = Group.withId(meta.id)
group.update(meta.changes)
me = Me.first()
// Clear out prerequisiteGroups so they can be reset when the UPDATE completes
group.update({ prerequisiteGroups: [] })
// Triggers an update to redux-orm for the membership
membership = Membership.safeGet({ group: meta.id, person: me.id }).update({ forceUpdate: new Date() })
break
case UPDATE_GROUP_SETTINGS:
// Set new join questions in the ORM
if (payload.data.updateGroupSettings && (payload.data.updateGroupSettings.joinQuestions || payload.data.updateGroupSettings.prerequisiteGroups)) {
group = Group.withId(meta.id)
clearCacheFor(Group, meta.id)
}
break
case UPDATE_GROUP_TOPIC_PENDING:
groupTopic = GroupTopic.withId(meta.id)
groupTopic.update(meta.data)
clearCacheFor(GroupTopic, meta.id)
break
case UPDATE_MEMBERSHIP_SETTINGS_PENDING:
me = Me.first()
membership = Membership.safeGet({ group: meta.groupId, person: me.id })
if (!membership) break
membership.update({
settings: {
...membership.settings,
...meta.settings
}
})
break
case UPDATE_POST_PENDING:
// deleting all attachments and removing topics here because we restore them from the result of the UPDATE_POST action
post = Post.withId(meta.id)
post.attachments.toModelArray().map(a => a.delete())
post.update({ topics: [] })
break
case UPDATE_POST: {
// This is needed right now to make sure posts update in real time on the landing page
if (payload.data.updatePost.groups) {
payload.data.updatePost.groups.forEach(g => clearCacheFor(Group, g.id))
}
break
}
case UPDATE_THREAD_READ_TIME:
MessageThread.withId(meta.id).markAsRead()
break
case UPDATE_USER_SETTINGS_PENDING:
case UPDATE_USER_SETTINGS_GLOBAL_PENDING:
me = Me.first()
const changes = {
...meta.changes,
settings: {
...me.settings,
...meta.changes.settings
}
}
me.update(changes)
break
case UPDATE_WIDGET: {
clearCacheFor(Group, payload.data.updateWidget.group.id)
}
case USE_INVITATION:
me = Me.first()
me.updateAppending({ memberships: [payload.data.useInvitation.membership.id] })
Invitation.filter({ email: me.email, group: payload.data.useInvitation.membership.group.id }).delete()
break
case VOTE_ON_POST_PENDING:
post = session.Post.withId(meta.postId)
if (post.myVote) {
!meta.isUpvote && post.update({ myVote: false, votesTotal: (post.votesTotal || 1) - 1 })
} else {
meta.isUpvote && post.update({ myVote: true, votesTotal: (post.votesTotal || 0) + 1 })
}
break
}
values(sessionReducers).forEach(fn => fn(session, action))
return session.state
}