-
Notifications
You must be signed in to change notification settings - Fork 46
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
Media Post Implementation #72
Conversation
…nd media post to codec.go
updated codecs for post and media post implemented keeper methods for media post implemented strings type and some useful methods
Codecov Report
@@ Coverage Diff @@
## master #72 +/- ##
=========================================
Coverage ? 75.99%
=========================================
Files ? 21
Lines ? 1083
Branches ? 0
=========================================
Hits ? 823
Misses ? 238
Partials ? 22
Continue to review full report at Codecov.
|
@bragaz I've noticed that currently you've implemented the I personally think we should follow what @kwunyeung suggested inside his comment on #72:
And
This means that there is no need for on-chain verification of mime types verification nor provider support at all. Do you think they should be kept? If so, why? |
Yes I was referring to your comment in #36 :
I Miss @kwunyeung 's one and he's totally right, the apps should take care of that. This will also keep chain cleaner and clients could implement as many providers they want. |
edited all method in keeper, handler and querier to fit the new models todo: Fix tests
- edited keeper_test, handler_test, querier_test to fit the new Post Interface - edited Post response to fit the new Post Interface - updated codec - added new tests for media posts TODO: - complete MsgCreateMediaPost tests - edit CLI command to let user make media posts
todo: - fix bugs in marshalJSON, unmarshalJSON
package types_test | ||
|
||
import ( | ||
"fmt" |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
File is not goimports
-ed (from goimports
)
"fmt" | |
"fmt" | |
"testing" | |
I've been struggling all day with |
You are doing the exact opposite of what should be done. If you take a look at the
You are overriding the So the good implementation should be something like func (mp MediaPost) MarshalJson() ([]byte, err) {
type mediaPostJson MediaPost
return json.Marshal(mediaPostJson(mp))
}
func (mp MediaPost) String() string {
json, err := json.Marshal(&mp)
if err != nil {
panic(err)
}
return string(json)
} Actually, it's not needed to use the We should change it inside EditFor future reference, you can look at existing code. For example, you could have found the implementation for:
|
I was doing that way since I discovered through the |
I would prefer we stick to an implementation like the one I've suggested, and we later discuss any problem that might raise individually once we have the tests setup and failing. Please don't change the implementations completely just because tests are failing, and do not remove the tests too. Keep them as it might be useful to understand what's wrong. |
Besides the conflicts, we need to understand why the marshalling of // String implements fmt.Stringer
func (pm PostMedia) String() string {
bytes, err := json.Marshal(&pm)
if err != nil {
panic(err)
}
return string(bytes)
} The result of the function's test is this:
And it's wrong because I think we need to get something like:
I've tried to implement func (mp MediaPost) String() string {
bytes, err := mp.TextPost.MarshalJSON()
bytes2, err := json.Marshal(&mp.Medias)
bb := append(bytes[:], bytes2[:]...)
if err != nil {
panic(err)
}
return string(bb)
} It isn't printing a beautiful result but at least print medias...I really don't understand why this is happening..maybe I'm missing something! Let me know. This problem is the same in |
@bragaz Please pull from master to solve the existing conflicts |
…eonardo/media-posts-impl � Conflicts: � x/posts/client/cli/tx.go � x/posts/client/rest/tx.go � x/posts/internal/keeper/common_test.go � x/posts/internal/keeper/handler.go � x/posts/internal/keeper/handler_test.go � x/posts/internal/keeper/keeper.go � x/posts/internal/keeper/keeper_test.go � x/posts/internal/keeper/querier_test.go � x/posts/internal/types/msgs.go � x/posts/internal/types/msgs_test.go � x/posts/internal/types/post.go � x/posts/internal/types/post_response_test.go � x/posts/internal/types/text_post_test.go � x/posts/legacy/v0.2.0/migrate.go � x/posts/legacy/v0.2.0/types.go
@bragaz I had the time to look deeper into this, and I have found something really interesting. How JSON encoding works and how to fix the problemCurrently we have the following type inheritances:
Both What does this mean? It means that it stops as soon as one type implements By removing the implementation of both var m MediaPost = ...
json.Marshal(m)
// {"id":"2","parent_id":"0","message":"media Post","created":"2020-01-01T15:15:00Z","last_edited":"0001-01-01T00:00:00Z","allows_comments":false,"subspace":"desmos","creator":"cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns","medias":[{"uri":"uri","provider":"provider","mime_Type":"text/plain"}]} It is completely fine to remove both However, we need to implement those methods into Changes in the serialization fashionLooking at this, it made me think about the best way we can handle all of this properly. What we could do is the following. JSON serialized posts that extend This will result in {
"id": "2",
"parent_id": "0",
"message": "media Post",
"created": "2020-01-01T15:15:00Z",
"last_edited": "0001-01-01T00:00:00Z",
"allows_comments": false,
"subspace": "desmos",
"creator": "cosmos1cjf97gpzwmaf30pzvaargfgr884mpp5ak8f7ns",
"medias": [
{
"uri": "uri",
"provider": "provider",
"mime_Type": "text/plain"
}
]
} As you can see, we do not encapsulate the This results in a more clean serialization overall. Problems with this approachThinking ahead of time, this solution might however bring some new problems when we will start to implement poll posts (#14) too. What would happen if a users wants to create a poll having an image inside? This should result in a hybrid type that inherits from both However, we can think this thought later, but it is still important to now it now. |
- fixed tests
- implemented stringer String() method for types.Posts
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've reviewed just the implementations and not the tests. Will review the latter once the issues and comments on the first have been completely solved.
Removing cdc.RegisterInterface((*Post)(nil), nil) Will result in some errors in tests and handler because it's an unregistered interface. |
- waiting for removing provider
x/posts/client/cli/tx.go
Outdated
arg := strings.Split(args[i], ",") | ||
if len(arg) == 3 { | ||
media := types.NewPostMedia(arg[0], arg[1], arg[2]) | ||
medias = append(medias, media) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
What would happen if the user specifies two times the same media data? Should we use something like appendIfMissing
instead of append
here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was thinking about that when I was implementing it...it is good to forbid user to post the same media more times? I mean, I can post the same GIF two times, but that will not be the same for a video or a PDF...so yeah, we should use appendIfMissing
instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@bragaz Using appendIfMissing
here means that we are not allowing to use the same media inside the same post. E.g., the user will not be able to create a post having the same exact GIF, PDF, or file in it.
This is correct, and does not refer to creating two posts with the same media inside. This means
- Alice creating post P1 with media M1 will be allowed
- Alice creating post P1 with media M1 and M2 will be allowed
- Alice creating post P1 with media M1 and M1 (again) will not be allowed
- Alice creating post P1 with media M1 and post P2 with the same media M1 will be allowed
x/posts/client/cli/tx.go
Outdated
return sdk.ErrUnknownRequest("If medias are present, you should specify uri, provider and mime type, if you are confused, please use the --help flag") | ||
} | ||
} | ||
if textMsg, ok := msg.(types.MsgCreateTextPost); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check will always succeed, can we simply delete it and replace it with something like the following?
msg = types.NewMsgCreateMediaPost(MsgCreateTextPost(msg), medias))
x/posts/client/rest/tx.go
Outdated
addr, req.CreationTime) | ||
|
||
if len(req.Medias) != 0 { | ||
if textMsg, ok := msg.(types.MsgCreateTextPost); ok { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This check will always succeed. Can we replace it with something like the following?
msg = types.NewMsgCreateMediaPost(MsgCreateTextPost(msg), req.Medias)
Co-Authored-By: Riccardo Montagnin <riccardo.montagnin@gmail.com>
Co-Authored-By: Riccardo Montagnin <riccardo.montagnin@gmail.com>
Following the discussion made about Interfaces and types, I'm closing this PR in order to open another one with the following changes:
type Post struct {
// All TextPost's field
Medias []Media
}
type MsgCreatePost {
// All TextPost's field
Medias []Media
} |
Description
This PR implements
MediaPost
and relative methods.TODO:
Checklist
CHANGELOG.md
file.Files changed
in the Github PR explorer.