Skip to content

Commit 778d7c2

Browse files
committed
WIP: parachain SDK
1 parent ab5f641 commit 778d7c2

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

52 files changed

+6138
-6243
lines changed

.eslintrc.js

+99-4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,101 @@
1-
const base = require('@subsocial/config/eslintrc');
2-
3-
// add override for any (a metric ton of them, initial conversion)
41
module.exports = {
5-
...base
2+
parser: '@typescript-eslint/parser', // Specifies the ESLint parser
3+
parserOptions: {
4+
ecmaVersion: 2018, // Allows for the parsing of modern ECMAScript features
5+
sourceType: 'module', // Allows for the use of imports
6+
ecmaFeatures: {
7+
jsx: true, // Allows for the parsing of JSX
8+
},
9+
},
10+
settings: {
11+
react: {
12+
// Tells eslint-plugin-react to automatically detect the version of
13+
// React to use
14+
version: 'detect',
15+
},
16+
},
17+
extends: [
18+
// Uses the recommended rules from @eslint-plugin-react
19+
'plugin:react/recommended',
20+
21+
// Uses the recommended rules from the @typescript-eslint/eslint-plugin
22+
'plugin:@typescript-eslint/recommended',
23+
],
24+
plugins: [ 'jsx-a11y', 'prefer-object-spread', 'prettier', 'react-hooks' ],
25+
rules: {
26+
// In an ideal world, we'd never have to use @ts-ignore, but that's not
27+
// possible right now.
28+
'@typescript-eslint/ban-ts-ignore': 'off',
29+
'@typescript-eslint/ban-ts-comment': 'off',
30+
31+
// Again, in theory this is a good rule, but it can cause a bit of
32+
// unhelpful noise.
33+
'@typescript-eslint/explicit-function-return-type': 'off',
34+
35+
// Another theoretically good rule, but sometimes we know better than
36+
// the linter.
37+
'@typescript-eslint/no-non-null-assertion': 'off',
38+
39+
// Accessibility rules
40+
'jsx-a11y/accessible-emoji': 'error',
41+
'jsx-a11y/alt-text': 'error',
42+
'jsx-a11y/anchor-has-content': 'error',
43+
'jsx-a11y/aria-activedescendant-has-tabindex': 'error',
44+
'jsx-a11y/aria-props': 'error',
45+
'jsx-a11y/aria-proptypes': 'error',
46+
'jsx-a11y/aria-role': 'error',
47+
'jsx-a11y/aria-unsupported-elements': 'error',
48+
'jsx-a11y/heading-has-content': 'error',
49+
'jsx-a11y/html-has-lang': 'error',
50+
'jsx-a11y/iframe-has-title': 'error',
51+
'jsx-a11y/interactive-supports-focus': 'error',
52+
'jsx-a11y/media-has-caption': 'error',
53+
'jsx-a11y/mouse-events-have-key-events': 'error',
54+
'jsx-a11y/no-access-key': 'error',
55+
'jsx-a11y/no-distracting-elements': 'error',
56+
'jsx-a11y/no-interactive-element-to-noninteractive-role': 'error',
57+
'jsx-a11y/no-noninteractive-element-interactions': 'error',
58+
'jsx-a11y/no-noninteractive-element-to-interactive-role': 'error',
59+
'jsx-a11y/no-redundant-roles': 'error',
60+
'jsx-a11y/role-has-required-aria-props': 'error',
61+
'jsx-a11y/role-supports-aria-props': 'error',
62+
'jsx-a11y/scope': 'error',
63+
'jsx-a11y/tabindex-no-positive': 'error',
64+
'jsx-a11y/label-has-associated-control': 'error',
65+
66+
'prefer-object-spread/prefer-object-spread': 'error',
67+
68+
// Use template strings instead of string concatenation
69+
'prefer-template': 'error',
70+
71+
'react-hooks/rules-of-hooks': 'error',
72+
'react-hooks/exhaustive-deps': 'warn',
73+
// Place to specify ESLint rules. Can be used to overwrite rules
74+
// specified from the extended configs
75+
76+
// We're using TypeScript, so prop-types aren't so interesting
77+
'react/prop-types': 'off',
78+
'no-unused-vars': 'off',
79+
'@typescript-eslint/no-unused-vars': [ 'off' ],
80+
'semi': 'off',
81+
'comma-spacing': 'warn',
82+
'array-bracket-spacing': [ 'warn', 'always' ],
83+
'react/display-name': 'off',
84+
'padded-blocks': 'off',
85+
'prefer-promise-reject-errors': 'warn',
86+
'@typescript-eslint/explicit-member-accessibility': 'off',
87+
'@typescript-eslint/indent': [ 'warn', 2 ],
88+
'@typescript-eslint/no-use-before-define': 'off',
89+
'@typescript-eslint/member-delimiter-style': 'off',
90+
'@typescript-eslint/no-explicit-any': 'off',
91+
'@typescript-eslint/explicit-function-return-type': 'off',
92+
'@typescript-eslint/prefer-interface': 'off',
93+
'@typescript-eslint/no-object-literal-type-assertion': 'off',
94+
'@typescript-eslint/camelcase': 'off',
95+
'react/prop-types': 'off',
96+
'@typescript-eslint/array-type': 'off',
97+
98+
'@typescript-eslint/no-var-requires': 'off',
99+
'@typescript-eslint/explicit-module-boundary-types': 'off',
100+
},
6101
};

.prettierrc.js

-7
This file was deleted.

lerna.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
"packages/definitions",
88
"packages/i18n"
99
],
10-
"version": "0.6.11",
10+
"version": "0.6.12-parachain.1",
1111
"npmClient": "yarn",
1212
"useWorkspaces": true
1313
}

packages/api/package.json

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "@subsocial/api",
3-
"version": "0.6.11",
3+
"version": "0.6.12-parachain.1",
44
"description": "JavaScript API for Subsocial blockchain.",
55
"author": "DappForce contributors",
66
"license": "GPL-3.0-only",
@@ -20,8 +20,8 @@
2020
"homepage": "https://github.com/dappforce/dappforce-subsocial-js",
2121
"dependencies": {
2222
"@polkadot/api": "latest",
23-
"@subsocial/types": "0.6.11",
24-
"@subsocial/utils": "0.6.11",
23+
"@subsocial/types": "0.6.12-parachain.1",
24+
"@subsocial/utils": "0.6.12-parachain.1",
2525
"axios": "^0.24.0"
2626
},
2727
"scripts": {

packages/api/src/flat-subsocial/flatteners.ts

+6-77
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Option } from '@polkadot/types/codec'
22
import { Post, Space, SpacePermissionSet, SpacePermissions } from '@subsocial/types/substrate/interfaces'
33
import { notEmptyObj } from '@subsocial/utils'
44
import { FlatSpacePermissionKey, FlatSpacePermissionMap, FlatSpacePermissions, FlatSpacePermissionsKey } from '@subsocial/types/substrate/rpc'
5-
import { CanBeUpdated, CanHaveContent, CanHaveHandle, CanHaveParentId, CanHaveSpaceId, CommentExtension, CommentStruct, CommonContent, EntityData, EntityId, FlatPostExtension, FlatSuperCommon, HasId, HasOwner, PostStruct, ProfileStruct, PublicProfileStruct, SharedPostExtension, SharedPostStruct, SpaceOrPostStruct, SpaceStruct, SuperCommonStruct, FlatSpaceOrPost, SocialAccountWithId } from '@subsocial/types/dto'
5+
import { CanHaveContent, CanHaveParentId, CanHaveSpaceId, CommentExtension, CommentStruct, CommonContent, EntityData, EntityId, FlatPostExtension, FlatSuperCommon, HasId, HasOwner, PostStruct, SharedPostExtension, SharedPostStruct, SpaceOrPostStruct, SpaceStruct, SuperCommonStruct, FlatSpaceOrPost } from '@subsocial/types/dto'
66

77
type EntityDataWithField<S extends {}> = EntityData<HasId & S, CommonContent> | (HasId & S)
88

@@ -29,22 +29,6 @@ export const getUniqueContentIds = (entities: EntityDataWithField<CanHaveContent
2929
export const getUniqueSpaceIds = (entities: EntityDataWithField<CanHaveSpaceId>[]) =>
3030
getUniqueIds(entities, 'spaceId')
3131

32-
function getUpdatedFields ({ updated }: SuperCommonStruct): CanBeUpdated {
33-
const maybeUpdated = updated.unwrapOr(undefined)
34-
let res: CanBeUpdated = {
35-
isUpdated: updated.isSome,
36-
}
37-
if (maybeUpdated) {
38-
res = {
39-
...res,
40-
updatedByAccount: maybeUpdated.account.toHuman(),
41-
updatedAtBlock: maybeUpdated.block.toNumber(),
42-
updatedAtTime: maybeUpdated.time.toNumber()
43-
}
44-
}
45-
return res
46-
}
47-
4832
function getContentFields ({ content }: SuperCommonStruct): CanHaveContent {
4933
let res: CanHaveContent = {}
5034
if (content.isIpfs) {
@@ -64,7 +48,7 @@ export function flattenCommonFields (struct: SuperCommonStruct): FlatSuperCommon
6448
createdAtBlock: created.block.toNumber(),
6549
createdAtTime: created.time.toNumber(),
6650

67-
...getUpdatedFields(struct),
51+
isUpdated: struct.updated.toHuman(),
6852
...getContentFields(struct),
6953
}
7054
}
@@ -97,10 +81,7 @@ export const flattenPermisions = (permissions?: SpacePermissions) => {
9781
return flatPermissions
9882
}
9983

100-
export function flattenSpaceStruct (struct: Space): SpaceStruct {
101-
const postsCount = struct.postsCount.toNumber()
102-
const hiddenPostsCount = struct.hiddenPostsCount.toNumber()
103-
const visiblePostsCount = postsCount - hiddenPostsCount
84+
export function flattenSpaceStruct (struct: Space,): SpaceStruct {
10485
const flatPermissions = flattenPermisions(struct.permissions.unwrapOr(undefined))
10586

10687
let parentField: CanHaveParentId = {}
@@ -110,27 +91,13 @@ export function flattenSpaceStruct (struct: Space): SpaceStruct {
11091
}
11192
}
11293

113-
let handleField: CanHaveHandle = {}
114-
if (struct.handle.isSome) {
115-
handleField = {
116-
handle: struct.handle.toHuman()?.toString()
117-
}
118-
}
119-
12094

12195
return {
12296
...flattenSpaceOrPostStruct(struct),
12397
...parentField,
124-
...handleField,
125-
12698
...flatPermissions,
12799
canFollowerCreatePosts: !!flatPermissions.followerPermissions?.CreatePosts, //TODO: check CreatePosts permissions in follower set
128100
canEveryoneCreatePosts: !!flatPermissions.everyonePermissions?.CreatePosts, //TODO: check CreatePosts permissions in everyone set
129-
postsCount,
130-
hiddenPostsCount,
131-
visiblePostsCount,
132-
followersCount: struct.followersCount.toNumber(),
133-
score: struct.score.toNumber()
134101
}
135102
}
136103

@@ -139,12 +106,13 @@ export function flattenSpaceStructs (structs: Space[]): SpaceStruct[] {
139106
}
140107

141108
function flattenPostExtension (struct: Post): FlatPostExtension {
142-
const { isSharedPost, isComment } = struct.extension
109+
const { isSharedPost, isComment, isRegularPost } = struct.extension
143110
let normExt: FlatPostExtension = {}
144111

145112
if (isSharedPost) {
113+
const originalPostId = struct.extension.asSharedPost
146114
const sharedPost: SharedPostExtension = {
147-
sharedPostId: struct.extension.asSharedPost.toString()
115+
originalPostId: originalPostId.toString()
148116
}
149117
normExt = sharedPost
150118
} else if (isComment) {
@@ -162,9 +130,6 @@ function flattenPostExtension (struct: Post): FlatPostExtension {
162130
}
163131

164132
export function flattenPostStruct (struct: Post): PostStruct {
165-
const repliesCount = struct.repliesCount.toNumber()
166-
const hiddenRepliesCount = struct.hiddenRepliesCount.toNumber()
167-
const visibleRepliesCount = repliesCount - hiddenRepliesCount
168133
const { isRegularPost, isSharedPost, isComment } = struct.extension
169134
const extensionFields = flattenPostExtension(struct)
170135

@@ -180,14 +145,8 @@ export function flattenPostStruct (struct: Post): PostStruct {
180145
...spaceField,
181146
...extensionFields,
182147

183-
repliesCount,
184-
hiddenRepliesCount,
185-
visibleRepliesCount,
186-
187-
sharesCount: struct.sharesCount.toNumber(),
188148
upvotesCount: struct.upvotesCount.toNumber(),
189149
downvotesCount: struct.downvotesCount.toNumber(),
190-
score: struct.score.toNumber(),
191150

192151
isRegularPost,
193152
isSharedPost,
@@ -210,33 +169,3 @@ export function asCommentStruct (post: PostStruct): CommentStruct {
210169

211170
return post as CommentStruct
212171
}
213-
214-
export function asPublicProfileStruct (profile: ProfileStruct): PublicProfileStruct {
215-
if (!profile.hasProfile) throw new Error('Account has no profile')
216-
217-
return profile as PublicProfileStruct
218-
}
219-
220-
export function flattenProfileStruct (struct: SocialAccountWithId): ProfileStruct {
221-
const profile = struct.profile?.unwrapOr(undefined)
222-
const hasProfile = struct.profile?.isSome
223-
const maybeProfile: Partial<FlatSuperCommon> = profile
224-
? flattenCommonFields(profile)
225-
: {}
226-
227-
return {
228-
id: struct.id.toString(),
229-
230-
followersCount: struct.followersCount.toNumber(),
231-
followingAccountsCount: struct.followingAccountsCount.toNumber(),
232-
followingSpacesCount: struct.followingSpacesCount.toNumber(),
233-
reputation: struct.reputation.toNumber(),
234-
235-
hasProfile,
236-
...maybeProfile
237-
}
238-
}
239-
240-
export function flattenProfileStructs (accounts: SocialAccountWithId[]): ProfileStruct[] {
241-
return accounts.map(flattenProfileStruct)
242-
}

packages/api/src/flat-subsocial/index.ts

+2-31
Original file line numberDiff line numberDiff line change
@@ -4,34 +4,26 @@ import {
44
convertToNewPostWithAllDetailsArray,
55
convertToNewSpaceData,
66
convertToNewSpaceDataArray,
7-
convertToNewProfileData,
87
convertToNewPostData,
98
convertToNewPostWithSomeDetails,
109
convertToNewPostWithSomeDetailsArray,
11-
convertToNewProfileDataArray,
1210
} from './utils'
1311
import { FindPostQuery, FindPostsQuery, FindPostsWithDetailsQuery, FindSpaceQuery } from '../filters'
14-
import { AnyAccountId } from '@subsocial/types'
1512
import { SubsocialApi } from '../subsocial'
16-
import { ProfileData, SpaceData, PostData, PostWithSomeDetails, PostWithAllDetails, AnyId, ProfileStruct, SpaceStruct, PostStruct } from '@subsocial/types/dto'
13+
import { SpaceData, PostData, PostWithSomeDetails, PostWithAllDetails, AnyId, SpaceStruct, PostStruct } from '@subsocial/types/dto'
1714
import { getFirstOrUndefined, idsToBns, idToBn } from '@subsocial/utils'
18-
import { flattenSpaceStructs, flattenPostStructs, flattenProfileStructs } from './flatteners'
15+
import { flattenSpaceStructs, flattenPostStructs } from './flatteners'
1916

2017
export interface IFlatSubsocialApi {
21-
findProfile: (id: AnyAccountId) => Promise<ProfileData | undefined>
22-
findProfiles: (ids: AnyAccountId[]) => Promise<ProfileData[]>
23-
2418
findSpace: (query: FindSpaceQuery) => Promise<SpaceData | undefined>
2519
findPublicSpaces: (ids: AnyId[]) => Promise<SpaceData[]>
2620
findUnlistedSpaces: (ids: AnyId[]) => Promise<SpaceData[]>
2721

2822
findSpaceStructs: (ids: AnyId[]) => Promise<SpaceStruct[]>
2923
findPostStructs: (ids: AnyId[]) => Promise<PostStruct[]>
30-
findProfileStructs: (ids: AnyAccountId[]) => Promise<ProfileStruct[]>
3124

3225
findSpaceStruct: (id: AnyId) => Promise<SpaceStruct | undefined>
3326
findPostStruct: (id: AnyId) => Promise<PostStruct | undefined>
34-
findProfileStruct: (id: AnyAccountId) => Promise<ProfileStruct | undefined>
3527

3628
findPost: (query: FindPostQuery) => Promise<PostData | undefined>
3729
findPublicPosts: (ids: AnyId[]) => Promise<PostData[]>
@@ -64,11 +56,6 @@ export class FlatSubsocialApi implements IFlatSubsocialApi {
6456
return flattenPostStructs(structs)
6557
}
6658

67-
public async findProfileStructs (ids: AnyAccountId[]): Promise<ProfileStruct[]> {
68-
const structs = await this.subsocial.substrate.findSocialAccounts(ids)
69-
return flattenProfileStructs(structs)
70-
}
71-
7259
public async findSpaceStruct (id: AnyId): Promise<SpaceStruct | undefined> {
7360
return getFirstOrUndefined(await this.findSpaceStructs([ id ]))
7461
}
@@ -77,22 +64,6 @@ export class FlatSubsocialApi implements IFlatSubsocialApi {
7764
return getFirstOrUndefined(await this.findPostStructs([ id ]))
7865
}
7966

80-
public async findProfileStruct (id: AnyAccountId): Promise<ProfileStruct | undefined>{
81-
return getFirstOrUndefined(await this.findProfileStructs([ id ]))
82-
}
83-
84-
85-
public async findProfile (id: AnyAccountId) {
86-
const old = await this.subsocial.findProfile(id)
87-
return !old ? old : convertToNewProfileData(old)
88-
}
89-
90-
public async findProfiles (ids: AnyAccountId[]) {
91-
return convertToNewProfileDataArray(
92-
await this.subsocial.findProfiles(ids)
93-
)
94-
}
95-
9667
public async findSpace (query: FindSpaceQuery) {
9768
const old = await this.subsocial.findSpace(query)
9869
return !old ? old: convertToNewSpaceData(old)

0 commit comments

Comments
 (0)