p2p-database-gun.ts 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  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-v1.0.0";
  8. constructor() {
  9. this.gun = Gun(["https://hybrid-osn.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. const createdAtDate = new Date(entry["created_at"]);
  72. return createdAtDate < intervalStart && createdAtDate >= intervalEnd;
  73. })
  74. .map(entry => {
  75. entry.userId = userId;
  76. return entry;
  77. });
  78. } else {
  79. return [];
  80. }
  81. }
  82. /**
  83. * Adds a like to a tweet privately
  84. * @param tweetId tweet id
  85. */
  86. public async addLike(tweetId: string) {
  87. const likeEntry = await this.getLikes(tweetId);
  88. const likes = this.gun.get(tweetId).put({
  89. likes: likeEntry.likes + 1
  90. });
  91. this.gun
  92. .get(this.osnPrefix)
  93. .get("likes")
  94. .set(likes);
  95. }
  96. /**
  97. * Retrieves the private likes for a tweet
  98. * @param tweetId tweet id
  99. */
  100. public async getLikes(tweetId: string) {
  101. const likeEntry = await this.gun
  102. .get(this.osnPrefix)
  103. .get("likes")
  104. .get(tweetId)
  105. .then();
  106. if (likeEntry === undefined) {
  107. return {
  108. likes: 0
  109. };
  110. } else {
  111. return likeEntry;
  112. }
  113. }
  114. }