Skip to content

Commit

Permalink
Migrate @rss3/js-sdk to @rss3/sdk (#14)
Browse files Browse the repository at this point in the history
* chore: migrate `@rss3/js-sdk` to `@rss3/api-core`

* chore: update .gitignore

* chore: migrate `@rss3/api-core` to `@rss3/sdk`
  • Loading branch information
runjuu authored Oct 2, 2024
1 parent 3e511ed commit 9110ba3
Show file tree
Hide file tree
Showing 16 changed files with 162 additions and 90 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,5 @@ node_modules/
.yarn/install-state.gz
.pnp.*

# IntelliJ
.idea
2 changes: 1 addition & 1 deletion packages/site/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
"@radix-ui/react-label": "^2.0.2",
"@radix-ui/react-slot": "^1.0.2",
"@radix-ui/react-toast": "^1.1.5",
"@rss3/js-sdk": "^0.6.53",
"@rss3/sdk": "^0.0.11",
"ahooks": "^3.7.8",
"autoprefixer": "^10.4.16",
"class-variance-authority": "^0.7.0",
Expand Down
3 changes: 2 additions & 1 deletion packages/site/src/pages/monitor/create.tsx
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
import { useState, useEffect, useMemo, useContext } from 'react';
import * as z from 'zod';
import { type Profile, formatAddressAndNS } from '@rss3/js-sdk';
import { formatAddressAndNS } from '@rss3/sdk';
import { useForm } from 'react-hook-form';
import { zodResolver } from '@hookform/resolvers/zod';
import { navigate } from 'gatsby';
import { Profile } from '@/types';
import { Badge } from '@/components/ui/badge';
import { Button } from '@/components/ui/button';

Expand Down
2 changes: 1 addition & 1 deletion packages/site/src/types/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1 @@
export { type GetSnapsResponse, type Snap } from './snap';
export * from './snap';
21 changes: 21 additions & 0 deletions packages/site/src/types/snap.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,24 @@ export type Snap = {
version: string;
initialPermissions: Record<string, unknown>;
};

// TODO: share this type with the `snap` package
export type Profile = {
action?: 'create' | 'renew' | 'unwrap' | 'update' | 'wrap';
address?: string;
bannerURI?: string[];
bio?: string;
expireAt?: string | null;
expiry?: string | null;
handle?: string;
image_uri?: string;
key?: string;
name?: string;
network: string;
platform: string;
profileURI?: string[];
profile_id?: string;
socialURI?: string[];
url?: string;
value?: string;
};
5 changes: 2 additions & 3 deletions packages/site/src/utils/snap.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import { MetaMaskInpageProvider } from '@metamask/providers';
import type { Profile } from '@rss3/js-sdk';
import type { MetaMaskInpageProvider } from '@metamask/providers';
import { defaultSnapOrigin } from '../config';
import { GetSnapsResponse, Snap } from '../types';
import type { GetSnapsResponse, Snap, Profile } from '../types';

/**
* Get the installed snaps in MetaMask.
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
"@metamask/snaps-types": "^3.1.0",
"@metamask/snaps-ui": "^3.1.0",
"@metamask/utils": "^8.1.0",
"@rss3/js-sdk": "^0.6.53",
"@rss3/sdk": "^0.0.11",
"@urql/core": "^4.1.4",
"axios": "^1.6.1",
"buffer": "^6.0.3",
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/snap.manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
"url": "https://github.com/NaturalSelectionLabs/RSS3-Social-Notifier-Snap.git"
},
"source": {
"shasum": "JzJh23BBGdsijFOEAQw8fREDnXCr9VJDCVESa8+qMX0=",
"shasum": "huDZtni5U6N5JLPuNdLI+Hd2EB9k27vZHofTZBymoLw=",
"location": {
"npm": {
"filePath": "dist/bundle.js",
Expand Down
108 changes: 51 additions & 57 deletions packages/snap/src/fetch.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,14 @@
import moment from 'moment';
import {
type Activity,
getActivities,
formatAddressAndNS,
format as sdkFormat,
formatContent,
themePlain,
type Theme,
} from '@rss3/js-sdk';
} from '@rss3/sdk';

import { themePlain } from '@rss3/js-sdk/lib/readable/activity/theme';
import { CronActivity, SocialMonitor } from './state';

export const getSocialActivitiesUrl = (address: string) =>
Expand All @@ -20,9 +21,12 @@ export const getSocialActivitiesUrl = (address: string) =>
* @returns The social activities.
*/
export async function getSocialActivities(address: string) {
const resp = await fetch(getSocialActivitiesUrl(address));
const { data } = (await resp.json()) as { data: Activity[] };
const activities = data.map((item: Activity) => {
const { data } = await getActivities({
account: address,
tag: ['social'],
direction: 'out',
});
const activities = data.map((item): CronActivity => {
// formatContent only for social and governance tag.
const content = formatContent(item);

Expand Down Expand Up @@ -88,67 +92,57 @@ export function format(activity: Activity) {
* @returns The social count array.
*/
export async function getMultiple(addresses: string[]) {
const activities: CronActivity[] = [];
let hasNextPage = true;
let cursor: string | undefined;

const filtedAddresses = addresses.filter(
const filteredAddresses = addresses.filter(
(addr) => addr !== undefined,
) as string[];

if (filtedAddresses.length === 0) {
if (filteredAddresses.length === 0) {
return [];
}

const executeAddresses = filtedAddresses;

// 1 hour ago
const timestamp = moment().subtract(1, 'hour').unix();
while (hasNextPage) {
const params = {
action_limit: 10,
limit: 500,
account: executeAddresses,
tag: ['social'],
type: ['post', 'comment'],
direction: 'out',
since_timestamp: timestamp,
cursor,
};
const resp = await fetch(
'https://testnet.rss3.io/data/accounts/activities',
{
method: 'POST',
headers: {
accept: 'application/json',
'content-type': 'application/json',
},
body: JSON.stringify(params),
},
);
const { data, meta } = (await resp.json()) as {
data: Activity[];
meta: null | { cursor: string };
};

data.map((item) => {
const content = formatContent(item);
const { id, owner } = item;
const text = format(item).join('');
const image = content?.media?.[0]?.address;
return content
? activities.push({ id, text, image, owner })
: activities.push({ id, text, owner });
});

if (meta === null) {
hasNextPage = false;
} else {
cursor = meta.cursor;
}
}

return executeAddresses.map((addr) => {
const activities = (
await Promise.all(
filteredAddresses.map(async (account) => {
let cursor: string | undefined;
const result: CronActivity[] = [];

do {
const res = await getActivities({
account,
actionLimit: 10,
limit: 500,
tag: ['social'],
type: ['post', 'comment'],
direction: 'out',
sinceTimestamp: timestamp,
cursor,
});

res.data.forEach((item) => {
const content = formatContent(item);
const { id, owner } = item;
const text = format(item).join('');
const image = content?.media?.[0]?.address;

if (content) {
result.push({ id, text, image, owner });
} else {
result.push({ id, text, owner });
}
});

cursor = res.cursor;
} while (cursor);

return result;
}),
)
).flat();

return filteredAddresses.map((addr) => {
const groupBy = activities.filter(
(activity) =>
activity.owner?.toLocaleLowerCase() === addr?.toLocaleLowerCase(),
Expand Down
3 changes: 1 addition & 2 deletions packages/snap/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import {
import { divider, heading, panel, text, image } from '@metamask/snaps-ui';
import { assert } from '@metamask/utils';

import { Profile } from '@rss3/js-sdk';
import {
CronActivity,
SocialActivity,
Expand All @@ -18,7 +17,7 @@ import {
setState,
} from './state';
import { diff, getSocialActivities } from './fetch';
import { getProfilesBySearch } from './social-graph';
import { getProfilesBySearch, type Profile } from './social-graph';
import {
coverIpfsToUrl,
imageBufferToBase64,
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/social-graph/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,4 +31,4 @@ export { Platform };
export { LensHandler, LensFormat };
export { FarcasterHandler, FarcasterFormat };
export { CrossbellHandler, CrossbellFormat, type TCSBProfile };
export { getProfilesBySearch } from './profiles';
export { getProfilesBySearch, type Profile } from './profiles';
24 changes: 23 additions & 1 deletion packages/snap/src/social-graph/profiles.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,29 @@
import { isSupportedNS, type Profile } from '@rss3/js-sdk';
import { isSupportedNS } from '@rss3/sdk';

import { isValidWalletAddress } from './utils';
import { Platform } from '.';

// TODO: share this type with the `site` package
export type Profile = {
action?: 'create' | 'renew' | 'unwrap' | 'update' | 'wrap';
address?: string;
bannerURI?: string[];
bio?: string;
expireAt?: string | null;
expiry?: string | null;
handle?: string;
image_uri?: string;
key?: string;
name?: string;
network: string;
platform: string;
profileURI?: string[];
profile_id?: string;
socialURI?: string[];
url?: string;
value?: string;
};

export const profileApi = (search: string) =>
`https://testnet.rss3.io/data/accounts/${search}/profiles`;

Expand Down
3 changes: 2 additions & 1 deletion packages/snap/src/state.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { ManageStateOperation } from '@metamask/snaps-types';
import { Profile } from '@rss3/js-sdk';

import { Profile } from './social-graph';
import { TSocialGraphResult } from '.';

export type CronActivity = {
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/tests/index.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { installSnap } from '@metamask/snaps-jest';
import { expect } from '@jest/globals';
import { type Activity } from '@rss3/js-sdk';
import { type Activity } from '@rss3/sdk';
import { format, getSocialActivitiesUrl } from '../fetch';

const DEFAULT_WALLET_ADDRESS = '0xc6d5a3c98ec9073b54fa0969957bd582e8d874bf';
Expand Down
2 changes: 1 addition & 1 deletion packages/snap/src/utils/activitiy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,14 +6,14 @@ import {
panel,
type Component,
} from '@metamask/snaps-ui';
import { Profile } from '@rss3/js-sdk';
import moment from 'moment';
import { CronActivity, getState, setState } from '../state';
import {
CrossbellHandler,
FarcasterHandler,
LensHandler,
Platform,
Profile,
} from '../social-graph';
import { TSocialGraphResult } from '..';
import { downloadAndCovertImage, wrapBase64ToSvg } from './image';
Expand Down
Loading

0 comments on commit 9110ba3

Please sign in to comment.