@nina-protocol/nina-db 0.0.87 → 0.0.89

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,7 @@ import Post from './Post.js';
9
9
  import Tag from './Tag.js';
10
10
  import axios from 'axios';
11
11
  import { customAlphabet } from 'nanoid';
12
+ import promiseRetry from 'promise-retry';
12
13
  const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
13
14
  const randomStringGenerator = customAlphabet(alphabet, 12);
14
15
  export default class Release extends Model {
@@ -46,42 +47,66 @@ export default class Release extends Model {
46
47
  },
47
48
  };
48
49
  static findOrCreate = async (publicKey, hubPublicKey = null) => {
49
- let release = await Release.query().findOne({ publicKey });
50
- if (release) {
51
- return release;
52
- }
53
- const connection = new anchor.web3.Connection(process.env.SOLANA_CLUSTER_URL);
54
- const provider = new anchor.AnchorProvider(connection, {}, { commitment: 'processed' });
55
- const program = await anchor.Program.at(process.env.NINA_PROGRAM_ID, provider);
56
- const metaplex = new Metaplex(connection);
57
- const releaseAccount = await program.account.release.fetch(new anchor.web3.PublicKey(publicKey), 'confirmed');
58
- let metadataAccount = (await metaplex.nfts().findAllByMintList({ mints: [releaseAccount.releaseMint] }, { commitment: 'confirmed' }))[0];
59
- let json;
60
50
  try {
61
- json = (await axios.get(metadataAccount.uri.replace('www.', '').replace('arweave.net', 'gateway.irys.xyz'))).data;
51
+ let release = await Release.query().findOne({ publicKey });
52
+ if (release) {
53
+ return release;
54
+ }
55
+ const connection = new anchor.web3.Connection(process.env.SOLANA_CLUSTER_URL);
56
+ const provider = new anchor.AnchorProvider(connection, {}, { commitment: 'confirmed' });
57
+ const program = await anchor.Program.at(process.env.NINA_PROGRAM_ID, provider);
58
+ const metaplex = new Metaplex(connection);
59
+ let attempts = 0;
60
+ const releaseAccount = await promiseRetry(async (retry) => {
61
+ try {
62
+ attempts += 1;
63
+ const result = await program.account.release.fetch(new anchor.web3.PublicKey(publicKey), 'confirmed');
64
+ return result;
65
+ }
66
+ catch (error) {
67
+ console.log('error fetching release account', error);
68
+ retry(error);
69
+ }
70
+ }, {
71
+ retries: 50,
72
+ minTimeout: 500,
73
+ maxTimeout: 1500,
74
+ });
75
+ let metadataAccount = (await metaplex.nfts().findAllByMintList({ mints: [releaseAccount.releaseMint] }, { commitment: 'confirmed' }))[0];
76
+ if (!metadataAccount) {
77
+ throw new Error('No metadata account found for release - is not a complete release');
78
+ }
79
+ let json;
80
+ try {
81
+ json = (await axios.get(metadataAccount.uri.replace('www.', '').replace('arweave.net', 'gateway.irys.xyz'))).data;
82
+ }
83
+ catch (error) {
84
+ json = (await axios.get(metadataAccount.uri.replace('gateway.irys.xyz', 'arweave.net'))).data;
85
+ }
86
+ const slug = await this.generateSlug(json);
87
+ let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
88
+ release = await this.createRelease({
89
+ publicKey,
90
+ mint: releaseAccount.releaseMint.toBase58(),
91
+ metadata: json,
92
+ datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
93
+ slug,
94
+ publisherId: publisher.id,
95
+ releaseAccount
96
+ });
97
+ if (hubPublicKey) {
98
+ const hub = await Hub.query().findOne({ publicKey: hubPublicKey });
99
+ await release.$query().patch({ hubId: hub.id });
100
+ await Hub.relatedQuery('releases').for(hub.id).patch({
101
+ visible: true,
102
+ }).where({ id: release.id });
103
+ }
104
+ return release;
62
105
  }
63
106
  catch (error) {
64
- json = (await axios.get(metadataAccount.uri.replace('gateway.irys.xyz', 'arweave.net'))).data;
65
- }
66
- const slug = await this.generateSlug(json);
67
- let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
68
- release = await this.createRelease({
69
- publicKey,
70
- mint: releaseAccount.releaseMint.toBase58(),
71
- metadata: json,
72
- datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
73
- slug,
74
- publisherId: publisher.id,
75
- releaseAccount
76
- });
77
- if (hubPublicKey) {
78
- const hub = await Hub.query().findOne({ publicKey: hubPublicKey });
79
- await release.$query().patch({ hubId: hub.id });
80
- await Hub.relatedQuery('releases').for(hub.id).patch({
81
- visible: true,
82
- }).where({ id: release.id });
107
+ console.log('error finding or creating release: ', error);
108
+ return null;
83
109
  }
84
- return release;
85
110
  };
86
111
  static createRelease = async ({ publicKey, mint, metadata, datetime, publisherId, releaseAccount }) => {
87
112
  const slug = await this.generateSlug(metadata);
@@ -11,7 +11,7 @@ class Subscription extends Model {
11
11
  static get jsonSchema() {
12
12
  return {
13
13
  type: 'object',
14
- required: ['publicKey', 'datetime'],
14
+ required: ['datetime'],
15
15
  properties: {
16
16
  publicKey: { type: 'string' },
17
17
  datetime: { type: 'string' },
@@ -25,12 +25,12 @@ class Subscription extends Model {
25
25
  };
26
26
  }
27
27
  static async findOrCreate({ publicKey, from, to, datetime, subscriptionType }) {
28
- let subscription = await Subscription.query().findOne({ publicKey });
28
+ let subscription = await Subscription.query().findOne({ from, to });
29
29
  if (subscription) {
30
30
  return subscription;
31
31
  }
32
32
  subscription = await Subscription.query().insert({
33
- publicKey, from, to, datetime, subscriptionType
33
+ from, to, datetime, subscriptionType
34
34
  });
35
35
  console.log('Inserted subscription: ', publicKey);
36
36
  return subscription;
@@ -0,0 +1,22 @@
1
+ import 'dotenv/config';
2
+ import axios from 'axios';
3
+ import { Account, connectDb } from '../index.js';
4
+ export const seed = async () => {
5
+ await connectDb();
6
+ const accountsWithoutHandle = await Account.query().whereNull('handle');
7
+ for await (let account of accountsWithoutHandle) {
8
+ try {
9
+ const response = await axios.get(`${process.env.ID_SERVER_ENDPOINT}/profile/${account.publicKey}`);
10
+ await account.$query().patch({
11
+ handle: response.data.profile.handle,
12
+ displayName: response.data.profile.displayName,
13
+ image: response.data.profile.image,
14
+ description: response.data.profile.description,
15
+ });
16
+ console.log('Updated Account:', account.handle, account.publicKey);
17
+ }
18
+ catch (error) {
19
+ console.log('Error updating Account', account.publicKey, error);
20
+ }
21
+ }
22
+ };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@nina-protocol/nina-db",
3
- "version": "0.0.87",
3
+ "version": "0.0.89",
4
4
  "description": "",
5
5
  "source": "src/index.js",
6
6
  "main": "dist/index.js",
@@ -29,6 +29,7 @@
29
29
  "nanoid": "^4.0.2",
30
30
  "objection": "2.2.15",
31
31
  "pg": "^8.7.3",
32
+ "promise-retry": "^2.0.1",
32
33
  "striptags": "^3.2.0",
33
34
  "twitter-api-v2": "^1.12.9"
34
35
  },
@@ -0,0 +1,15 @@
1
+ /**
2
+ * @param { import("knex").Knex } knex
3
+ * @returns { Promise<void> }
4
+ */
5
+ export const up = function(knex) {
6
+ return knex.schema.alterTable('subscriptions', table => {
7
+ table.dropColumn('publicKey');
8
+ });
9
+ };
10
+ /**
11
+ * @param { import("knex").Knex } knex
12
+ * @returns { Promise<void> }
13
+ */
14
+ export const down = function(knex) {
15
+ };
@@ -9,6 +9,8 @@ import Post from './Post.js';
9
9
  import Tag from './Tag.js';
10
10
  import axios from 'axios';
11
11
  import { customAlphabet } from 'nanoid';
12
+ import promiseRetry from 'promise-retry';
13
+
12
14
  const alphabet = '0123456789abcdefghijklmnopqrstuvwxyz';
13
15
  const randomStringGenerator = customAlphabet(alphabet, 12);
14
16
 
@@ -49,49 +51,75 @@ export default class Release extends Model {
49
51
  }
50
52
 
51
53
  static findOrCreate = async (publicKey, hubPublicKey=null) => {
52
- let release = await Release.query().findOne({ publicKey });
53
- if (release) {
54
- return release;
55
- }
56
-
57
- const connection = new anchor.web3.Connection(process.env.SOLANA_CLUSTER_URL);
58
- const provider = new anchor.AnchorProvider(connection, {}, {commitment: 'processed'})
59
- const program = await anchor.Program.at(
60
- process.env.NINA_PROGRAM_ID,
61
- provider,
62
- )
63
- const metaplex = new Metaplex(connection);
64
- const releaseAccount = await program.account.release.fetch(new anchor.web3.PublicKey(publicKey), 'confirmed')
65
- let metadataAccount = (await metaplex.nfts().findAllByMintList({mints: [releaseAccount.releaseMint]}, { commitment: 'confirmed' }))[0];
66
- let json
67
54
  try {
68
- json = (await axios.get(metadataAccount.uri.replace('www.','').replace('arweave.net', 'gateway.irys.xyz'))).data
69
- } catch (error) {
70
- json = (await axios.get(metadataAccount.uri.replace('gateway.irys.xyz', 'arweave.net'))).data
71
- }
72
-
73
- const slug = await this.generateSlug(json);
74
- let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
75
- release = await this.createRelease({
76
- publicKey,
77
- mint: releaseAccount.releaseMint.toBase58(),
78
- metadata: json,
79
- datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
80
- slug,
81
- publisherId: publisher.id,
82
- releaseAccount
83
- });
84
-
85
- if (hubPublicKey) {
55
+ let release = await Release.query().findOne({ publicKey });
56
+ if (release) {
57
+ return release;
58
+ }
59
+
60
+ const connection = new anchor.web3.Connection(process.env.SOLANA_CLUSTER_URL);
61
+ const provider = new anchor.AnchorProvider(connection, {}, {commitment: 'confirmed'})
62
+ const program = await anchor.Program.at(
63
+ process.env.NINA_PROGRAM_ID,
64
+ provider,
65
+ )
66
+ const metaplex = new Metaplex(connection);
67
+ let attempts = 0;
86
68
 
87
- const hub = await Hub.query().findOne({ publicKey: hubPublicKey })
88
- await release.$query().patch({ hubId: hub.id })
89
- await Hub.relatedQuery('releases').for(hub.id).patch({
90
- visible: true,
91
- }).where( {id: release.id });
69
+ const releaseAccount = await promiseRetry(
70
+ async (retry) => {
71
+ try {
72
+ attempts += 1
73
+ const result = await program.account.release.fetch(new anchor.web3.PublicKey(publicKey), 'confirmed')
74
+ return result
75
+ } catch (error) {
76
+ console.log('error fetching release account', error)
77
+ retry(error)
78
+ }
79
+ }, {
80
+ retries: 50,
81
+ minTimeout: 500,
82
+ maxTimeout: 1500,
83
+ }
84
+ )
85
+
86
+ let metadataAccount = (await metaplex.nfts().findAllByMintList({mints: [releaseAccount.releaseMint]}, { commitment: 'confirmed' }))[0];
87
+ if (!metadataAccount) {
88
+ throw new Error('No metadata account found for release - is not a complete release')
89
+ }
90
+ let json
91
+ try {
92
+ json = (await axios.get(metadataAccount.uri.replace('www.','').replace('arweave.net', 'gateway.irys.xyz'))).data
93
+ } catch (error) {
94
+ json = (await axios.get(metadataAccount.uri.replace('gateway.irys.xyz', 'arweave.net'))).data
95
+ }
96
+
97
+ const slug = await this.generateSlug(json);
98
+ let publisher = await Account.findOrCreate(releaseAccount.authority.toBase58());
99
+ release = await this.createRelease({
100
+ publicKey,
101
+ mint: releaseAccount.releaseMint.toBase58(),
102
+ metadata: json,
103
+ datetime: new Date(releaseAccount.releaseDatetime.toNumber() * 1000).toISOString(),
104
+ slug,
105
+ publisherId: publisher.id,
106
+ releaseAccount
107
+ });
108
+
109
+ if (hubPublicKey) {
110
+
111
+ const hub = await Hub.query().findOne({ publicKey: hubPublicKey })
112
+ await release.$query().patch({ hubId: hub.id })
113
+ await Hub.relatedQuery('releases').for(hub.id).patch({
114
+ visible: true,
115
+ }).where( {id: release.id });
116
+ }
117
+
118
+ return release;
119
+ } catch (error) {
120
+ console.log('error finding or creating release: ', error)
121
+ return null;
92
122
  }
93
-
94
- return release;
95
123
  }
96
124
 
97
125
  static createRelease = async ({publicKey, mint, metadata, datetime, publisherId, releaseAccount}) => {
@@ -12,7 +12,7 @@ class Subscription extends Model {
12
12
  static get jsonSchema() {
13
13
  return {
14
14
  type: 'object',
15
- required: ['publicKey', 'datetime'],
15
+ required: ['datetime'],
16
16
  properties: {
17
17
  publicKey: { type: 'string' },
18
18
  datetime: { type: 'string' },
@@ -27,13 +27,13 @@ class Subscription extends Model {
27
27
  }
28
28
 
29
29
  static async findOrCreate({publicKey, from, to, datetime, subscriptionType}) {
30
- let subscription = await Subscription.query().findOne({ publicKey });
30
+ let subscription = await Subscription.query().findOne({ from, to });
31
31
  if (subscription) {
32
32
  return subscription;
33
33
  }
34
34
 
35
35
  subscription = await Subscription.query().insert({
36
- publicKey, from, to, datetime, subscriptionType
36
+ from, to, datetime, subscriptionType
37
37
  });
38
38
  console.log('Inserted subscription: ', publicKey)
39
39
  return subscription;
@@ -0,0 +1,23 @@
1
+ import 'dotenv/config';
2
+ import axios from 'axios';
3
+ import { Account, connectDb } from '../index.js';
4
+ export const seed = async () => {
5
+ await connectDb();
6
+
7
+ const accountsWithoutHandle = await Account.query().whereNull('handle');
8
+
9
+ for await (let account of accountsWithoutHandle) {
10
+ try {
11
+ const response = await axios.get(`${process.env.ID_SERVER_ENDPOINT}/profile/${account.publicKey}`);
12
+ await account.$query().patch({
13
+ handle: response.data.profile.handle,
14
+ displayName: response.data.profile.displayName,
15
+ image: response.data.profile.image,
16
+ description: response.data.profile.description,
17
+ });
18
+ console.log('Updated Account:', account.handle, account.publicKey);
19
+ } catch (error) {
20
+ console.log('Error updating Account', account.publicKey, error);
21
+ }
22
+ }
23
+ }
@@ -1,15 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- exports.up = function(knex) {
6
-
7
- };
8
-
9
- /**
10
- * @param { import("knex").Knex } knex
11
- * @returns { Promise<void> }
12
- */
13
- exports.down = function(knex) {
14
-
15
- };
@@ -1,15 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- exports.up = function(knex) {
6
-
7
- };
8
-
9
- /**
10
- * @param { import("knex").Knex } knex
11
- * @returns { Promise<void> }
12
- */
13
- exports.down = function(knex) {
14
-
15
- };
@@ -1,15 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- exports.up = function(knex) {
6
-
7
- };
8
-
9
- /**
10
- * @param { import("knex").Knex } knex
11
- * @returns { Promise<void> }
12
- */
13
- exports.down = function(knex) {
14
-
15
- };
@@ -1,15 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- exports.up = function(knex) {
6
-
7
- };
8
-
9
- /**
10
- * @param { import("knex").Knex } knex
11
- * @returns { Promise<void> }
12
- */
13
- exports.down = function(knex) {
14
-
15
- };
@@ -1,15 +0,0 @@
1
- /**
2
- * @param { import("knex").Knex } knex
3
- * @returns { Promise<void> }
4
- */
5
- exports.up = function(knex) {
6
-
7
- };
8
-
9
- /**
10
- * @param { import("knex").Knex } knex
11
- * @returns { Promise<void> }
12
- */
13
- exports.down = function(knex) {
14
-
15
- };