Skip to content

Commit

Permalink
Implement NIP-09 Event Deletion
Browse files Browse the repository at this point in the history
  • Loading branch information
prolic committed Nov 30, 2024
1 parent 9db642f commit b856a9c
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 10 deletions.
44 changes: 44 additions & 0 deletions resources/qml/content/Components/PostContent.ui.qml
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,18 @@ Pane {
}
}

Button {
flat: true
icon.source: "qrc:/icons/delete.svg"
icon.width: 20
icon.height: 20
implicitWidth: 36
implicitHeight: 36
padding: 8
icon.color: Material.secondaryTextColor
onClicked: deleteDialog.open()
}

Item { Layout.fillWidth: true }
}

Expand All @@ -147,4 +159,36 @@ Pane {
Layout.topMargin: Constants.spacing_xs
}
}

Dialog {
id: deleteDialog
title: "Delete Post"
modal: true
standardButtons: Dialog.Ok | Dialog.Cancel
anchors.centerIn: parent
width: 300

ColumnLayout {
spacing: Constants.spacing_m
width: parent.width

Text {
text: "Are you sure you want to delete this post?"
color: Material.foreground
wrapMode: Text.Wrap
Layout.fillWidth: true
}

TextField {
id: reasonField
placeholderText: "Reason for deletion (optional)"
Layout.fillWidth: true
}
}

onAccepted: {
deleteEvent(post.id, reasonField.text)
reasonField.text = "" // Clear the field after use
}
}
}
21 changes: 18 additions & 3 deletions src/Futr.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,9 @@ import Logging
import KeyMgmt (Account(..), AccountId(..), KeyMgmt, KeyMgmtState(..))
import Nostr
import Nostr.Bech32
import Nostr.Event (createComment, createFollowList, createQuoteRepost, createRepost, createRumor, createShortTextNote)
import Nostr.Event ( createComment, createEventDeletion, createFollowList
, createQuoteRepost, createRepost, createRumor, createShortTextNote
)
import Nostr.Keys (PubKeyXO, derivePublicKeyXO, keyPairToPubKeyXO, secKeyToKeyPair)
import Nostr.GiftWrap
import Nostr.Publisher
Expand Down Expand Up @@ -76,6 +78,7 @@ data Futr :: Effect where
Repost :: EventId -> Futr m ()
QuoteRepost :: EventId -> Text -> Futr m ()
Comment :: EventId -> Text -> Futr m ()
DeleteEvent :: EventId -> Text -> Futr m ()


-- | Dispatch type for Futr effect.
Expand Down Expand Up @@ -288,7 +291,7 @@ runFutr = interpret $ \_ -> \case
Just s -> publishToOutbox s
Nothing -> logError "Failed to sign quote repost"

Comment eid comment -> do
Comment eid comment' -> do
st <- get @AppState
case keyPair st of
Nothing -> logError "No keypair found"
Expand All @@ -298,7 +301,7 @@ runFutr = interpret $ \_ -> \case
case mEvent of
Nothing -> logError $ "Failed to fetch event " <> pack (show eid)
Just (event, _) -> do
let c = createComment event comment (Right eid) Nothing Nothing (keyPairToPubKeyXO kp) now
let c = createComment event comment' (Right eid) Nothing Nothing (keyPairToPubKeyXO kp) now
signed <- signEvent c kp
case signed of
Just s -> do
Expand All @@ -319,6 +322,18 @@ runFutr = interpret $ \_ -> \case
Nothing -> return ()
Nothing -> logError "Failed to sign comment"

DeleteEvent eid reason -> do
st <- get @AppState
case keyPair st of
Nothing -> logError "No keypair found"
Just kp -> do
now <- getCurrentTime
let deletion = createEventDeletion [eid] reason (keyPairToPubKeyXO kp) now
signed <- signEvent deletion kp
case signed of
Just s -> publishToOutbox s
Nothing -> logError "Failed to sign event deletion"


-- Helper function to fetch an event
fetchEvent :: EventId -> FutrEff es => Eff es (Maybe (Event, [RelayURI]))
Expand Down
21 changes: 15 additions & 6 deletions src/Nostr/Subscription.hs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import Data.Aeson (eitherDecode)
import Data.ByteString qualified as BS
import Data.ByteString.Base16 qualified as B16
import Data.ByteString.Lazy (fromStrict)
import Data.List (nubBy, sortBy)
import Data.List (sortBy)
import Data.Map.Strict qualified as Map
import Data.Ord (comparing)
import Data.Text (pack, unpack, isInfixOf)
Expand Down Expand Up @@ -34,7 +34,6 @@ import Nostr.Types ( Event(..), EventId(..), Filter, Kind(..), Relay(..)
import Nostr.Types qualified as NT
import Nostr.Util
import RelayMgmt
import TimeFormatter (Language(..), formatDateTime)
import Types hiding (Repost, ShortTextNote)
import Types qualified as Types

Expand Down Expand Up @@ -117,7 +116,7 @@ handleEvent' event' r = do
-- Check for q-tag to identify quote reposts
let qTags = [t | t@(QTag _ _ _) <- tags event']
case qTags of
(QTag quotedId mRelay _:_) -> do
(QTag quotedId _ _:_) -> do
-- Verify content contains NIP-21 identifier
let contentText = content event'
hasNIP21 = "nostr:" `isInfixOf` contentText &&
Expand All @@ -144,8 +143,7 @@ handleEvent' event' r = do
(byteStringToHex $ getEventId (eventId event'))
pure emptyUpdates

[] -> do
-- Regular short text note
_ -> do
let note = Types.Post
{ postId = eventId event'
, postType = Types.ShortTextNote
Expand Down Expand Up @@ -205,7 +203,18 @@ handleEvent' event' r = do
pure emptyUpdates

EventDeletion -> do
pure emptyUpdates
let eventIdsToDelete = [eid | ETag eid _ _ <- tags event']

modify @AppState $ \st -> st
{ events = foldr Map.delete (events st) eventIdsToDelete
, posts = Map.map (filter (\p -> postId p `notElem` eventIdsToDelete)) (posts st)
, chats = Map.map (filter (\dm -> chatMessageId dm `notElem` eventIdsToDelete)) (chats st)
}

pure $ emptyUpdates
{ postsChanged = True
, privateMessagesChanged = True
}

Metadata -> do
case eitherDecode (fromStrict $ encodeUtf8 $ content event') of
Expand Down
7 changes: 6 additions & 1 deletion src/UI.hs
Original file line number Diff line number Diff line change
Expand Up @@ -449,7 +449,12 @@ runUI = interpret $ \_ -> \case
defMethod' "comment" $ \_ eid input -> runE $ do -- NIP-22 comment
let unquoted = read (unpack eid) :: String
let eid' = read unquoted :: EventId
comment eid' input
comment eid' input,

defMethod' "deleteEvent" $ \_ eid input -> runE $ do -- NIP-09 delete post
let unquoted = read (unpack eid) :: String
let eid' = read unquoted :: EventId
deleteEvent eid' input
]

rootObj <- newObject rootClass ()
Expand Down

0 comments on commit b856a9c

Please sign in to comment.