Skip to content

docs: Add support for emoji reactions to posts. - #391

Draft
MichelMajdalani wants to merge 3 commits into
deso-protocol:mainfrom
MichelMajdalani:docs-add-emoji-reactions
Draft

docs: Add support for emoji reactions to posts.#391
MichelMajdalani wants to merge 3 commits into
deso-protocol:mainfrom
MichelMajdalani:docs-add-emoji-reactions

Conversation

@MichelMajdalani

Copy link
Copy Markdown
Contributor

What is done?
As part of the documentation (walkthrough) for a first core change, I added the required code changes a new user needs to perform to add support to emoji reactions to posts.

Why is it done?
The documentation for making the first core change is missing in the docs.

How is it tested?
This is still WIP. It hasn't been tested.

Linked discussion:

Comment thread lib/block_view_flush.go
// If the LikeEntry has isDeleted=true then there's nothing to do because
// we already deleted the entry above.
} else {
// If the LikeEntry has (isDeleted = false) then we put the corresponding

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If the LikeEntry has (isDeleted = false) then we put the corresponding
// If the ReactEntry has (isDeleted = false) then we put the corresponding

Comment thread lib/block_view_flush.go
for _, reactEntry := range bav.ReactionKeyToReactionEntry {

if reactEntry.isDeleted {
// If the LikeEntry has isDeleted=true then there's nothing to do because

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
// If the LikeEntry has isDeleted=true then there's nothing to do because
// If the ReactEntry has isDeleted=true then there's nothing to do because

Comment thread lib/block_view_reaction.go Outdated
Comment on lines +22 to +26
if bav.Postgres != nil {
reactionExists = bav.Postgres.GetReaction(reactionKey.ReactorPubKey[:], &reactionKey.ReactedPostHash, reactionKey.ReactEmoji) != nil
} else {
reactionExists = DbGetReactorPubKeyToPostHashMapping(bav.Handle, reactionKey.ReactorPubKey[:], reactionKey.ReactedPostHash, reactionKey.ReactEmoji) != nil
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should move this to the DbAdapter

Comment thread lib/block_view_reaction.go Outdated
Comment on lines +75 to +98
if adapter.postgresDb != nil {
reactions := adapter.postgresDb.GetReactionsForPost(postHash)
for _, reaction := range reactions {
bav._setReactionEntryMappings(reaction.NewReactionEntry())
}
} else {
handle := adapter.badgerDb
dbPrefix := append([]byte{}, Prefixes.PrefixPostHashToReactorPubKey...)
dbPrefix = append(dbPrefix, postHash[:]...)
keysFound, _ := EnumerateKeysForPrefix(handle, dbPrefix)

// Iterate over all the db keys & values and load them into the view.
expectedKeyLength := 1 + HashSizeBytes + btcec.PubKeyBytesLenCompressed
for _, key := range keysFound {
// Sanity check that this is a reasonable key.
if len(key) != expectedKeyLength {
return nil, fmt.Errorf("UtxoView.GetReactuibsForPostHash: Invalid key length found: %d", len(key))
}

reactorPubKey := key[1+HashSizeBytes:]
reactKey := MakeReactionKey(reactorPubKey, *postHash, reactionEmoji)
bav._getReactionEntryForReactionKey(&reactKey)
}
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

write a function on DbAdapter to get Reactions for a post hash called GetReactionsForPost first and then we can simplify the below as follows

Suggested change
if adapter.postgresDb != nil {
reactions := adapter.postgresDb.GetReactionsForPost(postHash)
for _, reaction := range reactions {
bav._setReactionEntryMappings(reaction.NewReactionEntry())
}
} else {
handle := adapter.badgerDb
dbPrefix := append([]byte{}, Prefixes.PrefixPostHashToReactorPubKey...)
dbPrefix = append(dbPrefix, postHash[:]...)
keysFound, _ := EnumerateKeysForPrefix(handle, dbPrefix)
// Iterate over all the db keys & values and load them into the view.
expectedKeyLength := 1 + HashSizeBytes + btcec.PubKeyBytesLenCompressed
for _, key := range keysFound {
// Sanity check that this is a reasonable key.
if len(key) != expectedKeyLength {
return nil, fmt.Errorf("UtxoView.GetReactuibsForPostHash: Invalid key length found: %d", len(key))
}
reactorPubKey := key[1+HashSizeBytes:]
reactKey := MakeReactionKey(reactorPubKey, *postHash, reactionEmoji)
bav._getReactionEntryForReactionKey(&reactKey)
}
}
reactions := adapter.GetReactionsForPost(postHash)
for _, reaction := range reactions {
reactKey := MakeReactionKey(reaction.ReactorPubKey, reaction.PostHash)
if _, exists := bav.ReactionKeyToReactionEntry[reactKey]; !exists {
bav._setReactionEntryMappings(reaction.NewReactionEntry())
}
}

Comment thread lib/block_view_reaction.go Outdated
// Iterate over the view and create the final list to return.
var reactorPubKeys [][]byte
for _, reactionEntry := range bav.ReactionKeyToReactionEntry {
if !reactionEntry.isDeleted && reflect.DeepEqual(reactionEntry.ReactedPostHash[:], postHash[:]) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we'll want to use bytes.Equal instead of reflect.DeepEqual here since we're comparing bytes after you use the [:] operator

Comment thread lib/db_utils.go
func (txnMeta *ReactTxindexMetadata) RawDecodeWithoutMetadata(blockHeight uint64, rr *bytes.Reader) error {
var err error

txnMeta.IsRemove, err = ReadBoolByte(rr)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I usually put isRemove as the last one. I think logically we would expect PostHashHex then EmojiReaction then IsRemove

Comment thread lib/network.go Outdated
Comment on lines +3411 to +3415
// Set to true when a user is requesting to "remove" a reaction.
IsRemove bool

// The Unicode for the emoji reaction.
EmojiReaction rune

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: I would flip these two

Comment thread lib/network.go
Comment on lines +3434 to +3440
// Add IsRemove
data = append(data, BoolToByte(txnData.IsRemove))

// Add EmojiReaction.
// It is possible for a single character to be encoded with different code point sequences.
// By normalizing the Unicode (NFC), we ensure that a character will have a unique code point sequence.
data = append(data, norm.NFC.Bytes([]byte(string(txnData.EmojiReaction)))...)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would flip these two. Also, will the EmojiReaction always be the same exact length of bytes? if not, we will want to prepend the number of bytes.

Comment thread lib/postgres.go Outdated
TransactionHash *BlockHash `pg:",pk,type:bytea"`
PostHash *BlockHash `pg:",type:bytea"`
IsRemove bool `pg:",use_zero"`
EmojiReaction rune `pg:",type:integer"`

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Are you sure this is an integer? I just frankly don't know off the top of my head.

Comment thread lib/postgres.go
}
return reacts
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You'll also want to write a migration for postgres in the migrations directory

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants