Skip to content

Commit

Permalink
Merge pull request #2720 from ecency/mb/reblog
Browse files Browse the repository at this point in the history
add a reblog functionality into reblogScreen
  • Loading branch information
feruzm authored May 16, 2024
2 parents fa4f90d + 467b467 commit 06deb8e
Show file tree
Hide file tree
Showing 11 changed files with 329 additions and 135 deletions.
19 changes: 10 additions & 9 deletions src/components/postCard/children/postCardActionsPanel.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,16 @@ export const PostCardActionsPanel = ({ content, handleCardInteraction }: Props)
};

const _onReblogsPress = () => {
if (content.reblogs > 0) {
handleCardInteraction(PostCardActionIds.NAVIGATE, {
name: ROUTES.SCREENS.REBLOGS,
params: {
author: content.author,
permlink: content.permlink,
},
});
}
const { author, permlink } = content;

handleCardInteraction(PostCardActionIds.NAVIGATE, {
name: ROUTES.SCREENS.REBLOGS,
params: {
author,
permlink,
},
});

};

return (
Expand Down
5 changes: 3 additions & 2 deletions src/components/postView/container/postDisplayContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -71,8 +71,9 @@ const PostDisplayContainer = ({
navigation.navigate({
name: ROUTES.SCREENS.REBLOGS,
params: {
author: post.author,
permlink: post.permlink,
reblogs,
author,
permlink,
},
key: post.permlink + post.reblogs.length,
} as never);
Expand Down
4 changes: 3 additions & 1 deletion src/config/locales/en-US.json
Original file line number Diff line number Diff line change
Expand Up @@ -960,7 +960,9 @@
"time": "TIME"
},
"reblog": {
"title": "Reblog Info"
"title": "Reblog Info",
"reblog_post": "Reblog Post",
"reblog_delete": "Undo Reblog"
},
"dsteem": {
"date_error": {
Expand Down
8 changes: 8 additions & 0 deletions src/globalStyles.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import EStyleSheet from 'react-native-extended-stylesheet';
import { FlipInEasyX } from 'react-native-reanimated';

export default EStyleSheet.create({
containerHorizontal16: {
Expand Down Expand Up @@ -87,4 +88,11 @@ export default EStyleSheet.create({
tabBarBottom: {
paddingBottom: 60,
},
mainbutton: {
width: '40%',
justifyContent: 'center',
position: 'relative',
left: 180,
bottom: 20,
},
});
62 changes: 15 additions & 47 deletions src/providers/hive/dhive.js
Original file line number Diff line number Diff line change
Expand Up @@ -412,7 +412,7 @@ export const getUser = async (user, loggedIn = true) => {
export const getUserReputation = async (author) => {
try {
const response = await client.call('condenser_api', 'get_account_reputations', [author, 1]);

if (response && response.length < 1) {
return 0;
}
Expand Down Expand Up @@ -1751,62 +1751,30 @@ const _postContent = async (

// Re-blog
// TODO: remove pinCode
export const reblog = (account, pinCode, author, permlink) =>
_reblog(account, pinCode, author, permlink).then((resp) => {
export const reblog = (account, pinCode, author, permlink, undo = false) =>
_reblog(account, pinCode, author, permlink, undo).then((resp) => {
return resp;
});

const _reblog = async (account, pinCode, author, permlink) => {
const pin = getDigitPinCode(pinCode);
const key = getAnyPrivateKey(account.local, pin);

if (account.local.authType === AUTH_TYPE.STEEM_CONNECT) {
const token = decryptKey(account.local.accessToken, pin);
const api = new hsClient({
accessToken: token,
});
const _reblog = async (account, pinCode, author, permlink, undo = false) => {

const follower = account.name;

return api.reblog(follower, author, permlink).then((resp) => resp.result);
}

if (key) {
const privateKey = PrivateKey.fromString(key);
const follower = account.name;

const json = {
id: 'follow',
json: jsonStringify([
'reblog',
{
account: follower,
author,
permlink,
},
]),
required_auths: [],
required_posting_auths: [follower],
};
const json = [
'reblog',
{
account: account.name,
author,
permlink,
delete: undo ? 'delete' : undefined
},
];

const opArray = [['custom_json', json]];

return new Promise((resolve, reject) => {
sendHiveOperations(opArray, privateKey)
.then((result) => {
resolve(result);
})
.catch((err) => {
reject(err);
});
});
}
return broadcastPostingJSON('follow', json, account, pinCode)

return Promise.reject(
new Error('Check private key permission! Required private posting key or above.'),
);
};


export const claimRewardBalance = (account, pinCode, rewardHive, rewardHbd, rewardVests) => {
const pin = getDigitPinCode(pinCode);
const key = getAnyPrivateKey(get(account, 'local'), pin);
Expand Down
3 changes: 2 additions & 1 deletion src/providers/queries/postQueries/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import * as postQueries from './postQueries';
import * as wavesQueries from './wavesQueries';
import * as pollQueries from './pollQueries';
import * as reblogQueries from './reblogQueries';

export { postQueries, wavesQueries, pollQueries };
export { postQueries, wavesQueries, pollQueries, reblogQueries };
128 changes: 128 additions & 0 deletions src/providers/queries/postQueries/reblogQueries.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { useMutation, useQuery, useQueryClient } from "@tanstack/react-query";
import QUERIES from "../queryKeys";
import { useAppSelector } from "../../../hooks";
import { useDispatch } from "react-redux";
import { setRcOffer, toastNotification } from "../../../redux/actions/uiAction";
import { useIntl } from "react-intl";
import { getPostReblogs, reblog } from "../../hive/dhive";
import { get } from 'lodash';
import { PointActivityIds } from "../../ecency/ecency.types";
import { useUserActivityMutation } from "../pointQueries";



/** hook used to return post poll */
export const useGetReblogsQuery = (author: string, permlink: string) => {


const query = useQuery<string[]>(
[QUERIES.POST.GET_REBLOGS, author, permlink],
async () => {
if (!author || !permlink) {
return null;
}

try {
const reblogs = await getPostReblogs(author, permlink);
if (!reblogs) {
new Error('Reblog data unavailable');
}

return reblogs
} catch (err) {
console.warn('Failed to get post', err);
return []
}
},
{
initialData: [],
cacheTime: 30 * 60 * 1000, // keeps cache for 30 minutes
},
);

return query;
};




export function useReblogMutation(author: string, permlink: string) {
// const { activeUser } = useMappedStore();
const intl = useIntl();
const dispatch = useDispatch();
const queryClient = useQueryClient()
const currentAccount = useAppSelector(state => state.account.currentAccount);
const pinHash = useAppSelector(state => state.application.pin);

const userActivityMutation = useUserActivityMutation();


return useMutation({
mutationKey: [QUERIES.POST.REBLOG_POST],
mutationFn: async ({ undo }:{undo:boolean}) => {

if (!author || !permlink || !currentAccount) {
throw new Error("Not enough data to reblog post")
}

const resp = await reblog(currentAccount, pinHash, author, permlink, undo)

// track user activity points ty=130
userActivityMutation.mutate({
pointsTy: PointActivityIds.REBLOG,
transactionId: resp.id,

});

return resp;

},
retry: 3,

onSuccess: (resp, vars) => {
console.log("reblog response", resp);
//update poll cache here
queryClient.setQueryData<ReturnType<typeof useGetReblogsQuery>["data"]>(
[QUERIES.POST.GET_REBLOGS, author, permlink],
(data) => {
if (!data || !resp) {
return data;
}

const _curIndex = data.indexOf(currentAccount.username)
if(vars.undo){
data.splice(_curIndex, 1)
}
else if (_curIndex < 0) {
data.splice(0, 0, currentAccount.username);
}

return [...data] as ReturnType<typeof useGetReblogsQuery>["data"];
}
)
},
onError: (error) => {
if (String(get(error, 'jse_shortmsg', '')).indexOf('has already reblogged') > -1) {
dispatch(
toastNotification(
intl.formatMessage({
id: 'alert.already_rebloged',
}),
),
);
} else {
if (error && error.jse_shortmsg.split(': ')[1].includes('wait to transact')) {
// when RC is not enough, offer boosting account
dispatch(setRcOffer(true));
} else {
// when other errors
dispatch(toastNotification(intl.formatMessage({ id: 'alert.fail' })));
}
}
}


});
}


4 changes: 3 additions & 1 deletion src/providers/queries/queryKeys.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ const QUERIES = {
GET: 'QUERY_GET_POST',
GET_POLL: 'QUERY_GET_POLL',
GET_DISCUSSION: 'QUERY_GET_DISCUSSION',
SIGN_POLL_VOTE:"SIGN_POLL_VOTE"
SIGN_POLL_VOTE:"SIGN_POLL_VOTE",
GET_REBLOGS:"GET_REBLOGS",
REBLOG_POST:"REBLOG_POST"
},
LEADERBOARD: {
GET: 'QUERY_GET_LEADERBOARD',
Expand Down
74 changes: 0 additions & 74 deletions src/screens/reblogs/screen/reblogScreen.js

This file was deleted.

Loading

0 comments on commit 06deb8e

Please sign in to comment.