Exemplo n.º 1
0
// 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
}