Skip to content

Commit

Permalink
Merge pull request #2761 from ecency/nt/wave-post-data
Browse files Browse the repository at this point in the history
Nt/wave post data
  • Loading branch information
feruzm authored Sep 25, 2023
2 parents e915dcb + d026f8c commit 369fe36
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 16 deletions.
28 changes: 17 additions & 11 deletions src/components/quickReplyModal/usePostSubmitter.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { useDispatch } from "react-redux";
import { useAppSelector } from "../../hooks";
import { postComment } from "../../providers/hive/dhive";
import { extractMetadata, generateReplyPermlink, makeJsonMetadata } from "../../utils/editor";
import { extractMetadata, generateUniquePermlink, makeJsonMetadata } from "../../utils/editor";
import { Alert } from "react-native";
import { updateCommentCache } from "../../redux/actions/cacheActions";
import { toastNotification } from "../../redux/actions/uiAction";
Expand All @@ -10,6 +10,7 @@ import { useState } from "react";
import { useUserActivityMutation, wavesQueries } from "../../providers/queries";
import { PointActivityIds } from "../../providers/ecency/ecency.types";
import { usePublishWaveMutation } from "../../providers/queries/postQueries/wavesQueries";
import { PostTypes } from "../../constants/postTypes";


export const usePostSubmitter = () => {
Expand All @@ -27,9 +28,9 @@ export const usePostSubmitter = () => {


// handle submit reply
const _submitReply = async (commentBody: string, parentPost: any) => {
const _submitReply = async (commentBody: string, parentPost: any, postType: PostTypes = PostTypes.COMMENT) => {
if (!commentBody) {
return false ;
return false;
}
if (isSending) {
return false;
Expand All @@ -38,7 +39,11 @@ export const usePostSubmitter = () => {
if (currentAccount) {
setIsSending(true);

const permlink = generateReplyPermlink(parentPost.author);
const _prefix = postType === PostTypes.WAVE
? postType
: `re-${parentPost.author.replace(/\./g, '')}`
const permlink = generateUniquePermlink(_prefix);

const author = currentAccount.name;
const parentAuthor = parentPost.author;
const parentPermlink = parentPost.permlink;
Expand All @@ -48,8 +53,9 @@ export const usePostSubmitter = () => {

//adding jsonmeta with image ratios here....
const meta = await extractMetadata({
body:commentBody,
fetchRatios:true
body: commentBody,
fetchRatios: true,
postType
})
const jsonMetadata = makeJsonMetadata(meta, parentTags || ['ecency'])

Expand Down Expand Up @@ -91,7 +97,7 @@ export const usePostSubmitter = () => {
);

// add comment cache entry
const _cacheCommentData = {
const _cacheCommentData = {
author,
permlink,
url,
Expand All @@ -100,7 +106,7 @@ export const usePostSubmitter = () => {
markdownBody: commentBody,
json_metadata: jsonMetadata
}

dispatch(
updateCommentCache(
`${author}/${permlink}`,
Expand Down Expand Up @@ -137,15 +143,15 @@ export const usePostSubmitter = () => {


//feteced lates wafves container and post wave to that container
const _submitWave = async (body:string) => {
const _submitWave = async (body: string) => {

try {
const _wavesHost = 'ecency.waves' //TODO: make waves host selection dynamic
const latestWavesPost = await wavesQueries.fetchLatestWavesContainer(_wavesHost);

const _cacheCommentData = await _submitReply(body, latestWavesPost)
const _cacheCommentData = await _submitReply(body, latestWavesPost, PostTypes.WAVE)

if(_cacheCommentData){
if (_cacheCommentData) {
pusblishWaveMutation.mutate(_cacheCommentData)
}

Expand Down
6 changes: 4 additions & 2 deletions src/screens/editor/container/editorContainer.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import { default as ROUTES } from '../../../constants/routeNames';
// Utilities
import {
generatePermlink,
generateReplyPermlink,
generateUniquePermlink,
makeJsonMetadata,
makeOptions,
extractMetadata,
Expand Down Expand Up @@ -707,7 +707,9 @@ class EditorContainer extends Component<EditorContainerProps, any> {
});

const { post } = this.state;
const permlink = generateReplyPermlink(post.author);

const _prefix = `re-${post.author.replace(/\./g, '')}`
const permlink = generateUniquePermlink(_prefix);

const parentAuthor = post.author;
const parentPermlink = post.permlink;
Expand Down
13 changes: 10 additions & 3 deletions src/utils/editor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Image } from 'react-native';
import { diff_match_patch as diffMatchPatch } from 'diff-match-patch';
import VersionNumber from 'react-native-version-number';
import MimeTypes from 'mime-types';
import { PostTypes } from '../constants/postTypes';

export const getWordsCount = (text) =>
text && typeof text === 'string' ? text.replace(/^\s+|\s+$/g, '').split(/\s+/).length : 0;
Expand Down Expand Up @@ -80,8 +81,8 @@ export const extractWordAtIndex = (text: string, index: number) => {
return word;
};

export const generateReplyPermlink = (toAuthor) => {
if (!toAuthor) {
export const generateUniquePermlink = (prefix) => {
if (!prefix) {
return '';
}

Expand All @@ -93,7 +94,7 @@ export const generateReplyPermlink = (toAuthor) => {
.getSeconds()
.toString()}${t.getMilliseconds().toString()}z`;

return `re-${toAuthor.replace(/\./g, '')}-${timeFormat}`;
return `${prefix}-${timeFormat}`;
};

export const makeOptions = (postObj) => {
Expand Down Expand Up @@ -214,10 +215,12 @@ export const extractMetadata = async ({
body,
thumbUrl,
fetchRatios,
postType,
}: {
body: string;
thumbUrl?: string;
fetchRatios?: boolean;
postType?: PostTypes;
}) => {
// NOTE: keepting regex to extract usernames as reference for later usage if any
// const userReg = /(^|\s)(@[a-z][-.a-z\d]+[a-z\d])/gim;
Expand Down Expand Up @@ -254,6 +257,10 @@ export const extractMetadata = async ({
);
}

//setting post type, primary usecase for separating waves from other posts
out.type = postType || PostTypes.POST


return out;
};

Expand Down

0 comments on commit 369fe36

Please sign in to comment.