musora-content-services 1.0.163 → 1.0.164
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/package.json +1 -1
- package/src/services/contentProgress.js +29 -9
- package/test/contentProgress.test.js +37 -5
package/CHANGELOG.md
CHANGED
|
@@ -2,6 +2,8 @@
|
|
|
2
2
|
|
|
3
3
|
All notable changes to this project will be documented in this file. See [standard-version](https://github.com/conventional-changelog/standard-version) for commit guidelines.
|
|
4
4
|
|
|
5
|
+
### [1.0.164](https://github.com/railroadmedia/musora-content-services/compare/v1.0.163...v1.0.164) (2024-11-09)
|
|
6
|
+
|
|
5
7
|
### [1.0.163](https://github.com/railroadmedia/musora-content-services/compare/v1.0.162...v1.0.163) (2024-11-08)
|
|
6
8
|
|
|
7
9
|
### [1.0.162](https://github.com/railroadmedia/musora-content-services/compare/v1.0.161...v1.0.162) (2024-11-08)
|
package/package.json
CHANGED
|
@@ -2,17 +2,17 @@ import {
|
|
|
2
2
|
fetchContentProgress,
|
|
3
3
|
postContentCompleted,
|
|
4
4
|
postContentReset,
|
|
5
|
-
postContentStarted,
|
|
6
5
|
postRecordWatchSession
|
|
7
6
|
} from "./railcontent";
|
|
8
7
|
import {DataContext, ContentProgressVersionKey} from "./dataContext";
|
|
9
|
-
import {fetchHierarchy
|
|
8
|
+
import {fetchHierarchy} from "./sanity";
|
|
10
9
|
|
|
11
10
|
const STATE_STARTED = 'started';
|
|
12
11
|
const STATE_COMPLETED = 'completed';
|
|
13
12
|
const DATA_KEY_STATUS = 's';
|
|
14
13
|
const DATA_KEY_PROGRESS = 'p';
|
|
15
14
|
const DATA_KEY_RESUME_TIME = 't';
|
|
15
|
+
const DATA_KEY_LAST_UPDATED_TIME = 'u';
|
|
16
16
|
export let dataContext = new DataContext(ContentProgressVersionKey, fetchContentProgress);
|
|
17
17
|
|
|
18
18
|
export async function getProgressPercentage(contentId) {
|
|
@@ -43,23 +43,41 @@ export async function getProgressStateByIds(contentIds) {
|
|
|
43
43
|
return progress;
|
|
44
44
|
}
|
|
45
45
|
|
|
46
|
-
export async function getAllStarted() {
|
|
46
|
+
export async function getAllStarted(limit = null) {
|
|
47
47
|
const data = await dataContext.getData();
|
|
48
48
|
let ids = Object.keys(data).filter(function (key) {
|
|
49
49
|
return data[parseInt(key)][DATA_KEY_STATUS] === STATE_STARTED;
|
|
50
50
|
}).map(function (key) {
|
|
51
51
|
return parseInt(key);
|
|
52
|
+
}).sort(function (a, b) {
|
|
53
|
+
let v1 = data[a][DATA_KEY_LAST_UPDATED_TIME];
|
|
54
|
+
let v2 = data[b][DATA_KEY_LAST_UPDATED_TIME];
|
|
55
|
+
if (v1 > v2) return -1;
|
|
56
|
+
else if (v1 < v2) return 1;
|
|
57
|
+
return 0;
|
|
52
58
|
});
|
|
59
|
+
if (limit) {
|
|
60
|
+
ids = ids.slice(0, limit);
|
|
61
|
+
}
|
|
53
62
|
return ids;
|
|
54
63
|
}
|
|
55
64
|
|
|
56
|
-
export async function getAllCompleted() {
|
|
65
|
+
export async function getAllCompleted(limit = null) {
|
|
57
66
|
const data = await dataContext.getData();
|
|
58
67
|
let ids = Object.keys(data).filter(function (key) {
|
|
59
68
|
return data[parseInt(key)][DATA_KEY_STATUS] === STATE_COMPLETED;
|
|
60
69
|
}).map(function (key) {
|
|
61
70
|
return parseInt(key);
|
|
71
|
+
}).sort(function (a, b) {
|
|
72
|
+
let v1 = data[a][DATA_KEY_LAST_UPDATED_TIME];
|
|
73
|
+
let v2 = data[b][DATA_KEY_LAST_UPDATED_TIME];
|
|
74
|
+
if (v1 > v2) return -1;
|
|
75
|
+
else if (v1 < v2) return 1;
|
|
76
|
+
return 0;
|
|
62
77
|
});
|
|
78
|
+
if (limit) {
|
|
79
|
+
ids = ids.slice(0, limit);
|
|
80
|
+
}
|
|
63
81
|
return ids;
|
|
64
82
|
}
|
|
65
83
|
|
|
@@ -70,8 +88,8 @@ export async function getResumeTimeSeconds(contentId) {
|
|
|
70
88
|
|
|
71
89
|
export async function contentStatusCompleted(contentId) {
|
|
72
90
|
await dataContext.update(
|
|
73
|
-
function (localContext) {
|
|
74
|
-
let hierarchy = fetchHierarchy(contentId);
|
|
91
|
+
async function (localContext) {
|
|
92
|
+
let hierarchy = await fetchHierarchy(contentId);
|
|
75
93
|
completeStatusInLocalContext(contentId, localContext, hierarchy);
|
|
76
94
|
},
|
|
77
95
|
async function () {
|
|
@@ -80,9 +98,10 @@ export async function contentStatusCompleted(contentId) {
|
|
|
80
98
|
}
|
|
81
99
|
|
|
82
100
|
function completeStatusInLocalContext(contentId, localContext, hierarchy) {
|
|
83
|
-
let data = localContext.data[contentId] ??
|
|
101
|
+
let data = localContext.data[contentId] ?? {};
|
|
84
102
|
data[DATA_KEY_STATUS] = STATE_COMPLETED;
|
|
85
103
|
data[DATA_KEY_PROGRESS] = 100;
|
|
104
|
+
data[DATA_KEY_LAST_UPDATED_TIME] = Math.round(new Date().getTime() / 1000);
|
|
86
105
|
localContext.data[contentId] = data;
|
|
87
106
|
|
|
88
107
|
let children = hierarchy.children[contentId] ?? [];
|
|
@@ -125,7 +144,7 @@ export async function recordWatchSession(contentId, mediaType, mediaCategory, me
|
|
|
125
144
|
await dataContext.update(
|
|
126
145
|
async function (localContext) {
|
|
127
146
|
if (contentId && updateLocalProgress) {
|
|
128
|
-
let data = localContext.data[contentId] ??
|
|
147
|
+
let data = localContext.data[contentId] ?? {};
|
|
129
148
|
let progress = data?.[DATA_KEY_PROGRESS] ?? 0;
|
|
130
149
|
let status = data?.[DATA_KEY_STATUS] ?? 0;
|
|
131
150
|
|
|
@@ -137,6 +156,7 @@ export async function recordWatchSession(contentId, mediaType, mediaCategory, me
|
|
|
137
156
|
data[DATA_KEY_PROGRESS] = progress;
|
|
138
157
|
data[DATA_KEY_STATUS] = status;
|
|
139
158
|
data[DATA_KEY_RESUME_TIME] = currentSeconds;
|
|
159
|
+
data[DATA_KEY_LAST_UPDATED_TIME] = Math.round(new Date().getTime() / 1000);
|
|
140
160
|
localContext.data[contentId] = data;
|
|
141
161
|
|
|
142
162
|
let hierarchy = await fetchHierarchy(contentId);
|
|
@@ -175,7 +195,7 @@ function uuidv4() {
|
|
|
175
195
|
function bubbleProgress(hierarchy, contentId, localContext) {
|
|
176
196
|
let parentId = hierarchy.parents[contentId];
|
|
177
197
|
if (!parentId) return;
|
|
178
|
-
let data = localContext.data[parentId] ??
|
|
198
|
+
let data = localContext.data[parentId] ?? {};
|
|
179
199
|
let progress = data[DATA_KEY_PROGRESS];
|
|
180
200
|
let status = data[DATA_KEY_STATUS];
|
|
181
201
|
if (status !== STATE_COMPLETED && progress !== 100) {
|
|
@@ -2,9 +2,15 @@ import {
|
|
|
2
2
|
getProgressPercentage,
|
|
3
3
|
dataContext,
|
|
4
4
|
recordWatchSession,
|
|
5
|
-
getProgressPercentageByIds,
|
|
5
|
+
getProgressPercentageByIds,
|
|
6
|
+
getProgressState,
|
|
7
|
+
getProgressStateByIds,
|
|
8
|
+
getAllStarted,
|
|
9
|
+
getAllCompleted,
|
|
10
|
+
contentStatusCompleted
|
|
6
11
|
} from "../src/services/contentProgress";
|
|
7
12
|
import {initializeTestService} from "./initializeTests";
|
|
13
|
+
import {postContentCompleted} from "../src";
|
|
8
14
|
|
|
9
15
|
const railContentModule = require('../src/services/railcontent.js')
|
|
10
16
|
|
|
@@ -15,7 +21,7 @@ describe('contentProgressDataContext', function () {
|
|
|
15
21
|
beforeEach(() => {
|
|
16
22
|
initializeTestService();
|
|
17
23
|
mock = jest.spyOn(dataContext, 'fetchData');
|
|
18
|
-
var json = JSON.parse(`{"version":${testVersion},"data":{"234191":{"s":"started","p":6,"t":20},"233955":{"s":"started","p":1},"259426":{"s":"completed","p":100}}}`);
|
|
24
|
+
var json = JSON.parse(`{"version":${testVersion},"data":{"234191":{"s":"started","p":6,"t":20,"u":1731108082},"233955":{"s":"started","p":1,"u":1731108083},"259426":{"s":"completed","p":100,"u":1731108085}}}`);
|
|
19
25
|
mock.mockImplementation(() => json);
|
|
20
26
|
|
|
21
27
|
});
|
|
@@ -49,15 +55,41 @@ describe('contentProgressDataContext', function () {
|
|
|
49
55
|
|
|
50
56
|
test('getAllStarted', async () => {
|
|
51
57
|
let result = await getAllStarted();
|
|
52
|
-
expect(result).
|
|
53
|
-
|
|
58
|
+
expect(result).toStrictEqual([233955, 234191]);
|
|
59
|
+
|
|
60
|
+
result = await getAllStarted(1);
|
|
61
|
+
expect(result).toStrictEqual([233955]);
|
|
54
62
|
});
|
|
55
63
|
|
|
64
|
+
// test('getAllStartedWithUpdate', async () => {
|
|
65
|
+
// let mock2 = jest.spyOn(railContentModule, 'postRecordWatchSession');
|
|
66
|
+
// let serverVersion = 2;
|
|
67
|
+
// mock2.mockImplementation(() => JSON.parse(`{"version": ${serverVersion}}`));
|
|
68
|
+
// let result = await getAllStarted();
|
|
69
|
+
// expect(result).toStrictEqual([233955, 234191]);
|
|
70
|
+
// await recordWatchSession(111111, "video", "vimeo", 100, 50, 50);
|
|
71
|
+
//
|
|
72
|
+
// let result2 = await getAllStarted();
|
|
73
|
+
// expect(result2).toStrictEqual([111111, 233955, 234191]);
|
|
74
|
+
// });
|
|
75
|
+
|
|
56
76
|
test('getAllCompleted', async () => {
|
|
57
77
|
let result = await getAllCompleted();
|
|
58
|
-
expect(result).
|
|
78
|
+
expect(result).toStrictEqual([259426]);
|
|
59
79
|
});
|
|
60
80
|
|
|
81
|
+
// test('getAllCompletedWithUpdate', async () => {
|
|
82
|
+
// let mock2 = jest.spyOn(railContentModule, 'postContentCompleted');
|
|
83
|
+
// let serverVersion = 2;
|
|
84
|
+
// mock2.mockImplementation(() => JSON.parse(`{"version": ${serverVersion}}`));
|
|
85
|
+
//
|
|
86
|
+
// let result = await getAllCompleted();
|
|
87
|
+
// expect(result).toStrictEqual([259426]);
|
|
88
|
+
// await contentStatusCompleted(111111);
|
|
89
|
+
// let result2 = await getAllCompleted();
|
|
90
|
+
// expect(result2).toStrictEqual([111111, 259426]);
|
|
91
|
+
// });
|
|
92
|
+
|
|
61
93
|
test('progressBubbling', async () => {
|
|
62
94
|
let mock2 = jest.spyOn(railContentModule, 'postRecordWatchSession');
|
|
63
95
|
let serverVersion = 2;
|