forked from compdemocracy/polis
-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathindex.js
1728 lines (1464 loc) · 41.7 KB
/
index.js
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
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Copyright (C) 2012-present, The Authors. This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License, version 3, as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see <http://www.gnu.org/licenses/>.
import $ from 'jquery'
import PolisNet from '../util/net'
/* ======= Types ======= */
export const REQUEST_USER = 'REQUEST_USER'
export const RECEIVE_USER = 'RECEIVE_USER'
export const USER_FETCH_ERROR = 'USER_FETCH_ERROR'
export const CREATE_NEW_CONVERSATION = 'CREATE_NEW_CONVERSATION'
export const CREATE_NEW_CONVERSATION_SUCCESS = 'CREATE_NEW_CONVERSATION_SUCCESS'
export const CREATE_NEW_CONVERSATION_ERROR = 'CREATE_NEW_CONVERSATION_ERROR'
export const REQUEST_CONVERSATIONS = 'REQUEST_CONVERSATIONS'
export const RECEIVE_CONVERSATIONS = 'RECEIVE_CONVERSATIONS'
export const CONVERSATIONS_FETCH_ERROR = 'CONVERSATIONS_FETCH_ERROR'
/* zid for clarity - this is conversation config */
export const REQUEST_ZID_METADATA = 'REQUEST_ZID_METADATA'
export const RECEIVE_ZID_METADATA = 'RECEIVE_ZID_METADATA'
export const ZID_METADATA_FETCH_ERROR = 'ZID_METADATA_FETCH_ERROR'
export const ZID_METADATA_RESET = 'ZID_METADATA_RESET'
export const UPDATE_ZID_METADATA_STARTED = 'UPDATE_ZID_METADATA_STARTED'
export const UPDATE_ZID_METADATA_SUCCESS = 'UPDATE_ZID_METADATA_SUCCESS'
export const UPDATE_ZID_METADATA_ERROR = 'UPDATE_ZID_METADATA_ERROR'
export const OPTIMISTIC_ZID_METADATA_UPDATE = 'OPTIMISTIC_ZID_METADATA_UPDATE'
/* report */
export const UPDATE_REPORT_STARTED = 'UPDATE_REPORT_STARTED'
export const UPDATE_REPORT_SUCCESS = 'UPDATE_REPORT_SUCCESS'
export const UPDATE_REPORT_ERROR = 'UPDATE_REPORT_ERROR'
export const OPTIMISTIC_REPORT_UPDATE = 'OPTIMISTIC_REPORT_UPDATE'
/* moderation */
export const REQUEST_COMMENTS = 'REQUEST_COMMENTS'
export const RECEIVE_COMMENTS = 'RECEIVE_COMMENTS'
export const COMMENTS_FETCH_ERROR = 'COMMENTS_FETCH_ERROR'
export const REQUEST_UNMODERATED_COMMENTS = 'REQUEST_UNMODERATED_COMMENTS'
export const RECEIVE_UNMODERATED_COMMENTS = 'RECEIVE_UNMODERATED_COMMENTS'
export const UNMODERATED_COMMENTS_FETCH_ERROR =
'UNMODERATED_COMMENTS_FETCH_ERROR'
export const REQUEST_ACCEPTED_COMMENTS = 'REQUEST_ACCEPTED_COMMENTS'
export const RECEIVE_ACCEPTED_COMMENTS = 'RECEIVE_ACCEPTED_COMMENTS'
export const ACCEPTED_COMMENTS_FETCH_ERROR = 'ACCEPTED_COMMENTS_FETCH_ERROR'
export const REQUEST_REJECTED_COMMENTS = 'REQUEST_REJECTED_COMMENTS'
export const RECEIVE_REJECTED_COMMENTS = 'RECEIVE_REJECTED_COMMENTS'
export const REJECTED_COMMENTS_FETCH_ERROR = 'REJECTED_COMMENTS_FETCH_ERROR'
export const ACCEPT_COMMENT = 'ACCEPT_COMMENT'
export const ACCEPT_COMMENT_SUCCESS = 'ACCEPT_COMMENT_SUCCESS'
export const ACCEPT_COMMENT_ERROR = 'ACCEPT_COMMENT_ERROR'
export const REJECT_COMMENT = 'REJECT_COMMENT'
export const REJECT_COMMENT_SUCCESS = 'REJECT_COMMENT_SUCCESS'
export const REJECT_COMMENT_ERROR = 'REJECT_COMMENT_ERROR'
export const COMMENT_IS_META = 'COMMENT_IS_META'
export const COMMENT_IS_META_SUCCESS = 'COMMENT_IS_META_SUCCESS'
export const COMMENT_IS_META_ERROR = 'COMMENT_IS_META_ERROR'
export const REQUEST_PARTICIPANTS = 'REQUEST_PARTICIPANTS'
export const RECEIVE_PARTICIPANTS = 'RECEIVE_PARTICIPANTS'
export const PARTICIPANTS_FETCH_ERROR = 'PARTICIPANTS_FETCH_ERROR'
export const REQUEST_DEFAULT_PARTICIPANTS = 'REQUEST_DEFAULT_PARTICIPANTS'
export const RECEIVE_DEFAULT_PARTICIPANTS = 'RECEIVE_DEFAULT_PARTICIPANTS'
export const DEFAULT_PARTICIPANTS_FETCH_ERROR =
'DEFAULT_PARTICIPANTS_FETCH_ERROR'
export const REQUEST_FEATURED_PARTICIPANTS = 'REQUEST_FEATURED_PARTICIPANTS'
export const RECEIVE_FEATURED_PARTICIPANTS = 'RECEIVE_FEATURED_PARTICIPANTS'
export const FEATURED_PARTICIPANTS_FETCH_ERROR =
'FEATURED_PARTICIPANTS_FETCH_ERROR'
export const REQUEST_HIDDEN_PARTICIPANTS = 'REQUEST_HIDDEN_PARTICIPANTS'
export const RECEIVE_HIDDEN_PARTICIPANTS = 'RECEIVE_HIDDEN_PARTICIPANTS'
export const HIDDEN_PARTICIPANTS_FETCH_ERROR = 'HIDDEN_PARTICIPANTS_FETCH_ERROR'
/* participant actions */
export const FEATURE_PARTICIPANT = 'FEATURE_PARTICIPANT'
export const FEATURE_PARTICIPANT_SUCCESS = 'FEATURE_PARTICIPANT_SUCCESS'
export const FEATURE_PARTICIPANT_ERROR = 'FEATURE_PARTICIPANT_ERROR'
export const HIDE_PARTICIPANT = 'HIDE_PARTICIPANT'
export const HIDE_PARTICIPANT_SUCCESS = 'HIDE_PARTICIPANT_SUCCESS'
export const HIDE_PARTICIPANT_ERROR = 'HIDE_PARTICIPANT_ERROR'
/* submit seed comment */
export const SEED_COMMENT_LOCAL_UPDATE = 'SEED_COMMENT_LOCAL_UPDATE'
export const SUBMIT_SEED_COMMENT = 'SUBMIT_SEED_COMMENT'
export const SUBMIT_SEED_COMMENT_SUCCESS = 'SUBMIT_SEED_COMMENT_SUCCESS'
export const SUBMIT_SEED_COMMENT_ERROR = 'SUBMIT_SEED_COMMENT_ERROR'
/* submit tweet seed comment */
export const SEED_COMMENT_TWEET_LOCAL_UPDATE = 'SEED_COMMENT_TWEET_LOCAL_UPDATE'
export const SUBMIT_SEED_COMMENT_TWEET = 'SUBMIT_SEED_COMMENT_TWEET'
export const SUBMIT_SEED_COMMENT_TWEET_SUCCESS =
'SUBMIT_SEED_COMMENT_TWEET_SUCCESS'
export const SUBMIT_SEED_COMMENT_TWEET_ERROR = 'SUBMIT_SEED_COMMENT_TWEET_ERROR'
export const REQUEST_SEED_COMMENTS = 'REQUEST_SEED_COMMENTS'
export const RECEIVE_SEED_COMMENTS = 'RECEIVE_SEED_COMMENTS'
export const SEED_COMMENTS_FETCH_ERROR = 'SEED_COMMENTS_FETCH_ERROR'
/* conversation stats */
export const REQUEST_CONVERSATION_STATS = 'REQUEST_CONVERSATION_STATS'
export const RECEIVE_CONVERSATION_STATS = 'RECEIVE_CONVERSATION_STATS'
export const CONVERSATION_STATS_FETCH_ERROR = 'CONVERSATION_STATS_FETCH_ERROR'
export const DATA_EXPORT_STARTED = 'DATA_EXPORT_STARTED'
export const DATA_EXPORT_SUCCESS = 'DATA_EXPORT_SUCCESS'
export const DATA_EXPORT_ERROR = 'DATA_EXPORT_ERROR'
export const CREATEUSER_INITIATED = 'CREATEUSER_INITIATED'
// export const CREATEUSER_SUCCESSFUL = "CREATEUSER_SUCCESSFUL";
export const CREATEUSER_ERROR = 'CREATEUSER_ERROR'
export const SIGNIN_INITIATED = 'SIGNIN_INITIATED'
// export const SIGNIN_SUCCESSFUL = "SIGNIN_SUCCESSFUL";
export const SIGNIN_ERROR = 'SIGNIN_ERROR'
export const SIGNOUT_INITIATED = 'SIGNOUT_INITIATED'
export const SIGNOUT_SUCCESSFUL = 'SIGNOUT_SUCCESSFUL'
export const SIGNOUT_ERROR = 'SIGNOUT_ERROR'
export const PWRESET_INIT_INITIATED = 'PWRESET_INIT_INITIATED'
export const PWRESET_INIT_SUCCESS = 'PWRESET_INIT_SUCCESS'
export const PWRESET_INIT_ERROR = 'PWRESET_INIT_ERROR'
export const PWRESET_INITIATED = 'PWRESET_INITIATED'
export const PWRESET_SUCCESS = 'PWRESET_SUCCESS'
export const PWRESET_ERROR = 'PWRESET_ERROR'
export const FACEBOOK_SIGNIN_INITIATED = 'FACEBOOK_SIGNIN_INITIATED'
export const FACEBOOK_SIGNIN_SUCCESSFUL = 'FACEBOOK_SIGNIN_SUCCESSFUL'
export const FACEBOOK_SIGNIN_FAILED = 'FACEBOOK_SIGNIN_FAILED'
export const SUBMIT_CONTRIB = 'SUBMIT_CONTRIB'
export const SUBMIT_CONTRIB_SUCCESS = 'SUBMIT_CONTRIB_SUCCESS'
export const SUBMIT_CONTRIB_ERROR = 'SUBMIT_CONTRIB_ERROR'
/* MATH */
export const REQUEST_MATH = 'REQUEST_MATH'
export const RECEIVE_MATH = 'RECEIVE_MATH'
export const MATH_FETCH_ERROR = 'MATH_FETCH_ERROR'
/* ======= Actions ======= */
/*
populate is the function the component calls
fetch is the api call itself
request tells everyone we"re loading
receive proxies the data to the store
*/
/* User */
const requestUser = () => {
return {
type: REQUEST_USER
}
}
const receiveUser = (data) => {
return {
type: RECEIVE_USER,
data: data
}
}
const userFetchError = (err) => {
return {
type: USER_FETCH_ERROR,
status: err.status,
data: err
}
}
const fetchUser = () => {
return PolisNet.polisGet('/api/v3/users', { errIfNoAuth: true })
}
export const populateUserStore = () => {
return (dispatch) => {
dispatch(requestUser())
return fetchUser().then(
(res) => dispatch(receiveUser(res)),
(err) => dispatch(userFetchError(err))
)
}
}
/* signin */
const signinInitiated = () => {
return {
type: SIGNIN_INITIATED
}
}
// SIGNIN_SUCCESSFUL Not needed since redirecting to clear password from memory
const signinError = (err) => {
return {
type: SIGNIN_ERROR,
data: err
}
}
const signinPost = (attrs) => {
return PolisNet.polisPost('/api/v3/auth/login', attrs)
}
export const doSignin = (attrs) => {
return (dispatch) => {
dispatch(signinInitiated())
return signinPost(attrs).then(
() => {
setTimeout(() => {
// Force page to load so we can be sure the password is cleared from memory
// delay a bit so the cookie has time to set
dispatch({ type: 'signin completed successfully' })
window.location = '/'
}, 3000)
},
(err) => dispatch(signinError(err))
)
}
}
/* createUser */
const createUserInitiated = () => {
return {
type: CREATEUSER_INITIATED
}
}
// SIGNIN_SUCCESSFUL Not needed since redirecting to clear password from memory
const createUserError = (err) => {
return {
type: CREATEUSER_ERROR,
data: err
}
}
const createUserPost = (attrs) => {
return PolisNet.polisPost('/api/v3/auth/new', attrs)
}
export const doCreateUser = (attrs, dest) => {
return (dispatch) => {
dispatch(createUserInitiated())
return createUserPost(attrs).then(
() => {
setTimeout(() => {
// Force page to load so we can be sure the password is cleared from memory
// delay a bit so the cookie has time to set
window.location = dest || ''
}, 3000)
},
(err) => dispatch(createUserError(err))
)
}
}
/* passwordResetInit */
const passwordResetInitInitiated = () => {
return {
type: PWRESET_INIT_INITIATED
}
}
const passwordResetInitSuccess = () => {
return {
type: PWRESET_INIT_SUCCESS
}
}
const passwordResetInitError = (err) => {
return {
type: PWRESET_INIT_ERROR,
data: err
}
}
const passwordResetInitPost = (attrs) => {
return PolisNet.polisPost('/api/v3/auth/pwresettoken', attrs)
}
export const doPasswordResetInit = (attrs) => {
return (dispatch) => {
dispatch(passwordResetInitInitiated())
return passwordResetInitPost(attrs).then(
() => {
setTimeout(() => {
// Force page to load so we can be sure the password is cleared from memory
// delay a bit so the cookie has time to set
window.location = '/pwresetinit/done'
}, 3000)
return dispatch(passwordResetInitSuccess())
},
(err) => dispatch(passwordResetInitError(err))
)
}
}
/* passwordReset */
const passwordResetInitiated = () => {
return {
type: PWRESET_INITIATED
}
}
const passwordResetSuccess = () => {
return {
type: PWRESET_SUCCESS
}
}
const passwordResetError = (err) => {
return {
type: PWRESET_ERROR,
data: err
}
}
const passwordResetPost = (attrs) => {
return PolisNet.polisPost('/api/v3/auth/password', attrs)
}
export const doPasswordReset = (attrs) => {
return (dispatch) => {
dispatch(passwordResetInitiated())
return passwordResetPost(attrs).then(
() => {
setTimeout(() => {
// Force page to load so we can be sure the password is cleared from memory
// delay a bit so the cookie has time to set
window.location = '/'
}, 3000)
return dispatch(passwordResetSuccess())
},
(err) => dispatch(passwordResetError(err))
)
}
}
/* facebook */
const facebookSigninInitiated = () => {
return {
type: FACEBOOK_SIGNIN_INITIATED
}
}
// FIXME
// eslint-disable-next-line no-unused-vars
const facebookSigninSuccessful = () => {
return {
type: FACEBOOK_SIGNIN_SUCCESSFUL
}
}
const facebookSigninFailed = (errorCode) => {
return {
type: FACEBOOK_SIGNIN_FAILED,
errorCode: errorCode
}
}
const getFriends = () => {
const dfd = $.Deferred()
const getMoreFriends = (friendsSoFar, urlForNextCall) => {
return $.get(urlForNextCall).then((response) => {
if (response.data.length) {
for (let i = 0; i < response.data.length; i++) {
friendsSoFar.push(response.data[i])
}
if (response.paging.next) {
return getMoreFriends(friendsSoFar, response.paging.next)
}
return friendsSoFar
} else {
return friendsSoFar
}
})
}
FB.api('/me/friends', (response) => {
if (response && !response.error) {
const friendsSoFar = response.data
if (response.data.length && response.paging.next) {
getMoreFriends(friendsSoFar, response.paging.next).then(
dfd.resolve,
dfd.reject
)
} else {
dfd.resolve(friendsSoFar || [])
}
} else {
// "failed to find friends"
dfd.reject(response)
}
})
return dfd.promise()
}
const getInfo = () => {
const dfd = $.Deferred()
FB.api('/me', (response) => {
// {"id":"10152802017421079"
// "email":"[email protected]"
// "first_name":"Mike"
// "gender":"male"
// "last_name":"Bjorkegren"
// "link":"https://www.facebook.com/app_scoped_user_id/10152802017421079/"
// "locale":"en_US"
// "location": {
// "id": "110843418940484", ------------> we can make another call to get the lat,lng for this
// "name": "Seattle, Washington"
// },
// "name":"Mike Bjorkegren"
// "timezone":-7
// "updated_time":"2014-07-03T06:38:02+0000"
// "verified":true}
if (response && !response.error) {
if (response.location && response.location.id) {
FB.api('/' + response.location.id, (locationResponse) => {
if (locationResponse) {
response.locationInfo = locationResponse
}
dfd.resolve(response)
})
} else {
dfd.resolve(response)
}
} else {
// alert("failed to find data");
dfd.reject(response)
}
})
return dfd.promise()
}
const saveFacebookFriendsData = (data, dest, dispatch) => {
$.ajax({
url: '/api/v3/auth/facebook',
contentType: 'application/json; charset=utf-8',
headers: {
'Cache-Control': 'max-age=0'
},
xhrFields: {
withCredentials: true
},
dataType: 'json',
data: JSON.stringify(data),
type: 'POST'
}).then(
() => {
setTimeout(() => {
// Force page to load so we can be sure the old user"s state is cleared from memory
// delay a bit so the cookies have time to clear too.
window.location = dest || '/'
}, 1000)
},
(err) => {
console.dir(err)
if (
err.responseText &&
/polis_err_user_with_this_email_exists/.test(err.responseText)
) {
// Todo handle
// var password = prompt("A pol.is user "+data.fb_email+", the same email address as associted with your facebook account, already exists. Enter your pol.is password to enable facebook login for your pol.is account.");
// that.linkMode = true;
dispatch(facebookSigninFailed('polis_err_user_with_this_email_exists')) // handle case user already exists enter your password
// that.model.set({
// create: false, // don"t show create account stuff, account exists.
// linkMode: true,
// email: data.fb_email,
// });
} else {
alert('error logging in with Facebook')
}
}
)
}
const processFacebookFriendsData = (
response,
dest,
dispatch,
optionalPassword
) => {
return (fb_public_profile, friendsData) => {
// alert(JSON.stringify(friendsData));
const data = {
fb_public_profile: JSON.stringify(fb_public_profile),
fb_friends_response: JSON.stringify(friendsData),
response: JSON.stringify(response)
}
// cleaner as fb_email: fb_public_profile.email ? fb_public_profile.email : null
if (fb_public_profile.email) {
data.fb_email = fb_public_profile.email
} else {
data.provided_email = prompt('Please enter your email address.')
}
const hname = [
fb_public_profile.first_name,
fb_public_profile.last_name
].join(' ')
if (hname.length) {
data.hname = hname
}
if (
response &&
response.authResponse &&
response.authResponse.grantedScopes
) {
data.fb_granted_scopes = response.authResponse.grantedScopes
}
if (optionalPassword) {
data.password = optionalPassword
}
saveFacebookFriendsData(data, dest, dispatch)
}
}
const onFbLoginOk = (response, dest, dispatch, optionalPassword) => {
$.when(getInfo(), getFriends()).then(
processFacebookFriendsData(response, dest, dispatch, optionalPassword),
(err) => {
console.error(err)
}
)
}
const callFacebookLoginAPI = (dest, dispatch, optionalPassword) => {
console.log('ringing facebook...')
FB.login(
(res) => {
return onFbLoginOk(res, dest, dispatch, optionalPassword)
},
{
return_scopes: true, // response should contain the scopes the user allowed
scope: [
// "taggable_friends", // requires review.
// invitable_friends NOTE: only for games with a fb Canvas presence, so don"t use this
'public_profile',
'user_friends',
'email'
].join(',')
}
)
}
export const doFacebookSignin = (dest, optionalPassword) => {
return (dispatch) => {
dispatch(facebookSigninInitiated())
return callFacebookLoginAPI(dest, dispatch, optionalPassword)
}
}
/* signout */
const signoutInitiated = () => {
return {
type: SIGNOUT_INITIATED
}
}
// SIGNOUT_SUCCESSFUL Not needed since redirecting to clear old user"s state from memory
const signoutError = (err) => {
return {
type: SIGNOUT_ERROR,
data: err
}
}
const signoutPost = (dest) => {
// relying on server to clear cookies
return $.ajax({
type: 'POST',
url: '/api/v3/auth/deregister',
data: {},
dataType: 'text' // server returns an empty response, so can"t parse as JSON
})
}
export const doSignout = (dest) => {
return (dispatch) => {
dispatch(signoutInitiated())
return signoutPost().then(
(res) => {
setTimeout(() => {
// Force page to load so we can be sure the old user"s state is cleared from memory
// delay a bit so the cookies have time to clear too.
window.location = dest || '/home'
}, 1000)
},
(err) => dispatch(signoutError(err))
)
}
}
/* Conversations */
const requestConversations = () => {
return {
type: REQUEST_CONVERSATIONS
}
}
const receiveConversations = (data) => {
return {
type: RECEIVE_CONVERSATIONS,
data: data
}
}
const conversationsError = (err) => {
return {
type: CONVERSATIONS_FETCH_ERROR,
data: err
}
}
const fetchConversations = () => {
return $.get('/api/v3/conversations?include_all_conversations_i_am_in=true')
}
export const populateConversationsStore = () => {
return (dispatch) => {
dispatch(requestConversations())
return fetchConversations().then(
(res) => dispatch(receiveConversations(res)),
(err) => dispatch(conversationsError(err))
)
}
}
/* zid metadata */
const requestZidMetadata = (conversation_id) => {
return {
type: REQUEST_ZID_METADATA,
data: {
conversation_id: conversation_id
}
}
}
const receiveZidMetadata = (data) => {
return {
type: RECEIVE_ZID_METADATA,
data: data
}
}
const zidMetadataFetchError = (err) => {
return {
type: ZID_METADATA_FETCH_ERROR,
data: err
}
}
export const resetMetadataStore = () => {
return {
type: ZID_METADATA_RESET
}
}
const fetchZidMetadata = (conversation_id) => {
return $.get('/api/v3/conversations?conversation_id=' + conversation_id)
}
export const populateZidMetadataStore = (conversation_id) => {
return (dispatch, getState) => {
const state = getState()
const hasConversationId =
state.zid_metadata &&
state.zid_metadata.zid_metadata &&
state.zid_metadata.zid_metadata.conversation_id
const isLoading = state.zid_metadata.loading
// NOTE: if there are multiple calls outstanding this may be wrong.
const isLoadingThisConversation =
state.zid_metadata.conversation_id === conversation_id && isLoading
if (isLoadingThisConversation) {
return
}
// don"t fetch again if we already have data loaded for that conversation.
if (
hasConversationId &&
state.zid_metadata.zid_metadata.conversation_id === conversation_id
) {
return
}
dispatch(requestZidMetadata(conversation_id))
return fetchZidMetadata(conversation_id).then(
(res) => dispatch(receiveZidMetadata(res)),
(err) => dispatch(zidMetadataFetchError(err))
)
}
}
/* zid metadata update */
const updateZidMetadataStarted = () => {
return {
type: UPDATE_ZID_METADATA_STARTED
}
}
const updateZidMetadataSuccess = (data) => {
return {
type: UPDATE_ZID_METADATA_SUCCESS,
data: data
}
}
const updateZidMetadataError = (err) => {
return {
type: UPDATE_ZID_METADATA_ERROR,
data: err
}
}
const updateZidMetadata = (zm, field, value) => {
const data = {}
data[field] = value
return $.ajax({
url: '/api/v3/conversations',
method: 'PUT',
contentType: 'application/json; charset=utf-8',
headers: { 'Cache-Control': 'max-age=0' },
xhrFields: { withCredentials: true },
dataType: 'json',
data: JSON.stringify(Object.assign({}, zm, data))
})
}
export const handleZidMetadataUpdate = (zm, field, value) => {
return (dispatch) => {
dispatch(updateZidMetadataStarted())
return updateZidMetadata(zm, field, value)
.then((res) => dispatch(updateZidMetadataSuccess(res)))
.fail((err) => dispatch(updateZidMetadataError(err)))
}
}
export const optimisticZidMetadataUpdateOnTyping = (zm, field, value) => {
zm[field] = value
return {
type: OPTIMISTIC_ZID_METADATA_UPDATE,
data: zm
}
}
/* seed comments submit */
export const seedCommentChanged = (text) => {
return {
type: SEED_COMMENT_LOCAL_UPDATE,
text: text
}
}
const submitSeedCommentStart = () => {
return {
type: SUBMIT_SEED_COMMENT
}
}
const submitSeedCommentPostSuccess = () => {
console.log('seed comment post success')
return {
type: SUBMIT_SEED_COMMENT_SUCCESS
}
}
const submitSeedCommentPostError = (err) => {
return {
type: SUBMIT_SEED_COMMENT_ERROR,
data: err
}
}
const postSeedComment = (comment) => {
return PolisNet.polisPost('/api/v3/comments', comment)
}
export const handleSeedCommentSubmit = (comment) => {
return (dispatch) => {
dispatch(submitSeedCommentStart())
return postSeedComment(comment)
.then(
(res) => dispatch(submitSeedCommentPostSuccess(res)),
(err) => dispatch(submitSeedCommentPostError(err))
)
.then(dispatch(populateAllCommentStores(comment.conversation_id)))
}
}
// FIXME
// eslint-disable-next-line no-unused-vars
const makeStandardStart = (type) => {
return {
type: type
}
}
// FIXME
// eslint-disable-next-line no-unused-vars
const makeStandardError = (type, err) => {
return {
type: type,
data: err
}
}
// FIXME
// eslint-disable-next-line no-unused-vars
const makeStandardSuccess = (type, data) => {
return {
type: type,
data: data
}
}
/* seed tweets submit */
export const seedCommentTweetChanged = (text) => {
return {
type: SEED_COMMENT_TWEET_LOCAL_UPDATE,
text: text
}
}
const submitSeedCommentTweetStart = () => {
return {
type: SUBMIT_SEED_COMMENT_TWEET
}
}
const submitSeedCommentPostTweetSuccess = () => {
return {
type: SUBMIT_SEED_COMMENT_TWEET_SUCCESS
}
}
const submitSeedCommentPostTweetError = (err) => {
return {
type: SUBMIT_SEED_COMMENT_TWEET_ERROR,
data: err
}
}
const postSeedCommentTweet = (o) => {
return PolisNet.polisPost('/api/v3/comments', o)
}
export const handleSeedCommentTweetSubmit = (o) => {
return (dispatch) => {
dispatch(submitSeedCommentTweetStart())
return postSeedCommentTweet(o)
.then(
(res) => dispatch(submitSeedCommentPostTweetSuccess(res)),
(err) => dispatch(submitSeedCommentPostTweetError(err))
)
.then(dispatch(populateAllCommentStores(o.conversation_id)))
}
}
/* seed comments fetch */
// const requestSeedComments = () => {
// return {
// type: REQUEST_SEED_COMMENTS,
// };
// };
// const receiveSeedComments = (data) => {
// return {
// type: RECEIVE_SEED_COMMENTS,
// data: data
// };
// };
// const seedCommentsFetchError = (err) => {
// return {
// type: SEED_COMMENTS_FETCH_ERROR,
// data: err
// }
// }
// const fetchSeedComments = (conversation_id) => {
// return $.get("/api/v3/comments?moderation=true&mod=0&conversation_id=" + conversation_id);
// }
// export const populateSeedCommentStore = (conversation_id) => {
// return (dispatch) => {
// dispatch(requestSeedComments())
// return fetchSeedComments(conversation_id).then(
// res => dispatch(receiveSeedComments(res)),
// err => dispatch(seedCommentsFetchError(err))
// )
// }
// }
/* create conversation */
const createConversationStart = () => {
return {
type: CREATE_NEW_CONVERSATION
}
}
const createConversationPostSuccess = (res) => {
return {
type: CREATE_NEW_CONVERSATION_SUCCESS,
data: res
}
}
const createConversationPostError = (err) => {
return {
type: CREATE_NEW_CONVERSATION_ERROR,
data: err
}
}
const postCreateConversation = () => {
return PolisNet.polisPost('/api/v3/conversations', {
is_draft: true,
is_active: true
})
}
export const handleCreateConversationSubmit = (routeTo) => {
return (dispatch) => {
dispatch(createConversationStart())
return postCreateConversation()
.then(
(res) => {
dispatch(createConversationPostSuccess(res))
return res
},
(err) => dispatch(createConversationPostError(err))
)
.then((res) => {
window.location = '/m/' + res.conversation_id
})
}
}
/* request all comments */
const requestComments = () => {
return {
type: REQUEST_COMMENTS
}
}
const receiveComments = (data) => {
return {