|
@@ -32,7 +32,7 @@ export class FeedProvider {
|
|
|
const intervalEnd: Date = this.getOldestTweetTimestamp(tweets);
|
|
|
|
|
|
// Fetch private tweet hashs from P2P DB for corresponding interval
|
|
|
- const privateTweetHashs: string[] = await this.gun.fetchPrivateTweetHashsForUserInInterval(
|
|
|
+ const privateTweetHashs: object[] = await this.gun.fetchPrivateTweetHashsForUserInInterval(
|
|
|
userId,
|
|
|
intervalStart,
|
|
|
intervalEnd
|
|
@@ -107,19 +107,33 @@ export class FeedProvider {
|
|
|
}
|
|
|
}
|
|
|
|
|
|
- private async fetchPrivateTweets(privateTweetHashs, userId: string) {
|
|
|
+ private async fetchPrivateTweets(
|
|
|
+ privateTweetsData: object[],
|
|
|
+ userId: string
|
|
|
+ ) {
|
|
|
+ const privateTweets = [];
|
|
|
+
|
|
|
// Load private tweets from P2P storage
|
|
|
- const privateEncryptedTweets = await this.ipfs.fetchTweets(
|
|
|
- privateTweetHashs
|
|
|
- );
|
|
|
+ for (let i = 0; i < privateTweetsData.length; i++) {
|
|
|
+ const hash = privateTweetsData[i]["hash"];
|
|
|
+ privateTweetsData[i]["encryptedTweet"] = await this.ipfs.fetchTweet(hash);
|
|
|
+ }
|
|
|
|
|
|
- // Fetch public key
|
|
|
- const publicKey = await this.cryptoUtils.fetchPublicKeyForUser(userId);
|
|
|
+ // Fetch public key history for user
|
|
|
+ const publicKeyHistory: object[] = await this.cryptoUtils.fetchPublicKeyHistoryForUser(
|
|
|
+ userId
|
|
|
+ );
|
|
|
|
|
|
// Decrypt tweets
|
|
|
- const privateTweets = privateEncryptedTweets.map(encryptedTweet =>
|
|
|
- JSON.parse(this.cryptoUtils.decrypt(encryptedTweet, publicKey))
|
|
|
- );
|
|
|
+ for (let i = 0; i < privateTweetsData.length; i++) {
|
|
|
+ const encryptedTweet = privateTweetsData[i]["encryptedTweet"];
|
|
|
+ const timestamp = privateTweetsData[i]["created_at"];
|
|
|
+ const decryptedTweet = this.cryptoUtils.decrypt(
|
|
|
+ encryptedTweet,
|
|
|
+ this.getPublicKeyAt(timestamp, publicKeyHistory)
|
|
|
+ );
|
|
|
+ privateTweets.push(JSON.parse(decryptedTweet));
|
|
|
+ }
|
|
|
|
|
|
// Add user object to private tweets
|
|
|
return await Promise.all(
|
|
@@ -127,6 +141,21 @@ export class FeedProvider {
|
|
|
);
|
|
|
}
|
|
|
|
|
|
+ private getPublicKeyAt(
|
|
|
+ timestamp: string,
|
|
|
+ publicKeyHistory: object[]
|
|
|
+ ): string {
|
|
|
+ const timestampTweet = new Date(timestamp).getTime();
|
|
|
+ for (let key of publicKeyHistory) {
|
|
|
+ const timestampKey = new Date(key["validFrom"]).getTime();
|
|
|
+ if (timestampTweet > timestampKey) {
|
|
|
+ return key["key"];
|
|
|
+ }
|
|
|
+ }
|
|
|
+ // todo: throw error
|
|
|
+ return "";
|
|
|
+ }
|
|
|
+
|
|
|
private async addUserToTweet(tweet) {
|
|
|
tweet.user = await this.twitter.fetchUser(tweet.user_id);
|
|
|
return tweet;
|