Skip to content

Commit

Permalink
fix: gnods are good
Browse files Browse the repository at this point in the history
  • Loading branch information
iuricmp committed Oct 17, 2024
1 parent 7b12137 commit b98aff8
Show file tree
Hide file tree
Showing 7 changed files with 55 additions and 65 deletions.
23 changes: 13 additions & 10 deletions mobile/app/[account]/index.tsx
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import { useCallback, useEffect, useState } from "react";
import { useDispatch } from "react-redux";
import { router, useNavigation } from "expo-router";
import { router, useNavigation, usePathname } from "expo-router";
import { AccountView } from "@gno/components/view";
import { useSearch } from "@gno/hooks/use-search";
import { Following, Post, User } from "@gno/types";
import { broadcastTxCommit, clearLinking, selectQueryParamsTxJsonSigned, setPostToReply, useAppSelector, selectAccount } from "@gno/redux";
import { broadcastTxCommit, clearLinking, selectQueryParamsTxJsonSigned, setPostToReply, useAppSelector, selectAccount, gnodTxAndRedirectToSign } from "@gno/redux";
import { followTxAndRedirectToSign, selectProfileAccountName, setFollows, unfollowTxAndRedirectToSign } from "redux/features/profileSlice";
import { useFeed } from "@gno/hooks/use-feed";
import { useUserCache } from "@gno/hooks/use-user-cache";
Expand All @@ -31,6 +31,7 @@ export default function Page() {
const currentUser = useAppSelector(selectAccount);
const txJsonSigned = useAppSelector(selectQueryParamsTxJsonSigned);

const pathName = usePathname();

useEffect(() => {

Expand Down Expand Up @@ -140,14 +141,16 @@ export default function Page() {

if (!currentUser) throw new Error("No active account");

try {
await feed.onGnod(post, currentUser.address);
await fetchData();
} catch (error) {
console.error("Error while adding reaction: " + error);
} finally {
setLoading(undefined);
}
dispatch(gnodTxAndRedirectToSign({ post, callerAddressBech32: currentUser.bech32, pathName })).unwrap();

// try {
// await feed.onGnod(post, currentUser.address);
// await fetchData();
// } catch (error) {
// console.error("Error while adding reaction: " + error);
// } finally {
// setLoading(undefined);
// }
};

const onPressPost = async (item: Post) => {
Expand Down
16 changes: 4 additions & 12 deletions mobile/app/home/feed.tsx
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
import { ActivityIndicator, FlatList, Platform, StyleSheet, View, Alert as RNAlert, SafeAreaView } from "react-native";
import React, { useEffect, useRef, useState } from "react";
import { useNavigation, useRouter } from "expo-router";
import { useNavigation, usePathname, useRouter } from "expo-router";
import { useFeed } from "@gno/hooks/use-feed";
import Layout from "@gno/components/layout";
import useScrollToTop from "@gno/components/utils/useScrollToTopWithOffset";
import Button from "@gno/components/button";
import { Post } from "@gno/types";
import { selectAccount, setPostToReply, useAppDispatch, useAppSelector } from "@gno/redux";
import { gnodTxAndRedirectToSign, selectAccount, setPostToReply, useAppDispatch, useAppSelector } from "@gno/redux";
import Alert from "@gno/components/alert";
import { FeedView } from "@gno/components/view";

Expand All @@ -22,6 +22,7 @@ export default function Page() {
const dispatch = useAppDispatch();

const account = useAppSelector(selectAccount);
const pathName = usePathname();

useScrollToTop(ref, Platform.select({ ios: -150, default: 0 }));

Expand Down Expand Up @@ -53,17 +54,8 @@ export default function Page() {
};

const onGnod = async (post: Post) => {
setIsLoading(true);

if (!account) throw new Error("No active account");

try {
await feed.onGnod(post, account.address);
} catch (error) {
RNAlert.alert("Error", "Error while adding reaction: " + error);
} finally {
setIsLoading(false);
}
dispatch(gnodTxAndRedirectToSign({ post, callerAddressBech32: account.bech32, pathName })).unwrap();
};

if (isLoading)
Expand Down
19 changes: 6 additions & 13 deletions mobile/app/post/[post_id].tsx
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useEffect, useState } from "react";
import { useLocalSearchParams, useRouter } from "expo-router";
import { useLocalSearchParams, usePathname, useRouter } from "expo-router";
import Text from "@gno/components/text";
import { selectAccount, selectPostToReply, useAppSelector } from "@gno/redux";
import { gnodTxAndRedirectToSign, selectAccount, selectPostToReply, useAppDispatch, useAppSelector } from "@gno/redux";
import Layout from "@gno/components/layout";
import TextInput from "@gno/components/textinput";
import Button from "@gno/components/button";
Expand All @@ -26,9 +26,12 @@ function Page() {
const { gnonative } = useGnoNativeContext();
const account = useAppSelector(selectAccount);

const pathName = usePathname();
const params = useLocalSearchParams();
const { post_id, address } = params;

const dispatch = useAppDispatch();

useEffect(() => {
fetchData();
}, [post_id]);
Expand Down Expand Up @@ -84,18 +87,8 @@ function Page() {

const onGnod = async (post: Post) => {
console.log("gnodding post: ", post);
setLoading("Gnoding...");

if (!account) throw new Error("No active account");

try {
await feed.onGnod(post, account.address);
await fetchData();
} catch (error) {
RNAlert.alert("Error", "Error while adding reaction: " + error);
} finally {
setLoading(undefined);
}
dispatch(gnodTxAndRedirectToSign({ post, callerAddressBech32: account.bech32, pathName }))
};

if (!post) {
Expand Down
34 changes: 28 additions & 6 deletions mobile/redux/features/linkingSlice.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { Post } from "@gno/types";
import { GnoNativeApi, MakeTxResponse } from "@gnolang/gnonative";
import { createAsyncThunk, createSlice } from "@reduxjs/toolkit";
import * as Linking from 'expo-linking';
Expand Down Expand Up @@ -27,13 +28,12 @@ type MakeTxAndRedirectParams = {
export const postTxAndRedirectToSign = createAsyncThunk<MakeTxResponse, MakeTxAndRedirectParams, ThunkExtra>("tx/makeCallTxAndRedirectToSign", async (props, thunkAPI) => {
const { callerAddressBech32, postContent } = props;

const packagePath = "gno.land/r/berty/social";
const fnc = "PostMessage";
const args: Array<string> = [postContent];
const gasFee = "1000000ugnot";
const gasWanted = BigInt(10000000);

const res = await makeCallTx({ packagePath, fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);
const res = await makeCallTx({ fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);

setTimeout(() => {
const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${callerAddressBech32}`, `client_name=dSocial`, `reason=Post a message`];
Expand All @@ -44,7 +44,6 @@ export const postTxAndRedirectToSign = createAsyncThunk<MakeTxResponse, MakeTxAn
})

type MakeCallTxParams = {
packagePath: string,
fnc: string,
args: string[],
gasFee: string,
Expand All @@ -54,13 +53,13 @@ type MakeCallTxParams = {
callerAddressBech32: string,
};

export const makeCallTx = async (props : MakeCallTxParams, gnonative: GnoNativeApi) : Promise<MakeTxResponse> => {
const { packagePath, fnc, callerAddressBech32, gasFee, gasWanted, args } = props;
export const makeCallTx = async (props: MakeCallTxParams, gnonative: GnoNativeApi): Promise<MakeTxResponse> => {
const { fnc, callerAddressBech32, gasFee, gasWanted, args } = props;

console.log("making a tx for: ", callerAddressBech32);
const address = await gnonative.addressFromBech32(callerAddressBech32);

return await gnonative.makeCallTx(packagePath, fnc, args, gasFee, gasWanted, address)
return await gnonative.makeCallTx("gno.land/r/berty/social", fnc, args, gasFee, gasWanted, address)
}

export const broadcastTxCommit = createAsyncThunk<void, string, ThunkExtra>("tx/broadcastTxCommit", async (signedTx, thunkAPI) => {
Expand All @@ -71,6 +70,29 @@ export const broadcastTxCommit = createAsyncThunk<void, string, ThunkExtra>("tx/
console.log("broadcasted tx: ", res);
});

interface GnodCallTxParams {
post: Post,
callerAddressBech32: string,
pathName: string,
}

export const gnodTxAndRedirectToSign = createAsyncThunk<void, GnodCallTxParams, ThunkExtra>("tx/gnodTxAndRedirectToSign", async (props, thunkAPI) => {
console.log("gnodding post: ", props.post);
const { post, callerAddressBech32, pathName } = props;

const fnc = "AddReaction";
const gasFee = "1000000ugnot";
const gasWanted = BigInt(2000000);
// post.user.address is in fact a bech32 address
const args: Array<string> = [String(post.user.address), String(post.id), String(post.id), String("0")];
const res = await makeCallTx({ fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);

setTimeout(() => {
const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${callerAddressBech32}`, `client_name=dSocial`, `reason=Gnoding a message`, `callback=${encodeURIComponent('tech.berty.dsocial://' + pathName)}`];
Linking.openURL('land.gno.gnokey://tosign?' + params.join('&'))
}, 500)
});

/**
* Slice to handle linking between the app and the GnokeyMobile app
*/
Expand Down
6 changes: 2 additions & 4 deletions mobile/redux/features/profileSlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,13 @@ export const followTxAndRedirectToSign = createAsyncThunk<void, { address: strin
console.log("Follow user: %s", address);
const gnonative = thunkAPI.extra.gnonative;

const packagePath = "gno.land/r/berty/social";
const fnc = "Follow";
const args: Array<string> = [address];
const gasFee = "1000000ugnot";
const gasWanted = BigInt(10000000);
const callerAddressBech32 = await gnonative.addressToBech32(callerAddress);

const res = await makeCallTx({ packagePath, fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);
const res = await makeCallTx({ fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);

setTimeout(() => {
const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${callerAddressBech32}`, CLIENT_NAME_PARAM, 'reason=Follow a user', `callback=${encodeURIComponent('tech.berty.dsocial://account')}`];
Expand All @@ -46,14 +45,13 @@ export const unfollowTxAndRedirectToSign = createAsyncThunk<void, { address: str
console.log("Follow user: %s", address);
const gnonative = thunkAPI.extra.gnonative;

const packagePath = "gno.land/r/berty/social";
const fnc = "Unfollow";
const args: Array<string> = [address];
const gasFee = "1000000ugnot";
const gasWanted = BigInt(10000000);
const callerAddressBech32 = await gnonative.addressToBech32(callerAddress);

const res = await makeCallTx({ packagePath, fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);
const res = await makeCallTx({ fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);

setTimeout(() => {
const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${callerAddressBech32}`, CLIENT_NAME_PARAM, 'reason=Unfollow a user', `callback=${encodeURIComponent('tech.berty.dsocial://account')}`];
Expand Down
3 changes: 1 addition & 2 deletions mobile/redux/features/replySlice.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,13 @@ interface RepostTxAndRedirectParams {
export const repostTxAndRedirectToSign = createAsyncThunk<MakeTxResponse, RepostTxAndRedirectParams, ThunkExtra>("tx/repostTxAndRedirectToSign", async (props, thunkAPI) => {
const { post, replyContent, callerAddressBech32 } = props;

const packagePath = "gno.land/r/berty/social";
const fnc = "RepostThread";
// post.user.address is in fact a bech32 address
const args: Array<string> = [String(post.user.address), String(post.id), replyContent];
const gasFee = "1000000ugnot";
const gasWanted = BigInt(10000000);

const res = await makeCallTx({ packagePath, fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);
const res = await makeCallTx({ fnc, args, gasFee, gasWanted, callerAddressBech32 }, thunkAPI.extra.gnonative);

setTimeout(() => {
const params = [`tx=${encodeURIComponent(res.txJson)}`, `address=${callerAddressBech32}`, `client_name=dSocial`, `reason=Repost a message`, `callback=${encodeURIComponent('tech.berty.dsocial://repost')}`];
Expand Down
19 changes: 1 addition & 18 deletions mobile/src/hooks/use-feed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,22 +107,5 @@ export const useFeed = () => {
return nHomePosts;
}

async function onGnod(post: Post, callerAddress: Uint8Array) : Promise<void> {

try {
const gasFee = "1000000ugnot";
const gasWanted = BigInt(2000000);
// post.user.address is in fact a bech32 address
const args: Array<string> = [String(post.user.address), String(post.id), String(post.id), String("0")];
console.log("AddReaction args2: ", args.join(", "));
for await (const response of await gnonative.call("gno.land/r/berty/social", "AddReaction", args, gasFee, gasWanted, callerAddress)) {
const result = JSON.parse(JSON.stringify(response)).result;
// Alert.alert("AddReaction Result", base64.decode(result));
}
} catch (error) {
Alert.alert("Error", "Error while adding reaction: " + error);
}
}

return { fetchFeed, fetchCount, fetchThread, fetchThreadPosts, onGnod };
return { fetchFeed, fetchCount, fetchThread, fetchThreadPosts };
};

0 comments on commit b98aff8

Please sign in to comment.