Skip to content

Commit

Permalink
feat: premium content (#976)
Browse files Browse the repository at this point in the history
* feat: premium content

Signed-off-by: Norman Meier <[email protected]>

* chore: prettier premium toggle

Signed-off-by: Norman Meier <[email protected]>

* feat: return premium level from backend

Signed-off-by: Norman Meier <[email protected]>

* chore: refactor premium filter

Signed-off-by: Norman Meier <[email protected]>

* chore: remove deadcode

Signed-off-by: Norman Meier <[email protected]>

* chore: fix typo

Signed-off-by: Norman Meier <[email protected]>

* chore: simplify

Signed-off-by: Norman Meier <[email protected]>

* chore: clearer post view auth logic

Signed-off-by: Norman Meier <[email protected]>

* chore: typos

Signed-off-by: Norman Meier <[email protected]>

* chore: fix typo

Signed-off-by: Norman Meier <[email protected]>

* chore: restrict possible premium level values

Signed-off-by: Norman Meier <[email protected]>

* chore: add comment

Signed-off-by: Norman Meier <[email protected]>

* feat: hide premium toggle if network does not support it

Signed-off-by: Norman Meier <[email protected]>

* fix: return premium level from backend

Signed-off-by: Norman Meier <[email protected]>

* fix: premium level decoding in go and code dup

Signed-off-by: Norman Meier <[email protected]>

---------

Signed-off-by: Norman Meier <[email protected]>
  • Loading branch information
n0izn0iz authored Feb 18, 2024
1 parent 61a7ecb commit ae83fac
Show file tree
Hide file tree
Showing 38 changed files with 383 additions and 305 deletions.
5 changes: 4 additions & 1 deletion api/feed/v1/feed.proto
Original file line number Diff line number Diff line change
Expand Up @@ -30,15 +30,18 @@ message Post {
uint32 sub_post_length = 6;
string author_id = 7;
int64 created_at = 8;
int64 tip_amount = 10;
repeated Reaction reactions = 9;
int64 tip_amount = 10;
uint32 premium_level = 11;
}

message PostFilter {
string user = 1;
repeated string mentions = 2;
repeated uint32 categories = 3;
repeated string hashtags = 4;
int32 premium_level_min = 5; // inclusive
int32 premium_level_max = 6; // inclusive, -1 means infinity
}

message PostsRequest {
Expand Down
7 changes: 1 addition & 6 deletions go/internal/indexerdb/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,6 @@ import (
"github.com/TERITORI/teritori-dapp/go/pkg/networks"
)

/*
userReactions = [
{icon: [userId, ...]}
]
*/

type Post struct {
Identifier string `gorm:"primaryKey"`
ParentPostIdentifier string `gorm:"index"`
Expand All @@ -18,6 +12,7 @@ type Post struct {
Metadata ObjectJSONB `gorm:"type:jsonb;default:'{}'"`
UserReactions ObjectJSONB `gorm:"type:jsonb;default:'{}'"`
AuthorId networks.UserID `gorm:"index"`
PremiumLevel uint32 `gorm:"index"`
CreatedAt int64
IsDeleted bool
IsFree bool
Expand Down
14 changes: 13 additions & 1 deletion go/internal/indexerhandler/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package indexerhandler

import (
"encoding/json"
"math"

wasmtypes "github.com/CosmWasm/wasmd/x/wasm/types"
"github.com/TERITORI/teritori-dapp/go/internal/indexerdb"
Expand Down Expand Up @@ -167,7 +168,17 @@ func (h *Handler) createPost(
) error {
var metadataJSON map[string]interface{}
if err := json.Unmarshal([]byte(createPostMsg.Metadata), &metadataJSON); err != nil {
return errors.Wrap(err, "failed to unmarshal metadata")
// We ignore this case because there's no validation for the data on the blockchain, and wrong data would stall the indexer if we returned an error.
h.logger.Info("ignored post with malformed metadada", zap.String("tx", e.TxHash), zap.String("contract", execMsg.Contract), zap.Error(err))
return nil
}

premium := uint32(0)
ipremium, ok := metadataJSON["premium"]
if ok {
if cpremium, ok := ipremium.(float64); ok && cpremium > 0 {
premium = uint32(math.Ceil(cpremium))
}
}

createdAt, err := e.GetBlockTime()
Expand All @@ -185,6 +196,7 @@ func (h *Handler) createPost(
CreatedAt: createdAt.Unix(),
IsBot: isBot,
NetworkID: h.config.Network.ID,
PremiumLevel: premium,
}

if err := h.db.Create(&post).Error; err != nil {
Expand Down
22 changes: 18 additions & 4 deletions go/pkg/feed/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,16 +77,22 @@ 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 user string
var mentions []string
var categories []uint32
var hashtags []string
var (
user string
mentions []string
categories []uint32
hashtags []string
premiumLevelMin int32
premiumLevelMax int32
)

if filter != nil {
categories = filter.Categories
user = filter.User
hashtags = filter.Hashtags
mentions = filter.Mentions
premiumLevelMin = filter.PremiumLevelMin
premiumLevelMax = filter.PremiumLevelMax
}

queryUserID := req.GetQueryUserId()
Expand Down Expand Up @@ -138,6 +144,13 @@ func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*fee
query = query.Where(fmt.Sprintf("metadata -> 'mentions' ?| array[%s]", strings.Join(formattedMentions, ",")))
}

if premiumLevelMin > 0 {
query = query.Where("premium_level >= ?", premiumLevelMin)
}
if premiumLevelMax >= 0 {
query = query.Where("premium_level <= ?", premiumLevelMax)
}

query = query.
Order("created_at DESC").
Limit(int(limit)).
Expand Down Expand Up @@ -185,6 +198,7 @@ func (s *FeedService) Posts(ctx context.Context, req *feedpb.PostsRequest) (*fee
CreatedAt: dbPost.CreatedAt,
Reactions: reactions,
TipAmount: dbPost.TipAmount,
PremiumLevel: dbPost.PremiumLevel,
}
}

Expand Down
122 changes: 77 additions & 45 deletions go/pkg/feedpb/feed.pb.go

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

Loading

0 comments on commit ae83fac

Please sign in to comment.