This repository has been archived by the owner on Apr 27, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
user.go
113 lines (97 loc) · 3.22 KB
/
user.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
// Copyright 2014 Google Inc. All rights reserved.
//
// 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 abelana
import (
"fmt"
"io"
"net/http"
"strings"
"appengine"
"appengine/datastore"
"appengine/urlfetch"
"code.google.com/p/goauth2/oauth"
"google.golang.org/cloud"
"google.golang.org/cloud/storage"
)
// findUser Lookup the user (This can be called from a Transaction)
func findUser(cx appengine.Context, id string) (*User, error) {
key := datastore.NewKey(cx, "User", id, 0, nil)
u := &User{}
err := datastore.Get(cx, key, u)
return u, err
}
// createUser will create the initial datastore entry for the user
func createUser(cx appengine.Context, user User) error {
cx.Infof("CreateUser: %v", user)
user.IFollow = abelanaConfig().AutoFollowers
_, err := datastore.Put(cx, datastore.NewKey(cx, "User", user.UserID, 0, nil), &user)
if err != nil {
cx.Errorf(" CreateUser %v %v", err, user.UserID)
return err
}
addUser(cx, user.UserID, user.DisplayName) // Tell Redis
delayInitialSetup.Call(cx, user.UserID, user.Email)
return nil
}
// initialSetup will add the initial things in a somewhat reasonable way.
func initialSetup(cx appengine.Context, ID, email string) error {
for _, key := range abelanaConfig().AutoFollowers {
if err := followById(cx, ID, key); err != nil { // follow francesc
return fmt.Errorf("initialSetup: %v %v", key, err)
}
}
delayInitialPhotos.Call(cx, ID)
delayFindFollows.Call(cx, ID, email)
return nil
}
// copyUserPhoto will copy the photo from, will likey be called from delayFunc
func copyUserPhoto(cx appengine.Context, url string, userID string) error {
// We want a larger photo
url = strings.Replace(url, "sz=50", "sz=2048", 1)
if DEBUG {
cx.Infof("copyUserPhoto: %v %v", userID, url)
}
if url == "null" {
url = abelanaConfig().Silhouette
}
client := urlfetch.Client(cx)
resp, err := client.Get(url)
defer resp.Body.Close()
if err != nil {
cx.Errorf(" copyUserPhoto: %v %v %v", userID, url, err)
return err
}
tok, _, err := appengine.AccessToken(cx, "https://www.googleapis.com/auth/devstorage.read_write")
if err != nil {
cx.Errorf(" AccessToken %v", err)
return err
}
transport := &oauth.Transport{
Token: &oauth.Token{AccessToken: tok},
Transport: &urlfetch.Transport{Context: cx},
}
clnt := &http.Client{Transport: transport}
ctx := cloud.NewContext(abelanaConfig().ProjectID, clnt)
w := storage.NewWriter(ctx, abelanaConfig().Bucket, userID+".jpg", &storage.Object{ContentType: "image/jpg"})
defer w.Close()
_, err = io.Copy(w, resp.Body)
if err := w.Close(); err != nil {
cx.Errorf(" cup closing %v", err)
}
if _, err := w.Object(); err != nil {
cx.Errorf(" .Object %v", err)
}
cx.Infof("CopyUserPhoto ok %v %v", userID, url)
return nil
}