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
|
import AsyncStorage from '@react-native-community/async-storage';
import RNFetchBlob from 'rn-fetch-blob';
import {
CHECK_MOMENT_UPLOAD_DONE_PROCESSING_ENDPOINT,
MOMENTS_ENDPOINT,
MOMENTTAG_ENDPOINT,
MOMENT_TAGS_ENDPOINT,
MOMENT_THUMBNAIL_ENDPOINT,
MOMENT_VIEW_COUNT_API,
PRESIGNED_URL_ENDPOINT,
TAGG_CUSTOMER_SUPPORT,
} from '../constants';
import {
ERROR_SOMETHING_WENT_WRONG,
ERROR_SOMETHING_WENT_WRONG_REFRESH,
} from '../constants/strings';
import {MomentPostType, MomentTagType, PresignedURLResponse} from '../types';
import {checkImageUploadStatus} from '../utils';
export const postMoment = async (
uri: string,
caption: string,
category: string,
) => {
try {
const request = new FormData();
const fileExtension = uri.split('.').pop();
const extension = fileExtension ? `.${fileExtension}` : '.jpg';
request.append('image', {
uri: uri,
name: Math.random().toString(36).substring(7) + extension,
type: 'image/jpg',
});
request.append('moment', category);
request.append('captions', JSON.stringify({image: caption}));
const token = await AsyncStorage.getItem('token');
let response = await fetch(MOMENTS_ENDPOINT, {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
Authorization: 'Token ' + token,
},
body: request,
});
let statusCode = response.status;
let data: {
moments: any;
moment_id: string;
profile_completion_stage: number;
} = await response.json();
if (statusCode === 200 && checkImageUploadStatus(data.moments)) {
return data;
}
} catch (err) {
console.log(err);
}
return undefined;
};
export const patchMoment = async (
momentId: string,
caption: string,
category: string,
tags: {
x: number;
y: number;
z: number;
user_id: string;
}[],
) => {
try {
const request = new FormData();
request.append('moment_id', momentId);
request.append('caption', caption);
request.append('category', category);
request.append('tags', JSON.stringify(tags));
const token = await AsyncStorage.getItem('token');
let response = await fetch(MOMENTS_ENDPOINT, {
method: 'PATCH',
headers: {
'Content-Type': 'multipart/form-data',
Authorization: 'Token ' + token,
},
body: request,
});
let statusCode = response.status;
return statusCode === 200 || statusCode === 201;
} catch (err) {
console.log(err);
}
return false;
};
export const loadMoments = async (userId: string, token: string) => {
try {
const response = await fetch(MOMENTS_ENDPOINT + '?user_id=' + userId, {
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
});
if (response.status === 200) {
const typedData: MomentPostType[] = await response.json();
return typedData;
}
} catch (err) {
console.log(err);
}
return [];
};
export const deleteMoment = async (momentId: string) => {
try {
const token = await AsyncStorage.getItem('token');
const response = await fetch(MOMENTS_ENDPOINT + `${momentId}/`, {
method: 'DELETE',
headers: {
Authorization: 'Token ' + token,
},
});
return response.status === 200;
} catch (error) {
console.log(error);
console.log('Unable to delete moment', momentId);
return false;
}
};
export const loadMomentThumbnail = async (momentId: string) => {
try {
const token = await AsyncStorage.getItem('token');
const response = await RNFetchBlob.config({
fileCache: false,
appendExt: 'jpg',
}).fetch('GET', MOMENT_THUMBNAIL_ENDPOINT + `${momentId}/`, {
Authorization: 'Token ' + token,
});
const status = response.info().status;
if (status === 200) {
return response.path();
} else {
return undefined;
}
} catch (error) {
console.log(error);
return undefined;
}
};
export const loadMomentTags = async (moment_id: string) => {
try {
const token = await AsyncStorage.getItem('token');
const response = await fetch(
MOMENT_TAGS_ENDPOINT + `?moment_id=${moment_id}`,
{
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
},
);
if (response.status === 200) {
const tags: MomentTagType[] = await response.json();
return tags;
}
} catch (error) {
console.error(error);
return [];
}
};
export const postMomentTags = async (
moment_id: string,
tags: {
x: number;
y: number;
z: number;
user_id: string;
}[],
) => {
try {
const token = await AsyncStorage.getItem('token');
const form = new FormData();
form.append('moment_id', moment_id);
form.append('tags', JSON.stringify(tags));
const response = await fetch(MOMENTTAG_ENDPOINT, {
method: 'POST',
headers: {
Authorization: 'Token ' + token,
},
body: form,
});
return response.status === 201 || response.status === 200;
} catch (error) {
console.error(error);
return false;
}
};
export const deleteMomentTag = async (moment_tag_id: string) => {
try {
const token = await AsyncStorage.getItem('token');
const response = await fetch(MOMENTTAG_ENDPOINT + `${moment_tag_id}/`, {
method: 'DELETE',
headers: {
Authorization: 'Token ' + token,
},
});
return response.status === 200;
} catch (error) {
console.error(error);
return false;
}
};
/**
* This function makes a request to the server in order to provide the client with a presigned URL.
* This is called first, in order for the client to directly upload a file to S3
* @param value: string | undefined
* @returns a PresignedURLResponse object
*/
export const handlePresignedURL = async (
caption: string,
momentCategory: string,
) => {
try {
// TODO: just a random filename for video poc, we should not need to once complete
const randHash = Math.random().toString(36).substring(7);
const filename = `pc_${randHash}.mov`;
const token = await AsyncStorage.getItem('token');
const response = await fetch(PRESIGNED_URL_ENDPOINT, {
method: 'POST',
headers: {
Authorization: 'Token ' + token,
},
body: JSON.stringify({
filename,
caption: caption,
category: momentCategory,
}),
});
const status = response.status;
let data: PresignedURLResponse = await response.json();
if (status === 200) {
return data;
} else {
if (status === 404) {
console.log(
`Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`,
);
} else {
console.log(ERROR_SOMETHING_WENT_WRONG_REFRESH);
}
console.log(response);
}
} catch (error) {
console.log(error);
console.log(ERROR_SOMETHING_WENT_WRONG);
}
};
/**
* This util function takes in the file object and the PresignedURLResponse object, creates form data from the latter,
* and makes a post request to the presigned URL, sending the file object inside of the form data.
* @param filePath: the path to the file, including filename
* @param urlObj PresignedURLResponse | undefined
* @returns responseURL or boolean
*/
export const handleVideoUpload = async (
filePath: string,
urlObj: PresignedURLResponse | undefined,
) => {
try {
if (urlObj === null || urlObj === undefined) {
console.log('Invalid urlObj');
return false;
}
const form = new FormData();
form.append('key', urlObj.response_url.fields.key);
form.append(
'x-amz-algorithm',
urlObj.response_url.fields['x-amz-algorithm'],
);
form.append(
'x-amz-credential',
urlObj.response_url.fields['x-amz-credential'],
);
form.append('x-amz-date', urlObj.response_url.fields['x-amz-date']);
form.append('policy', urlObj.response_url.fields.policy);
form.append(
'x-amz-signature',
urlObj.response_url.fields['x-amz-signature'],
);
form.append('file', {
uri: filePath,
// other types such as 'quicktime' 'image' etc exist, and we can programmatically type this, but for now sticking with simple 'video'
type: 'video',
name: urlObj.response_url.fields.key,
});
const response = await fetch(urlObj.response_url.url, {
method: 'POST',
headers: {
'Content-Type': 'multipart/form-data',
},
body: form,
});
const status = response.status;
if (status === 200 || status === 204) {
console.log('complete');
return true;
} else {
if (status === 404) {
console.log(
`Please make sure that the email / username entered is registered with us. You may contact our customer support at ${TAGG_CUSTOMER_SUPPORT}`,
);
} else {
console.log(ERROR_SOMETHING_WENT_WRONG_REFRESH);
}
}
} catch (error) {
console.log(error);
console.log(ERROR_SOMETHING_WENT_WRONG);
}
return false;
};
/*
* Records a view on a moment
*/
export const increaseMomentViewCount = async (moment_id: string) => {
const token = await AsyncStorage.getItem('token');
const response = await fetch(MOMENT_VIEW_COUNT_API + `${moment_id}/`, {
method: 'PATCH',
headers: {
Authorization: 'Token ' + token,
},
});
if (response.status === 200) {
const {view_count} = await response.json();
return view_count;
}
return;
};
export const checkMomentDoneProcessing = async (momentId: string) => {
try {
const token = await AsyncStorage.getItem('token');
const response = await fetch(
CHECK_MOMENT_UPLOAD_DONE_PROCESSING_ENDPOINT + '?moment_id=' + momentId,
{
method: 'GET',
headers: {
Authorization: 'Token ' + token,
},
},
);
return response.status === 200;
} catch (error) {
console.error(error);
return false;
}
};
|