Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
191 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import { Action } from '@ngrx/store'; | ||
|
||
export const SHOW_DONATION_ALERT = 'SHOW_DONATION_ALERT'; | ||
export const HIDE_DONATION_ALERT = 'HIDE_DONATION_ALERT'; | ||
|
||
export class ShowDonationAlertAction implements Action { | ||
readonly type = SHOW_DONATION_ALERT; | ||
} | ||
|
||
export class HideDonationAlertAction implements Action { | ||
readonly type = HIDE_DONATION_ALERT; | ||
} | ||
|
||
export type Action = | ||
| ShowDonationAlertAction | ||
| HideDonationAlertAction; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
import { Action } from '@ngrx/store'; | ||
|
||
import * as donation from '../actions/donation'; | ||
|
||
export interface State { | ||
showAlert: boolean; | ||
} | ||
|
||
export const initialState: State = { | ||
showAlert: false | ||
}; | ||
|
||
export function donationReducer(state = initialState, action: donation.Action): State { | ||
switch (action.type) { | ||
case donation.SHOW_DONATION_ALERT: | ||
return { ...state, showAlert: true }; | ||
case donation.HIDE_DONATION_ALERT: | ||
return { ...state, showAlert: false }; | ||
default: | ||
return state; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
import { Injectable } from '@angular/core'; | ||
|
||
import { Observable } from 'rxjs/Observable'; | ||
import { Subscriber } from 'rxjs/Subscriber'; | ||
|
||
import { DbService } from './'; | ||
import config from '../config'; | ||
import { uaSeedHash } from '../utils/simple_hash'; | ||
|
||
@Injectable() | ||
export class DonationService { | ||
|
||
private actionCountKey = 'dac'; | ||
private seedKey = 'ds'; | ||
private hashKey = 'dh'; | ||
private seedBuff = 100000; | ||
|
||
constructor( | ||
private dbService: DbService | ||
) { } | ||
|
||
donated() { | ||
const seed = Math.random() * this.seedBuff; | ||
|
||
// Store the seed | ||
this.dbService.setItem(this.seedKey, seed); | ||
|
||
// Store the seed hash | ||
this.dbService.setItem(this.hashKey, uaSeedHash(seed)); | ||
|
||
// Reset the count | ||
this.dbService.setItem(this.actionCountKey, 0); | ||
} | ||
|
||
/** | ||
* counts the action and checks if the action is eligible to display the donation alert | ||
*/ | ||
trackAndCheckIfEligible(): Observable<boolean> { | ||
/** | ||
* Check if the count threshold has been reached. | ||
* Check if random seed exist | ||
* Check if hash of (seed+ua) matches [donated] | ||
* ~~ Show alert ~~ | ||
* Reset counter | ||
*/ | ||
const actionCount$ = this.dbService.getItem(this.actionCountKey); | ||
const seed$ = this.dbService.getItem(this.seedKey); | ||
const curHash$ = this.dbService.getItem(this.hashKey); | ||
|
||
return Observable.create((obs: Subscriber<boolean>) => { | ||
Observable.combineLatest(actionCount$, seed$, curHash$).subscribe(([actionCount, seed, curHash]) => { | ||
if (actionCount && actionCount >= config.donation.action_count_threshold) { | ||
// Reset count | ||
this.dbService.setItem(this.actionCountKey, 0); | ||
|
||
if (seed && uaSeedHash(seed) === curHash) { | ||
// User has donated already | ||
return obs.next(false); | ||
} else { | ||
// User has not donated | ||
return obs.next(true); | ||
} | ||
} else { | ||
// Increment count | ||
this.dbService.setItem(this.actionCountKey, actionCount + 1); | ||
|
||
return obs.next(false); | ||
} | ||
}); | ||
}); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
export const hash = (s) => { | ||
/* Simple hash function. */ | ||
let a = 1, c = 0, h, o; | ||
if (s) { | ||
a = 0; | ||
/*jshint plusplus:false bitwise:false*/ | ||
/*tslint:disable */ | ||
for (h = s.length - 1; h >= 0; h--) { | ||
o = s.charCodeAt(h); | ||
a = (a << 6&268435455) + o + (o << 14); | ||
c = a & 266338304; | ||
a = c !== 0 ? a ^ c >> 21 : a; | ||
} | ||
} | ||
return String(a); | ||
}; | ||
|
||
export function uaSeedHash(seed) { | ||
return hash(`${navigator.userAgent}:${seed}`); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters