import { Injectable } from "@angular/core"; import Gun from "gun/gun"; import "gun/lib/then"; import "gun/lib/load"; /* Generated class for the P2pDatabaseGunProvider provider. See https://angular.io/guide/dependency-injection for more info on providers and Angular DI. */ @Injectable() export class P2pDatabaseGunProvider { private gun; osnPrefix: string = "hybridOSN-"; constructor() { this.gun = Gun(); } public storeLastTweetHashForUser(userId, hash, timestamp): void { const tweet = this.gun .get(timestamp) .put({ hash: hash, created_at: timestamp }); this.gun .get(this.osnPrefix + userId) .get("tweets") .set(tweet); } public async getLastTweetFromUser(userId) { return new Promise(resolve => this.gun .get(this.osnPrefix + userId) .get("tweets") .once(resolve) ); } public async fetchPrivateTweetHashsForUserInInterval( userId, intervalStart, intervalEnd ): Promise { const gunIds = await this.gun .get(this.osnPrefix + userId) .get("tweets") .then(); if (gunIds) { const entryPromises = []; Object.keys(gunIds) .filter(key => key !== "_") .forEach(key => entryPromises.push(this.gun.get(key).then())); const entries = await Promise.all(entryPromises); return entries .filter( entry => new Date(entry["created_at"]) <= intervalStart && new Date(entry["created_at"]) > intervalEnd ) .map(entry => entry["hash"]); } else { return []; } } }