Add post deletion

zio/stable
Paul Frazee 2022-11-16 16:16:43 -06:00
parent bd1a4b198e
commit 41ae87e770
8 changed files with 114 additions and 9 deletions

View File

@ -1,6 +1,7 @@
import {makeAutoObservable, runInAction} from 'mobx' import {makeAutoObservable, runInAction} from 'mobx'
import * as GetTimeline from '../../third-party/api/src/client/types/app/bsky/feed/getTimeline' import * as GetTimeline from '../../third-party/api/src/client/types/app/bsky/feed/getTimeline'
import * as GetAuthorFeed from '../../third-party/api/src/client/types/app/bsky/feed/getAuthorFeed' import * as GetAuthorFeed from '../../third-party/api/src/client/types/app/bsky/feed/getAuthorFeed'
import {AtUri} from '../../third-party/uri'
import {RootStoreModel} from './root-store' import {RootStoreModel} from './root-store'
import * as apilib from '../lib/api' import * as apilib from '../lib/api'
import {cleanError} from '../../view/lib/strings' import {cleanError} from '../../view/lib/strings'
@ -135,6 +136,13 @@ export class FeedItemModel implements GetTimeline.FeedItem {
}) })
} }
} }
async delete() {
await this.rootStore.api.app.bsky.feed.post.delete({
did: this.author.did,
rkey: new AtUri(this.uri).rkey,
})
}
} }
export class FeedModel { export class FeedModel {

View File

@ -175,6 +175,13 @@ export class PostThreadViewPostModel implements GetPostThread.Post {
}) })
} }
} }
async delete() {
await this.rootStore.api.app.bsky.feed.post.delete({
did: this.author.did,
rkey: new AtUri(this.uri).rkey,
})
}
} }
export class PostThreadViewModel { export class PostThreadViewModel {

View File

@ -1,4 +1,4 @@
import React, {useMemo} from 'react' import React, {useMemo, useState} from 'react'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {StyleSheet, Text, View} from 'react-native' import {StyleSheet, Text, View} from 'react-native'
import Svg, {Line} from 'react-native-svg' import Svg, {Line} from 'react-native-svg'
@ -9,6 +9,7 @@ import {PostThreadViewPostModel} from '../../../state/models/post-thread-view'
import {Link} from '../util/Link' import {Link} from '../util/Link'
import {RichText} from '../util/RichText' import {RichText} from '../util/RichText'
import {PostDropdownBtn} from '../util/DropdownBtn' import {PostDropdownBtn} from '../util/DropdownBtn'
import Toast from '../util/Toast'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import {s, colors} from '../../lib/styles' import {s, colors} from '../../lib/styles'
import {ago, pluralize} from '../../lib/strings' import {ago, pluralize} from '../../lib/strings'
@ -28,6 +29,7 @@ export const PostThreadItem = observer(function PostThreadItem({
onPostReply: () => void onPostReply: () => void
}) { }) {
const store = useStores() const store = useStores()
const [deleted, setDeleted] = useState(false)
const record = item.record as unknown as PostType.Record const record = item.record as unknown as PostType.Record
const hasEngagement = const hasEngagement =
item.upvoteCount || item.downvoteCount || item.repostCount item.upvoteCount || item.downvoteCount || item.repostCount
@ -76,6 +78,22 @@ export const PostThreadItem = observer(function PostThreadItem({
.toggleDownvote() .toggleDownvote()
.catch(e => console.error('Failed to toggle downvote', record, e)) .catch(e => console.error('Failed to toggle downvote', record, e))
} }
const onDeletePost = () => {
item.delete().then(
() => {
setDeleted(true)
Toast.show('Post deleted', {
position: Toast.positions.TOP,
})
},
e => {
console.error(e)
Toast.show('Failed to delete post, please try again', {
position: Toast.positions.TOP,
})
},
)
}
if (item._isHighlightedPost) { if (item._isHighlightedPost) {
return ( return (
@ -250,6 +268,8 @@ export const PostThreadItem = observer(function PostThreadItem({
authorHandle={item.author.handle} authorHandle={item.author.handle}
authorDisplayName={item.author.displayName} authorDisplayName={item.author.displayName}
timestamp={item.indexedAt} timestamp={item.indexedAt}
isAuthor={item.author.did === store.me.did}
onDeletePost={onDeletePost}
/> />
{item.replyingToAuthor && {item.replyingToAuthor &&
item.replyingToAuthor !== item.author.handle && ( item.replyingToAuthor !== item.author.handle && (

View File

@ -10,14 +10,15 @@ import {UserInfoText} from '../util/UserInfoText'
import {PostMeta} from '../util/PostMeta' import {PostMeta} from '../util/PostMeta'
import {PostCtrls} from '../util/PostCtrls' import {PostCtrls} from '../util/PostCtrls'
import {RichText} from '../util/RichText' import {RichText} from '../util/RichText'
import Toast from '../util/Toast'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import {useStores} from '../../../state' import {useStores} from '../../../state'
import {s, colors} from '../../lib/styles' import {s, colors} from '../../lib/styles'
import {ago} from '../../lib/strings'
export const Post = observer(function Post({uri}: {uri: string}) { export const Post = observer(function Post({uri}: {uri: string}) {
const store = useStores() const store = useStores()
const [view, setView] = useState<PostThreadViewModel | undefined>() const [view, setView] = useState<PostThreadViewModel | undefined>()
const [deleted, setDeleted] = useState(false)
useEffect(() => { useEffect(() => {
if (view?.params.uri === uri) { if (view?.params.uri === uri) {
@ -28,6 +29,12 @@ export const Post = observer(function Post({uri}: {uri: string}) {
newView.setup().catch(err => console.error('Failed to fetch post', err)) newView.setup().catch(err => console.error('Failed to fetch post', err))
}, [uri, view?.params.uri, store]) }, [uri, view?.params.uri, store])
// deleted
// =
if (deleted) {
return <View />
}
// loading // loading
// = // =
if (!view || view.isLoading || view.params.uri !== uri) { if (!view || view.isLoading || view.params.uri !== uri) {
@ -83,6 +90,22 @@ export const Post = observer(function Post({uri}: {uri: string}) {
.toggleDownvote() .toggleDownvote()
.catch(e => console.error('Failed to toggle downvote', record, e)) .catch(e => console.error('Failed to toggle downvote', record, e))
} }
const onDeletePost = () => {
item.delete().then(
() => {
setDeleted(true)
Toast.show('Post deleted', {
position: Toast.positions.TOP,
})
},
e => {
console.error(e)
Toast.show('Failed to delete post, please try again', {
position: Toast.positions.TOP,
})
},
)
}
return ( return (
<Link style={styles.outer} href={itemHref} title={itemTitle}> <Link style={styles.outer} href={itemHref} title={itemTitle}>
@ -102,6 +125,8 @@ export const Post = observer(function Post({uri}: {uri: string}) {
authorHandle={item.author.handle} authorHandle={item.author.handle}
authorDisplayName={item.author.displayName} authorDisplayName={item.author.displayName}
timestamp={item.indexedAt} timestamp={item.indexedAt}
isAuthor={item.author.did === store.me.did}
onDeletePost={onDeletePost}
/> />
{replyHref !== '' && ( {replyHref !== '' && (
<View style={[s.flexRow, s.mb2, {alignItems: 'center'}]}> <View style={[s.flexRow, s.mb2, {alignItems: 'center'}]}>

View File

@ -1,16 +1,16 @@
import React, {useMemo} from 'react' import React, {useMemo, useState} from 'react'
import {observer} from 'mobx-react-lite' import {observer} from 'mobx-react-lite'
import {StyleSheet, Text, View} from 'react-native' import {StyleSheet, Text, View} from 'react-native'
import {AtUri} from '../../../third-party/uri' import {AtUri} from '../../../third-party/uri'
import * as PostType from '../../../third-party/api/src/client/types/app/bsky/feed/post' import * as PostType from '../../../third-party/api/src/client/types/app/bsky/feed/post'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {FeedItemModel} from '../../../state/models/feed-view' import {FeedItemModel} from '../../../state/models/feed-view'
import {SharePostModel} from '../../../state/models/shell-ui'
import {Link} from '../util/Link' import {Link} from '../util/Link'
import {UserInfoText} from '../util/UserInfoText' import {UserInfoText} from '../util/UserInfoText'
import {PostMeta} from '../util/PostMeta' import {PostMeta} from '../util/PostMeta'
import {PostCtrls} from '../util/PostCtrls' import {PostCtrls} from '../util/PostCtrls'
import {RichText} from '../util/RichText' import {RichText} from '../util/RichText'
import Toast from '../util/Toast'
import {UserAvatar} from '../util/UserAvatar' import {UserAvatar} from '../util/UserAvatar'
import {s, colors} from '../../lib/styles' import {s, colors} from '../../lib/styles'
import {useStores} from '../../../state' import {useStores} from '../../../state'
@ -21,6 +21,7 @@ export const FeedItem = observer(function FeedItem({
item: FeedItemModel item: FeedItemModel
}) { }) {
const store = useStores() const store = useStores()
const [deleted, setDeleted] = useState(false)
const record = item.record as unknown as PostType.Record const record = item.record as unknown as PostType.Record
const itemHref = useMemo(() => { const itemHref = useMemo(() => {
const urip = new AtUri(item.uri) const urip = new AtUri(item.uri)
@ -57,8 +58,25 @@ export const FeedItem = observer(function FeedItem({
.toggleDownvote() .toggleDownvote()
.catch(e => console.error('Failed to toggle downvote', record, e)) .catch(e => console.error('Failed to toggle downvote', record, e))
} }
const onPressShare = (uri: string) => { const onDeletePost = () => {
store.shell.openModal(new SharePostModel(uri)) item.delete().then(
() => {
setDeleted(true)
Toast.show('Post deleted', {
position: Toast.positions.TOP,
})
},
e => {
console.error(e)
Toast.show('Failed to delete post, please try again', {
position: Toast.positions.TOP,
})
},
)
}
if (deleted) {
return <View />
} }
return ( return (
@ -107,6 +125,8 @@ export const FeedItem = observer(function FeedItem({
authorHandle={item.author.handle} authorHandle={item.author.handle}
authorDisplayName={item.author.displayName} authorDisplayName={item.author.displayName}
timestamp={item.indexedAt} timestamp={item.indexedAt}
isAuthor={item.author.did === store.me.did}
onDeletePost={onDeletePost}
/> />
{replyHref !== '' && ( {replyHref !== '' && (
<View style={[s.flexRow, s.mb5, {alignItems: 'center'}]}> <View style={[s.flexRow, s.mb5, {alignItems: 'center'}]}>

View File

@ -13,7 +13,7 @@ import RootSiblings from 'react-native-root-siblings'
import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome' import {FontAwesomeIcon} from '@fortawesome/react-native-fontawesome'
import {colors} from '../../lib/styles' import {colors} from '../../lib/styles'
import {useStores} from '../../../state' import {useStores} from '../../../state'
import {SharePostModel} from '../../../state/models/shell-ui' import {SharePostModel, ConfirmModel} from '../../../state/models/shell-ui'
export interface DropdownItem { export interface DropdownItem {
icon?: IconProp icon?: IconProp
@ -69,11 +69,15 @@ export function PostDropdownBtn({
children, children,
itemHref, itemHref,
itemTitle, itemTitle,
isAuthor,
onDeletePost,
}: { }: {
style?: StyleProp<ViewStyle> style?: StyleProp<ViewStyle>
children?: React.ReactNode children?: React.ReactNode
itemHref: string itemHref: string
itemTitle: string itemTitle: string
isAuthor: boolean
onDeletePost: () => void
}) { }) {
const store = useStores() const store = useStores()
@ -92,7 +96,22 @@ export function PostDropdownBtn({
store.shell.openModal(new SharePostModel(itemHref)) store.shell.openModal(new SharePostModel(itemHref))
}, },
}, },
] isAuthor
? {
icon: ['far', 'trash-can'],
label: 'Delete post',
onPress() {
store.shell.openModal(
new ConfirmModel(
'Delete this post?',
'Are you sure? This can not be undone.',
onDeletePost,
),
)
},
}
: undefined,
].filter(Boolean) as DropdownItem[]
return ( return (
<DropdownBtn style={style} items={dropdownItems} menuWidth={200}> <DropdownBtn style={style} items={dropdownItems} menuWidth={200}>

View File

@ -13,6 +13,8 @@ interface PostMetaOpts {
authorHandle: string authorHandle: string
authorDisplayName: string | undefined authorDisplayName: string | undefined
timestamp: string timestamp: string
isAuthor: boolean
onDeletePost: () => void
} }
export function PostMeta(opts: PostMetaOpts) { export function PostMeta(opts: PostMetaOpts) {
@ -48,7 +50,9 @@ export function PostMeta(opts: PostMetaOpts) {
<PostDropdownBtn <PostDropdownBtn
style={styles.metaItem} style={styles.metaItem}
itemHref={opts.itemHref} itemHref={opts.itemHref}
itemTitle={opts.itemTitle}> itemTitle={opts.itemTitle}
isAuthor={opts.isAuthor}
onDeletePost={opts.onDeletePost}>
<FontAwesomeIcon icon="ellipsis-h" size={14} style={[s.mt2, s.mr5]} /> <FontAwesomeIcon icon="ellipsis-h" size={14} style={[s.mt2, s.mr5]} />
</PostDropdownBtn> </PostDropdownBtn>
</View> </View>

View File

@ -52,6 +52,7 @@ import {faUserCheck} from '@fortawesome/free-solid-svg-icons/faUserCheck'
import {faUserPlus} from '@fortawesome/free-solid-svg-icons/faUserPlus' import {faUserPlus} from '@fortawesome/free-solid-svg-icons/faUserPlus'
import {faUserXmark} from '@fortawesome/free-solid-svg-icons/faUserXmark' import {faUserXmark} from '@fortawesome/free-solid-svg-icons/faUserXmark'
import {faTicket} from '@fortawesome/free-solid-svg-icons/faTicket' import {faTicket} from '@fortawesome/free-solid-svg-icons/faTicket'
import {faTrashCan} from '@fortawesome/free-regular-svg-icons/faTrashCan'
import {faX} from '@fortawesome/free-solid-svg-icons/faX' import {faX} from '@fortawesome/free-solid-svg-icons/faX'
export function setup() { export function setup() {
@ -108,6 +109,7 @@ export function setup() {
faUserPlus, faUserPlus,
faUserXmark, faUserXmark,
faTicket, faTicket,
faTrashCan,
faX, faX,
) )
} }