docs: Add support for emoji reactions to posts. - #391
Conversation
| // 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 |
There was a problem hiding this comment.
| // If the LikeEntry has (isDeleted = false) then we put the corresponding | |
| // If the ReactEntry has (isDeleted = false) then we put the corresponding |
| for _, reactEntry := range bav.ReactionKeyToReactionEntry { | ||
|
|
||
| if reactEntry.isDeleted { | ||
| // If the LikeEntry has isDeleted=true then there's nothing to do because |
There was a problem hiding this comment.
| // 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 |
| 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 | ||
| } |
There was a problem hiding this comment.
We should move this to the DbAdapter
| 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) | ||
| } | ||
| } |
There was a problem hiding this comment.
write a function on DbAdapter to get Reactions for a post hash called GetReactionsForPost first and then we can simplify the below as follows
| 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()) | |
| } | |
| } |
| // 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[:]) { |
There was a problem hiding this comment.
I think we'll want to use bytes.Equal instead of reflect.DeepEqual here since we're comparing bytes after you use the [:] operator
| func (txnMeta *ReactTxindexMetadata) RawDecodeWithoutMetadata(blockHeight uint64, rr *bytes.Reader) error { | ||
| var err error | ||
|
|
||
| txnMeta.IsRemove, err = ReadBoolByte(rr) |
There was a problem hiding this comment.
I usually put isRemove as the last one. I think logically we would expect PostHashHex then EmojiReaction then IsRemove
| // Set to true when a user is requesting to "remove" a reaction. | ||
| IsRemove bool | ||
|
|
||
| // The Unicode for the emoji reaction. | ||
| EmojiReaction rune |
There was a problem hiding this comment.
nit: I would flip these two
| // 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)))...) |
There was a problem hiding this comment.
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.
| TransactionHash *BlockHash `pg:",pk,type:bytea"` | ||
| PostHash *BlockHash `pg:",type:bytea"` | ||
| IsRemove bool `pg:",use_zero"` | ||
| EmojiReaction rune `pg:",type:integer"` |
There was a problem hiding this comment.
Are you sure this is an integer? I just frankly don't know off the top of my head.
| } | ||
| return reacts | ||
| } | ||
|
|
There was a problem hiding this comment.
You'll also want to write a migration for postgres in the migrations directory
…ajdalani/core into docs-add-emoji-reactions
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: