2022-12-25 17:59:44 +01:00
|
|
|
import {
|
2023-01-03 04:21:11 +01:00
|
|
|
accountAccessSingleUrl,
|
|
|
|
accountAccessUrl,
|
2022-12-25 17:59:44 +01:00
|
|
|
accountPasswordUrl,
|
|
|
|
accountSettingsUrl,
|
|
|
|
accountSubscriptionSingleUrl,
|
|
|
|
accountSubscriptionUrl,
|
|
|
|
accountTokenUrl,
|
|
|
|
accountUrl,
|
2022-12-27 03:27:07 +01:00
|
|
|
withBasicAuth,
|
2023-01-03 04:21:11 +01:00
|
|
|
withBearerAuth
|
2022-12-25 17:59:44 +01:00
|
|
|
} from "./utils";
|
|
|
|
import session from "./Session";
|
2022-12-25 19:42:44 +01:00
|
|
|
import subscriptionManager from "./SubscriptionManager";
|
2023-01-03 04:21:11 +01:00
|
|
|
import i18n from "i18next";
|
|
|
|
import prefs from "./Prefs";
|
|
|
|
import routes from "../components/routes";
|
2022-12-25 19:42:44 +01:00
|
|
|
|
|
|
|
const delayMillis = 45000; // 45 seconds
|
|
|
|
const intervalMillis = 900000; // 15 minutes
|
2022-12-25 17:59:44 +01:00
|
|
|
|
|
|
|
class AccountApi {
|
2022-12-25 19:42:44 +01:00
|
|
|
constructor() {
|
|
|
|
this.timer = null;
|
2023-01-03 04:21:11 +01:00
|
|
|
this.listener = null; // Fired when account is fetched from remote
|
|
|
|
}
|
|
|
|
|
|
|
|
registerListener(listener) {
|
|
|
|
this.listener = listener;
|
|
|
|
}
|
|
|
|
|
|
|
|
resetListener() {
|
|
|
|
this.listener = null;
|
2022-12-25 19:42:44 +01:00
|
|
|
}
|
|
|
|
|
2022-12-25 17:59:44 +01:00
|
|
|
async login(user) {
|
|
|
|
const url = accountTokenUrl(config.baseUrl);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Checking auth for ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "POST",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBasicAuth({}, user.username, user.password)
|
2022-12-25 17:59:44 +01:00
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
const json = await response.json();
|
|
|
|
if (!json.token) {
|
|
|
|
throw new Error(`Unexpected server response: Cannot find token`);
|
|
|
|
}
|
|
|
|
return json.token;
|
|
|
|
}
|
|
|
|
|
2022-12-28 21:51:09 +01:00
|
|
|
async logout() {
|
2022-12-25 17:59:44 +01:00
|
|
|
const url = accountTokenUrl(config.baseUrl);
|
2022-12-28 21:51:09 +01:00
|
|
|
console.log(`[AccountApi] Logging out from ${url} using token ${session.token()}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "DELETE",
|
2022-12-28 21:51:09 +01:00
|
|
|
headers: withBearerAuth({}, session.token())
|
2022-12-25 17:59:44 +01:00
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async create(username, password) {
|
|
|
|
const url = accountUrl(config.baseUrl);
|
|
|
|
const body = JSON.stringify({
|
|
|
|
username: username,
|
|
|
|
password: password
|
|
|
|
});
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Creating user account ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
body: body
|
|
|
|
});
|
|
|
|
if (response.status === 409) {
|
|
|
|
throw new UsernameTakenError(username);
|
|
|
|
} else if (response.status === 429) {
|
|
|
|
throw new AccountCreateLimitReachedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async get() {
|
|
|
|
const url = accountUrl(config.baseUrl);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Fetching user account ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token())
|
2022-12-25 17:59:44 +01:00
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
const account = await response.json();
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Account`, account);
|
2023-01-03 04:21:11 +01:00
|
|
|
if (this.listener) {
|
|
|
|
this.listener(account);
|
|
|
|
}
|
2022-12-25 17:59:44 +01:00
|
|
|
return account;
|
|
|
|
}
|
|
|
|
|
|
|
|
async delete() {
|
|
|
|
const url = accountUrl(config.baseUrl);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Deleting user account ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "DELETE",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token())
|
2022-12-25 17:59:44 +01:00
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async changePassword(newPassword) {
|
|
|
|
const url = accountPasswordUrl(config.baseUrl);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Changing account password ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "POST",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token()),
|
2022-12-25 17:59:44 +01:00
|
|
|
body: JSON.stringify({
|
|
|
|
password: newPassword
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async extendToken() {
|
|
|
|
const url = accountTokenUrl(config.baseUrl);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Extending user access token ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "PATCH",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token())
|
2022-12-25 17:59:44 +01:00
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateSettings(payload) {
|
|
|
|
const url = accountSettingsUrl(config.baseUrl);
|
|
|
|
const body = JSON.stringify(payload);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Updating user account ${url}: ${body}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "PATCH",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token()),
|
2022-12-25 17:59:44 +01:00
|
|
|
body: body
|
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async addSubscription(payload) {
|
|
|
|
const url = accountSubscriptionUrl(config.baseUrl);
|
|
|
|
const body = JSON.stringify(payload);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Adding user subscription ${url}: ${body}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "POST",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token()),
|
2022-12-25 17:59:44 +01:00
|
|
|
body: body
|
|
|
|
});
|
2022-12-26 04:29:55 +01:00
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
const subscription = await response.json();
|
|
|
|
console.log(`[AccountApi] Subscription`, subscription);
|
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
async updateSubscription(remoteId, payload) {
|
|
|
|
const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId);
|
|
|
|
const body = JSON.stringify(payload);
|
|
|
|
console.log(`[AccountApi] Updating user subscription ${url}: ${body}`);
|
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "PATCH",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token()),
|
2022-12-26 04:29:55 +01:00
|
|
|
body: body
|
|
|
|
});
|
2022-12-25 17:59:44 +01:00
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
const subscription = await response.json();
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Subscription`, subscription);
|
2022-12-25 17:59:44 +01:00
|
|
|
return subscription;
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteSubscription(remoteId) {
|
|
|
|
const url = accountSubscriptionSingleUrl(config.baseUrl, remoteId);
|
2022-12-25 19:42:44 +01:00
|
|
|
console.log(`[AccountApi] Removing user subscription ${url}`);
|
2022-12-25 17:59:44 +01:00
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "DELETE",
|
2022-12-27 03:27:07 +01:00
|
|
|
headers: withBearerAuth({}, session.token())
|
2022-12-25 17:59:44 +01:00
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
2022-12-25 19:42:44 +01:00
|
|
|
|
2023-01-03 03:52:20 +01:00
|
|
|
async upsertAccess(topic, everyone) {
|
|
|
|
const url = accountAccessUrl(config.baseUrl);
|
|
|
|
console.log(`[AccountApi] Upserting user access to topic ${topic}, everyone=${everyone}`);
|
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "POST",
|
|
|
|
headers: withBearerAuth({}, session.token()),
|
|
|
|
body: JSON.stringify({
|
|
|
|
topic: topic,
|
|
|
|
everyone: everyone
|
|
|
|
})
|
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
async deleteAccess(topic) {
|
|
|
|
const url = accountAccessSingleUrl(config.baseUrl, topic);
|
|
|
|
console.log(`[AccountApi] Removing topic reservation ${url}`);
|
|
|
|
const response = await fetch(url, {
|
|
|
|
method: "DELETE",
|
|
|
|
headers: withBearerAuth({}, session.token())
|
|
|
|
});
|
|
|
|
if (response.status === 401 || response.status === 403) {
|
|
|
|
throw new UnauthorizedError();
|
|
|
|
} else if (response.status !== 200) {
|
|
|
|
throw new Error(`Unexpected server response ${response.status}`);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-01-03 04:21:11 +01:00
|
|
|
async sync() {
|
|
|
|
try {
|
|
|
|
if (!session.token()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
console.log(`[AccountApi] Syncing account`);
|
2023-01-03 17:28:04 +01:00
|
|
|
const account = await this.get();
|
|
|
|
if (account.language) {
|
|
|
|
await i18n.changeLanguage(account.language);
|
2023-01-03 04:21:11 +01:00
|
|
|
}
|
2023-01-03 17:28:04 +01:00
|
|
|
if (account.notification) {
|
|
|
|
if (account.notification.sound) {
|
|
|
|
await prefs.setSound(account.notification.sound);
|
2023-01-03 04:21:11 +01:00
|
|
|
}
|
2023-01-03 17:28:04 +01:00
|
|
|
if (account.notification.delete_after) {
|
|
|
|
await prefs.setDeleteAfter(account.notification.delete_after);
|
2023-01-03 04:21:11 +01:00
|
|
|
}
|
2023-01-03 17:28:04 +01:00
|
|
|
if (account.notification.min_priority) {
|
|
|
|
await prefs.setMinPriority(account.notification.min_priority);
|
2023-01-03 04:21:11 +01:00
|
|
|
}
|
|
|
|
}
|
2023-01-03 17:28:04 +01:00
|
|
|
if (account.subscriptions) {
|
|
|
|
await subscriptionManager.syncFromRemote(account.subscriptions, account.reservations);
|
2023-01-03 04:21:11 +01:00
|
|
|
}
|
2023-01-03 17:28:04 +01:00
|
|
|
return account;
|
2023-01-03 04:21:11 +01:00
|
|
|
} catch (e) {
|
|
|
|
console.log(`[AccountApi] Error fetching account`, e);
|
|
|
|
if ((e instanceof UnauthorizedError)) {
|
|
|
|
session.resetAndRedirect(routes.login);
|
|
|
|
}
|
|
|
|
}
|
2022-12-27 03:27:07 +01:00
|
|
|
}
|
|
|
|
|
2022-12-25 19:42:44 +01:00
|
|
|
startWorker() {
|
|
|
|
if (this.timer !== null) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`[AccountApi] Starting worker`);
|
|
|
|
this.timer = setInterval(() => this.runWorker(), intervalMillis);
|
|
|
|
setTimeout(() => this.runWorker(), delayMillis);
|
|
|
|
}
|
|
|
|
|
|
|
|
async runWorker() {
|
|
|
|
if (!session.token()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
console.log(`[AccountApi] Extending user access token`);
|
|
|
|
try {
|
|
|
|
await this.extendToken();
|
|
|
|
} catch (e) {
|
|
|
|
console.log(`[AccountApi] Error extending user access token`, e);
|
|
|
|
}
|
|
|
|
}
|
2022-12-25 17:59:44 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
export class UsernameTakenError extends Error {
|
|
|
|
constructor(username) {
|
|
|
|
super("Username taken");
|
|
|
|
this.username = username;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class AccountCreateLimitReachedError extends Error {
|
|
|
|
constructor() {
|
|
|
|
super("Account creation limit reached");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
export class UnauthorizedError extends Error {
|
|
|
|
constructor() {
|
|
|
|
super("Unauthorized");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
const accountApi = new AccountApi();
|
|
|
|
export default accountApi;
|