-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathapiStore.js
More file actions
225 lines (186 loc) · 6.01 KB
/
Copy pathapiStore.js
File metadata and controls
225 lines (186 loc) · 6.01 KB
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
var EventEmitter = require('events');
var AddonCollection = require('stremio-addon-client').AddonCollection;
var mapURL = require('stremio-addon-client').mapURL;
var officialAddons = require('stremio-official-addons');
var MemoryStorage = require('./lib/memoryStorage');
var addonsDifferent = require('./lib/addonsDifferent');
var ApiClient = require('./apiClient');
var ENDPOINT = 'https://api.strem.io';
var INTERRUPTED_ERROR_MESSAGE = 'request interrupted';
var USER_REQUIRED = 'user required to invoke this';
function ApiStore(options) {
options = options || {};
var endpoint = options.endpoint || ENDPOINT;
var storage = options.storage || new MemoryStorage();
var client = new ApiClient({ endpoint: endpoint, authKey: storage.getJSON('authKey') });
var self = this;
this.endpoint = endpoint;
this.events = new EventEmitter();
this.user = storage.getJSON('user');
// Migration from legacy format
if (this.user && this.user.authKey) {
storage.setJSON('authKey', this.user.authKey);
client = new ApiClient({ endpoint: endpoint, authKey: this.user.authKey });
storage.setJSON('user', this.user);
}
this.addons = new AddonCollection();
this.addons.load(storage.getJSON('addons') || officialAddons);
this.request = function(method, params) {
var currentClient = client;
return new Promise(function(resolve, reject) {
currentClient.request(method, params)
.then(function(resp) {
if (currentClient !== client) {
reject(new Error(INTERRUPTED_ERROR_MESSAGE));
return;
}
resolve(resp);
})
.catch(function(err) {
if (currentClient !== client) {
reject(new Error(INTERRUPTED_ERROR_MESSAGE));
return;
}
reject(err);
});
});
};
this.login = function(params) {
return this.request('login', params)
.then(function(result) {
self.userChange(result.authKey, result.user);
addonsUpdated(null, null);
});
};
this.loginWithToken = function(params) {
return this.request('loginWithToken', params)
.then(function(result) {
self.userChange(result.authKey, result.user);
addonsUpdated(null, null);
});
};
this.authWithApple = function(params) {
return this.request('authWithApple', params)
.then(function(result) {
self.userChange(result.authKey, result.user);
addonsUpdated(null, null);
});
};
this.register = function(params) {
return this.request('register', params)
.then(function(result) {
self.userChange(result.authKey, result.user);
addonsUpdated(null, null);
});
};
this.logout = function() {
return this.request('logout')
.then(function() {
self.userChange(null, null);
addonsUpdated(null, null);
})
.catch(function(err) {
if (err && err.message === INTERRUPTED_ERROR_MESSAGE) {
throw err;
}
self.userChange(null, null);
addonsUpdated(null, null);
});
};
//
// pullAddonCollection, pushAddonCollection
//
this.pullAddonCollection = function() {
if (!self.user) {
return Promise.reject(new Error(USER_REQUIRED));
}
var params = { update: true, addFromURL: [] };
var legacyKey = 'addons:' + (self.user ? self.user._id : '');
params.addFromURL = mapLegacyAddonRepo(storage.getJSON(legacyKey));
var lastModifiedOriginal = storage.getJSON('addonsLastModified') || 0;
return this.request('addonCollectionGet', params)
.then(function(resp) {
if (!Array.isArray(resp.addons)) {
throw 'no resp.addons';
}
var lastModified = storage.getJSON('addonsLastModified') || 0;
var newLastModified = new Date(resp.lastModified).getTime();
if (resp.addons.length && lastModified == lastModifiedOriginal && newLastModified > lastModified) {
addonsUpdated(resp.addons, newLastModified);
if (params.addFromURL.length) {
storage.setJSON(legacyKey, null);
}
}
});
};
this.pushAddonCollection = function() {
var descriptors = self.addons.save();
storage.setJSON('addons', descriptors);
storage.setJSON('addonsLastModified', Date.now());
if (!self.user) {
return Promise.resolve();
}
return self.request('addonCollectionSet', { addons: descriptors });
};
//
// pullUser, pushUser
//
this.pullUser = function() {
if (!self.user) {
return Promise.reject(new Error(USER_REQUIRED));
}
return this.request('getUser')
.then(function(user) {
if (!(user && user._id)) {
throw 'invalid user returned';
}
var lastModified = new Date(self.user.lastModified).getTime();
var newLastModified = new Date(user.lastModified).getTime();
if (newLastModified > lastModified) {
storage.setJSON('user', user);
self.user = user;
}
});
};
this.pushUser = function() {
if (!self.user) {
return Promise.reject(new Error(USER_REQUIRED));
}
self.user.lastModified = new Date();
storage.setJSON('user', self.user);
return self.request('saveUser', self.user);
};
//
// Private methods
//
// only invoked when the user is changed by _id (different user)
// call this with (null, null) when logging out
this.userChange = function(authKey, user) {
storage.setJSON('authKey', authKey);
storage.setJSON('user', user);
client = new ApiClient({ endpoint: endpoint, authKey: authKey });
self.user = user;
self.events.emit('user-change', user);
};
// this may be invoked when the add-on set is updated
function addonsUpdated(descriptors, lastModified) {
var isDifferent = addonsDifferent(descriptors || [], self.addons.getAddons());
storage.setJSON('addons', descriptors);
storage.setJSON('addonsLastModified', lastModified || 0);
self.addons.load(descriptors || officialAddons);
self.events.emit('addons-change');
if (isDifferent) self.events.emit('addons-different');
}
// remaps old add-on format into a list of URLs
function mapLegacyAddonRepo(repo) {
if (repo && Array.isArray(repo.addons)) {
return repo.addons
.filter(function(x) { return Array.isArray(x.endpoints) && typeof (x.endpoints[0]) === 'string'; })
.map(function(x) { return mapURL(x.endpoints[0]); });
}
return [];
}
Object.seal(this);
return this;
}
module.exports = ApiStore;