publish interface: back up to old parity

This commit is contained in:
Logan Allen 2020-11-17 13:59:43 -06:00
parent 125dd0d910
commit 5fb9d1f0fe
4 changed files with 80 additions and 14 deletions

View File

@ -6,6 +6,42 @@ import {makeResource, resourceFromPath} from '../lib/group';
import {GroupPolicy, Enc, Post, NodeMap, Content} from '~/types'; import {GroupPolicy, Enc, Post, NodeMap, Content} from '~/types';
import { numToUd, unixToDa } from '~/logic/lib/util'; import { numToUd, unixToDa } from '~/logic/lib/util';
export const createBlankNodeWithChildPost = (
parentIndex: string = '',
childIndex: string = '',
contents: Content[]
) => {
const date = unixToDa(Date.now()).toString();
const nodeIndex = parentIndex + '/' + date;
const childGraph = {};
childGraph[childIndex] = {
post: {
author: `~${window.ship}`,
index: nodeIndex + '/' + childIndex,
'time-sent': Date.now(),
contents,
hash: null,
signatures: []
},
children: { empty: null }
};
return {
post: {
author: `~${window.ship}`,
index: nodeIndex,
'time-sent': Date.now(),
contents: [],
hash: null,
signatures: []
},
children: {
graph: childGraph
}
};
};
export const createPost = (contents: Content[], parentIndex: string = '') => { export const createPost = (contents: Content[], parentIndex: string = '') => {
return { return {
author: `~${window.ship}`, author: `~${window.ship}`,
@ -38,6 +74,7 @@ export default class GraphApi extends BaseApi<StoreState> {
} }
private hookAction(ship: Patp, action: any): Promise<any> { private hookAction(ship: Patp, action: any): Promise<any> {
console.log(action);
return this.action('graph-push-hook', 'graph-update', action); return this.action('graph-push-hook', 'graph-update', action);
} }
@ -150,6 +187,19 @@ export default class GraphApi extends BaseApi<StoreState> {
}); });
} }
addNode(ship: Patp, name: string, node: Object) {
let nodes = {};
const resource = { ship, name };
nodes[node.post.index] = node;
return this.hookAction(ship, {
'add-nodes': {
resource,
nodes
}
});
}
addNodes(ship: Patp, name: string, nodes: Object) { addNodes(ship: Patp, name: string, nodes: Object) {
return this.hookAction(ship, { return this.hookAction(ship, {
'add-nodes': { 'add-nodes': {

View File

@ -84,12 +84,25 @@ export function getLatestRevision(node: GraphNode): [number, string, string, Pos
} }
const [revNum, rev] = [...revs.children][0]; const [revNum, rev] = [...revs.children][0];
if(!rev) { if(!rev) {
return empty return empty;
} }
const [title, body] = rev.post.contents as TextContent[]; const [title, body] = rev.post.contents as TextContent[];
return [revNum.toJSNumber(), title.text, body.text, rev.post]; return [revNum.toJSNumber(), title.text, body.text, rev.post];
} }
export function getLatestCommentRevision(node: GraphNode): [number, Post] {
const empty = [1, buntPost()] as [number, Post];
if (node.children.size <= 0) {
return empty;
}
const [revNum, rev] = [...node.children][0];
if(!rev) {
return empty;
}
return [revNum.toJSNumber(), rev.post];
}
export function getComments(node: GraphNode): GraphNode { export function getComments(node: GraphNode): GraphNode {
const comments = node.children.get(bigInt(2)); const comments = node.children.get(bigInt(2));
if(!comments) { if(!comments) {

View File

@ -8,6 +8,7 @@ import { GraphNode, TextContent } from '~/types/graph-update';
import tokenizeMessage from '~/logic/lib/tokenizeMessage'; import tokenizeMessage from '~/logic/lib/tokenizeMessage';
import { LocalUpdateRemoteContentPolicy } from '~/types'; import { LocalUpdateRemoteContentPolicy } from '~/types';
import { MentionText } from '~/views/components/MentionText'; import { MentionText } from '~/views/components/MentionText';
import { getLatestCommentRevision } from '~/logic/lib/publish';
const ClickBox = styled(Box)` const ClickBox = styled(Box)`
cursor: pointer; cursor: pointer;
@ -27,24 +28,22 @@ interface CommentItemProps {
} }
export function CommentItem(props: CommentItemProps) { export function CommentItem(props: CommentItemProps) {
const { ship, contacts, name, api, remoteContentPolicy } = props; const { ship, contacts, name, api, remoteContentPolicy, comment } = props;
const commentData = props.comment?.post; const [revNum, post] = getLatestCommentRevision(comment);
const comment = commentData.contents; const disabled = props.pending || window.ship !== post?.author;
const disabled = props.pending || window.ship !== commentData.author;
const onDelete = async () => { const onDelete = async () => {
await api.graph.removeNodes(ship, name, [commentData?.index]); await api.graph.removeNodes(ship, name, [comment.post?.index]);
}; };
return ( return (
<Box mb={4} opacity={commentData?.pending ? '60%' : '100%'}> <Box mb={4} opacity={post?.pending ? '60%' : '100%'}>
<Row bg="white" my={3}> <Row bg="white" my={3}>
<Author <Author
showImage showImage
contacts={contacts} contacts={contacts}
ship={commentData?.author} ship={post?.author}
date={commentData?.['time-sent']} date={post?.['time-sent']}
hideAvatars={props.hideAvatars} hideAvatars={props.hideAvatars}
hideNicknames={props.hideNicknames} hideNicknames={props.hideNicknames}
> >
@ -60,7 +59,7 @@ export function CommentItem(props: CommentItemProps) {
<Box mb={2}> <Box mb={2}>
<MentionText <MentionText
contacts={contacts} contacts={contacts}
content={comment} content={post?.contents}
remoteContentPolicy={remoteContentPolicy} remoteContentPolicy={remoteContentPolicy}
/> />
</Box> </Box>

View File

@ -6,7 +6,7 @@ import { Contacts } from '~/types/contact-update';
import GlobalApi from '~/logic/api/global'; import GlobalApi from '~/logic/api/global';
import { FormikHelpers } from 'formik'; import { FormikHelpers } from 'formik';
import { GraphNode } from '~/types/graph-update'; import { GraphNode } from '~/types/graph-update';
import { createPost } from '~/logic/api/graph'; import { createPost, createBlankNodeWithChildPost } from '~/logic/api/graph';
import { LocalUpdateRemoteContentPolicy } from '~/types'; import { LocalUpdateRemoteContentPolicy } from '~/types';
import { scanForMentions } from '~/logic/lib/graph'; import { scanForMentions } from '~/logic/lib/graph';
@ -30,8 +30,12 @@ export function Comments(props: CommentsProps) {
) => { ) => {
try { try {
const content = scanForMentions(comment); const content = scanForMentions(comment);
const post = createPost(content, comments?.post?.index); const node = createBlankNodeWithChildPost(
await api.graph.addPost(ship, name, post); comments?.post?.index,
'1',
content
);
await api.graph.addNode(ship, name, node);
actions.resetForm(); actions.resetForm();
actions.setStatus({ success: null }); actions.setStatus({ success: null });
} catch (e) { } catch (e) {