p2p-database-gun.ts 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. import { Injectable } from "@angular/core";
  2. import Gun from "gun/gun";
  3. import "gun/lib/then";
  4. @Injectable()
  5. export class P2pDatabaseGunProvider {
  6. private gun;
  7. osnPrefix: string = "hybridOSN-v2.0.0";
  8. constructor() {
  9. this.gun = Gun(["https://hosn-twitter-app.herokuapp.com/gun"]);
  10. }
  11. /**
  12. * Hashtags are stored without reference to the users to provide these information on an extra dashboard to twitter
  13. * @param hashtagEntity extracted hashtags
  14. */
  15. public async publishHashtags(hashtagEntity): Promise < void > {
  16. const timestamp = new Date().setHours(0, 0, 1, 0);
  17. const hashtagsSeparated = hashtagEntity
  18. .map(el => el.hashtag)
  19. .sort()
  20. .join("|");
  21. const randomId = Math.floor(Math.random() * 10000000000);
  22. const hashtags = this.gun
  23. .get(randomId)
  24. .put({ hashtags: hashtagsSeparated });
  25. this.gun
  26. .get(this.osnPrefix)
  27. .get("hashtags")
  28. .get(timestamp)
  29. .set(hashtags);
  30. }
  31. /**
  32. * Store the ipfs hash to a private tweet in GUN
  33. * @param userId user id
  34. * @param hash ipfs hash
  35. * @param timestamp timestamp
  36. */
  37. public storeLastTweetHashForUser(userId, hash, timestamp): void {
  38. const tweet = this.gun
  39. .get(timestamp)
  40. .put({ hash: hash, created_at: timestamp });
  41. this.gun
  42. .get(this.osnPrefix)
  43. .get("tweets")
  44. .get(userId)
  45. .set(tweet);
  46. }
  47. /**
  48. * Retrieves the ipfs hashes of private tweets for a user in the given time interval
  49. * @param userId user id
  50. * @param intervalStart interval start timestamp
  51. * @param intervalEnd interval end timestamp
  52. */
  53. public async fetchPrivateTweetHashsForUserInInterval(
  54. userId,
  55. intervalStart,
  56. intervalEnd
  57. ): Promise < object[] > {
  58. const privateTweets = await this.gun
  59. .get(this.osnPrefix)
  60. .get("tweets")
  61. .get(userId)
  62. .then();
  63. if (privateTweets) {
  64. const entries = await Promise.all(
  65. Object.keys(privateTweets)
  66. .filter(key => key !== "_")
  67. .map(key => this.gun.get(key).then())
  68. );
  69. return entries
  70. .filter(entry => {
  71. if (entry) {
  72. const createdAtDate = new Date(entry["created_at"]);
  73. return createdAtDate < intervalStart && createdAtDate >= intervalEnd;
  74. }
  75. })
  76. .map(entry => {
  77. entry.userId = userId;
  78. return entry;
  79. });
  80. } else {
  81. return [];
  82. }
  83. }
  84. /**
  85. * Adds a like to a tweet privately
  86. * @param tweetId tweet id
  87. */
  88. public async addLike(tweetId: string) {
  89. const likeEntry = await this.getLikes(tweetId);
  90. const likes = this.gun.get(tweetId).put({
  91. likes: likeEntry.likes + 1
  92. });
  93. this.gun
  94. .get(this.osnPrefix)
  95. .get("likes")
  96. .set(likes);
  97. }
  98. /**
  99. * Retrieves the private likes for a tweet
  100. * @param tweetId tweet id
  101. */
  102. public async getLikes(tweetId: string) {
  103. const likeEntry = await this.gun
  104. .get(this.osnPrefix)
  105. .get("likes")
  106. .get(tweetId)
  107. .then();
  108. if (likeEntry === undefined) {
  109. return {
  110. likes: 0
  111. };
  112. } else {
  113. return likeEntry;
  114. }
  115. }
  116. public async setEmail(userId, email) {
  117. const emailadd = this.gun.get(userId).put({
  118. email: email
  119. });
  120. this.gun
  121. .get(this.osnPrefix)
  122. .get("email")
  123. .set(emailadd);
  124. }
  125. public async getEmail(userId) {
  126. let entry = await this.gun
  127. .get(this.osnPrefix)
  128. .get("email");
  129. const emailEntry = await this.gun
  130. .get(this.osnPrefix)
  131. .get("email")
  132. .get(userId)
  133. .then();
  134. if (emailEntry === undefined) {
  135. return null;
  136. } else {
  137. return emailEntry;
  138. }
  139. }
  140. public async storePrivateKeyHistory(userId, email, ipfs) {
  141. const pvtKey = this.gun.get(userId).put({
  142. key: ipfs
  143. });
  144. console.log("pvtKey:",pvtKey);
  145. this.gun
  146. .get(this.osnPrefix)
  147. .get("privateKey")
  148. .set(pvtKey);
  149. let test = await this.getPvtKeyHistory(userId);
  150. console.log("test:",test);
  151. }
  152. public async getPvtKeyHistory(userId) {
  153. let entry = await this.gun
  154. .get(this.osnPrefix)
  155. .get("privateKey");
  156. console.log("entry:",entry);
  157. const pvtKeyEntry = await this.gun
  158. .get(this.osnPrefix)
  159. .get("privateKey")
  160. .get(userId)
  161. .then();
  162. if (pvtKeyEntry === undefined) {
  163. return null;
  164. } else {
  165. return pvtKeyEntry;
  166. }
  167. }
  168. }