Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add reaction state to posts #34

Merged
merged 1 commit into from
Jan 16, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions core/feed.go
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,11 @@ func FeedEvents(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down Expand Up @@ -382,6 +387,11 @@ func FeedNews(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down Expand Up @@ -439,6 +449,11 @@ func FeedNews(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down Expand Up @@ -609,6 +624,11 @@ func FeedPosts(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down
5 changes: 5 additions & 0 deletions core/like.go
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,11 @@ func LikesUser(
}

if !r.isSelf {
err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err := enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down
75 changes: 73 additions & 2 deletions core/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,21 @@ const TypePost = "tg_post"

var defaultOwned = true

// HasReacted bundles the binary state of a user per reaction.
type HasReacted struct {
Like bool `json:"like"`
Love bool `json:"love"`
Haha bool `json:"haha"`
Wow bool `json:"wow"`
Sad bool `json:"sad"`
Angry bool `json:"angry"`
}

// Post is the intermediate representation for posts.
type Post struct {
Counts PostCounts
IsLiked bool
Counts PostCounts
IsLiked bool
HasReacted HasReacted

*object.Object
}
Expand Down Expand Up @@ -253,6 +264,11 @@ func PostListAll(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down Expand Up @@ -340,6 +356,11 @@ func PostListUser(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, ps)
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, ps)
if err != nil {
return nil, err
Expand Down Expand Up @@ -409,6 +430,11 @@ func PostRetrieve(
return nil, err
}

err = enrichHasReacted(reactions, currentApp, origin, PostList{post})
if err != nil {
return nil, err
}

err = enrichIsLiked(events, currentApp, origin, PostList{post})
if err != nil {
return nil, err
Expand Down Expand Up @@ -640,6 +666,51 @@ func enrichCounts(
return nil
}

func enrichHasReacted(
reactions reaction.Service,
currentApp *app.App,
origin uint64,
ps PostList,
) error {
for _, p := range ps {
rs, err := reactions.Query(currentApp.Namespace(), reaction.QueryOptions{
Deleted: &defaultDeleted,
ObjectIDs: []uint64{
p.ID,
},
OwnerIDs: []uint64{
origin,
},
})
if err != nil {
return nil
}

hasReacted := HasReacted{}

for _, r := range rs {
switch r.Type {
case reaction.TypeLike:
hasReacted.Like = true
case reaction.TypeLove:
hasReacted.Love = true
case reaction.TypeHaha:
hasReacted.Haha = true
case reaction.TypeWow:
hasReacted.Wow = true
case reaction.TypeSad:
hasReacted.Sad = true
case reaction.TypeAngry:
hasReacted.Angry = true
}
}

p.HasReacted = hasReacted
}

return nil
}

func enrichIsLiked(
events event.Service,
currentApp *app.App,
Expand Down
2 changes: 2 additions & 0 deletions handler/http/post.go
Original file line number Diff line number Diff line change
Expand Up @@ -354,6 +354,7 @@ func (p *payloadPost) MarshalJSON() ([]byte, error) {
Attachments []*payloadAttachment `json:"attachments"`
Counts postCounts `json:"counts"`
CreatedAt time.Time `json:"created_at,omitempty"`
HasReacted core.HasReacted `json:"has_reacted"`
ID string `json:"id"`
IsLiked bool `json:"is_liked"`
Restrictions *object.Restrictions `json:"restrictions,omitempty"`
Expand All @@ -376,6 +377,7 @@ func (p *payloadPost) MarshalJSON() ([]byte, error) {
},
},
CreatedAt: p.post.CreatedAt,
HasReacted: p.post.HasReacted,
ID: strconv.FormatUint(p.post.ID, 10),
IsLiked: p.post.IsLiked,
Restrictions: p.post.Restrictions,
Expand Down