-
Notifications
You must be signed in to change notification settings - Fork 488
docs: add socialDB primitives #3060
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
Open
matiasbenary
wants to merge
2
commits into
master
Choose a base branch
from
docs/add-socialDB
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,199 @@ | ||
| --- | ||
| title: Social | ||
| icon: users | ||
| description: "Learn how to build social features on NEAR - profiles, posts, follows, likes and notifications - using SocialDB (social.near) and the near-social-js SDK." | ||
| --- | ||
|
|
||
| [SocialDB](https://github.com/NearSocial/social-db) is a smart contract deployed at `social.near` that stores social data on the NEAR blockchain: profiles, posts, follows, likes, comments and more. It powers [near.social](https://near.social) and is widely used across the ecosystem. | ||
|
|
||
| <Note> | ||
| While the BOS framework (components/widgets) is deprecated, the social graph stored in SocialDB is still live and maintained. This page covers how to read and write that data directly, independently of BOS. | ||
| </Note> | ||
|
|
||
| The easiest way to interact with `social.near` is through the [`near-social-js`](https://nearbuilders.github.io/near-social-js/) SDK, which wraps the contract with helper methods for common social features. | ||
|
|
||
| --- | ||
|
|
||
| ## Add the SDK to a project | ||
|
|
||
| ```bash npm2yarn | ||
| npm install near-social-js | ||
| ``` | ||
|
|
||
| The SDK exposes two classes: | ||
|
|
||
| - `Social` — high-level helpers for profiles, posts, follows, likes and notifications. | ||
| - `Graph` — low-level access to the raw `social.near` contract (custom keys, storage, permissions). `Social` extends `Graph`. | ||
|
|
||
| ### Reading (no wallet) | ||
|
|
||
| Reads are free and require no signer: | ||
|
|
||
| ```typescript | ||
| import { Social } from 'near-social-js'; | ||
|
|
||
| const social = new Social(); // defaults to social.near on mainnet | ||
| ``` | ||
|
|
||
| ### Writing (requires a signer) | ||
|
|
||
| Writes return a transaction builder that you send with a signer. Pass a configured [`near-kit`](https://www.npmjs.com/package/near-kit) instance: | ||
|
|
||
| ```typescript | ||
| import { Social } from 'near-social-js'; | ||
| import { Near } from 'near-kit'; | ||
|
|
||
| const near = new Near({ network: 'mainnet' }); | ||
| const social = new Social({ near }); | ||
| ``` | ||
|
|
||
| To target testnet, point the SDK at the testnet contract: | ||
|
Check warning on line 50 in primitives/social.mdx
|
||
|
|
||
| ```typescript | ||
| const social = new Social({ | ||
| network: 'testnet', | ||
| contractId: 'v1.social08.testnet', | ||
| }); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Profile | ||
|
|
||
| A profile is stored under the `<account>/profile/**` key and holds the user's name, description, image and links. | ||
|
|
||
| ### Get a profile | ||
|
|
||
| ```typescript | ||
| const profile = await social.getProfile('alice.near'); | ||
|
|
||
| console.log(profile?.name); // "Alice" | ||
| console.log(profile?.description); // "Hello world!" | ||
| ``` | ||
|
|
||
| ### Set a profile | ||
|
|
||
| Only the account itself (the signer) can update its own profile: | ||
|
|
||
| ```typescript | ||
| const tx = await social.setProfile('alice.near', { | ||
| name: 'Alice', | ||
| description: 'Building on NEAR', | ||
| }); | ||
|
|
||
| await tx.send(); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Posts | ||
|
|
||
| Posts live under the `<account>/post/main` key and are addressed by the block height at which they were created. | ||
|
|
||
| ### Create a post | ||
|
|
||
| `createPost` automatically extracts `@mentions` and `#hashtags` and indexes them so other users get notified: | ||
|
|
||
| ```typescript | ||
| const tx = await social.createPost('alice.near', { | ||
| text: 'My first post on NEAR! cc @bob.near #near', | ||
| }); | ||
|
|
||
| await tx.send(); | ||
| ``` | ||
|
|
||
| ### Get posts from a user | ||
|
|
||
| ```typescript | ||
| // Recent posts by a single account | ||
| const feed = await social.getAccountFeed('alice.near', { limit: 20 }); | ||
|
|
||
| // A specific post by block height | ||
| const post = await social.getPost('alice.near', 123456789); | ||
| ``` | ||
|
|
||
| Other feed helpers are available for broader queries: | ||
|
|
||
| ```typescript | ||
| await social.getActivityFeed({ limit: 20 }); // all recent posts | ||
| await social.getHashtagFeed('near', { limit: 20 }); // posts tagged #near | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Interactions | ||
|
|
||
| ```typescript | ||
| // Follow / unfollow (sends a notification to the target) | ||
| await (await social.follow('alice.near', 'bob.near')).send(); | ||
| await (await social.unfollow('alice.near', 'bob.near')).send(); | ||
|
|
||
| const following = await social.getFollowing('alice.near'); | ||
| const followers = await social.getFollowers('alice.near'); | ||
|
|
||
| // Like / repost a post or comment | ||
| const item = { type: 'social', path: 'bob.near/post/main', blockHeight: 123456789 }; | ||
| await (await social.like('alice.near', item)).send(); | ||
| await (await social.repost('alice.near', item)).send(); | ||
|
|
||
| const likes = await social.getLikes(item); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Indexer | ||
|
|
||
| SocialDB exposes an **index** API: writes can be tagged with an `action` and `key`, and anyone can later query all entries for that pair, ordered by block height. This is how feeds and notifications are built — there is no off-chain indexer to run. | ||
|
|
||
| ### Index an action | ||
|
|
||
| High-level methods (`createPost`, `follow`, `like`, `repost`) write index entries for you. To write a custom one, send a notification with `notify`: | ||
|
|
||
| ```typescript | ||
| const tx = await social.notify( | ||
| 'alice.near', // signer | ||
| 'bob.near', // account to notify | ||
| { type: 'social', path: 'alice.near/post/main', blockHeight: 123456789 }, | ||
| 'like', // notification type | ||
| ); | ||
|
|
||
| await tx.send(); | ||
| ``` | ||
|
|
||
| ### Retrieve indexed actions | ||
|
|
||
| Query the raw index by `action` + `key` with the low-level `index` method: | ||
|
|
||
| ```typescript | ||
| const entries = await social.index({ | ||
| action: 'like', | ||
| key: 'bob.near/post/main', | ||
| order: 'desc', | ||
| limit: 20, | ||
| }); | ||
| // → [{ accountId, blockHeight, value }, ...] | ||
| ``` | ||
|
|
||
| ### Get notifications | ||
|
|
||
| `getNotifications` reads the `notify` index for an account and returns mentions, likes, comments, follows and reposts: | ||
|
|
||
| ```typescript | ||
| const notifications = await social.getNotifications('alice.near', { limit: 20 }); | ||
|
|
||
| for (const n of notifications) { | ||
| console.log(n.value.type, 'from', n.accountId); | ||
| } | ||
|
|
||
| // Only the posts where the account was mentioned | ||
| const mentions = await social.getMentionedFeed('alice.near', { limit: 20 }); | ||
| ``` | ||
|
|
||
| --- | ||
|
|
||
| ## Additional resources | ||
|
|
||
| - [near-social-js SDK docs](https://nearbuilders.github.io/near-social-js/) — full API reference | ||
| - [near-social-js on GitHub](https://github.com/NEARBuilders/near-social-js) — SDK source code | ||
| - [social-db contract](https://github.com/NearSocial/social-db) — the `social.near` smart contract | ||
| - [near.social](https://near.social) — the live social network | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
"SocialDB is a smart contract that stores ..."
let's move deployment addresses (both testnet and mainnet) into a dedicated markdown table