-
Notifications
You must be signed in to change notification settings - Fork 0
/
artwork.ts
326 lines (284 loc) · 9.55 KB
/
artwork.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
import { Context } from '@nuxt/types'
import Transaction from 'arweave/node/lib/transaction'
import _ from 'lodash'
import {
ArtworkTransactionFactory,
ArtworkCreationOptions,
ArtworkFactory,
Artwork
} from '~/app/core/artwork'
import { FeedItem } from '~/app/core/feed'
import { uuidv4 } from '~/app/util'
import { LIKED_ENTITY_TAG } from './likes'
import { TransactionService, LikesService } from '.'
const PAGE_SIZE = 9
export default class ArtworkService extends TransactionService {
$likesService!: LikesService
artworkTransactionFactory!: ArtworkTransactionFactory
cache: {
slugs: { [slug: string]: string }
artwork: { [id: string]: Artwork }
} = { slugs: {}, artwork: {} }
constructor(context: Context) {
super(context)
this.$likesService = context.$likesService
this.artworkTransactionFactory = new ArtworkTransactionFactory(
this.$arweave,
this.config.app.name,
this.config.app.version,
this.config.contracts.atomicLicenseBase
)
}
async createArtworkTransaction(
opts: ArtworkCreationOptions,
logCb?: Function
): Promise<Transaction> {
return await this.artworkTransactionFactory.create(opts, logCb)
}
async fetchByTxIdOrSlug(
txIdOrSlug: string,
owner: string
): Promise<Artwork | null> {
if (this.cache.slugs[txIdOrSlug]) {
return await this.fetch(this.cache.slugs[txIdOrSlug])
}
const { transactions } = await this.transactionFactory.searchTransactions(
'artwork:bundle',
owner,
{
sort: 'HEIGHT_DESC',
tags: [
{ tag: 'slug', value: txIdOrSlug },
{ tag: 'Bundle-Format', value: 'binary' },
{ tag: 'Bundle-Version', value: '2.0.0' }
]
}
)
if (transactions[0]) {
const tx = transactions[0]
try {
const manifestTag = tx.tags.find((tag) => tag.name === 'Manifest-ID')
if (manifestTag) {
this.cache.slugs[txIdOrSlug] = manifestTag.value
return await this.fetch(manifestTag.value)
}
} catch (err) {}
}
const v0result = await this.transactionFactory.searchTransactions(
'artwork',
owner,
{
type: 'application/json',
sort: 'HEIGHT_DESC',
tags: [{ tag: 'slug', value: txIdOrSlug }]
}
)
if (v0result.transactions[0]) {
this.cache.slugs[txIdOrSlug] = v0result.transactions[0].id
return await this.fetch(v0result.transactions[0].id)
}
// If no slug matches, try treating it as a txid
return txIdOrSlug.length === 43
? await this.fetch(txIdOrSlug)
: null
}
async fetch(id: string): Promise<Artwork | null> {
try {
if (!this.cache.artwork[id]) {
const url = `${this.context.$arweaveService.config.gateway}/${id}`
const res = await this.context.$axios.get(url)
const artwork = new ArtworkFactory().create(id, res.data)
this.cache.artwork[id] = artwork
}
let views
try {
const viewsResponse = await this.context.$axios.get(
`${this.context.$arweaveService.config.gateway}/views/${id}`
)
if (typeof viewsResponse.data === 'number') {
views = viewsResponse.data
}
} catch (viewFetchError) {}
return { views, ...this.cache.artwork[id] }
} catch (error) {
console.error(error)
return null
}
}
async fetchFeed(
creator?: string | string[] | null,
cursor?: string,
cursorV0?: string,
limit: number = PAGE_SIZE
): Promise<{ cursor: string, cursorV0?: string, feed: FeedItem[]}> {
if (!creator) {
switch (this.config.app.name) {
case 'ArtByCity':
creator = [
'mKRPxOSIe08BddCnrL9en8C3hUGqwA5l1sUZilGsjDg',
'zIe2L7WAptLeDdDUcGPOFtBkItZuRE2wE2GQh2LfFqc',
'hyL5aEp4K7fd7hYEjKxi6caxjyL2UANONOnnnFe7jwc',
'aJIPwBoPqt1FGaa4pRoMotuDZr68PHRAoXe3lUerFTs',
'1KZdIq1mkiTjb1gf6f5c__MUkheFyU6UK8-MMciSKnE',
'dYRuag6nzcdFXJ02_Ljf8ks-WDUrpAMeSI0agFd_ZBQ',
'276ituk07Igo0QzT5sa_ISRXwp04T5RfzBwZHS808h4',
'0EeHrLUiUepAW3IZ8ZrNRu8bxEOlumouTdMYKBPPPTM',
'1cX5WcLKn2kPWmiXPKbRIPpAWm5YP9H1172HpkYN1QI',
'qoFt_qa74r8rPAIeZVd7wJUdQwLi8_7NvDOxs1dWf3c',
'A2JEmT4LTk5WeBUFRGESX07OR-FUbtqfdBlBl8ooB64',
'k4fkc6NDiMmfVR06qX5P5XvAOlLi9J9fgBaa8pLvIBc',
'6uzZ2xVslWpFib3BR-J731uogU-FRuKDUifFVRZq8xE',
'CxuvfybNxiEPpXGqIbM7tzzdSYOlOW9VOUWkf0FWH4c',
'9XaFpHnpswVxZ7juWnG5iK9tYocYG_EYRf9WRmzbfCQ',
'83cE_sKfvJ2XugWumQZPY8KU9PpWqC8P_CnItpJWXn4',
'Qk6U75ZH_Wtwq5BsNSZK35eYq2lUZbVtP_qfif2JeZk',
'xzCrjqG0qjX66T8mGNFyQEKd4qK6clSlp8XVvw2glwE',
'i_0lXhTIsKKABEKYNlNTiKtY3QTbo9Dm56uzEvvi85g',
'Q-47UUx60RSHNKS8jWkJA0DHX--PhGDC_0njFIplraQ',
'3VOaO9LbI-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LtILfPM8agd7RU6AaQmwh0SFEvxPu-tb06E_iHksvUM',
'P9TlkKY8NEuiBWf-YGIcVV9ZVWjhh9WgtoM8ej8jhJ8',
// 'lULoyhunyPeZGVrZnDnTfDBxS-XOFV-qaNphiwPH2Ps',
'mKRPxOSIe08BddCnrL9en8C3hUGqwA5l1sUZilGsjDg',
'zIe2L7WAptLeDdDUcGPOFtBkItZuRE2wE2GQh2LfFqc',
'hyL5aEp4K7fd7hYEjKxi6caxjyL2UANONOnnnFe7jwc',
'aJIPwBoPqt1FGaa4pRoMotuDZr68PHRAoXe3lUerFTs',
'1KZdIq1mkiTjb1gf6f5c__MUkheFyU6UK8-MMciSKnE',
'dYRuag6nzcdFXJ02_Ljf8ks-WDUrpAMeSI0agFd_ZBQ'
]
break
default:
creator = [
'MlV6DeOtRmakDOf6vgOBlif795tcWimgyPsYYNQ8q1Y',
'mKRPxOSIe08BddCnrL9en8C3hUGqwA5l1sUZilGsjDg',
'zIe2L7WAptLeDdDUcGPOFtBkItZuRE2wE2GQh2LfFqc',
'hyL5aEp4K7fd7hYEjKxi6caxjyL2UANONOnnnFe7jwc',
'aJIPwBoPqt1FGaa4pRoMotuDZr68PHRAoXe3lUerFTs',
'1KZdIq1mkiTjb1gf6f5c__MUkheFyU6UK8-MMciSKnE'
]
}
}
const {
transactions,
cursor: nextCursor
} = await this.transactionFactory.searchTransactions(
['artwork:bundle'],
creator,
{
sort: 'HEIGHT_DESC',
cursor,
limit
}
)
let nextCursorV0: string | undefined = undefined
if (limit - transactions.length > 0) {
// Search for v0 artwork (published before block #891282)
const maxV0artworkBlockHeight = 891282
const {
transactions: transactionsV0,
cursor: _nextCursorV0
} = await this.transactionFactory.searchTransactions(
['artwork'],
creator,
{
sort: 'HEIGHT_DESC',
limit: 100, // There won't be many if we are using max block height
cursor: cursorV0,
max: maxV0artworkBlockHeight
}
)
if (transactionsV0.length > 0) {
nextCursorV0 = _nextCursorV0
transactions.push(...transactionsV0)
}
}
const txIds = _
.chain(transactions)
.map(tx => {
try {
const manifestTag = tx.tags.find((tag) => tag.name === 'Manifest-ID')
if (manifestTag) {
return manifestTag.value
}
return tx.id
} catch (err) {
return ''
}
})
.filter(txId => !!txId)
.uniq()
.value()
return {
cursor: nextCursor || cursor || '',
cursorV0: nextCursorV0 || cursorV0,
feed: this.buildFeed(txIds)
}
}
async fetchLikedArtworkFeed(
address: string,
cursor?: string
): Promise<{ cursor: string, feed: FeedItem[] }> {
const {
transactions,
cursor: nextCursor
} = await this.$likesService.fetchUserLikes(
address,
cursor,
false,
PAGE_SIZE
)
const likedEntityTxIds = transactions.map(tx => {
try {
const likedEntityTag = tx.tags.find(
tag => tag.name === LIKED_ENTITY_TAG
)
if (likedEntityTag) {
return likedEntityTag.value
}
return ''
} catch (err) {
return ''
}
}).filter(txId => !!txId)
return {
cursor: nextCursor,
feed: this.buildFeed(likedEntityTxIds)
}
}
private buildFeed(txs: string[]): FeedItem[] {
return txs.map((txId) => {
const item: FeedItem = {
guid: uuidv4(),
category: 'artwork',
txId
}
return item
})
}
}