-
Notifications
You must be signed in to change notification settings - Fork 0
/
meme.go
237 lines (193 loc) · 5.54 KB
/
meme.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
package models
import (
"cloud.google.com/go/datastore"
"golang.org/x/net/context"
"github.com/miyurusagarage/memeeconomy/shared"
"fmt"
"time"
"github.com/nu7hatch/gouuid"
"github.com/miyurusagarage/memeeconomy/utils"
"google.golang.org/api/iterator"
"strconv"
)
type Meme struct {
Key *datastore.Key `datastore:"__key__"`
CreatedDate time.Time
CreatedUserId string
CurrentInvestments int
Description string
ExpirationDate time.Time
ImagePath string
InternalLikes int
ModifiedDate time.Time
SocialPostThreshold int
SocialFbPostLink string
SocialLikes int
SocialPostsCreated bool
SocialPostedDate time.Time
SocialShares int
SocialUpdatedDate time.Time
IsExpired bool
Title string
TotalFame int
}
func (this *Meme) Save() (err error) {
this.CreatedDate = time.Now()
socialPostThreshold, _ := GetConfigByName("SocialPostThreshold")
this.SocialPostThreshold, _ = strconv.Atoi(socialPostThreshold.Value)
this.IsExpired = false
var id *uuid.UUID
id, _ = uuid.NewV4()
urlId := id.String()
key := datastore.NameKey(this.kind(), urlId, nil)
ctx := context.Background()
memeExpirationInDaysConfig , _ := GetConfigByName("MemeExpirationInDays")
memeExpirationInDays , _ := strconv.Atoi(memeExpirationInDaysConfig.Value)
this.ExpirationDate = time.Now().Add(time.Duration(memeExpirationInDays) * (time.Hour * 24))
if _, err := shared.DatastoreClient.Put(ctx, key, this); err != nil {
fmt.Println(err)
return err
}
return nil
}
func (this *Meme) Update() (err error) {
this.ModifiedDate = time.Now()
if this.CurrentInvestments >= this.SocialPostThreshold {
if this.SocialFbPostLink == "" {
postId := utils.PostMemeToFb(this.ImagePath, this.Title)
this.SocialFbPostLink = postId
this.SocialPostedDate = time.Now()
memeExpirationInDaysConfig , _ := GetConfigByName("MemeExpirationInDays")
memeExpirationInDays , _ := strconv.Atoi(memeExpirationInDaysConfig.Value)
this.ExpirationDate = time.Now().Add(time.Duration(memeExpirationInDays) * (time.Hour * 24))
this.SocialPostsCreated = true
}
}
this.TotalFame = (this.SocialLikes * 2) + (this.SocialShares * 10) + this.CurrentInvestments + (this.InternalLikes * 2)
ctx := context.Background()
if _, err := shared.DatastoreClient.Put(ctx, this.Key, this); err != nil {
fmt.Println(err)
return err
}
return nil
}
func (this *Meme) kind() (str string) {
return "meme"
}
func (this *Meme) TotalLikes() (str int) {
return this.InternalLikes + this.SocialLikes
}
func (this *Meme) CanInvest() ( bool) {
return !this.IsExpired
}
func (this *Meme) DaysToExpire() ( string) {
diff := this.ExpirationDate.Sub(time.Now()).Hours()
expireString := strconv.Itoa(int(diff/24)) + " days left"
if diff < 0 {
expireString = "Expired"
}
return expireString
}
func GetMemeFromId(id string) (objs *Meme, err error) {
ctx := context.Background()
key := datastore.NameKey("meme", id, nil)
var data Meme
er := shared.DatastoreClient.Get(ctx, key, &data)
if er != nil {
return nil, er
}
return &data, nil
}
func GetMemeFromKey(key *datastore.Key) (objs *Meme, err error) {
ctx := context.Background()
var data Meme
er := shared.DatastoreClient.Get(ctx, key, &data)
if er != nil {
return nil, er
}
return &data, nil
}
func GetRecentMemes(offset int, pageSize int, fromTime time.Time) (objs *[]Meme, total int, err error) {
ctx := context.Background()
q := datastore.NewQuery("meme")
q = q.Filter("CreatedDate <", fromTime)
q = q.Order("-CreatedDate")
q = q.Offset(offset)
q = q.Limit(pageSize)
var data []Meme
_, er := shared.DatastoreClient.GetAll(ctx, q, &data)
//count for pagination
q = datastore.NewQuery("meme")
count := 0
count, er = shared.DatastoreClient.Count(ctx, q, )
if er != nil {
return nil, 0, er
}
return &data, count , nil
}
func GetTopMemes(offset int, pageSize int) (objs *[]Meme, total int, err error) {
ctx := context.Background()
q := datastore.NewQuery("meme")
//q = q.Filter("IsExpired =",false)
q = q.Order("-TotalFame")
q = q.Offset(offset)
q = q.Limit(pageSize)
var data []Meme
it := shared.DatastoreClient.Run(ctx, q)
for {
var meme Meme
_, err := it.Next(&meme)
if err == iterator.Done {
break
}
if err != nil {
println(err.Error())
break
}
data = append(data, meme)
}
//count for pagination
q = datastore.NewQuery("meme")
count, er := shared.DatastoreClient.Count(ctx, q, )
if er != nil {
return nil, 0, er
}
return &data, count , nil
}
func GetRecentMemesByUser(key string ,offset int, pageSize int) (objs *[]Meme, total int, err error) {
ctx := context.Background()
q := datastore.NewQuery("meme")
q = q.Filter("CreatedUserId =", key)
q = q.Offset(offset)
q = q.Limit(pageSize)
var data []Meme
_, er := shared.DatastoreClient.GetAll(ctx, q, &data)
//count for pagination
q = datastore.NewQuery("meme")
count := 0
count, er = shared.DatastoreClient.Count(ctx, q, )
if er != nil {
return nil, 0, er
}
return &data, count , nil
}
func GetAllMemes( ) (objs *[]Meme, err error) {
ctx := context.Background()
q := datastore.NewQuery("meme")
var data []Meme
_, er := shared.DatastoreClient.GetAll(ctx, q, &data)
if er != nil {
return nil, er
}
return &data , nil
}
func GetToBeExpiredMemes( ) (objs *[]Meme, err error) {
ctx := context.Background()
q := datastore.NewQuery("meme").Filter("ExpirationDate <", time.Now()).Filter("IsExpired =", false)
var data []Meme
_, er := shared.DatastoreClient.GetAll(ctx, q, &data)
if er != nil {
return nil, er
}
return &data , nil
}