Skip to content

Commit

Permalink
feat: cross-networks posts (#1268)
Browse files Browse the repository at this point in the history
Signed-off-by: Norman Meier <norman@samourai.coop>
  • Loading branch information
n0izn0iz authored Sep 11, 2024
1 parent 3f938cc commit f4267e2
Show file tree
Hide file tree
Showing 30 changed files with 266 additions and 139 deletions.
1 change: 1 addition & 0 deletions api/feed/v1/feed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ message PostFilter {
repeated string hashtags = 4;
int32 premium_level_min = 5; // inclusive
int32 premium_level_max = 6; // inclusive, -1 means infinity
string network_id = 7;
}

message PostsRequest {
Expand Down
6 changes: 3 additions & 3 deletions go/internal/indexerdb/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
)

type Post struct {
Identifier string `gorm:"primaryKey"`
NetworkID string `gorm:"primaryKey"`
LocalIdentifier string `gorm:"primaryKey"`

ParentPostIdentifier string `gorm:"index"`
Category uint32 `gorm:"index"`
IsBot bool `gorm:"default:false"`
Expand All @@ -17,6 +19,4 @@ type Post struct {
IsDeleted bool
IsFree bool
TipAmount int64

NetworkID string `gorm:"index"`
}
17 changes: 9 additions & 8 deletions go/internal/indexerhandler/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ func removeUserFromList(users []networks.UserID, user networks.UserID) []network
return res
}

func (h *Handler) handleExecuteDeletePost(e *Message, execMsg *wasmtypes.MsgExecuteContract) error {
func (h *Handler) handleExecuteDeletePost(_ *Message, execMsg *wasmtypes.MsgExecuteContract) error {
var execDeletePostMsg ExecDeletePostMsg
if err := json.Unmarshal(execMsg.Msg, &execDeletePostMsg); err != nil {
return errors.Wrap(err, "failed to unmarshal execute delete post msg")
Expand All @@ -76,7 +76,7 @@ func (h *Handler) handleExecuteDeletePost(e *Message, execMsg *wasmtypes.MsgExec
deletePost := execDeletePostMsg.DeletePost

post := indexerdb.Post{}
if err := h.db.Where("identifier = ?", deletePost.Identifier).First(&post).Error; err != nil {
if err := h.db.Where("network_id = ? AND local_identifier = ?", h.config.Network.ID, deletePost.Identifier).First(&post).Error; err != nil {
return errors.Wrap(err, "failed to get post to delete")
}

Expand All @@ -89,7 +89,7 @@ func (h *Handler) handleExecuteDeletePost(e *Message, execMsg *wasmtypes.MsgExec
return nil
}

func (h *Handler) handleExecuteReactPost(e *Message, execMsg *wasmtypes.MsgExecuteContract) error {
func (h *Handler) handleExecuteReactPost(_ *Message, execMsg *wasmtypes.MsgExecuteContract) error {
var execReactPostMsg ExecReactPostMsg
if err := json.Unmarshal(execMsg.Msg, &execReactPostMsg); err != nil {
return errors.Wrap(err, "failed to unmarshal execute react post msg")
Expand All @@ -98,7 +98,7 @@ func (h *Handler) handleExecuteReactPost(e *Message, execMsg *wasmtypes.MsgExecu
reactPost := execReactPostMsg.ReactPost

post := indexerdb.Post{}
if err := h.db.Where("identifier = ?", reactPost.Identifier).First(&post).Error; err != nil {
if err := h.db.Where("network_id = ? AND local_identifier = ?", h.config.Network.ID, reactPost.Identifier).First(&post).Error; err != nil {
return errors.Wrap(err, "failed to get post to react")
}

Expand Down Expand Up @@ -187,15 +187,15 @@ func (h *Handler) createPost(
}

post := indexerdb.Post{
Identifier: createPostMsg.Identifier,
NetworkID: h.config.Network.ID,
LocalIdentifier: createPostMsg.Identifier,
ParentPostIdentifier: createPostMsg.ParentPostIdentifier,
Category: createPostMsg.Category,
Metadata: metadataJSON,
UserReactions: map[string]interface{}{},
AuthorId: h.config.Network.UserID(execMsg.Sender),
CreatedAt: createdAt.Unix(),
IsBot: isBot,
NetworkID: h.config.Network.ID,
PremiumLevel: premium,
}

Expand All @@ -216,7 +216,8 @@ func (h *Handler) handleExecuteTipPost(e *Message, execMsg *wasmtypes.MsgExecute
}

post := indexerdb.Post{
Identifier: execTipPostMsg.TipPost.Identifier,
NetworkID: h.config.Network.ID,
LocalIdentifier: execTipPostMsg.TipPost.Identifier,
}

if err := h.db.First(&post).Error; err != nil {
Expand Down Expand Up @@ -246,7 +247,7 @@ func (h *Handler) handleQuests(
execMsg *wasmtypes.MsgExecuteContract,
createPostMsg *CreatePostMsg,
) error {
questId := "unknown"
var questId string
switch createPostMsg.Category {
case 1:
questId = "social_feed_first_comment"
Expand Down
11 changes: 8 additions & 3 deletions go/pkg/feed/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,7 @@ func (s *FeedService) IPFSKey(ctx context.Context, req *feedpb.IPFSKeyRequest) (
func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*feedpb.PostsResponse, error) {
filter := req.GetFilter()
var (
networkId string
user string
mentions []string
categories []uint32
Expand All @@ -89,6 +90,7 @@ func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*fee
)

if filter != nil {
networkId = filter.NetworkId
categories = filter.Categories
user = filter.User
hashtags = filter.Hashtags
Expand Down Expand Up @@ -126,6 +128,9 @@ func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*fee
if user != "" {
query = query.Where("author_id = ?", user)
}
if networkId != "" {
query = query.Where("network_id = ?", networkId)
}
if len(hashtags) > 0 {
formattedHashtags := make([]string, 0)
for _, hashtag := range hashtags {
Expand Down Expand Up @@ -195,11 +200,11 @@ func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*fee
}

posts[idx] = &feedpb.Post{
Id: string(n.GetBase().PostID(dbPost.Identifier)),
Id: string(n.GetBase().PostID(dbPost.LocalIdentifier)),
Category: dbPost.Category,
IsDeleted: dbPost.IsDeleted,
Identifier: dbPost.Identifier,
LocalIdentifier: dbPost.Identifier,
Identifier: dbPost.LocalIdentifier,
LocalIdentifier: dbPost.LocalIdentifier,
NetworkId: dbPost.NetworkID,
Metadata: string(metadata),
ParentPostIdentifier: dbPost.ParentPostIdentifier,
Expand Down
56 changes: 33 additions & 23 deletions go/pkg/feedpb/feed.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

26 changes: 25 additions & 1 deletion packages/api/feed/v1/feed.ts

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion packages/components/music/FeedMusicList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -30,17 +30,19 @@ const gap = layout.spacing_x2;

export const FeedMusicList: React.FC<{
title?: string;
networkId?: string;
authorId?: string;
allowUpload?: boolean;
style?: StyleProp<ViewStyle>;
}> = ({ title, authorId, allowUpload, style }) => {
}> = ({ title, networkId, authorId, allowUpload, style }) => {
const [appMode] = useAppMode();
const selectedWallet = useSelectedWallet();
const { showConnectWalletModal } = useWalletControl();
const [openUploadModal, setOpenUploadModal] = useState<boolean>(false);

const musicFeedRequest: Partial<PostsRequest> = {
filter: {
networkId: networkId || "",
categories: [PostCategory.MusicAudio],
user: authorId || "",
mentions: [],
Expand Down
4 changes: 3 additions & 1 deletion packages/components/socialFeed/NewsFeed/NewsFeed.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -36,11 +36,13 @@ import { SocialArticleCard } from "../SocialCard/cards/SocialArticleCard";
import { SocialThreadCard } from "../SocialCard/cards/SocialThreadCard";
import { SocialVideoCard } from "../SocialCard/cards/SocialVideoCard";

import { DeepPartial } from "@/utils/typescript";

const OFFSET_Y_LIMIT_FLOATING = 224;

interface NewsFeedProps {
Header: React.ComponentType;
req: Partial<PostsRequest>;
req: DeepPartial<PostsRequest>;
// Receive this if the post is created from HashFeedScreen
additionalHashtag?: string;
// Receive this if the post is created from UserPublicProfileScreen (If the user doesn't own the UPP)
Expand Down
3 changes: 2 additions & 1 deletion packages/components/video/FeedVideosList.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,13 @@ import { BrandText } from "../BrandText";
import { GridList } from "../layout/GridList";

import { Spinner } from "@/components/Spinner";
import { DeepPartial } from "@/utils/typescript";

const minCardWidth = 261;

export const FeedVideosList: React.FC<{
title: string;
req: Partial<PostsRequest>;
req: DeepPartial<PostsRequest>;
allowUpload?: boolean;
style?: StyleProp<ViewStyle>;
}> = ({ title, req, allowUpload, style }) => {
Expand Down
9 changes: 5 additions & 4 deletions packages/hooks/feed/useFetchFeed.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { mustGetFeedClient } from "@/utils/backend";
import { TERITORI_FEED_ID } from "@/utils/feed/constants";
import { decodeGnoPost } from "@/utils/feed/gno";
import { extractGnoJSONString } from "@/utils/gno";
import { DeepPartial } from "@/utils/typescript";

export type PostsList = {
list: Post[];
Expand All @@ -26,10 +27,10 @@ export const combineFetchFeedPages = (pages: PostsList[]) =>

const fetchTeritoriFeed = async (
selectedNetwork: NetworkInfo,
req: Partial<PostsRequest>,
req: DeepPartial<PostsRequest>,
pageParam: number,
) => {
const postsRequest: Partial<PostsRequest> = {
const postsRequest: DeepPartial<PostsRequest> = {
...req,
offset: pageParam || 0,
};
Expand All @@ -42,7 +43,7 @@ const fetchTeritoriFeed = async (
const fetchGnoFeed = async (
selectedNetwork: GnoNetworkInfo,
callerAddress: string | undefined,
req: Partial<PostsRequest>,
req: DeepPartial<PostsRequest>,
pageParam: number,
) => {
if (!selectedNetwork.socialFeedsPkgPath) return { list: [], totalCount: 0 };
Expand Down Expand Up @@ -78,7 +79,7 @@ const fetchGnoFeed = async (
return result;
};

export const useFetchFeed = (req: Partial<PostsRequest>) => {
export const useFetchFeed = (req: DeepPartial<PostsRequest>) => {
const selectedNetwork = useSelectedNetworkInfo();
const wallet = useSelectedWallet();

Expand Down
Loading

0 comments on commit f4267e2

Please sign in to comment.