-
Notifications
You must be signed in to change notification settings - Fork 0
/
profile_state.go
107 lines (99 loc) · 3.05 KB
/
profile_state.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
package user
import (
"github.com/chris-sg/bst_api/db"
"github.com/chris-sg/bst_api/eagate/util"
"github.com/chris-sg/bst_api/models/user_models"
"github.com/chris-sg/bst_api/utilities"
bst_models "github.com/chris-sg/bst_server_models"
"github.com/golang/glog"
)
// createClientForUser will generate a http client for the provided user
// model. This is intended to only be used for this specific user model,
// as it will use cookies from the database for eagate integration.
func CreateClientForUser(userModel user_models.User) (client util.EaClient, err bst_models.Error) {
err = bst_models.ErrorOK
client = util.GenerateClient()
client.SetUserModel(userModel)
user, exists, errs := db.GetUserDb().RetrieveUserByUserId(userModel.Name)
if !exists {
err = bst_models.ErrorNoEaUser
return
}
if utilities.PrintErrors("failed to retrieve cookie:", errs) {
err = bst_models.ErrorNoCookie
return
}
if len(user.Cookie) == 0 {
err = bst_models.ErrorNoCookie
return
}
client.SetEaCookie(util.CookieFromRawCookie(user.Cookie))
if !client.LoginState() {
err = bst_models.ErrorBadCookie
}
return
}
func ProfileEaSubscriptionState(client util.EaClient) (subscriptionType string, err bst_models.Error) {
err = bst_models.ErrorOK
const paybookResource = "/payment/mybook/paybook.html"
PaybookUri := util.BuildEaURI(paybookResource)
document, _, err := util.GetPageContentAsGoQuery(client.Client, PaybookUri)
eaSubSelection := document.Find("div#id_paybook_all .cl_course_name").First()
if eaSubSelection == nil {
err = bst_models.ErrorBadCookie
return
}
subscriptionType = eaSubSelection.Text()
return
}
func RunUpdatesOnAllEaUsers() {
users, errs := db.GetUserDb().RetrieveUsersForUpdate()
if len(errs) > 0 {
glog.Errorf("failed to update users due to selection error")
glog.Error(errs)
return
}
for _, user := range users {
func() {
client, err := CreateClientForUser(user)
defer client.UpdateCookie()
if err.Equals(bst_models.ErrorBadCookie) || err.Equals(bst_models.ErrorNoCookie) {
user.Cookie = ""
user.Expiration = 0
errs = db.GetUserDb().UpdateUser(user)
if len(errs) > 0 {
glog.Errorf("failed to save user %s", user.WebUser)
glog.Error(errs)
}
return
}
if !err.Equals(bst_models.ErrorOK) {
glog.Warningf("client upgrade for %s failed: %s", user, err.Message)
return
}
if !client.LoginState() {
user.Cookie = ""
user.Expiration = 0
errs = db.GetUserDb().UpdateUser(user)
if len(errs) > 0 {
glog.Errorf("failed to save user %s", user.WebUser)
glog.Error(errs)
}
return
}
user.Cookie = client.GetEaCookie().String()
user.Expiration = client.GetEaCookie().Expires.UnixNano() / 1000
subState, err := ProfileEaSubscriptionState(client)
if !err.Equals(bst_models.ErrorOK) {
glog.Warningf("client upgrade for %s failed: %s", user, err.Message)
return
}
user.EaSubscription = subState
errs = db.GetUserDb().UpdateUser(user)
if len(errs) > 0 {
glog.Errorf("failed to save user %s", user.WebUser)
glog.Error(errs)
}
}()
}
}