Skip to content

Commit

Permalink
Add basic tracking
Browse files Browse the repository at this point in the history
  • Loading branch information
MaKleSoft committed Nov 6, 2017
1 parent c98663d commit 56ace57
Show file tree
Hide file tree
Showing 14 changed files with 535 additions and 281 deletions.
4 changes: 3 additions & 1 deletion app/src/core/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import * as exp from "./export";
import * as platform from "./platform";
import * as file from "./file";
import * as messages from "./messages";
import * as stats from "./stats";

export {
util,
Expand All @@ -19,5 +20,6 @@ export {
exp,
platform,
file,
messages
messages,
stats
}
11 changes: 6 additions & 5 deletions app/src/core/source.ts
Original file line number Diff line number Diff line change
Expand Up @@ -195,10 +195,11 @@ export class CloudSource extends AjaxSource {
return req;
}

async authenticate(email: string, create = false, authType = "api"): Promise<CloudAuthToken> {
async authenticate(email: string, create = false, authType = "api", redirect = ""): Promise<CloudAuthToken> {
const params = new URLSearchParams();
params.set("email", email);
params.set("type", authType);
params.set("redirect", redirect);

const req = await this.request(
create ? "POST" : "PUT",
Expand All @@ -216,19 +217,19 @@ export class CloudSource extends AjaxSource {
return authToken;
}

async requestAuthToken(email: string, create = false): Promise<CloudAuthToken> {
const authToken = await this.authenticate(email, create, "api");
async requestAuthToken(email: string, create = false, redirect = ""): Promise<CloudAuthToken> {
const authToken = await this.authenticate(email, create, "api", redirect);
this.settings.syncEmail = authToken.email;
this.settings.syncToken = authToken.token;
return authToken;
}

async getLoginUrl() {
async getLoginUrl(redirect: string) {
if (!this.settings.syncConnected) {
throw new CloudError("invalid_auth_token", "Need to be authenticated to get a login link.");
}

const authToken = await this.authenticate(this.settings.syncEmail, false, "web");
const authToken = await this.authenticate(this.settings.syncEmail, false, "web", redirect);
return authToken.actUrl;
}

Expand Down
26 changes: 26 additions & 0 deletions app/src/core/stats.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { FileSource } from "./source"

const statsSource = new FileSource("stats.json");

export type Stats = { [prop: string]: number|string };
let stats: Stats;

const statsLoaded = statsSource.get().then((data) => {
try {
stats = JSON.parse(data);
} catch (e) {
stats = {};
}
});
const saveStats = () => statsSource.set(JSON.stringify(stats));

export async function get(): Promise<Stats> {
await statsLoaded;
return stats;
}

export async function set(data: Stats): Promise<void> {
await statsLoaded;
Object.assign(stats, data);
return saveStats();
}
Loading

0 comments on commit 56ace57

Please sign in to comment.