123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687 |
- import { Injectable } from "@angular/core";
- import Gun from "gun/gun";
- import "gun/lib/then";
- /*
- 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-beta001";
- 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<object[]> {
- const gunIds = await this.gun
- .get(this.osnPrefix + "-" + userId)
- .get("tweets")
- .then();
- if (gunIds) {
- const entries = await Promise.all(
- Object.keys(gunIds)
- .filter(key => key !== "_")
- .map(key => this.gun.get(key).then())
- );
- return entries.filter(
- entry =>
- new Date(entry["created_at"]) < intervalStart &&
- new Date(entry["created_at"]) >= intervalEnd
- );
- } else {
- return [];
- }
- }
- /**
- * Hashtags are stored without reference to the users to provide these information on an extra dashboard to twitter
- * @param hashtagEntity extracted hashtags
- */
- public publishHashtags(hashtagEntity): void {
- const timestamp = Date.now();
- const hashtagsCommaSeparated = hashtagEntity
- .map(el => el.hashtag)
- .sort()
- .join("|");
- const hashtags = this.gun.get(timestamp).put({
- hashtags: hashtagsCommaSeparated,
- created_at: timestamp
- });
- this.gun
- .get(this.osnPrefix)
- .get("hashtags")
- .set(hashtags);
- }
- }
|