forked from OpenBazaar/haven
-
Notifications
You must be signed in to change notification settings - Fork 0
/
feed.js
128 lines (118 loc) · 4.06 KB
/
feed.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
import { call, put, takeEvery, select } from 'redux-saga/effects';
import { actions as feedActions } from '../feed';
import { actions as streamActions } from '../stream';
import * as feedApi from '../../api/feed';
import { getUserPeerID } from './sagaSelector';
import { convertFromReference } from '../../utils/feed';
export function* postFeed(action) {
const {
status, images, success, failure,
} = action.payload;
try {
const result = yield call(feedApi.postFeed, status, images);
const postData = yield call(feedApi.fetchFeedItem, result.slug);
const { post: content, ...other } = postData;
const peerID = yield select(getUserPeerID);
const { slug } = content;
const reference = `${peerID}/${slug}`;
const item = {
type: 'POST',
data: {
...content,
...other,
reference,
vendorID: { peerID },
},
};
yield put({ type: feedActions.postFeedSucceed, payload: { reference, item } });
yield put({ type: streamActions.createPost, payload: { reference, data: postData, success, failure } });
} catch (err) {
if (failure) {
failure(err);
}
yield put({ type: feedActions.postFeedFailure, payload: err });
console.log('PostFeed Saga Error: ', err);
}
}
export function* deleteFeedItem(action) {
const { peerID, slug, activityId, reactionId } = action.payload;
const reference = `${peerID}--${slug}`;
try {
yield call(feedApi.deleteFeedItem, slug);
yield put({ type: streamActions.deletePost, payload: { reference, activityId, reactionId } });
} catch (err) {
console.log('DeleteFeed Saga Error: ', err);
}
}
export function* removeFeedItem(action) {
const { peerID, slug, activityId, reactionId } = action.payload;
const reference = `${peerID}--${slug}`;
try {
yield put({ type: streamActions.removePost, payload: { reference, activityId, reactionId } });
} catch (err) {
console.log('RemoveFeed Saga Error: ', err);
}
}
export function* commentFeed(action) {
const {
uuid, comment, reference, activityId, onSuccess,
} = action.payload;
try {
const response = yield call(feedApi.commentFeed, reference, comment);
const { peerID: targetPeerID } = convertFromReference(reference);
yield put({ type: feedActions.commentFeedSucceed, payload: { uuid, slug: response.slug } });
yield put({
type: streamActions.commentPost,
payload: {
targetPeerID,
activityId,
comment,
onSuccess,
},
});
} catch (err) {
yield put({ type: feedActions.commentFeedFailure, payload: { uuid } });
}
}
export function* repostFeed(action) {
const { reference, comment, activityId, originalActivityId, success } = action.payload;
const myPeerId = yield select(getUserPeerID);
try {
const { slug } = yield call(feedApi.repostFeed, reference, comment);
const post = yield call(feedApi.fetchFeedItem, slug);
yield put({
type: streamActions.repostPost,
payload: {
peerID: myPeerId,
originalActivityId,
activityId,
post,
reference,
success,
},
});
} catch (err) {
yield put({ type: feedActions.repostFeedFailure, payload: err });
console.log('RepostFeed Saga Error:', err);
}
}
export function* deleteRepost(action) {
const { reference, activityId, reactionId, originalActivityId, success } = action.payload;
const { slug } = convertFromReference(reference);
try {
yield call(feedApi.deleteFeedItem, slug);
} catch (err) {}
try {
yield put({ type: streamActions.deleteRepost, payload: { activityId, reactionId, reference, originalActivityId, success } });
} catch (err) {
console.log('Delete Repost Saga Error:', err);
}
}
export default function* Feed() {
yield takeEvery(feedActions.commentFeed, commentFeed);
yield takeEvery(feedActions.postFeed, postFeed);
yield takeEvery(feedActions.repostFeed, repostFeed);
yield takeEvery(feedActions.deleteFeedItem, deleteFeedItem);
yield takeEvery(feedActions.removeFeedItem, removeFeedItem);
yield takeEvery(feedActions.deleteRepost, deleteRepost);
}