/
items.go
902 lines (814 loc) · 25 KB
/
items.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
package aliza
import (
"appengine"
"appengine/datastore"
"appengine/urlfetch"
"encoding/json"
"io/ioutil"
"log"
"net/http"
"strconv"
"strings"
"time"
"fmt"
)
type ItemMember struct {
UserKey string `json:"userkey"`
Attendant int `json:"attendant"`
PhoneNumber string `json:"phonenumber,omitempty"`
SkypeId string `json:"skypeid,omitempty"`
}
type Item struct {
Id string `json:"id" datastore:"-"`
Image string `json:"image"`
Thumbnail string `json:"thumbnail"`
People int `json:"people"`
Attendant int `json:"attendant"`
Latitude float64 `json:"latitude"`
Longitude float64 `json:"longitude"`
CreateTime time.Time `json:"createtime"`
// Members are whom join this item. The first member is the item owner.
// When the first member leaves, delete the item.
Members []ItemMember `json:"members"`
// Google Cloud Messaging group unique name and ID. Reference: https://developers.google.com/cloud-messaging/notifications
GcmGroupName string `json:"gcmgroupname"`
GcmGroupKey string `json:"gcmgroupkey"`
}
type ItemUpdateNotification struct {
Message string `json:"message"`
ItemId string `json:"itemid"`
RequestUserId string `json:"requestuserid"`
}
const ItemKind = "Item"
const ItemRoot = "Item Root"
// Update item state
type UpdateItemState int
const (
stateAppendMember UpdateItemState = iota
stateAddAttendant
stateDeleteMember
stateDeleteItem
stateLast // Dummy state to indicate the enum end
)
func storeItem(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
// Result, 0: success, 1: failed
var r int = http.StatusCreated
var cKey *datastore.Key = nil
// Write response finally
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusCreated {
// Changing the header after a call to WriteHeader (or Write) has no effect.
rw.Header().Set("Location", req.URL.String()+"/"+cKey.Encode())
rw.WriteHeader(http.StatusCreated)
} else {
// http.StatusBadRequest
http.Error(rw, http.StatusText(r), r)
}
}()
// Get data from body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = http.StatusBadRequest
return
}
var item Item
if err = json.Unmarshal(b, &item); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = http.StatusBadRequest
return
}
// Verify data
if item.Image == "" {
c.Errorf("The request does not specify item image URL")
r = http.StatusBadRequest
return
}
if item.Attendant <= 0 {
c.Errorf("Item attendant %d must be >= 0", item.Attendant)
r = http.StatusBadRequest
return
}
if item.Attendant >= item.People {
c.Errorf("Item attendant %d can't be greater or equal to item people %d", item.Attendant, item.People)
r = http.StatusBadRequest
return
}
if item.Latitude < -90 || item.Latitude > 90 {
c.Errorf("Latitude %d should be -90~90", item.Latitude)
r = http.StatusBadRequest
return
}
if item.Longitude < -180 || item.Longitude > 180 {
c.Errorf("Latitude %d should be -180~180", item.Longitude)
r = http.StatusBadRequest
return
}
if item.Members == nil || len(item.Members) != 1 {
c.Errorf("Owner is not set")
r = http.StatusBadRequest
return
}
if item.Members[0].Attendant <= 0 {
c.Errorf("Attendant %d <= 0", item.Members[0].Attendant)
r = http.StatusBadRequest
return
}
if item.Members[0].Attendant != item.Attendant {
c.Errorf("Confused attendant %d and owner's attendant", item.Attendant, item.Members[0].Attendant)
r = http.StatusBadRequest
return
}
if item.Members[0].PhoneNumber == "" && item.Members[0].SkypeId == "" {
c.Errorf("Phone number %s or Skype ID %s is not given", item.Members[0].PhoneNumber, item.Members[0].SkypeId)
r = http.StatusBadRequest
return
}
// Set the first member as owner to the user key
var pUser *User
var pUserKey *datastore.Key
var instanceId string = req.Header.Get(HttpHeaderInstanceId)
if pUserKey, pUser, err = searchUser(instanceId, c); err != nil {
c.Errorf("%s in searching user %v", err, instanceId)
r = http.StatusInternalServerError
return
}
item.Members[0].UserKey = pUserKey.Encode()
// Set now as the creation time. Precision to a second.
item.CreateTime = time.Unix(time.Now().Unix(), 0)
// Set GCM group name
item.GcmGroupName = pUserKey.Encode() + strconv.FormatInt(item.CreateTime.UnixNano(), 16)
// Vernon debug
c.Debugf("Create a GCM group...")
// Create a new GCM group with the owner
var operation GroupOperation
var gcmResponseCode int
// Create a new group on GCM server with the name of owner's user key
operation.Operation = "create"
operation.Notification_key_name = item.GcmGroupName
operation.Registration_ids = []string{pUser.RegistrationToken}
if gcmResponseCode = sendGroupOperationToGcm(&operation, c); gcmResponseCode != http.StatusOK {
c.Errorf("Send group operation to GCM failed")
r = gcmResponseCode
return
}
c.Infof("GCM group %s is created", item.GcmGroupName)
// Set GCM group key
item.GcmGroupKey = operation.Notification_key
// Vernon debug
c.Debugf("Store item %+v", item)
// Store item into datastore
pKey := datastore.NewKey(c, ItemKind, ItemRoot, 0, nil)
cKey, err = datastore.Put(c, datastore.NewIncompleteKey(c, ItemKind, pKey), &item)
if err != nil {
c.Errorf("%s in storing in datastore", err)
log.Println(err)
r = http.StatusInternalServerError
return
}
}
func queryItem(rw http.ResponseWriter, req *http.Request) {
// To log messages
c := appengine.NewContext(req)
if len(req.URL.Query()) == 0 {
// Get key from URL
tokens := strings.Split(req.URL.Path, "/")
var keyIndexInTokens int = 0
for i, v := range tokens {
if v == "items" {
keyIndexInTokens = i + 1
}
}
if keyIndexInTokens >= len(tokens) {
c.Debugf("Key is not given so that query all items")
queryAllItem(rw, req)
return
}
keyString := tokens[keyIndexInTokens]
if keyString == "" {
c.Debugf("Key is empty so that query all items")
queryAllItem(rw, req)
} else {
queryOneItem(rw, req, keyString)
}
} else {
searchItem(rw, req)
}
}
func queryAllItem(rw http.ResponseWriter, req *http.Request) {
// To access datastore and to log
c := appengine.NewContext(req)
c.Debugf("QueryAll()")
// Get all entities
var dst []Item
r := 0
k, err := datastore.NewQuery(ItemKind).Order("-CreateTime").GetAll(c, &dst)
if err != nil {
c.Errorf("%s", err)
r = 1
}
// Map keys and items
for i, v := range k {
dst[i].Id = v.Encode()
// Vernon debug
c.Debugf("Item from datastore %+v", dst[i])
}
// Return status. WriteHeader() must be called before call to Write
if r == 0 {
rw.WriteHeader(http.StatusOK)
} else {
http.Error(rw, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
// Vernon debug
b, _ := json.Marshal(dst)
c.Debugf("Item JSON %s", b)
// Return body
encoder := json.NewEncoder(rw)
if err = encoder.Encode(dst); err != nil {
c.Errorf("%s in encoding result %v", err, dst)
} else {
c.Infof("QueryAll() returns %d items", len(dst))
}
}
func queryOneItem(rw http.ResponseWriter, req *http.Request, keyString string) {
// To access datastore and to log
c := appengine.NewContext(req)
c.Debugf("QueryOneItem()")
// Entity
var dst Item
// Result
r := http.StatusOK
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusOK {
rw.WriteHeader(http.StatusOK)
// Return body
encoder := json.NewEncoder(rw)
if err := encoder.Encode(dst); err != nil {
c.Errorf("%s in encoding result %v", err, dst)
}
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Decode key from string
key, err := datastore.DecodeKey(keyString)
if err != nil {
c.Errorf("%s in decoding key string", err)
r = http.StatusBadRequest
return
}
// Get the entity
if err := datastore.Get(c, key, &dst); err != nil {
c.Errorf("%s in getting entity from datastore by key %s", err, keyString)
r = http.StatusNotFound
return
}
// Store key to item
dst.Id = keyString
// Vernon debug
c.Debugf("Got item %v", dst)
b, err := json.Marshal(dst)
if err != nil {
c.Errorf("%s in marshaling item %v", err, dst)
} else {
c.Debugf("Item JSON %s", b)
}
// GOTO defer()
return
}
func searchItem(rw http.ResponseWriter, req *http.Request) {
// Appengine
var c appengine.Context = appengine.NewContext(req)
c.Debugf("searchItem()")
// Get all entities
var dst []Item
// Error flag
r := 0
// Query
q := req.URL.Query()
f := datastore.NewQuery(ItemKind)
for key := range q {
switch key {
case "Image": // string
var v string = q.Get(key)
f = f.Filter(key+"=", v)
case "People": // int
v, err := strconv.Atoi(q.Get(key))
if err != nil {
c.Errorf("%s in converting %s number\n", err, key)
} else {
f = f.Filter(key+"=", v)
}
case "Attendant": // int
v, err := strconv.Atoi(q.Get(key))
if err != nil {
c.Errorf("%s in converting %s number\n", err, key)
} else {
f = f.Filter(key+"=", v)
}
case "Latitude": // float64
v, err := strconv.ParseFloat(q.Get(key), 64)
if err != nil {
c.Errorf("%s in converting %s number\n", err, key)
} else {
f = f.Filter(key+"=", v)
}
case "Longitude": // float64
v, err := strconv.ParseFloat(q.Get(key), 64)
if err != nil {
c.Errorf("%s in converting %s number\n", err, key)
} else {
f = f.Filter(key+"=", v)
}
case "CreateTime": // time.Time
// TODO: define the format of time so that time.Parse() can correctly parse
var tmp string = q.Get(key)
v, err := time.Parse(tmp, tmp)
if err != nil {
c.Errorf("%s in converting %s number\n", err, key)
} else {
f = f.Filter(key+"=", v)
}
// Vernon debug
c.Debugf("%s, %s\n", key, v)
default:
c.Infof("%s is a wrong query property\n", key)
}
}
k, err := f.GetAll(c, &dst)
if err != nil {
c.Errorf("%s in getting data from datastore\n", err)
r = 1
}
// Map keys and items
for i, v := range k {
dst[i].Id = v.Encode()
}
// Return status. WriteHeader() must be called before call to Write
if r == 0 {
rw.WriteHeader(http.StatusOK)
} else {
http.Error(rw, http.StatusText(http.StatusNotFound), http.StatusNotFound)
return
}
// Return body
encoder := json.NewEncoder(rw)
if err = encoder.Encode(dst); err != nil {
log.Println(err, "in encoding result", dst)
} else {
log.Printf("SearchItem() returns %d items\n", len(dst))
}
}
func updateItem(rw http.ResponseWriter, req *http.Request) {
// To access datastore and to log
c := appengine.NewContext(req)
c.Debugf("UpdateItem()")
// Result
r := http.StatusOK
// Item
var src Item
// Set response
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusOK {
rw.WriteHeader(http.StatusOK)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
// Get key from URL
tokens := strings.Split(req.URL.Path, "/")
var keyIndexInTokens int = 0
for i, v := range tokens {
if v == "items" {
keyIndexInTokens = i + 1
}
}
if keyIndexInTokens >= len(tokens) {
c.Debugf("Key is not given")
r = http.StatusBadRequest
return
}
keyString := tokens[keyIndexInTokens]
if keyString == "" {
c.Debugf("Key is empty")
r = http.StatusBadRequest
return
}
// Decode key from string
key, err := datastore.DecodeKey(keyString)
if err != nil {
c.Errorf("%s in decoding key string", err)
r = http.StatusBadRequest
return
}
// Get data from body
b, err := ioutil.ReadAll(req.Body)
if err != nil {
c.Errorf("%s in reading body %s", err, b)
r = http.StatusBadRequest
return
}
if err = json.Unmarshal(b, &src); err != nil {
c.Errorf("%s in decoding body %s", err, b)
r = http.StatusBadRequest
return
}
// Organize data
var pUser *User
var pKeyUser *datastore.Key
var instanceId string = req.Header.Get(HttpHeaderInstanceId)
if pKeyUser, pUser, err = searchUser(instanceId, c); err != nil {
c.Errorf("%s in searching user instance ID %s", err, instanceId)
r = http.StatusInternalServerError
return
}
if pKeyUser == nil {
c.Errorf("User instance ID %s is not found", instanceId)
r = http.StatusInternalServerError
return
}
if src.Members == nil || len(src.Members) == 0 {
src.Members = make([]ItemMember, 1)
}
src.Members[0].UserKey = pKeyUser.Encode()
src.Attendant = src.Members[0].Attendant
// Existing item got from datastore
var dst Item
var state UpdateItemState = stateLast
var notification ItemUpdateNotification
// Update datastore in a transaction
err = datastore.RunInTransaction(c, func(c appengine.Context) error {
var err1 error
r, state, err1 = updateOneItemInDatastore(c, key, src, &dst, pUser, pKeyUser, ¬ification)
return err1
}, nil)
if r != http.StatusOK || err != nil {
c.Errorf("%s in updating item in datastore", err)
return
}
// Indicate that the item is deleted
if state == stateDeleteItem {
r = http.StatusNotFound
}
// Response code received from GCM server
var gcmResponseCode int
// Notify members through Google Cloud Messaging
notification.ItemId = keyString
notification.RequestUserId = pKeyUser.Encode()
if gcmResponseCode = sendItemGcmMessage(c, &dst, ¬ification); gcmResponseCode != http.StatusOK {
c.Warningf("Send notification to all members failed")
// Keep going even in failure because datastore has updated
}
// Update Google Cloud Messaging group
if gcmResponseCode = updateItemGcmGroup(c, state, &dst, pUser); gcmResponseCode != http.StatusOK {
c.Warningf("Update GCM group failed")
// Keep going even in failure because datastore has updated
}
// GOTO defer()
return
}
func updateOneItemInDatastore(c appengine.Context,
key *datastore.Key,
src Item,
dst *Item,
pRequestUser *User,
pRequestUserKey *datastore.Key,
pNotification *ItemUpdateNotification) (r int, state UpdateItemState, err error) {
// Update state
state = stateLast
// Initial variables
r = http.StatusOK
err = nil
// Get the entity
if err = datastore.Get(c, key, dst); err != nil {
c.Errorf("%s in getting entity from datastore by key %s", err, key.Encode())
r = http.StatusNotFound
return
}
// Vernon debug
c.Debugf("Got from user %+v", src)
c.Debugf("Got from server %+v", dst)
// Modify attendant
switch {
case dst.Attendant + src.Attendant > dst.People:
c.Warningf("Too many attendants. %d + %d > %d", dst.Attendant, src.Attendant, dst.People)
r = http.StatusBadRequest
return
case dst.Attendant + src.Attendant < 0:
c.Warningf("Too few attendants. %d%d < 0", dst.Attendant, src.Attendant)
r = http.StatusBadRequest
return
default:
dst.Attendant += src.Attendant
}
// Search whether the member exists
var i int
a := dst.Members
m := src.Members[0]
for i = 0; i < len(a); i++ {
if a[i].UserKey == m.UserKey {
// The member already exists
break;
}
}
if i == len(a) && m.Attendant <= 0 {
// Non-existing has no attendant to decrease
c.Warningf("Duplicate leave. Ignore.")
r = http.StatusBadRequest
return
}
if i == len(a) {
// Append the new member
state = stateAppendMember
a = append(a, m)
pNotification.Message += fmt.Sprintf("A new user attended and now item reaches %d/%d. ",
dst.Attendant,
dst.People)
// Vernon debug
c.Infof("Notify user %s attends item %s and reaches %d/%d", pRequestUser.InstanceId, dst.GcmGroupName, dst.Attendant, dst.People)
} else {
// Add attendant to the existing member
state = stateAddAttendant
a[i].Attendant += m.Attendant
pNotification.Message += fmt.Sprintf("Member attended %d more and now item reaches %d/%d. ",
m.Attendant,
dst.Attendant,
dst.People)
// Vernon debug
c.Infof("Existing member %s attends %d more in item %s and reaches %d/%d", pRequestUser.InstanceId, m.Attendant, dst.GcmGroupName, dst.Attendant, dst.People)
}
if a[i].Attendant == 0 {
// The member leaves
if i == 0 {
// Delete item because its owner leaves
state = stateDeleteItem
pNotification.Message += "Item is closed because its owner left. "
// Vernon debug
c.Infof("Item %s is closed because its owner %s leaves", dst.GcmGroupName, pRequestUser.InstanceId)
} else {
// Delete the member from the item
state = stateDeleteMember
a[i] = a[len(a)-1]
a[len(a)-1] = ItemMember{UserKey:"", Attendant:0}
a = a[:len(a)-1]
pNotification.Message += fmt.Sprintf("A member left and item is now %d/%d. ",
dst.Attendant,
dst.People)
// Vernon debug
c.Infof("User %s leaves item %s. Now %d/%d", pRequestUser.InstanceId, dst.GcmGroupName, dst.Attendant, dst.People)
}
} else if i == 0 {
// Only the owner can update other properties
// Flag indicates whether item properties are modified
var flagModified bool = false
if (src.Image != "") {
dst.Image = src.Image
flagModified = true
}
if (src.Thumbnail != "") {
dst.Thumbnail = src.Thumbnail
flagModified = true
}
if (src.People != 0) {
dst.People = src.People
flagModified = true
}
if flagModified == true {
// Set now as the creation time. Precision to a second.
dst.CreateTime = time.Unix(time.Now().Unix(), 0)
// Don't update Latitude and Longitude because owner can update anywhere away from the shop
pNotification.Message += "Item information updated. "
// Vernon debug
c.Infof("Item %s information updated. ", dst.GcmGroupName)
}
}
// Appending and removing a member will make a point to another memory. So assign back.
dst.Members = a
// Check whether item is finished
if dst.Attendant == dst.People {
pNotification.Message += "Item is finished. Please get together! "
c.Infof("Item %s is finished. ", dst.GcmGroupName)
}
// Modify item in datastore
if state == stateDeleteItem {
// Vernon debug
c.Debugf("Item %s is going to be deleted from datastore", key.Encode())
// Delete item from datastore
if err = datastore.Delete(c, key); err != nil {
c.Errorf("%s, in deleting entity by key", err)
r = http.StatusInternalServerError
return
}
c.Infof("Item %s is deleted from datastore", key.Encode())
} else {
// Vernon debug
c.Debugf("Item %s is going to be modified in datastore", key.Encode())
// Update item in datastore
_, err = datastore.Put(c, key, dst)
if err != nil {
c.Errorf("%s in storing in datastore with key %s", err, key.Encode())
r = http.StatusInternalServerError
return
}
c.Infof("Datastore item is updated %+v", dst)
}
return
}
// Send a Google Cloud Messaging message to all the members in the item
// Success: return 200 OK
// Failure: return 500 Internal Server Error
func sendItemGcmMessage(c appengine.Context, pItem *Item, pNotification *ItemUpdateNotification) (r int) {
// Initial variables
r = http.StatusOK
// Make GCM message body
var bodyString string = fmt.Sprintf(`
{
"to":"%s",
"data": {
"message":"%s",
"itemid":"%s",
"requestuserid":"%s"
}
}`, pItem.GcmGroupKey, pNotification.Message, pNotification.ItemId, pNotification.RequestUserId)
// Make a POST request for GCM
pReq, err := http.NewRequest("POST", GcmURL, strings.NewReader(bodyString))
if err != nil {
c.Errorf("%s in makeing a HTTP request", err)
r = http.StatusInternalServerError
return
}
defer pReq.Body.Close()
pReq.Header.Add("Content-Type", "application/json")
pReq.Header.Add("Authorization", "key="+GcmApiKey)
// Debug
c.Infof("Send request to GCM server %s", *pReq)
// Send request
var client = urlfetch.Client(c)
resp, err := client.Do(pReq)
if err != nil {
c.Errorf("%s in sending request", err)
r = http.StatusInternalServerError
return
}
defer resp.Body.Close()
// Check response
c.Infof("%d %s", resp.StatusCode, resp.Status)
// Get response body
respBody, err := ioutil.ReadAll(resp.Body)
if err != nil {
c.Errorf("%s in reading response body", err)
r = http.StatusInternalServerError
return
}
c.Infof("Body: %s", respBody)
return
}
// Modify the item's Google Cloud Messaging group
// Success: 200 OK
// Failure: 400 Bad Request, 403 Forbidden, 500 Internal Server Error
func updateItemGcmGroup(c appengine.Context, state UpdateItemState, pItem *Item, pUser *User) (r int) {
// The operation structure which will be sent to GCM server
var operation GroupOperation
// The response code received from GCM server
var gcmResponseCode int
// Error
var err error
// Initial variables
r = http.StatusOK
switch state {
case stateAppendMember:
// Vernon debug
c.Infof("Adding user %s into GCM group %s", pUser.InstanceId, pItem.GcmGroupName)
// Add the member to the GCM group
operation.Operation = "add"
operation.Notification_key_name = pItem.GcmGroupName
operation.Notification_key = pItem.GcmGroupKey
operation.Registration_ids = []string{pUser.RegistrationToken}
case stateAddAttendant:
// Do nothing
return
case stateDeleteMember:
// Vernon debug
c.Infof("Removing user %s from GCM group %s", pUser.InstanceId, pItem.GcmGroupName)
// Remove the user from the GCM group
operation.Operation = "remove"
operation.Notification_key_name = pItem.GcmGroupName
operation.Notification_key = pItem.GcmGroupKey
operation.Registration_ids = []string{pUser.RegistrationToken}
case stateDeleteItem:
// Vernon debug
c.Infof("Deleting GCM group %s", pItem.GcmGroupName)
// Remove all members from the GCM group so that the group will be removed at the same time
operation.Operation = "remove"
operation.Notification_key_name = pItem.GcmGroupName
operation.Notification_key = pItem.GcmGroupKey
operation.Registration_ids = make([]string, 0, len(pItem.Members))
// Set all members' registration token
for _, v := range pItem.Members {
var user User
var pUserKey *datastore.Key
// Search user registration token
if pUserKey, err = datastore.DecodeKey(v.UserKey); err != nil {
// This should never happen
c.Errorf("%s in decodeing user key %s of existing member in group %s. This should never happen.", err, v.UserKey, pItem.GcmGroupName)
continue
}
if err = datastore.Get(c, pUserKey, &user); err != nil {
// This should never happen
c.Errorf("%s in getting user from datastore with key %s. This should never happen.", err, v.UserKey)
continue
}
operation.Registration_ids = append(operation.Registration_ids, user.RegistrationToken)
}
}
// Send the operation to GCM server
if gcmResponseCode = sendGroupOperationToGcm(&operation, c); gcmResponseCode != http.StatusOK {
c.Errorf("Send group operation %+v to GCM faied", operation)
r = gcmResponseCode
return
}
c.Infof("%s user %s to/from GCM group %s", operation.Operation, operation.Registration_ids, operation.Notification_key_name)
return
}
func deleteItem(rw http.ResponseWriter, req *http.Request) {
// To log
c := appengine.NewContext(req)
// Get key from URL
tokens := strings.Split(req.URL.Path, "/")
var keyIndexInTokens int = 0
for i, v := range tokens {
if v == "items" {
keyIndexInTokens = i + 1
}
}
if keyIndexInTokens >= len(tokens) {
c.Infof("Key is not given so that delete all items")
deleteAllItem(rw, req)
return
}
keyString := tokens[keyIndexInTokens]
if keyString == "" {
c.Infof("Key is empty so that delete all items")
deleteAllItem(rw, req)
} else {
deleteOneItem(rw, req, keyString)
}
}
func deleteAllItem(rw http.ResponseWriter, req *http.Request) {
// To access datastore and to log
c := appengine.NewContext(req)
c.Infof("deleteAll()")
// Delete root entity after other entities
r := 0
pKey := datastore.NewKey(c, ItemKind, ItemRoot, 0, nil)
if keys, err := datastore.NewQuery(ItemKind).KeysOnly().GetAll(c, nil); err != nil {
c.Errorf("%s", err)
r = 1
} else if err := datastore.DeleteMulti(c, keys); err != nil {
c.Errorf("%s", err)
r = 1
} else if err := datastore.Delete(c, pKey); err != nil {
c.Errorf("%s", err)
r = 1
}
// Return status. WriteHeader() must be called before call to Write
if r == 0 {
rw.WriteHeader(http.StatusOK)
} else {
http.Error(rw, http.StatusText(http.StatusInternalServerError), http.StatusInternalServerError)
}
}
func deleteOneItem(rw http.ResponseWriter, req *http.Request, keyString string) {
// To access datastore and to log
c := appengine.NewContext(req)
c.Infof("deleteOneItem()")
// Result
r := http.StatusNoContent
defer func() {
// Return status. WriteHeader() must be called before call to Write
if r == http.StatusNoContent {
rw.WriteHeader(http.StatusNoContent)
} else {
http.Error(rw, http.StatusText(r), r)
}
}()
key, err := datastore.DecodeKey(keyString)
if err != nil {
c.Errorf("%s in decoding key string", err)
r = http.StatusBadRequest
return
}
// Delete the entity
if err := datastore.Delete(c, key); err != nil {
c.Errorf("%s, in deleting entity by key", err)
r = http.StatusNotFound
return
}
c.Infof("Key %s is deleted", keyString)
}