forked from lino-network/lino
-
Notifications
You must be signed in to change notification settings - Fork 0
/
manager.go
293 lines (270 loc) · 9.17 KB
/
manager.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
package post
import (
"github.com/lino-network/lino/param"
"github.com/lino-network/lino/types"
"github.com/lino-network/lino/x/post/model"
sdk "github.com/cosmos/cosmos-sdk/types"
)
type PostManager struct {
postStorage model.PostStorage
paramHolder param.ParamHolder
}
// NewPostManager - create a new post manager
func NewPostManager(key sdk.StoreKey, holder param.ParamHolder) PostManager {
return PostManager{
postStorage: model.NewPostStorage(key),
paramHolder: holder,
}
}
// GetRedistributionSplitRate - get post redistribution split rate
func (pm PostManager) GetRedistributionSplitRate(ctx sdk.Context, permlink types.Permlink) (sdk.Rat, sdk.Error) {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return sdk.ZeroRat(), err
}
return postMeta.RedistributionSplitRate, nil
}
// GetCreatedTimeAndReward - get post created time and reward for evaluate of content value
func (pm PostManager) GetCreatedTimeAndReward(ctx sdk.Context, permlink types.Permlink) (int64, types.Coin, sdk.Error) {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return 0, types.NewCoinFromInt64(0), err
}
return postMeta.CreatedAt, postMeta.TotalReward, nil
}
// DoesPostExist - check if post exist
func (pm PostManager) DoesPostExist(ctx sdk.Context, permlink types.Permlink) bool {
return pm.postStorage.DoesPostExist(ctx, permlink)
}
// GetSourcePost - return root source post
func (pm PostManager) GetSourcePost(
ctx sdk.Context, permlink types.Permlink) (types.AccountKey, string, sdk.Error) {
postInfo, err := pm.postStorage.GetPostInfo(ctx, permlink)
if err != nil {
return types.AccountKey(""), "", err
}
// check source post's source, that's the root
if postInfo.SourceAuthor == types.AccountKey("") || postInfo.SourcePostID == "" {
return types.AccountKey(""), "", nil
}
return postInfo.SourceAuthor, postInfo.SourcePostID, nil
}
func (pm PostManager) setRootSourcePost(ctx sdk.Context, postInfo *model.PostInfo) sdk.Error {
if postInfo.SourceAuthor == types.AccountKey("") || postInfo.SourcePostID == "" {
return nil
}
permlink := types.GetPermlink(postInfo.Author, postInfo.PostID)
rootAuthor, rootPostID, err :=
pm.GetSourcePost(ctx, types.GetPermlink(postInfo.SourceAuthor, postInfo.SourcePostID))
if err != nil {
return ErrGetSourcePost(permlink)
}
if rootAuthor != types.AccountKey("") && rootPostID != "" {
postInfo.SourceAuthor = rootAuthor
postInfo.SourcePostID = rootPostID
}
return nil
}
// create the post
func (pm PostManager) CreatePost(
ctx sdk.Context, author types.AccountKey, postID string,
sourceAuthor types.AccountKey, sourcePostID string,
parentAuthor types.AccountKey, parentPostID string,
content string, title string, redistributionSplitRate sdk.Rat,
links []types.IDToURLMapping) sdk.Error {
postInfo := &model.PostInfo{
PostID: postID,
Title: title,
Content: content,
Author: author,
ParentAuthor: parentAuthor,
ParentPostID: parentPostID,
SourceAuthor: sourceAuthor,
SourcePostID: sourcePostID,
Links: links,
}
permlink := types.GetPermlink(postInfo.Author, postInfo.PostID)
if pm.DoesPostExist(ctx, permlink) {
return ErrPostAlreadyExist(permlink)
}
if err := pm.setRootSourcePost(ctx, postInfo); err != nil {
return ErrCreatePostSourceInvalid(permlink)
}
if err := pm.postStorage.SetPostInfo(ctx, postInfo); err != nil {
return err
}
postMeta := &model.PostMeta{
CreatedAt: ctx.BlockHeader().Time.Unix(),
LastUpdatedAt: ctx.BlockHeader().Time.Unix(),
LastActivityAt: ctx.BlockHeader().Time.Unix(),
AllowReplies: true, // Default
IsDeleted: false,
RedistributionSplitRate: redistributionSplitRate.Round(types.PrecisionFactor),
}
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
return nil
}
// UpdatePost - update post title, content and links. Can't update a deleted post
func (pm PostManager) UpdatePost(
ctx sdk.Context, author types.AccountKey, postID, title, content string,
links []types.IDToURLMapping) sdk.Error {
permlink := types.GetPermlink(author, postID)
postInfo, err := pm.postStorage.GetPostInfo(ctx, permlink)
if err != nil {
return err
}
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return err
}
postInfo.Title = title
postInfo.Content = content
postInfo.Links = links
// postMeta.RedistributionSplitRate = redistributionSplitRate
postMeta.LastUpdatedAt = ctx.BlockHeader().Time.Unix()
if err := pm.postStorage.SetPostInfo(ctx, postInfo); err != nil {
return err
}
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
return nil
}
// AddOrUpdateViewToPost - add or update view from the user if view exists
func (pm PostManager) AddOrUpdateViewToPost(
ctx sdk.Context, permlink types.Permlink, user types.AccountKey) sdk.Error {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return err
}
view, _ := pm.postStorage.GetPostView(ctx, permlink, user)
// override previous
if view == nil {
view = &model.View{Username: user}
}
postMeta.TotalViewCount++
view.Times++
view.LastViewAt = ctx.BlockHeader().Time.Unix()
if err := pm.postStorage.SetPostView(ctx, permlink, view); err != nil {
return err
}
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
return nil
}
// add comment to post comment list
func (pm PostManager) AddComment(
ctx sdk.Context, permlink types.Permlink, commentAuthor types.AccountKey, commentPostID string) sdk.Error {
comment := &model.Comment{
Author: commentAuthor,
PostID: commentPostID,
CreatedAt: ctx.BlockHeader().Time.Unix(),
}
if err := pm.postStorage.SetPostComment(ctx, permlink, comment); err != nil {
return err
}
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return err
}
postMeta.LastActivityAt = ctx.BlockHeader().Time.Unix()
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
return nil
}
// AddDonation - add donation to post donation list
func (pm PostManager) AddDonation(
ctx sdk.Context, permlink types.Permlink, donator types.AccountKey,
amount types.Coin, donationType types.DonationType) sdk.Error {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return err
}
donations, _ := pm.postStorage.GetPostDonations(ctx, permlink, donator)
if donations == nil {
donations = &model.Donations{Username: donator, Amount: types.NewCoinFromInt64(0), Times: 0}
}
donations.Amount = donations.Amount.Plus(amount)
donations.Times = donations.Times + 1
if err := pm.postStorage.SetPostDonations(ctx, permlink, donations); err != nil {
return err
}
postMeta.TotalReward = postMeta.TotalReward.Plus(amount)
postMeta.TotalDonateCount = postMeta.TotalDonateCount + 1
postMeta.LastActivityAt = ctx.BlockHeader().Time.Unix()
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
return nil
}
// DeletePost - delete post by author or content censorship
func (pm PostManager) DeletePost(ctx sdk.Context, permlink types.Permlink) sdk.Error {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return err
}
postMeta.IsDeleted = true
postMeta.RedistributionSplitRate = sdk.OneRat()
postMeta.LastUpdatedAt = ctx.BlockHeader().Time.Unix()
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
postInfo, err := pm.postStorage.GetPostInfo(ctx, permlink)
if err != nil {
return err
}
postInfo.Title = ""
postInfo.Content = ""
postInfo.Links = nil
if err := pm.postStorage.SetPostInfo(ctx, postInfo); err != nil {
return err
}
return nil
}
// IsDeleted - check if a post is deleted or not
func (pm PostManager) IsDeleted(ctx sdk.Context, permlink types.Permlink) (bool, sdk.Error) {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return false, err
}
return postMeta.IsDeleted, nil
}
// UpdateLastActivityAt - update post last activity at
func (pm PostManager) UpdateLastActivityAt(ctx sdk.Context, permlink types.Permlink) sdk.Error {
postMeta, err := pm.postStorage.GetPostMeta(ctx, permlink)
if err != nil {
return err
}
postMeta.LastActivityAt = ctx.BlockHeader().Time.Unix()
if err := pm.postStorage.SetPostMeta(ctx, permlink, postMeta); err != nil {
return err
}
return nil
}
// GetPenaltyScore - get penalty score from report and upvote
func (pm PostManager) GetPenaltyScore(ctx sdk.Context, reputation types.Coin) (sdk.Rat, sdk.Error) {
if reputation.IsNotNegative() {
return sdk.ZeroRat(), nil
}
reputation = types.NewCoinFromInt64(0).Minus(reputation)
postParam, err := pm.paramHolder.GetPostParam(ctx)
if err != nil {
return sdk.OneRat(), err
}
// if max report reputation is zero, any negative reputation should result in max penalty score
if postParam.MaxReportReputation.IsZero() {
return sdk.OneRat(), nil
}
if reputation.IsGTE(postParam.MaxReportReputation) {
return sdk.OneRat(), nil
}
penaltyScore := reputation.ToRat().Quo(postParam.MaxReportReputation.ToRat()).Round(types.PrecisionFactor)
if penaltyScore.GT(sdk.OneRat()) {
return sdk.OneRat(), nil
}
return penaltyScore, nil
}