-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add post hash debug for post author
- Loading branch information
1 parent
243f208
commit eb85c84
Showing
4 changed files
with
113 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
import React, { useState } from 'react' | ||
import { makeStyles } from '@material-ui/core/styles' | ||
import Button from '@material-ui/core/Button' | ||
import List from '@material-ui/core/List' | ||
import ListItem from '@material-ui/core/ListItem' | ||
import ListItemAvatar from '@material-ui/core/ListItemAvatar' | ||
import ListItemText from '@material-ui/core/ListItemText' | ||
import DialogTitle from '@material-ui/core/DialogTitle' | ||
import Dialog from '@material-ui/core/Dialog' | ||
import { blue } from '@material-ui/core/colors' | ||
import { PortalShadowRoot } from '../../utils/jss/ShadowRootPortal' | ||
import { useFriendsList } from '../DataSource/useActivatedUI' | ||
import { Avatar } from '../../utils/components/Avatar' | ||
import { Person } from '../../database' | ||
import { useAsync } from '../../utils/components/AsyncComponent' | ||
import Services from '../../extension/service' | ||
import { PostIVIdentifier } from '../../database/type' | ||
import { deconstructPayload, Payload } from '../../utils/type-transform/Payload' | ||
import { DialogContentText, DialogContent } from '@material-ui/core' | ||
|
||
const useStyles = makeStyles({ | ||
avatar: { | ||
backgroundColor: blue[100], | ||
color: blue[600], | ||
}, | ||
}) | ||
|
||
export interface SimpleDialogProps { | ||
open: boolean | ||
onClose: (value: string) => void | ||
friends: Person[] | ||
hashMap: [string, string][] | ||
} | ||
|
||
function SimpleDialog(props: SimpleDialogProps) { | ||
const { open } = props | ||
|
||
const map = new Map<string, string>() | ||
for (const data of props.hashMap) { | ||
map.set(data[0], data[1]) | ||
} | ||
|
||
return ( | ||
<Dialog container={PortalShadowRoot} onClose={props.onClose} open={open}> | ||
<DialogTitle> | ||
Post hash for each of your friends (Appear in this list is not related to if you have shared this post | ||
to someone or not.) | ||
</DialogTitle> | ||
<List> | ||
{props.friends.map(one => { | ||
return ( | ||
<ListItem> | ||
<ListItemAvatar> | ||
<Avatar person={one} /> | ||
</ListItemAvatar> | ||
<ListItemText | ||
primary={one.nickname || one.identifier.userId} | ||
secondary={ | ||
<span style={{ fontFamily: 'monospace' }}> | ||
{map.get(one.identifier.toText()) || 'Unknown'} | ||
</span> | ||
} | ||
/> | ||
</ListItem> | ||
) | ||
})} | ||
</List> | ||
</Dialog> | ||
) | ||
} | ||
|
||
export function DebugModeUI_PostHashDialog(props: { post: string; network: string }) { | ||
const [open, setOpen] = React.useState(false) | ||
const payload = deconstructPayload(props.post) | ||
const [hashMap, setHashMap] = useState<[string, string][]>([]) | ||
const friends = useFriendsList() | ||
useAsync(() => { | ||
if (!payload) return Promise.resolve([] as typeof hashMap) | ||
const ivID = new PostIVIdentifier(props.network, payload.iv) | ||
return Services.Crypto.debugShowAllPossibleHashForPost(ivID) | ||
}, [props.post]).then(setHashMap) | ||
return ( | ||
<> | ||
<Button variant="outlined" color="primary" onClick={() => setOpen(true)}> | ||
See what code my friends will see for this post | ||
</Button> | ||
<SimpleDialog hashMap={hashMap} friends={friends} open={open} onClose={() => setOpen(false)} /> | ||
</> | ||
) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
src/extension/background-script/CryptoServices/debugShowAllPossibleHashForPost.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { PostIVIdentifier } from '../../../database/type' | ||
import { queryPeopleDB } from '../../../database/people' | ||
import { hashPostSalt, hashCryptoKey } from '../../../network/gun/version.2/hash' | ||
|
||
export async function debugShowAllPossibleHashForPost(post: PostIVIdentifier) { | ||
const friends = await queryPeopleDB(x => x.network === post.network) | ||
return Promise.all( | ||
friends | ||
.filter(x => x.publicKey) | ||
.map( | ||
async x => | ||
[ | ||
x.identifier.toText(), | ||
(await hashPostSalt(post.postIV)) + '-' + (await hashCryptoKey(x.publicKey!)), | ||
] as [string, string], | ||
), | ||
) | ||
} |