forked from DeepInTheCode/QSLquery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
notify.go
146 lines (135 loc) · 4.54 KB
/
notify.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
// Copyright (C) 2013 Google Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package qslquery
import (
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"code.google.com/p/goauth2/oauth"
"code.google.com/p/google-api-go-client/mirror/v1"
"appengine"
"appengine/taskqueue"
)
// Because App Engine owns main and starts the HTTP service,
// we do our setup during initialization.
func init() {
http.HandleFunc("/notify", errorAdapter(notifyHandler))
http.HandleFunc("/processnotification", notifyProcessorHandler)
}
// notifyHandler starts a new Task Queue to process the notification ping.
func notifyHandler(w http.ResponseWriter, r *http.Request) error {
c := appengine.NewContext(r)
t := &taskqueue.Task{
Path: "/processnotification",
Method: "POST",
Header: r.Header,
}
payload, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("Unable to read request body: %s", err)
}
t.Payload = payload
// Insert a new Task in the default Task Queue.
if _, err = taskqueue.Add(c, t, ""); err != nil {
return fmt.Errorf("Failed to add new task: %s", err)
}
return nil
}
// notifyProcessorHandler processes notification pings from the API in a Task Queue.
func notifyProcessorHandler(w http.ResponseWriter, r *http.Request) {
c := appengine.NewContext(r)
not := new(mirror.Notification)
if err := json.NewDecoder(r.Body).Decode(not); err != nil {
c.Errorf("Unable to decode notification: %v", err)
return
}
userId := not.UserToken
t := authTransport(c, userId)
if t == nil {
c.Errorf("Unknown user ID: %s", userId)
return
}
svc, _ := mirror.New(t.Client())
var err error
if not.Collection == "locations" {
err = handleLocationsNotification(c, svc, not)
} else if not.Collection == "timeline" {
//And here's where the magic happens
if err := processQueries(r, svc); err != nil {
c.Errorf("Unable to process callsign queries: %v", err)
}
err = handleTimelineNotification(c, svc, not, t)
}
if err != nil {
c.Errorf("Error occured while processing notification: %s", err)
}
}
// handleLocationsNotification processes a location notification.
func handleLocationsNotification(c appengine.Context, svc *mirror.Service, not *mirror.Notification) error {
l, err := svc.Locations.Get(not.ItemId).Do()
if err != nil {
return fmt.Errorf("Unable to retrieve location: %s", err)
}
t := &mirror.TimelineItem{
Text: fmt.Sprintf("New location is %f, %f", l.Latitude, l.Longitude),
Location: l,
MenuItems: []*mirror.MenuItem{&mirror.MenuItem{Action: "NAVIGATE"}},
Notification: &mirror.NotificationConfig{Level: "DEFAULT"},
}
_, err = svc.Timeline.Insert(t).Do()
if err != nil {
return fmt.Errorf("Unable to insert timeline item: %s", err)
}
return nil
}
// handleTimelineNotification processes a timeline notification.
func handleTimelineNotification(c appengine.Context, svc *mirror.Service, not *mirror.Notification, transport *oauth.Transport) error {
for _, ua := range not.UserActions {
if ua.Type != "SHARE" {
c.Infof("I don't know what to do with this notification: %+v", ua)
continue
}
t, err := svc.Timeline.Get(not.ItemId).Do()
if err != nil {
return fmt.Errorf("Unable to retrieve timeline item: %s", err)
}
nt := &mirror.TimelineItem{
Text: fmt.Sprintf("Echoing your shared item: %s", t.Text),
Notification: &mirror.NotificationConfig{Level: "DEFAULT"},
}
tic := svc.Timeline.Insert(nt)
if t.Attachments != nil && len(t.Attachments) > 0 {
a, err := svc.Timeline.Attachments.Get(t.Id, t.Attachments[0].Id).Do()
if err != nil {
return fmt.Errorf("Unable to retrieve attachment metadata: %s", err)
}
req, err := http.NewRequest("GET", a.ContentUrl, nil)
if err != nil {
return fmt.Errorf("Unable to create new HTTP request: %s", err)
}
resp, err := transport.RoundTrip(req)
if err != nil {
return fmt.Errorf("Unable to retrieve attachment content: %s", err)
}
defer resp.Body.Close()
tic.Media(resp.Body)
}
_, err = tic.Do()
if err != nil {
return fmt.Errorf("Unable to insert timeline item: %s", err)
}
}
return nil
}