-
Notifications
You must be signed in to change notification settings - Fork 343
/
mediaSharesUtils.js
101 lines (84 loc) · 2.35 KB
/
mediaSharesUtils.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
import {CONTENT} from '../constants';
const MediaSharesUtils = {};
/**
* parses the relevant values for mediaShares: contentId, disposition
* @param {Object} mediaShares
* @returns {undefined}
*/
MediaSharesUtils.parse = (mediaShares) => {
if (mediaShares) {
return {
contentId: MediaSharesUtils.getContentId(mediaShares),
disposition: MediaSharesUtils.getDisposition(mediaShares)
};
}
return null;
};
/**
* get the previous and current mediaShares values parsed, as well as the boolean updates
* @param {Object} oldShare
* @param {Object} newShare
* @returns {Object}
* previous: {Object} old share, current: {Object} new share,
*/
MediaSharesUtils.getMediaShares = (oldShare, newShare) => {
const previous = oldShare && MediaSharesUtils.parse(oldShare);
const current = newShare && MediaSharesUtils.parse(newShare);
return {
previous,
current
};
};
/**
* get the floor disposition (released, granted)
* @param {Object} mediaShares
* @returns {Boolean} disposition
*/
MediaSharesUtils.getDisposition = (mediaShares) => {
const contentFloor = MediaSharesUtils.extractContentFloor(mediaShares);
return contentFloor ? contentFloor.disposition : null;
};
/**
* extract the content property from media shares
* @param {Object} mediaShares
* @returns {Object}
*/
MediaSharesUtils.extractContent = (mediaShares) => {
if (!mediaShares || !mediaShares.length) {
return null;
}
return mediaShares.find((share) => share.name === CONTENT) || null;
};
/**
* extract the floor property from content object
* @param {Object} content
* @returns {Object}
*/
MediaSharesUtils.extractFloor = (content) => {
if (!content) {
return null;
}
return content.floor;
};
/**
* extract the content's floor from media shares
* @param {Object} mediaShares
* @returns {Object}
*/
MediaSharesUtils.extractContentFloor = (mediaShares) => {
const content = MediaSharesUtils.extractContent(mediaShares);
return MediaSharesUtils.extractFloor(content);
};
/**
* get who is sharing from media shares
* @param {Object} mediaShares
* @returns {Object}
*/
MediaSharesUtils.getContentId = (mediaShares) => {
const contentFloor = MediaSharesUtils.extractContentFloor(mediaShares);
if (!contentFloor || !contentFloor.beneficiary) {
return null;
}
return contentFloor.beneficiary.id;
};
export default MediaSharesUtils;