Browse Source

Refactor Gun provider

Carsten Porth 5 years ago
parent
commit
d2aa66b7ea
1 changed files with 42 additions and 36 deletions
  1. 42 36
      app/src/providers/p2p-database-gun/p2p-database-gun.ts

+ 42 - 36
app/src/providers/p2p-database-gun/p2p-database-gun.ts

@@ -5,49 +5,47 @@ import "gun/lib/then";
 @Injectable()
 export class P2pDatabaseGunProvider {
   private gun;
-  osnPrefix: string = "hybridOSN-beta003";
+  osnPrefix: string = "hybridOSN-beta009";
 
   constructor() {
     this.gun = Gun(["https://hybrid-osn.herokuapp.com/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);
-  }
-
   /**
    * 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 {
+  public async publishHashtags(hashtagEntity): Promise<void> {
     const timestamp = new Date().setHours(0, 0, 1, 0);
-    const hashtagsCommaSeparated = hashtagEntity
+    const hashtagsSeparated = hashtagEntity
       .map(el => el.hashtag)
       .sort()
       .join("|");
 
-    const hashtags = this.gun.get(timestamp).put({
-      hashtags: hashtagsCommaSeparated,
-      created_at: timestamp
-    });
+    const randomId = Math.floor(Math.random() * 10000000000);
+
+    const hashtags = await this.gun
+      .get(randomId)
+      .put({ hashtags: hashtagsSeparated })
+      .then();
 
-    this.gun.get(this.osnPrefix + "/hashtags").set(hashtags);
+    this.gun
+      .get(this.osnPrefix)
+      .get("hashtags")
+      .get(timestamp)
+      .set(hashtags);
   }
 
-  public async getLastTweetFromUser(userId) {
-    return new Promise(resolve =>
-      this.gun
-        .get(this.osnPrefix + userId)
-        .get("tweets")
-        .once(resolve)
-    );
+  public storeLastTweetHashForUser(userId, hash, timestamp): void {
+    const tweet = this.gun
+      .get(timestamp)
+      .put({ hash: hash, created_at: timestamp });
+
+    this.gun
+      .get(this.osnPrefix)
+      .get("tweets")
+      .get(userId)
+      .set(tweet);
   }
 
   public async fetchPrivateTweetHashsForUserInInterval(
@@ -55,14 +53,15 @@ export class P2pDatabaseGunProvider {
     intervalStart,
     intervalEnd
   ): Promise<object[]> {
-    const gunIds = await this.gun
-      .get(this.osnPrefix + "-" + userId)
+    const privateTweets = await this.gun
+      .get(this.osnPrefix)
       .get("tweets")
+      .get(userId)
       .then();
 
-    if (gunIds) {
+    if (privateTweets) {
       const entries = await Promise.all(
-        Object.keys(gunIds)
+        Object.keys(privateTweets)
           .filter(key => key !== "_")
           .map(key => this.gun.get(key).then())
       );
@@ -77,18 +76,25 @@ export class P2pDatabaseGunProvider {
     }
   }
 
-  public async addLike(id: string) {
-    const likeEntry = await this.getLikes(id);
+  public async addLike(tweetId: string) {
+    const likeEntry = await this.getLikes(tweetId);
 
-    const hashtags = this.gun.get(id).put({
+    const likes = this.gun.get(tweetId).put({
       likes: likeEntry.likes + 1
     });
 
-    this.gun.get(this.osnPrefix + "/likes").set(hashtags);
+    this.gun
+      .get(this.osnPrefix)
+      .get("likes")
+      .set(likes);
   }
 
-  public async getLikes(id: string) {
-    const likeEntry = await this.gun.get(this.osnPrefix + "/likes").get(id);
+  public async getLikes(tweetId: string) {
+    const likeEntry = await this.gun
+      .get(this.osnPrefix)
+      .get("likes")
+      .get(tweetId)
+      .then();
 
     if (likeEntry === undefined) {
       return {