Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(user program): updated the app to use the new user program #1511

Merged
merged 8 commits into from
Feb 25, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions components/views/chat/chatbar/Chatbar.html
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
>
<div class="control-wrap file-upload-wrap">
<FilesUpload
v-if="recipient"
:editable="recipient ? true : false"
:recipient="recipient"
type="quick"
Expand Down
4 changes: 2 additions & 2 deletions components/views/files/controls/Controls.vue
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ export default Vue.extend({
return
}
this.text = ''
await this.$Bucket.updateIndex(this.$FileSystem.export)
await this.$TextileManager.bucket?.updateIndex(this.$FileSystem.export)
this.$store.commit('ui/setIsLoadingFileIndex', false)
this.$emit('forceRender')
},
Expand Down Expand Up @@ -142,7 +142,7 @@ export default Vue.extend({

// only update index if files have been updated
if (files.length) {
await this.$Bucket.updateIndex(this.$FileSystem.export)
await this.$TextileManager.bucket?.updateIndex(this.$FileSystem.export)
}

this.$store.commit('ui/setIsLoadingFileIndex', false)
Expand Down
6 changes: 3 additions & 3 deletions components/views/files/file/File.vue
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ export default Vue.extend({
async like() {
this.$store.commit('ui/setIsLoadingFileIndex', true)
this.item.toggleLiked()
await this.$Bucket.updateIndex(this.$FileSystem.export)
await this.$TextileManager.bucket?.updateIndex(this.$FileSystem.export)
this.item.liked
? this.$toast.show(this.$t('pages.files.add_favorite') as string)
: this.$toast.show(this.$t('pages.files.remove_favorite') as string)
Expand All @@ -131,7 +131,7 @@ export default Vue.extend({
if (!this.item.shared) {
this.$store.commit('ui/setIsLoadingFileIndex', true)
this.item.shareItem()
await this.$Bucket.updateIndex(this.$FileSystem.export)
await this.$TextileManager.bucket?.updateIndex(this.$FileSystem.export)
this.$store.commit('ui/setIsLoadingFileIndex', false)
}
navigator.clipboard.writeText(this.path).then(() => {
Expand All @@ -156,7 +156,7 @@ export default Vue.extend({
await this.$FileSystem.removeFile(this.item.name)
}
this.$FileSystem.removeChild(this.item.name)
await this.$Bucket.updateIndex(this.$FileSystem.export)
await this.$TextileManager.bucket?.updateIndex(this.$FileSystem.export)
this.$store.commit('ui/setIsLoadingFileIndex', false)
this.$emit('forceRender')
},
Expand Down
2 changes: 1 addition & 1 deletion components/views/files/view/View.vue
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ export default Vue.extend({
if (!this.file.shared) {
this.$store.commit('ui/setIsLoadingFileIndex', true)
this.file.shareItem()
await this.$Bucket.updateIndex(this.$FileSystem.export)
await this.$TextileManager.bucket?.updateIndex(this.$FileSystem.export)
this.$store.commit('ui/setIsLoadingFileIndex', false)
}
navigator.clipboard.writeText(this.path).then(() => {
Expand Down
5 changes: 3 additions & 2 deletions components/views/friends/add/Add.vue
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import { debounce } from 'lodash'
import { Friend } from '~/types/ui/friends'
import ServerProgram from '~/libraries/Solana/ServerProgram/ServerProgram'
WanderingHogan marked this conversation as resolved.
Show resolved Hide resolved
import SolanaManager from '~/libraries/Solana/SolanaManager/SolanaManager'
import UsersProgram from '~/libraries/Solana/UsersProgram/UsersProgram'

export default Vue.extend({
components: {
Expand Down Expand Up @@ -69,9 +70,9 @@ export default Vue.extend({
this.error = ''
try {
const $SolanaManager: SolanaManager = Vue.prototype.$SolanaManager
const serverProgram: ServerProgram = new ServerProgram($SolanaManager)
const usersProgram: UsersProgram = new UsersProgram($SolanaManager)

const friend = await serverProgram.getUser(new PublicKey(accountID))
const friend = await usersProgram.getUserInfo(accountID)
if (!friend) {
this.error = this.$t('friends.not_found') as string
return
Expand Down
3 changes: 3 additions & 0 deletions config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export const Config = {
browser: 'https://hub.textile.io',
groupChatThreadID:
'bafkv7ordeargenxdutqdltvlo6sbfcfdhuvmocrt4qe6kpohrdbrbdi',
fsTable: 'sat.json',
bucketName: 'personal-files',
},
ipfs: {
gateway: 'https://satellite.mypinata.cloud/ipfs/',
Expand Down Expand Up @@ -50,6 +52,7 @@ export const Config = {
groupchatsProgramId: 'bJhvwTYCkQceANgeShZ4xaxUqEBPsV8e1NgRnLRymxs',
defaultCommitment: 'confirmed' as Commitment,
defaultPreflightCommitment: 'confirmed' as Commitment,
usersProgramId: '7MaC2xrAmmFsuRBEkD6BEL3eJpXCmaikYhLM3eKBPhAH',
},
// Realms are just different chains we support
realms: [
Expand Down
2 changes: 1 addition & 1 deletion libraries/Files/TextileFileSystem.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class TextileFileSystem extends FilSystem {
* @returns {Bucket} bucket global to upload files to textile
*/
get bucket() {
return Vue.prototype.$Bucket
return Vue.prototype.$TextileManager.bucket
}

/**
Expand Down
89 changes: 33 additions & 56 deletions libraries/Files/remote/textile/Bucket.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,29 +7,22 @@ import {
} from '@textile/hub'
import { RFM } from '../abstracts/RFM.abstract'
import { RFMInterface } from '../interface/RFM.interface'
import { TextileErrors } from '../../errors/Errors'
import { Config } from '~/config'
import {
BucketConfig,
TextileInitializationData,
} from '~/types/textile/manager'
import IdentityManager from '~/libraries/Textile/IdentityManager'
import { TextileInitializationData } from '~/types/textile/manager'
import {
FileSystemExport,
FILESYSTEM_TYPE,
} from '~/libraries/Files/types/filesystem'

export class Bucket extends RFM implements RFMInterface {
private creds: { id: any; pass: any } = { id: null, pass: null }
private identityManager: IdentityManager
private _textile: TextileInitializationData | null = null
private _textile: TextileInitializationData
private _index: FileSystemExport | null = null
private buckets: Buckets | null
private key: Root['key'] | null

constructor() {
constructor(textile: TextileInitializationData) {
super()
this.identityManager = new IdentityManager()
this._textile = textile
this.buckets = null
this.key = null
}
Expand All @@ -56,58 +49,42 @@ export class Bucket extends RFM implements RFMInterface {
* @param param0 Bucket Configuration that includes id, password, SolanaWallet instance, and bucket name
* @returns a promise that resolves when the initialization completes
*/
async init({
id,
pass,
wallet,
name,
}: BucketConfig): Promise<FileSystemExport | null> {
if (!wallet) {
throw new Error(TextileErrors.MISSING_WALLET)
}

const identity = await this.identityManager.initFromWallet(wallet)
const { client, users } = await this.identityManager.authorize(identity)

this.creds = {
id,
pass,
}

this._textile = {
identity,
client,
wallet,
users,
}

async init(name: string): Promise<FileSystemExport> {
if (!Config.textile.key) {
throw new Error('Textile key not found')
}

this.buckets = await Buckets.withKeyInfo({ key: Config.textile.key })
await this.buckets.getToken(identity)
await this.buckets.getToken(this._textile.identity)

const result = await this.buckets.getOrCreate(name)

if (!result.root) throw new Error(`failed to open bucket ${name}`)

this.key = result.root.key

const hash = ((await this.buckets.listPath(this.key, 'sat.json')) as Path)
?.item?.path

this._index = await fetch(Config.textile.browser + hash)
.then((res) => {
return res.json()
})
.then((data) => {
return data
})
.catch(() => {
return {
type: FILESYSTEM_TYPE.DEFAULT,
version: 1,
content: [],
}
})
return this._index
try {
const path: Path | void = await this.buckets.listPath(
this.key,
Config.textile.fsTable,
)

const hash = path?.item?.path

this._index = await fetch(Config.textile.browser + hash).then((res) =>
res.json(),
)

if (!this._index) throw new Error('Index not found')

return this._index
} catch (e) {
return {
type: FILESYSTEM_TYPE.DEFAULT,
version: 1,
content: [],
}
}
}

/**
Expand All @@ -122,7 +99,7 @@ export class Bucket extends RFM implements RFMInterface {
this._index = index
await this.buckets.pushPath(
this.key,
'sat.json',
Config.textile.fsTable,
Buffer.from(JSON.stringify(index)),
)
}
Expand Down
7 changes: 1 addition & 6 deletions libraries/Files/test/Bucket.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
import { Fil } from '../Fil'
import { FilSystem } from '../FilSystem'
import { Bucket } from '../remote/textile/Bucket'
import { DIRECTORY_TYPE } from '../types/directory'
import { FileSystemExport } from '../types/filesystem'

Expand Down Expand Up @@ -34,9 +33,5 @@ describe('Test FileSystem Directory', () => {

expect(ex.version + 1).toEqual(fs.export.version)
})
it('get uninitialized textile', () => {
const bucket = new Bucket()

expect(bucket.textile).toBeNull()
})
// TODO: add test for Bucket
})
22 changes: 11 additions & 11 deletions libraries/Solana/FriendsProgram/FriendsProgram.ts
Original file line number Diff line number Diff line change
Expand Up @@ -637,17 +637,17 @@ export default class FriendsProgram extends EventEmitter {

const { connection } = this.solana

const userAccount = this.solana.getUserAccount()
if (!userAccount) {
const payerAccount = this.solana.getActiveAccount()
if (!payerAccount) {
throw new Error('User account not found')
}

const fromKeyAndStatus = base58(
Buffer.from([...userAccount.publicKey.toBytes(), status]),
Buffer.from([...payerAccount.publicKey.toBytes(), status]),
)

const statusAndToKey = base58(
Buffer.from([status, ...userAccount.publicKey.toBytes()]),
Buffer.from([status, ...payerAccount.publicKey.toBytes()]),
)

const outgoing = await connection.getProgramAccounts(FRIENDS_PROGRAM_ID, {
Expand Down Expand Up @@ -693,8 +693,8 @@ export default class FriendsProgram extends EventEmitter {
}

const { connection } = this.solana
const userAccount = this.solana.getUserAccount()
if (!userAccount) {
const payerAccount = this.solana.getActiveAccount()
if (!payerAccount) {
throw new Error('User account not found')
}

Expand All @@ -703,7 +703,7 @@ export default class FriendsProgram extends EventEmitter {
// formatted that way
// [32 bytes (sender public key)][1 byte (status)][32 bytes (recipient public key)]
const incomingRequestBytes = base58(
Buffer.from([FriendStatus.PENDING, ...userAccount.publicKey.toBytes()]),
Buffer.from([FriendStatus.PENDING, ...payerAccount.publicKey.toBytes()]),
)

const incomingRequestsFilter: GetProgramAccountsFilter = {
Expand All @@ -723,7 +723,7 @@ export default class FriendsProgram extends EventEmitter {
// This filter checks the sender public key (our) and the status
// [32 bytes (sender public key)][1 byte (status)][32 bytes (recipient public key)]
const newFriendFromOutgoingBytes = base58(
Buffer.from([...userAccount.publicKey.toBytes(), FriendStatus.ACCEPTED]),
Buffer.from([...payerAccount.publicKey.toBytes(), FriendStatus.ACCEPTED]),
)

const newFriendFromOutgoingFilter: GetProgramAccountsFilter = {
Expand All @@ -741,7 +741,7 @@ export default class FriendsProgram extends EventEmitter {
// This filter checks the sender public key (our) and the status
// [32 bytes (sender public key)][1 byte (status)][32 bytes (recipient public key)]
const friendRequestDeniedBytes = base58(
Buffer.from([...userAccount.publicKey.toBytes(), FriendStatus.REFUSED]),
Buffer.from([...payerAccount.publicKey.toBytes(), FriendStatus.REFUSED]),
)

const friendRequestDeniedFilter: GetProgramAccountsFilter = {
Expand All @@ -761,7 +761,7 @@ export default class FriendsProgram extends EventEmitter {
// This filter checks the sender public key (our) and the status
// [32 bytes (sender public key)][1 byte (status)][32 bytes (recipient public key)]
const friendRequestRemovedBytes = base58(
Buffer.from([...userAccount.publicKey.toBytes(), FriendStatus.REMOVED]),
Buffer.from([...payerAccount.publicKey.toBytes(), FriendStatus.REMOVED]),
)

const friendRequestRemovedFilter: GetProgramAccountsFilter = {
Expand All @@ -776,7 +776,7 @@ export default class FriendsProgram extends EventEmitter {
)

const friendRequestRemovedMirroredBytes = base58(
Buffer.from([FriendStatus.REMOVED, ...userAccount.publicKey.toBytes()]),
Buffer.from([FriendStatus.REMOVED, ...payerAccount.publicKey.toBytes()]),
)

const friendRequestRemovedMirroredFilter: GetProgramAccountsFilter = {
Expand Down
Loading