From a21741f4f81fc524a38c05448f2151a9653339f3 Mon Sep 17 00:00:00 2001 From: yo1110 Date: Thu, 10 Aug 2023 23:54:23 +0700 Subject: [PATCH] chore(social feed): disambiguate created_by (#666) * fix: handle ns more properly * wip: standarize userId for social_feeds * wip: standarize userId vs userAddress * fix: stadardize userId vs userAddress * fix: change id for proto field author_id * fix: proto gen * fix: revert proto field id for author_id --- api/feed/v1/feed.proto | 2 +- go/internal/indexerdb/feed.go | 2 +- go/internal/indexerhandler/feed.go | 2 +- go/pkg/feed/service.go | 2 +- go/pkg/feedpb/feed.pb.go | 82 +++++++++---------- packages/api/feed/v1/feed.ts | 16 ++-- .../SocialThread/SocialCommentCard.tsx | 8 +- .../SocialThread/SocialThreadCard.tsx | 10 +-- packages/components/socialFeed/utils.ts | 17 ++-- packages/hooks/feed/useFetchComments.ts | 47 +++++++++-- packages/hooks/feed/usePost.ts | 36 ++++---- .../FeedPostView/FeedPostViewScreen.tsx | 42 ++++++---- packages/utils/social-feed.ts | 2 +- 13 files changed, 149 insertions(+), 119 deletions(-) diff --git a/api/feed/v1/feed.proto b/api/feed/v1/feed.proto index ede210884e..7a63512a97 100644 --- a/api/feed/v1/feed.proto +++ b/api/feed/v1/feed.proto @@ -27,7 +27,7 @@ message Post { string metadata = 4; string parent_post_identifier = 5; uint32 sub_post_length = 6; - string created_by = 7; + string author_id = 7; int64 created_at = 8; int64 tip_amount = 10; repeated Reaction reactions = 9; diff --git a/go/internal/indexerdb/feed.go b/go/internal/indexerdb/feed.go index 47af40306d..0b43c618a2 100644 --- a/go/internal/indexerdb/feed.go +++ b/go/internal/indexerdb/feed.go @@ -17,7 +17,7 @@ type Post struct { IsBot bool `gorm:"default:false"` Metadata ObjectJSONB `gorm:"type:jsonb;default:'{}'"` UserReactions ObjectJSONB `gorm:"type:jsonb;default:'{}'"` - CreatedBy networks.UserID `gorm:"index"` + AuthorId networks.UserID `gorm:"index"` CreatedAt int64 IsDeleted bool IsFree bool diff --git a/go/internal/indexerhandler/feed.go b/go/internal/indexerhandler/feed.go index 7ef3fb7320..63c26e0b3b 100644 --- a/go/internal/indexerhandler/feed.go +++ b/go/internal/indexerhandler/feed.go @@ -181,7 +181,7 @@ func (h *Handler) createPost( Category: createPostMsg.Category, Metadata: metadataJSON, UserReactions: map[string]interface{}{}, - CreatedBy: h.config.Network.UserID(execMsg.Sender), + AuthorId: h.config.Network.UserID(execMsg.Sender), CreatedAt: createdAt.Unix(), IsBot: isBot, } diff --git a/go/pkg/feed/service.go b/go/pkg/feed/service.go index 7e1825f62b..0c19fdc744 100644 --- a/go/pkg/feed/service.go +++ b/go/pkg/feed/service.go @@ -168,7 +168,7 @@ func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*fee Metadata: string(metadata), ParentPostIdentifier: dbPost.ParentPostIdentifier, SubPostLength: dbPost.SubPostLength, - CreatedBy: string(dbPost.CreatedBy), + AuthorId: string(dbPost.AuthorId), CreatedAt: dbPost.CreatedAt, Reactions: reactions, TipAmount: dbPost.TipAmount, diff --git a/go/pkg/feedpb/feed.pb.go b/go/pkg/feedpb/feed.pb.go index fd0c58885f..d3458ca541 100644 --- a/go/pkg/feedpb/feed.pb.go +++ b/go/pkg/feedpb/feed.pb.go @@ -180,7 +180,7 @@ type Post struct { Metadata string `protobuf:"bytes,4,opt,name=metadata,proto3" json:"metadata,omitempty"` ParentPostIdentifier string `protobuf:"bytes,5,opt,name=parent_post_identifier,json=parentPostIdentifier,proto3" json:"parent_post_identifier,omitempty"` SubPostLength uint32 `protobuf:"varint,6,opt,name=sub_post_length,json=subPostLength,proto3" json:"sub_post_length,omitempty"` - CreatedBy string `protobuf:"bytes,7,opt,name=created_by,json=createdBy,proto3" json:"created_by,omitempty"` + AuthorId string `protobuf:"bytes,7,opt,name=author_id,json=authorId,proto3" json:"author_id,omitempty"` CreatedAt int64 `protobuf:"varint,8,opt,name=created_at,json=createdAt,proto3" json:"created_at,omitempty"` TipAmount int64 `protobuf:"varint,10,opt,name=tip_amount,json=tipAmount,proto3" json:"tip_amount,omitempty"` Reactions []*Reaction `protobuf:"bytes,9,rep,name=reactions,proto3" json:"reactions,omitempty"` @@ -260,9 +260,9 @@ func (x *Post) GetSubPostLength() uint32 { return 0 } -func (x *Post) GetCreatedBy() string { +func (x *Post) GetAuthorId() string { if x != nil { - return x.CreatedBy + return x.AuthorId } return "" } @@ -482,7 +482,7 @@ var file_feed_v1_feed_proto_rawDesc = []byte{ 0x08, 0x52, 0x65, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x12, 0x12, 0x0a, 0x04, 0x69, 0x63, 0x6f, 0x6e, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x69, 0x63, 0x6f, 0x6e, 0x12, 0x14, 0x0a, 0x05, 0x63, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x63, 0x6f, - 0x75, 0x6e, 0x74, 0x22, 0xe9, 0x02, 0x0a, 0x04, 0x50, 0x6f, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, + 0x75, 0x6e, 0x74, 0x22, 0xe7, 0x02, 0x0a, 0x04, 0x50, 0x6f, 0x73, 0x74, 0x12, 0x1a, 0x0a, 0x08, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x79, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x08, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x79, 0x12, 0x1d, 0x0a, 0x0a, 0x69, 0x73, 0x5f, 0x64, 0x65, 0x6c, 0x65, 0x74, 0x65, 0x64, 0x18, 0x02, 0x20, 0x01, 0x28, 0x08, 0x52, 0x09, 0x69, 0x73, @@ -496,43 +496,43 @@ var file_feed_v1_feed_proto_rawDesc = []byte{ 0x64, 0x65, 0x6e, 0x74, 0x69, 0x66, 0x69, 0x65, 0x72, 0x12, 0x26, 0x0a, 0x0f, 0x73, 0x75, 0x62, 0x5f, 0x70, 0x6f, 0x73, 0x74, 0x5f, 0x6c, 0x65, 0x6e, 0x67, 0x74, 0x68, 0x18, 0x06, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x0d, 0x73, 0x75, 0x62, 0x50, 0x6f, 0x73, 0x74, 0x4c, 0x65, 0x6e, 0x67, 0x74, - 0x68, 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x62, 0x79, 0x18, - 0x07, 0x20, 0x01, 0x28, 0x09, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x42, 0x79, - 0x12, 0x1d, 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x08, - 0x20, 0x01, 0x28, 0x03, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, - 0x1d, 0x0a, 0x0a, 0x74, 0x69, 0x70, 0x5f, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x0a, 0x20, - 0x01, 0x28, 0x03, 0x52, 0x09, 0x74, 0x69, 0x70, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x2f, - 0x0a, 0x09, 0x72, 0x65, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x09, 0x20, 0x03, 0x28, - 0x0b, 0x32, 0x11, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x65, 0x61, 0x63, - 0x74, 0x69, 0x6f, 0x6e, 0x52, 0x09, 0x72, 0x65, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, - 0x78, 0x0a, 0x0a, 0x50, 0x6f, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x12, 0x0a, - 0x04, 0x75, 0x73, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x73, 0x65, - 0x72, 0x12, 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x6e, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, - 0x03, 0x28, 0x09, 0x52, 0x08, 0x6d, 0x65, 0x6e, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x1e, 0x0a, - 0x0a, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, - 0x0d, 0x52, 0x0a, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x69, 0x65, 0x73, 0x12, 0x1a, 0x0a, - 0x08, 0x68, 0x61, 0x73, 0x68, 0x74, 0x61, 0x67, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, - 0x08, 0x68, 0x61, 0x73, 0x68, 0x74, 0x61, 0x67, 0x73, 0x22, 0x69, 0x0a, 0x0c, 0x50, 0x6f, 0x73, - 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2b, 0x0a, 0x06, 0x66, 0x69, 0x6c, - 0x74, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x66, 0x65, 0x65, 0x64, - 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x52, 0x06, - 0x66, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, - 0x02, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x16, 0x0a, 0x06, - 0x6f, 0x66, 0x66, 0x73, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x6f, 0x66, - 0x66, 0x73, 0x65, 0x74, 0x22, 0x34, 0x0a, 0x0d, 0x50, 0x6f, 0x73, 0x74, 0x73, 0x52, 0x65, 0x73, - 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x12, 0x23, 0x0a, 0x05, 0x70, 0x6f, 0x73, 0x74, 0x73, 0x18, 0x01, - 0x20, 0x03, 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x50, - 0x6f, 0x73, 0x74, 0x52, 0x05, 0x70, 0x6f, 0x73, 0x74, 0x73, 0x32, 0x83, 0x01, 0x0a, 0x0b, 0x46, - 0x65, 0x65, 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x36, 0x0a, 0x05, 0x50, 0x6f, - 0x73, 0x74, 0x73, 0x12, 0x15, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, - 0x73, 0x74, 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x66, 0x65, 0x65, - 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, - 0x73, 0x65, 0x12, 0x3c, 0x0a, 0x07, 0x49, 0x50, 0x46, 0x53, 0x4b, 0x65, 0x79, 0x12, 0x17, 0x2e, - 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x49, 0x50, 0x46, 0x53, 0x4b, 0x65, 0x79, 0x52, - 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, - 0x2e, 0x49, 0x50, 0x46, 0x53, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, - 0x42, 0x0a, 0x5a, 0x08, 0x2e, 0x2f, 0x66, 0x65, 0x65, 0x64, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, - 0x6f, 0x74, 0x6f, 0x33, + 0x68, 0x12, 0x1b, 0x0a, 0x09, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x5f, 0x69, 0x64, 0x18, 0x07, + 0x20, 0x01, 0x28, 0x09, 0x52, 0x08, 0x61, 0x75, 0x74, 0x68, 0x6f, 0x72, 0x49, 0x64, 0x12, 0x1d, + 0x0a, 0x0a, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x5f, 0x61, 0x74, 0x18, 0x08, 0x20, 0x01, + 0x28, 0x03, 0x52, 0x09, 0x63, 0x72, 0x65, 0x61, 0x74, 0x65, 0x64, 0x41, 0x74, 0x12, 0x1d, 0x0a, + 0x0a, 0x74, 0x69, 0x70, 0x5f, 0x61, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x18, 0x0a, 0x20, 0x01, 0x28, + 0x03, 0x52, 0x09, 0x74, 0x69, 0x70, 0x41, 0x6d, 0x6f, 0x75, 0x6e, 0x74, 0x12, 0x2f, 0x0a, 0x09, + 0x72, 0x65, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x09, 0x20, 0x03, 0x28, 0x0b, 0x32, + 0x11, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x52, 0x65, 0x61, 0x63, 0x74, 0x69, + 0x6f, 0x6e, 0x52, 0x09, 0x72, 0x65, 0x61, 0x63, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x22, 0x78, 0x0a, + 0x0a, 0x50, 0x6f, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x12, 0x12, 0x0a, 0x04, 0x75, + 0x73, 0x65, 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x09, 0x52, 0x04, 0x75, 0x73, 0x65, 0x72, 0x12, + 0x1a, 0x0a, 0x08, 0x6d, 0x65, 0x6e, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x18, 0x02, 0x20, 0x03, 0x28, + 0x09, 0x52, 0x08, 0x6d, 0x65, 0x6e, 0x74, 0x69, 0x6f, 0x6e, 0x73, 0x12, 0x1e, 0x0a, 0x0a, 0x63, + 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x69, 0x65, 0x73, 0x18, 0x03, 0x20, 0x03, 0x28, 0x0d, 0x52, + 0x0a, 0x63, 0x61, 0x74, 0x65, 0x67, 0x6f, 0x72, 0x69, 0x65, 0x73, 0x12, 0x1a, 0x0a, 0x08, 0x68, + 0x61, 0x73, 0x68, 0x74, 0x61, 0x67, 0x73, 0x18, 0x04, 0x20, 0x03, 0x28, 0x09, 0x52, 0x08, 0x68, + 0x61, 0x73, 0x68, 0x74, 0x61, 0x67, 0x73, 0x22, 0x69, 0x0a, 0x0c, 0x50, 0x6f, 0x73, 0x74, 0x73, + 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x12, 0x2b, 0x0a, 0x06, 0x66, 0x69, 0x6c, 0x74, 0x65, + 0x72, 0x18, 0x01, 0x20, 0x01, 0x28, 0x0b, 0x32, 0x13, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, + 0x31, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x46, 0x69, 0x6c, 0x74, 0x65, 0x72, 0x52, 0x06, 0x66, 0x69, + 0x6c, 0x74, 0x65, 0x72, 0x12, 0x14, 0x0a, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x18, 0x02, 0x20, + 0x01, 0x28, 0x0d, 0x52, 0x05, 0x6c, 0x69, 0x6d, 0x69, 0x74, 0x12, 0x16, 0x0a, 0x06, 0x6f, 0x66, + 0x66, 0x73, 0x65, 0x74, 0x18, 0x03, 0x20, 0x01, 0x28, 0x0d, 0x52, 0x06, 0x6f, 0x66, 0x66, 0x73, + 0x65, 0x74, 0x22, 0x34, 0x0a, 0x0d, 0x50, 0x6f, 0x73, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, + 0x6e, 0x73, 0x65, 0x12, 0x23, 0x0a, 0x05, 0x70, 0x6f, 0x73, 0x74, 0x73, 0x18, 0x01, 0x20, 0x03, + 0x28, 0x0b, 0x32, 0x0d, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x73, + 0x74, 0x52, 0x05, 0x70, 0x6f, 0x73, 0x74, 0x73, 0x32, 0x83, 0x01, 0x0a, 0x0b, 0x46, 0x65, 0x65, + 0x64, 0x53, 0x65, 0x72, 0x76, 0x69, 0x63, 0x65, 0x12, 0x36, 0x0a, 0x05, 0x50, 0x6f, 0x73, 0x74, + 0x73, 0x12, 0x15, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x73, 0x74, + 0x73, 0x52, 0x65, 0x71, 0x75, 0x65, 0x73, 0x74, 0x1a, 0x16, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, + 0x76, 0x31, 0x2e, 0x50, 0x6f, 0x73, 0x74, 0x73, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, + 0x12, 0x3c, 0x0a, 0x07, 0x49, 0x50, 0x46, 0x53, 0x4b, 0x65, 0x79, 0x12, 0x17, 0x2e, 0x66, 0x65, + 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x49, 0x50, 0x46, 0x53, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x71, + 0x75, 0x65, 0x73, 0x74, 0x1a, 0x18, 0x2e, 0x66, 0x65, 0x65, 0x64, 0x2e, 0x76, 0x31, 0x2e, 0x49, + 0x50, 0x46, 0x53, 0x4b, 0x65, 0x79, 0x52, 0x65, 0x73, 0x70, 0x6f, 0x6e, 0x73, 0x65, 0x42, 0x0a, + 0x5a, 0x08, 0x2e, 0x2f, 0x66, 0x65, 0x65, 0x64, 0x70, 0x62, 0x62, 0x06, 0x70, 0x72, 0x6f, 0x74, + 0x6f, 0x33, } var ( diff --git a/packages/api/feed/v1/feed.ts b/packages/api/feed/v1/feed.ts index 3799c5e9c9..601570ca35 100644 --- a/packages/api/feed/v1/feed.ts +++ b/packages/api/feed/v1/feed.ts @@ -26,7 +26,7 @@ export interface Post { metadata: string; parentPostIdentifier: string; subPostLength: number; - createdBy: string; + authorId: string; createdAt: number; tipAmount: number; reactions: Reaction[]; @@ -209,7 +209,7 @@ function createBasePost(): Post { metadata: "", parentPostIdentifier: "", subPostLength: 0, - createdBy: "", + authorId: "", createdAt: 0, tipAmount: 0, reactions: [], @@ -236,8 +236,8 @@ export const Post = { if (message.subPostLength !== 0) { writer.uint32(48).uint32(message.subPostLength); } - if (message.createdBy !== "") { - writer.uint32(58).string(message.createdBy); + if (message.authorId !== "") { + writer.uint32(58).string(message.authorId); } if (message.createdAt !== 0) { writer.uint32(64).int64(message.createdAt); @@ -277,7 +277,7 @@ export const Post = { message.subPostLength = reader.uint32(); break; case 7: - message.createdBy = reader.string(); + message.authorId = reader.string(); break; case 8: message.createdAt = longToNumber(reader.int64() as Long); @@ -304,7 +304,7 @@ export const Post = { metadata: isSet(object.metadata) ? String(object.metadata) : "", parentPostIdentifier: isSet(object.parentPostIdentifier) ? String(object.parentPostIdentifier) : "", subPostLength: isSet(object.subPostLength) ? Number(object.subPostLength) : 0, - createdBy: isSet(object.createdBy) ? String(object.createdBy) : "", + authorId: isSet(object.authorId) ? String(object.authorId) : "", createdAt: isSet(object.createdAt) ? Number(object.createdAt) : 0, tipAmount: isSet(object.tipAmount) ? Number(object.tipAmount) : 0, reactions: Array.isArray(object?.reactions) ? object.reactions.map((e: any) => Reaction.fromJSON(e)) : [], @@ -319,7 +319,7 @@ export const Post = { message.metadata !== undefined && (obj.metadata = message.metadata); message.parentPostIdentifier !== undefined && (obj.parentPostIdentifier = message.parentPostIdentifier); message.subPostLength !== undefined && (obj.subPostLength = Math.round(message.subPostLength)); - message.createdBy !== undefined && (obj.createdBy = message.createdBy); + message.authorId !== undefined && (obj.authorId = message.authorId); message.createdAt !== undefined && (obj.createdAt = Math.round(message.createdAt)); message.tipAmount !== undefined && (obj.tipAmount = Math.round(message.tipAmount)); if (message.reactions) { @@ -338,7 +338,7 @@ export const Post = { message.metadata = object.metadata ?? ""; message.parentPostIdentifier = object.parentPostIdentifier ?? ""; message.subPostLength = object.subPostLength ?? 0; - message.createdBy = object.createdBy ?? ""; + message.authorId = object.authorId ?? ""; message.createdAt = object.createdAt ?? 0; message.tipAmount = object.tipAmount ?? 0; message.reactions = object.reactions?.map((e) => Reaction.fromPartial(e)) || []; diff --git a/packages/components/socialFeed/SocialThread/SocialCommentCard.tsx b/packages/components/socialFeed/SocialThread/SocialCommentCard.tsx index c2d771641f..9f2ebf6d73 100644 --- a/packages/components/socialFeed/SocialThread/SocialCommentCard.tsx +++ b/packages/components/socialFeed/SocialThread/SocialCommentCard.tsx @@ -95,7 +95,7 @@ export const SocialCommentCard: React.FC = ({ const wallet = useSelectedWallet(); const selectedNetwork = useSelectedNetworkInfo(); const selectedNetworkId = selectedNetwork?.id || ""; - const [, userAddress] = parseUserId(localComment.createdBy); + const [, authorAddress] = parseUserId(localComment.authorId); const { data, refetch, fetchNextPage, hasNextPage, isFetching } = useFetchComments({ parentId: localComment.identifier, @@ -124,7 +124,7 @@ export const SocialCommentCard: React.FC = ({ ); const moreCommentsCount = localComment.subPostLength - comments.length; const metadata = JSON.parse(localComment.metadata); - const authorNSInfo = useNSUserInfo(localComment.createdBy); + const authorNSInfo = useNSUserInfo(localComment.authorId); const userInfo = useNSUserInfo(wallet?.userId); const username = authorNSInfo?.metadata?.tokenId ? tinyAddress(authorNSInfo?.metadata?.tokenId || "", 19) @@ -269,8 +269,8 @@ export const SocialCommentCard: React.FC = ({ {/*====== Card Header */} diff --git a/packages/components/socialFeed/SocialThread/SocialThreadCard.tsx b/packages/components/socialFeed/SocialThread/SocialThreadCard.tsx index 26d4fac814..81e8e2d0df 100644 --- a/packages/components/socialFeed/SocialThread/SocialThreadCard.tsx +++ b/packages/components/socialFeed/SocialThread/SocialThreadCard.tsx @@ -72,12 +72,12 @@ export const SocialThreadCard: React.FC<{ const wallet = useSelectedWallet(); const selectedNetworkInfo = useSelectedNetworkInfo(); const selectedNetworkId = selectedNetworkInfo?.id; - const authorNSInfo = useNSUserInfo(localPost.createdBy); - const [, userAddress] = parseUserId(localPost.createdBy); + const authorNSInfo = useNSUserInfo(localPost.authorId); + const [, authorAddress] = parseUserId(localPost.authorId); const userInfo = useNSUserInfo(wallet?.userId); const navigation = useAppNavigation(); const metadata: SocialFeedMetadata = JSON.parse(localPost.metadata); - const username = authorNSInfo?.metadata?.tokenId || userAddress; + const username = authorNSInfo?.metadata?.tokenId || authorAddress; //TODO: Handle this later // const communityHashtag = useMemo(() => { @@ -183,8 +183,8 @@ export const SocialThreadCard: React.FC<{ > {/*====== Card Header */} diff --git a/packages/components/socialFeed/utils.ts b/packages/components/socialFeed/utils.ts index 97d8ed4d80..b6585a3f2a 100644 --- a/packages/components/socialFeed/utils.ts +++ b/packages/components/socialFeed/utils.ts @@ -1,14 +1,7 @@ import { Post, Reaction } from "../../api/feed/v1/feed"; import { getUserId } from "../../networks"; -export const decodeGnoPost = ( - networkId: string, - postData: string, - // FIXME: due data inconsistency when we fetch data from chain - // for individual post (contains user address) vs from indexer for post list (contains user id) - // this args is for adapting the scenario but we should fix properly by standardizing output when get from chain vs from indexer - includingNetwork = true -): Post => { +export const decodeGnoPost = (networkId: string, postData: string): Post => { const buf = Buffer.from(postData, "base64"); let offset = 0; @@ -39,7 +32,7 @@ export const decodeGnoPost = ( const addrLen = buf.readUInt16BE(offset); offset += 2; - const createdBy = buf.slice(offset, offset + addrLen).toString(); + const authorAddress = buf.slice(offset, offset + addrLen).toString(); offset += addrLen; const createdAt = buf.readUInt32BE(offset); @@ -74,11 +67,11 @@ export const decodeGnoPost = ( const post: Post = { category, isDeleted: false, - identifier: "" + identifier, + identifier: identifier ? "" + identifier : "", metadata, - parentPostIdentifier: "" + parentPostIdentifier, + parentPostIdentifier: parentPostIdentifier ? "" + parentPostIdentifier : "", subPostLength: subpostIDs.length, - createdBy: includingNetwork ? getUserId(networkId, createdBy) : createdBy, + authorId: getUserId(networkId, authorAddress), createdAt: +createdAt * 1000, tipAmount: 0, reactions, diff --git a/packages/hooks/feed/useFetchComments.ts b/packages/hooks/feed/useFetchComments.ts index a5eaee0ff6..61bd2f7ce1 100644 --- a/packages/hooks/feed/useFetchComments.ts +++ b/packages/hooks/feed/useFetchComments.ts @@ -1,15 +1,21 @@ import { GnoJSONRPCProvider } from "@gnolang/gno-js-client"; import { useInfiniteQuery } from "@tanstack/react-query"; -import { Post } from "../../api/feed/v1/feed"; import { nonSigningSocialFeedClient } from "../../client-creators/socialFeedClient"; import { GNO_SOCIAL_FEEDS_PKG_PATH, TERITORI_FEED_ID, } from "../../components/socialFeed/const"; import { decodeGnoPost } from "../../components/socialFeed/utils"; -import { PostResult } from "../../contracts-clients/teritori-social-feed/TeritoriSocialFeed.types"; -import { GnoNetworkInfo, NetworkKind } from "../../networks"; +import { + PostResult, + Reaction, +} from "../../contracts-clients/teritori-social-feed/TeritoriSocialFeed.types"; +import { + GnoNetworkInfo, + NetworkKind, + parseNetworkObjectId, +} from "../../networks"; import { extractGnoString } from "../../utils/gno"; import { useSelectedNetworkInfo } from "../useSelectedNetwork"; @@ -17,6 +23,10 @@ export type FetchCommentResponse = { list: PostResult[]; } | null; +export type PostResultWithCreatedAt = PostResult & { + created_at: number; +}; + export const combineFetchCommentPages = (pages: FetchCommentResponse[]) => pages.reduce( (acc: PostResult[], page) => [...acc, ...(page?.list || [])], @@ -51,21 +61,38 @@ const fetchTeritoriComments = async ( const fetchGnoComments = async ( selectedNetwork: GnoNetworkInfo, parentId: string -) => { +): Promise => { const provider = new GnoJSONRPCProvider(selectedNetwork.endpoint); const output = await provider.evaluateExpression( GNO_SOCIAL_FEEDS_PKG_PATH, `GetComments(${TERITORI_FEED_ID}, ${parentId})` ); - const posts: Post[] = []; + const posts: PostResultWithCreatedAt[] = []; const outputStr = extractGnoString(output); for (const postData of outputStr.split(",")) { const post = decodeGnoPost(selectedNetwork.id, postData); - posts.push(post); + const [, creatorAddress] = parseNetworkObjectId(post.authorId); + + posts.push({ + identifier: post.identifier, + parent_post_identifier: post.parentPostIdentifier, + category: post.category, + metadata: post.metadata, + reactions: post.reactions as Reaction[], + user_reactions: [], + post_by: creatorAddress, + deleted: post.isDeleted, + sub_post_length: post.subPostLength, + tip_amount: "" + post.tipAmount, + created_at: post.createdAt, + }); } - return { list: posts.sort((p1, p2) => p2.createdAt - p1.createdAt) }; + + return { + list: posts.sort((p1, p2) => p2.created_at - p1.created_at) as PostResult[], + }; }; export const useFetchComments = ({ @@ -80,15 +107,17 @@ export const useFetchComments = ({ const data = useInfiniteQuery( ["FetchComment", parentId, selectedNetwork?.id], async ({ pageParam }) => { + let comments: FetchCommentResponse; if (selectedNetwork?.kind === NetworkKind.Gno) { - return await fetchGnoComments(selectedNetwork, parentId || ""); + comments = await fetchGnoComments(selectedNetwork, parentId || ""); } else { - return await fetchTeritoriComments( + comments = await fetchTeritoriComments( selectedNetwork?.id || "", pageParam, parentId ); } + return comments; }, { getNextPageParam: (_, pages) => { diff --git a/packages/hooks/feed/usePost.ts b/packages/hooks/feed/usePost.ts index 4a715e043d..9fb41f76dc 100644 --- a/packages/hooks/feed/usePost.ts +++ b/packages/hooks/feed/usePost.ts @@ -1,18 +1,18 @@ import { GnoJSONRPCProvider } from "@gnolang/gno-js-client"; import { useQuery } from "@tanstack/react-query"; +import { Post } from "../../api/feed/v1/feed"; import { nonSigningSocialFeedClient } from "../../client-creators/socialFeedClient"; import { GNO_SOCIAL_FEEDS_PKG_PATH } from "../../components/socialFeed/const"; import { decodeGnoPost } from "../../components/socialFeed/utils"; -import { NetworkKind, getNetwork } from "../../networks"; +import { NetworkKind, getNetwork, getUserId } from "../../networks"; import { extractGnoString } from "../../utils/gno"; export const usePost = (id: string, networkId: string) => { - const { data, ...other } = useQuery( + const { data, ...other } = useQuery( ["social-post", id, networkId], async () => { const network = getNetwork(networkId); - if (network?.kind === NetworkKind.Gno) { const provider = new GnoJSONRPCProvider(network.endpoint); const output = await provider.evaluateExpression( @@ -21,24 +21,26 @@ export const usePost = (id: string, networkId: string) => { ); const postData = extractGnoString(output); - const post = decodeGnoPost(network.id, postData, false); - - return { - identifier: id, - parent_post_identifier: post.parentPostIdentifier, // identifier of linked post - category: post.category, // PostCategory - metadata: post.metadata, - reactions: post.reactions, - user_reactions: [], // TODO: What this for ? - post_by: post.createdBy, - deleted: post.isDeleted, - sub_post_length: 0, // TODO: handle this - }; + return decodeGnoPost(networkId, postData); } else { const client = await nonSigningSocialFeedClient({ networkId, }); - return await client.queryPost({ identifier: id }); + const res = await client.queryPost({ identifier: id }); + + const post: Post = { + identifier: id, + parentPostIdentifier: res.parent_post_identifier || "", + category: res.category, + metadata: res.metadata, + reactions: res.reactions, + authorId: getUserId(networkId, res.post_by), + isDeleted: res.deleted, + subPostLength: res.sub_post_length, + createdAt: res.created_at, + tipAmount: res.tip_amount, + }; + return post; } } ); diff --git a/packages/screens/FeedPostView/FeedPostViewScreen.tsx b/packages/screens/FeedPostView/FeedPostViewScreen.tsx index 95e0e13c97..181779bec3 100644 --- a/packages/screens/FeedPostView/FeedPostViewScreen.tsx +++ b/packages/screens/FeedPostView/FeedPostViewScreen.tsx @@ -42,9 +42,9 @@ import { useIsMobile } from "../../hooks/useIsMobile"; import { useMaxResolution } from "../../hooks/useMaxResolution"; import { useNSUserInfo } from "../../hooks/useNSUserInfo"; import { useSelectedNetworkId } from "../../hooks/useSelectedNetwork"; -import { NetworkFeature, getUserId, parseUserId } from "../../networks"; +import { NetworkFeature, parseUserId } from "../../networks"; import { ScreenFC, useAppNavigation } from "../../utils/navigation"; -import { DEFAULT_USERNAME, postResultToPost } from "../../utils/social-feed"; +import { DEFAULT_USERNAME } from "../../utils/social-feed"; import { primaryColor } from "../../utils/style/colors"; import { fontSemibold20 } from "../../utils/style/fonts"; import { @@ -70,11 +70,12 @@ export const FeedPostViewScreen: ScreenFC<"FeedPostView"> = ({ id, selectedNetworkId ); - const authorNSInfo = useNSUserInfo( - getUserId(selectedNetworkId, postResult?.post_by) - ); - const [, userAddress] = parseUserId(postResult?.post_by); + const authorId = postResult?.authorId; + const authorNSInfo = useNSUserInfo(authorId); + + const [, authorAddress] = parseUserId(postResult?.authorId); + const feedInputRef = useRef(null); const [replyTo, setReplyTo] = useState(); const aref = useAnimatedRef(); @@ -91,7 +92,7 @@ export const FeedPostViewScreen: ScreenFC<"FeedPostView"> = ({ isLoading: isLoadingComments, } = useFetchComments({ parentId: postResult?.identifier, - totalCount: postResult?.sub_post_length, + totalCount: postResult?.subPostLength, enabled: true, }); const isNextPageAvailable = useSharedValue(hasNextPage); @@ -150,16 +151,22 @@ export const FeedPostViewScreen: ScreenFC<"FeedPostView"> = ({ const headerLabel = useMemo(() => { if (isLoadingPostResult) return "Loading Post..."; else if (!postResult) return "Post not found"; - const author = - authorNSInfo?.metadata?.tokenId || userAddress || DEFAULT_USERNAME; - if (postResult.category === PostCategory.Article) - return `Article by ${author}`; - if (postResult?.parent_post_identifier) return `Comment by ${author}`; - return `Post by ${author}`; + const authorDisplayName = + authorNSInfo?.metadata?.tokenId || authorAddress || DEFAULT_USERNAME; + + if (postResult.category === PostCategory.Article) { + return `Article by ${authorDisplayName}`; + } + + if (postResult?.parentPostIdentifier) { + return `Comment by ${authorDisplayName}`; + } + + return `Post by ${authorDisplayName}`; }, [ postResult, authorNSInfo?.metadata?.tokenId, - userAddress, + authorAddress, isLoadingPostResult, ]); @@ -173,10 +180,9 @@ export const FeedPostViewScreen: ScreenFC<"FeedPostView"> = ({ {headerLabel} } onBackPress={() => - postResult?.parent_post_identifier && - postResult?.parent_post_identifier !== "0" + postResult?.parentPostIdentifier ? navigation.navigate("FeedPostView", { - id: postResult?.parent_post_identifier || "", + id: postResult?.parentPostIdentifier || "", }) : navigation.canGoBack() ? navigation.goBack() @@ -232,7 +238,7 @@ export const FeedPostViewScreen: ScreenFC<"FeedPostView"> = ({ borderRightWidth: 0, } } - post={postResultToPost(selectedNetworkId, postResult)} + post={postResult} isPostConsultation onPressReply={onPressReply} /> diff --git a/packages/utils/social-feed.ts b/packages/utils/social-feed.ts index ff3238a4ca..7818accb68 100644 --- a/packages/utils/social-feed.ts +++ b/packages/utils/social-feed.ts @@ -89,7 +89,7 @@ export const postResultToPost = ( parentPostIdentifier: postResult.parent_post_identifier || "", subPostLength: postResult.sub_post_length, reactions: postResult.reactions, - createdBy: getUserId(networkId, postResult.post_by), + authorId: getUserId(networkId, postResult.post_by), createdAt: JSON.parse(postResult.metadata).createdAt, tipAmount: parseFloat(postResult.tip_amount), };