diff --git a/pkg/interface/src/logic/api/graph.ts b/pkg/interface/src/logic/api/graph.ts index 456776f42..da43054c1 100644 --- a/pkg/interface/src/logic/api/graph.ts +++ b/pkg/interface/src/logic/api/graph.ts @@ -6,6 +6,42 @@ import {makeResource, resourceFromPath} from '../lib/group'; import {GroupPolicy, Enc, Post, NodeMap, Content} from '~/types'; 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 = '') => { return { author: `~${window.ship}`, @@ -38,6 +74,7 @@ export default class GraphApi extends BaseApi { } private hookAction(ship: Patp, action: any): Promise { + console.log(action); return this.action('graph-push-hook', 'graph-update', action); } @@ -150,6 +187,19 @@ export default class GraphApi extends BaseApi { }); } + 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) { return this.hookAction(ship, { 'add-nodes': { diff --git a/pkg/interface/src/logic/lib/publish.ts b/pkg/interface/src/logic/lib/publish.ts index 4317f069c..8e44e423a 100644 --- a/pkg/interface/src/logic/lib/publish.ts +++ b/pkg/interface/src/logic/lib/publish.ts @@ -84,12 +84,25 @@ export function getLatestRevision(node: GraphNode): [number, string, string, Pos } const [revNum, rev] = [...revs.children][0]; if(!rev) { - return empty + return empty; } const [title, body] = rev.post.contents as TextContent[]; 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 { const comments = node.children.get(bigInt(2)); if(!comments) { diff --git a/pkg/interface/src/views/components/CommentItem.tsx b/pkg/interface/src/views/components/CommentItem.tsx index 4186848e1..0505451ae 100644 --- a/pkg/interface/src/views/components/CommentItem.tsx +++ b/pkg/interface/src/views/components/CommentItem.tsx @@ -8,6 +8,7 @@ import { GraphNode, TextContent } from '~/types/graph-update'; import tokenizeMessage from '~/logic/lib/tokenizeMessage'; import { LocalUpdateRemoteContentPolicy } from '~/types'; import { MentionText } from '~/views/components/MentionText'; +import { getLatestCommentRevision } from '~/logic/lib/publish'; const ClickBox = styled(Box)` cursor: pointer; @@ -27,24 +28,22 @@ interface CommentItemProps { } export function CommentItem(props: CommentItemProps) { - const { ship, contacts, name, api, remoteContentPolicy } = props; - const commentData = props.comment?.post; - const comment = commentData.contents; - - const disabled = props.pending || window.ship !== commentData.author; + const { ship, contacts, name, api, remoteContentPolicy, comment } = props; + const [revNum, post] = getLatestCommentRevision(comment); + const disabled = props.pending || window.ship !== post?.author; const onDelete = async () => { - await api.graph.removeNodes(ship, name, [commentData?.index]); + await api.graph.removeNodes(ship, name, [comment.post?.index]); }; return ( - + @@ -60,7 +59,7 @@ export function CommentItem(props: CommentItemProps) { diff --git a/pkg/interface/src/views/components/Comments.tsx b/pkg/interface/src/views/components/Comments.tsx index 2ed61075c..f3b165213 100644 --- a/pkg/interface/src/views/components/Comments.tsx +++ b/pkg/interface/src/views/components/Comments.tsx @@ -6,7 +6,7 @@ import { Contacts } from '~/types/contact-update'; import GlobalApi from '~/logic/api/global'; import { FormikHelpers } from 'formik'; import { GraphNode } from '~/types/graph-update'; -import { createPost } from '~/logic/api/graph'; +import { createPost, createBlankNodeWithChildPost } from '~/logic/api/graph'; import { LocalUpdateRemoteContentPolicy } from '~/types'; import { scanForMentions } from '~/logic/lib/graph'; @@ -30,8 +30,12 @@ export function Comments(props: CommentsProps) { ) => { try { const content = scanForMentions(comment); - const post = createPost(content, comments?.post?.index); - await api.graph.addPost(ship, name, post); + const node = createBlankNodeWithChildPost( + comments?.post?.index, + '1', + content + ); + await api.graph.addNode(ship, name, node); actions.resetForm(); actions.setStatus({ success: null }); } catch (e) {