-
Notifications
You must be signed in to change notification settings - Fork 48
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
See PR #274
- Loading branch information
Showing
8 changed files
with
180 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package v0100 | ||
|
||
import ( | ||
"time" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
) | ||
|
||
// PostID represents a unique post id | ||
type PostID string | ||
|
||
// OptionalData represents a Posts' optional data and allows for custom | ||
// Amino and JSON serialization and deserialization. | ||
type OptionalData map[string]string | ||
|
||
// Attachment contains the information representing any type of file provided with a post. | ||
// This file can be an image or a multimedia file (vocals, video, documents, etc.). | ||
type Attachment struct { | ||
URI string `json:"uri" yaml:"uri"` | ||
MimeType string `json:"mime_type" yaml:"mime_type"` | ||
Tags []sdk.AccAddress `json:"tags,omitempty" yaml:"tags,omitempty"` | ||
} | ||
|
||
// PollData contains the information of a poll that is associated to a post | ||
type PollData struct { | ||
Question string `json:"question" yaml:"question"` // Describes what poll is about | ||
ProvidedAnswers []PollAnswer `json:"provided_answers" yaml:"provided_answers"` // Lists of answers provided by the creator | ||
EndDate time.Time `json:"end_date" yaml:"end_date"` // RFC3339 date at which the poll will no longer accept new answers | ||
Open bool `json:"is_open" yaml:"is_open"` // Tells if the poll is still accepting answers | ||
AllowsMultipleAnswers bool `json:"allows_multiple_answers" yaml:"allows_multiple_answers"` // Tells if the poll is a single or multiple answers one | ||
AllowsAnswerEdits bool `json:"allows_answer_edits" yaml:"allows_answer_edits"` // Tells if the poll allows answer edits | ||
} | ||
|
||
// PollAnswer contains the data of a single poll answer inserted by the creator | ||
type PollAnswer struct { | ||
ID AnswerID `json:"id" yaml:"id"` // Unique id inside the post, serialized as a string for Javascript compatibility | ||
Text string `json:"text" yaml:"text"` // Text of the answer | ||
} | ||
|
||
// AnswerID represents a unique answer id | ||
type AnswerID uint64 | ||
|
||
// Post is a struct of a post | ||
type Post struct { | ||
PostID PostID `json:"id" yaml:"id" ` // Unique id | ||
ParentID PostID `json:"parent_id" yaml:"parent_id"` // Post of which this one is a comment | ||
Message string `json:"message" yaml:"message"` // Message contained inside the post | ||
Created time.Time `json:"created" yaml:"created"` // RFC3339 date at which the post has been created | ||
LastEdited time.Time `json:"last_edited" yaml:"last_edited"` // RFC3339 date at which the post has been edited the last time | ||
AllowsComments bool `json:"allows_comments" yaml:"allows_comments"` // Tells if users can reference this PostID as the parent | ||
Subspace string `json:"subspace" yaml:"subspace"` // Identifies the application that has posted the message | ||
OptionalData OptionalData `json:"optional_data,omitempty" yaml:"optional_data,omitempty"` // Arbitrary data that can be used from the developers | ||
Creator sdk.AccAddress `json:"creator" yaml:"creator"` // Creator of the Post | ||
Attachments []Attachment `json:"attachments,omitempty" yaml:"attachments,omitempty"` // Contains all the attachments that are shared with the post | ||
PollData *PollData `json:"poll_data,omitempty" yaml:"poll_data,omitempty"` // Contains the poll details, if existing | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,92 @@ | ||
package keeper | ||
|
||
import ( | ||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
|
||
v0100 "github.com/desmos-labs/desmos/x/posts/keeper/legacy/v0.10.0" | ||
"github.com/desmos-labs/desmos/x/posts/types" | ||
) | ||
|
||
// MigratePostsFrom0100To0120 migrates all the posts from v0.10.0 to v.12.0. | ||
// To do this it executes the following operations one post at a time: | ||
// 1. It reads the old post | ||
// 2. It converts the post removing the Open field from the PollData, if any | ||
// 3. It saves the post inside the store again | ||
func (k Keeper) MigratePostsFrom0100To0120(ctx sdk.Context) error { | ||
store := ctx.KVStore(k.StoreKey) | ||
iterator := sdk.KVStorePrefixIterator(store, types.PostStorePrefix) | ||
defer iterator.Close() | ||
|
||
for ; iterator.Valid(); iterator.Next() { | ||
postKey := iterator.Value() | ||
|
||
// Get the v0.10.0 post | ||
var v0100Post v0100.Post | ||
err := k.Cdc.UnmarshalBinaryBare(postKey, &v0100Post) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Convert the post | ||
v0120Post := types.Post{ | ||
PostID: types.PostID(v0100Post.PostID), | ||
ParentID: types.PostID(v0100Post.ParentID), | ||
Message: v0100Post.Message, | ||
Created: v0100Post.Created, | ||
LastEdited: v0100Post.LastEdited, | ||
AllowsComments: v0100Post.AllowsComments, | ||
Subspace: v0100Post.Subspace, | ||
OptionalData: types.OptionalData(v0100Post.OptionalData), | ||
Creator: v0100Post.Creator, | ||
Attachments: migrateAttachments(v0100Post.Attachments), | ||
PollData: migratePollData(v0100Post.PollData), | ||
} | ||
|
||
bz, err := k.Cdc.MarshalBinaryBare(&v0120Post) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
// Store the post | ||
store.Set(postKey, bz) | ||
|
||
} | ||
|
||
return nil | ||
} | ||
|
||
// migrateAttachments migrates the given attachments from v0.10.0 to v0.12.0 | ||
func migrateAttachments(attachments []v0100.Attachment) types.Attachments { | ||
var v1200Attachments = make([]types.Attachment, len(attachments)) | ||
for index, attachment := range attachments { | ||
v1200Attachments[index] = types.Attachment(attachment) | ||
} | ||
return v1200Attachments | ||
} | ||
|
||
// migratePollData migrates the given pollData from v0.10.0 to v0.12.0 | ||
func migratePollData(pollData *v0100.PollData) *types.PollData { | ||
if pollData == nil { | ||
return nil | ||
} | ||
|
||
return &types.PollData{ | ||
Question: pollData.Question, | ||
ProvidedAnswers: migrateProvidedAnswers(pollData.ProvidedAnswers), | ||
EndDate: pollData.EndDate, | ||
AllowsMultipleAnswers: pollData.AllowsMultipleAnswers, | ||
AllowsAnswerEdits: pollData.AllowsAnswerEdits, | ||
} | ||
} | ||
|
||
// migrateProvidedAnswers migrates the providedAnswers from v0.10.0 to v0.12.0 | ||
func migrateProvidedAnswers(providedAnswers []v0100.PollAnswer) types.PollAnswers { | ||
var v0120Answers = make([]types.PollAnswer, len(providedAnswers)) | ||
for index, answer := range providedAnswers { | ||
v0120Answers[index] = types.PollAnswer{ | ||
ID: types.AnswerID(answer.ID), | ||
Text: answer.Text, | ||
} | ||
} | ||
return v0120Answers | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters