feat: sign in to IMVU for Access Pass products (#4)
Co-authored-by: dhkatz <[email protected]>
This commit was merged in pull request #4.
This commit is contained in:
1 parent
dd8205602c
commit
145ebf25c1
11 files changed
+1001
-24
No files matched your search
@@ -30,6 +30,8 @@ type Importer struct {
|
||||
UserAgent string
|
||||
HTTP *http.Client // for uploads to the catalog
|
||||
Tmp string // a folder for working files
|
||||
Sessions string // a folder for the IMVU accounts' sessions (their cookies)
|
||||
APIURL string // IMVU's API (imvu.APIURL, or a test server's)
|
||||
Logger *log.Logger
|
||||
|
||||
ConfigEvery time.Duration // how often the config is fetched (default a minute)
|
||||
@@ -38,6 +40,7 @@ type Importer struct {
|
||||
|
||||
paused bool
|
||||
concurrency int
|
||||
signIn signedIn
|
||||
}
|
||||
|
||||
// Run runs until ctx ends. Jobs it's working on then are left claimed; the hub queues them again after 30 minutes.
|
||||
@@ -195,6 +198,8 @@ func (im *Importer) syncAccounts(ctx context.Context, list []hub.Account) {
|
||||
} else if n > 0 {
|
||||
im.Logger.Printf("accounts: forgot %d the hub no longer lists", n)
|
||||
}
|
||||
im.forgetSessions(ids)
|
||||
im.ensureSignedIn(ctx, list)
|
||||
}
|
||||
|
||||
func upperFirst(s string) string {
|
||||
@@ -211,15 +216,19 @@ func (im *Importer) job(ctx context.Context, j hub.Job) {
|
||||
if ctx.Err() != nil {
|
||||
return
|
||||
}
|
||||
what := fmt.Sprintf("IMVU product %d", j.IMVUProduct)
|
||||
if j.Kind == "list" {
|
||||
what = fmt.Sprintf("IMVU shop %d", j.IMVUCreator)
|
||||
}
|
||||
if err != nil {
|
||||
im.Logger.Printf("job %s (IMVU product %d): failed: %v", j.ID, j.IMVUProduct, err)
|
||||
im.Logger.Printf("job %s (%s): failed: %v", j.ID, what, err)
|
||||
out = &hub.Outcome{Status: "failed", Reason: upperFirst(err.Error()) + "."}
|
||||
}
|
||||
if out == nil {
|
||||
return // the catalog's upload registered it: the job is done
|
||||
}
|
||||
if out.Status == "unchanged" {
|
||||
im.Logger.Printf("job %s (IMVU product %d): no revision after %d", j.ID, j.IMVUProduct, out.IMVURevision)
|
||||
im.Logger.Printf("job %s (%s): no revision after %d", j.ID, what, out.IMVURevision)
|
||||
}
|
||||
if err := im.Hub.Report(ctx, j.ID, *out); err != nil && ctx.Err() == nil {
|
||||
im.Logger.Printf("job %s: reporting: %v", j.ID, err)
|
||||
@@ -229,8 +238,15 @@ func (im *Importer) job(ctx context.Context, j hub.Job) {
|
||||
// run imports a job's product: its newest revision after the last one imported. It returns the outcome to report,
|
||||
// or nil when the upload registered it.
|
||||
func (im *Importer) run(ctx context.Context, j hub.Job, start time.Time) (*hub.Outcome, error) {
|
||||
if j.Kind == "list" {
|
||||
return im.listShop(ctx, j)
|
||||
}
|
||||
if j.Rating == "" {
|
||||
return nil, errors.New("the hub doesn't know this product's rating: IMVU hides it from the signed out (an Access Pass product?), and the importer can't sign in to IMVU yet")
|
||||
rating, err := im.lookUp(ctx, j)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
j.Rating = rating
|
||||
}
|
||||
latest, probes, err := im.CDN.Latest(ctx, j.IMVUProduct, j.LastRevision+1)
|
||||
if err != nil {
|
||||
|
||||
@@ -15,6 +15,8 @@ import (
|
||||
"log"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"strings"
|
||||
"sync"
|
||||
"testing"
|
||||
@@ -29,20 +31,24 @@ import (
|
||||
|
||||
// fakeHub is the hub's importer API, as t5de-hub's docs/products.md has it, and a catalog's upload.
|
||||
type fakeHub struct {
|
||||
t *testing.T
|
||||
mu sync.Mutex
|
||||
cfg hub.Config
|
||||
queue []hub.Job
|
||||
claims int
|
||||
hello map[string]string
|
||||
reports map[string]hub.Outcome
|
||||
done map[string]map[string][]byte // job → the uploaded product's files
|
||||
account map[string]hub.AccountReport
|
||||
catalog *httptest.Server
|
||||
t *testing.T
|
||||
mu sync.Mutex
|
||||
cfg hub.Config
|
||||
queue []hub.Job
|
||||
claims int
|
||||
hello map[string]string
|
||||
reports map[string]hub.Outcome
|
||||
done map[string]map[string][]byte // job → the uploaded product's files
|
||||
account map[string]hub.AccountReport
|
||||
reportsOf map[string][]hub.AccountReport // every report, by account
|
||||
listings map[string][]hub.Seen // "list" jobs' products
|
||||
infos map[string]*hub.Seen // what the importer saw of jobs' products
|
||||
catalog *httptest.Server
|
||||
}
|
||||
|
||||
func newFakeHub(t *testing.T) (*fakeHub, *httptest.Server) {
|
||||
h := &fakeHub{t: t, reports: map[string]hub.Outcome{}, done: map[string]map[string][]byte{}, account: map[string]hub.AccountReport{}}
|
||||
h := &fakeHub{t: t, reports: map[string]hub.Outcome{}, done: map[string]map[string][]byte{}, account: map[string]hub.AccountReport{},
|
||||
reportsOf: map[string][]hub.AccountReport{}, listings: map[string][]hub.Seen{}, infos: map[string]*hub.Seen{}}
|
||||
h.catalog = httptest.NewServer(http.HandlerFunc(h.upload))
|
||||
t.Cleanup(h.catalog.Close)
|
||||
s := httptest.NewServer(http.HandlerFunc(h.serve))
|
||||
@@ -82,6 +88,26 @@ func (h *fakeHub) serve(w http.ResponseWriter, r *http.Request) {
|
||||
"token": fmt.Sprintf("import:%s:%v", job, body["imvuRevision"]),
|
||||
"url": h.catalog.URL + "/api/v1/uploads",
|
||||
})
|
||||
case strings.HasSuffix(path, "/listing"):
|
||||
var l struct{ Products []hub.Seen }
|
||||
b, _ := json.Marshal(body)
|
||||
json.Unmarshal(b, &l)
|
||||
job := strings.TrimSuffix(strings.TrimPrefix(path, "/jobs/"), "/listing")
|
||||
h.listings[job] = l.Products
|
||||
json.NewEncoder(w).Encode(map[string]int{"listed": len(l.Products), "queued": len(l.Products)})
|
||||
case strings.HasSuffix(path, "/info"):
|
||||
job := strings.TrimSuffix(strings.TrimPrefix(path, "/jobs/"), "/info")
|
||||
answer := map[string]any{"rating": nil, "covered": false}
|
||||
if body["found"] == true {
|
||||
var p hub.Seen
|
||||
b, _ := json.Marshal(body)
|
||||
json.Unmarshal(b, &p)
|
||||
h.infos[job] = &p
|
||||
answer = map[string]any{"rating": p.Rating, "covered": p.CreatorID == 903}
|
||||
} else {
|
||||
h.infos[job] = nil
|
||||
}
|
||||
json.NewEncoder(w).Encode(answer)
|
||||
case strings.HasPrefix(path, "/jobs/"):
|
||||
var o hub.Outcome
|
||||
b, _ := json.Marshal(body)
|
||||
@@ -93,6 +119,14 @@ func (h *fakeHub) serve(w http.ResponseWriter, r *http.Request) {
|
||||
b, _ := json.Marshal(body)
|
||||
json.Unmarshal(b, &a)
|
||||
h.account[strings.TrimPrefix(path, "/accounts/")] = a
|
||||
h.reportsOf[strings.TrimPrefix(path, "/accounts/")] = append(h.reportsOf[strings.TrimPrefix(path, "/accounts/")], a)
|
||||
if a.Received { // the hub drops its sealed copy
|
||||
for i := range h.cfg.Accounts {
|
||||
if h.cfg.Accounts[i].ID == strings.TrimPrefix(path, "/accounts/") {
|
||||
h.cfg.Accounts[i].Sealed = nil
|
||||
}
|
||||
}
|
||||
}
|
||||
w.Write([]byte(`{}`))
|
||||
default:
|
||||
http.NotFound(w, r)
|
||||
@@ -130,7 +164,7 @@ func (h *fakeHub) upload(w http.ResponseWriter, r *http.Request) {
|
||||
func (h *fakeHub) settled(n int) bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return len(h.reports)+len(h.done) >= n
|
||||
return len(h.reports)+len(h.done)+len(h.listings) >= n
|
||||
}
|
||||
|
||||
func sealFor(t *testing.T, k *keys.Key, imvuID int64, password string) *string {
|
||||
@@ -159,6 +193,10 @@ func newImporterIn(t *testing.T, h *httptest.Server, cdn *imvutest.CDN, dir stri
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
sessions := filepath.Join(dir, "sessions")
|
||||
if err := os.MkdirAll(sessions, 0o700); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
var logs bytes.Buffer
|
||||
t.Cleanup(func() {
|
||||
if t.Failed() {
|
||||
@@ -173,6 +211,7 @@ func newImporterIn(t *testing.T, h *httptest.Server, cdn *imvutest.CDN, dir stri
|
||||
Version: "1.2.3",
|
||||
UserAgent: "test",
|
||||
Tmp: t.TempDir(),
|
||||
Sessions: sessions,
|
||||
Logger: log.New(&logs, "", 0),
|
||||
ConfigEvery: 50 * time.Millisecond,
|
||||
ClaimEvery: 10 * time.Millisecond,
|
||||
@@ -247,7 +286,7 @@ func TestImports(t *testing.T) {
|
||||
if o := h.reports["b"]; o.Status != "unchanged" || o.IMVURevision != 3 {
|
||||
t.Errorf("job b: %+v", o)
|
||||
}
|
||||
if o := h.reports["c"]; o.Status != "failed" || !strings.Contains(o.Reason, "rating") {
|
||||
if o := h.reports["c"]; o.Status != "failed" || !strings.Contains(o.Reason, "no IMVU account is signed in") {
|
||||
t.Errorf("job c: %+v", o)
|
||||
}
|
||||
if o := h.reports["d"]; o.Status != "failed" || !strings.Contains(o.Reason, "no revision") {
|
||||
@@ -266,11 +305,12 @@ func TestAccountsAndPause(t *testing.T) {
|
||||
if err := im.Accounts.Put(accounts.Account{ID: "gone", IMVUID: 1, Sealed: "x"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
okSealed := sealFor(t, im.Key, 100, "pw")
|
||||
h.cfg = hub.Config{
|
||||
Paused: true,
|
||||
Settings: hub.Settings{Rate: 1, Concurrency: 1},
|
||||
Accounts: []hub.Account{
|
||||
{ID: "ok", IMVUID: 100, Username: "Importer1", Sealed: sealFor(t, im.Key, 100, "pw")},
|
||||
{ID: "ok", IMVUID: 100, Username: "Importer1", Sealed: okSealed},
|
||||
{ID: "moved", IMVUID: 200, Username: "Importer2", Sealed: sealFor(t, im.Key, 999, "pw")},
|
||||
{ID: "waiting", IMVUID: 300, Username: "Importer3"},
|
||||
},
|
||||
@@ -284,7 +324,7 @@ func TestAccountsAndPause(t *testing.T) {
|
||||
if !h.account["ok"].Received || h.account["moved"].State != "needs_password" {
|
||||
t.Errorf("account reports %+v", h.account)
|
||||
}
|
||||
if a, ok := im.Accounts.Get("ok"); !ok || a.IMVUID != 100 || a.Sealed != *h.cfg.Accounts[0].Sealed {
|
||||
if a, ok := im.Accounts.Get("ok"); !ok || a.IMVUID != 100 || a.Sealed != *okSealed {
|
||||
t.Errorf("kept %+v, %v", a, ok)
|
||||
}
|
||||
if _, ok := im.Accounts.Get("gone"); ok {
|
||||
@@ -317,3 +357,98 @@ func TestHelloRefused(t *testing.T) {
|
||||
t.Error("ran with a refused key")
|
||||
}
|
||||
}
|
||||
|
||||
func TestSignedIn(t *testing.T) {
|
||||
api := imvutest.NewAPI(t)
|
||||
api.Users = []imvutest.User{
|
||||
{ID: 777, Username: "ImporterAP", Password: "right", AccessPass: true},
|
||||
{ID: 778, Username: "NoLuck", Password: "right"},
|
||||
{ID: 903, Username: "apkeeperAv"},
|
||||
}
|
||||
api.Products = []imvutest.Product{
|
||||
{ID: 9501, Name: "Coat", CreatorID: 903},
|
||||
{ID: 9502, Name: "Robe", CreatorID: 903, AccessPass: true},
|
||||
}
|
||||
cdn := imvutest.New(t)
|
||||
cdn.Add(9502, 1, imvutest.File{Name: "index.xml", Data: imvutest.Index(5000)})
|
||||
|
||||
h, srv := newFakeHub(t)
|
||||
dir := t.TempDir()
|
||||
im := newImporterIn(t, srv, cdn, dir)
|
||||
im.APIURL = api.URL
|
||||
h.cfg = hub.Config{
|
||||
Settings: hub.Settings{Rate: 10, Concurrency: 1},
|
||||
Accounts: []hub.Account{
|
||||
{ID: "wrong", IMVUID: 778, Username: "NoLuck", Sealed: sealFor(t, im.Key, 778, "not the password")},
|
||||
{ID: "ap", IMVUID: 777, Username: "ImporterAP", Sealed: sealFor(t, im.Key, 777, "right")},
|
||||
},
|
||||
}
|
||||
h.queue = []hub.Job{
|
||||
{ID: "list", Kind: "list", IMVUCreator: 903},
|
||||
{ID: "robe", Kind: "import", IMVUProduct: 9502}, // hidden from the hub: no rating
|
||||
{ID: "gone", Kind: "import", IMVUProduct: 9599},
|
||||
}
|
||||
runUntil(t, im, func() bool { return h.settled(3) })
|
||||
|
||||
// The first account is refused; the second signs in, with Access Pass (which IMVU shows it alone).
|
||||
if r := h.account["wrong"]; r.State != "sign_in_failed" || !strings.Contains(r.Message, "Invalid username or password") {
|
||||
t.Errorf("refused account: %+v", r)
|
||||
}
|
||||
if r := h.account["ap"]; r.State != "ok" || r.AccessPass == nil || !*r.AccessPass {
|
||||
t.Errorf("signed-in account: %+v", r)
|
||||
}
|
||||
// The shop as the account lists it: the Access Pass product too.
|
||||
if got := h.listings["list"]; len(got) != 2 || got[1] != (hub.Seen{ID: 9502, Name: "Robe", Rating: "adult", CreatorID: 903, CreatorName: "creator903", Purchasable: true}) {
|
||||
t.Errorf("listing %+v", got)
|
||||
}
|
||||
// The hidden product: looked up, reported, imported as adult.
|
||||
if p := h.infos["robe"]; p == nil || p.Rating != "adult" || p.CreatorID != 903 {
|
||||
t.Errorf("robe info %+v", p)
|
||||
}
|
||||
if robe := h.done["robe"]; robe == nil || !bytes.Contains(robe["index.xml"], []byte("<ProductRating>AP</ProductRating>")) {
|
||||
t.Errorf("robe: %q (reports %v)", robe, h.reports)
|
||||
}
|
||||
if p, ok := h.infos["gone"]; !ok || p != nil {
|
||||
t.Errorf("gone info %+v, %v", p, ok)
|
||||
}
|
||||
if o := h.reports["gone"]; o.Status != "failed" || !strings.Contains(o.Reason, "deleted?") {
|
||||
t.Errorf("gone: %+v", o)
|
||||
}
|
||||
|
||||
// A restart resumes the saved session: no new sign-in.
|
||||
signIns := api.SignIns
|
||||
h.mu.Lock()
|
||||
h.queue = []hub.Job{{ID: "list2", Kind: "list", IMVUCreator: 903}}
|
||||
h.mu.Unlock()
|
||||
again := newImporterIn(t, srv, cdn, dir)
|
||||
again.Key = im.Key
|
||||
again.APIURL = api.URL
|
||||
runUntil(t, again, func() bool { return h.settled(4) })
|
||||
if api.SignIns != signIns {
|
||||
t.Errorf("signed in again: %d sign-ins, was %d", api.SignIns, signIns)
|
||||
}
|
||||
// The refused account isn't tried again within the hour (same password), even after a restart.
|
||||
refusals := 0
|
||||
for _, r := range h.reportsOf["wrong"] {
|
||||
if r.State == "sign_in_failed" {
|
||||
refusals++
|
||||
}
|
||||
}
|
||||
if refusals != 1 {
|
||||
t.Errorf("the refused account was refused %d times", refusals)
|
||||
}
|
||||
|
||||
// IMVU ends the session: the importer signs in again.
|
||||
api.EndSessions()
|
||||
h.mu.Lock()
|
||||
h.queue = []hub.Job{{ID: "list3", Kind: "list", IMVUCreator: 903}, {ID: "list4", Kind: "list", IMVUCreator: 903}}
|
||||
h.mu.Unlock()
|
||||
runUntil(t, again, func() bool {
|
||||
h.mu.Lock()
|
||||
defer h.mu.Unlock()
|
||||
return (h.listings["list3"] != nil || h.reports["list3"].Status != "") && (h.listings["list4"] != nil || h.reports["list4"].Status != "")
|
||||
})
|
||||
if api.SignIns != signIns+1 || len(h.listings["list3"]) != 2 || len(h.listings["list4"]) != 2 {
|
||||
t.Errorf("after the session ended: %d sign-ins (want %d); listings %d and %d, reports %v", api.SignIns, signIns+1, len(h.listings["list3"]), len(h.listings["list4"]), h.reports)
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,236 @@
|
||||
package importer
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
"sync"
|
||||
"time"
|
||||
|
||||
"git.t5de.dev/t5de/t5de-importer/internal/hub"
|
||||
"git.t5de.dev/t5de/t5de-importer/internal/imvu"
|
||||
)
|
||||
|
||||
// SignInRetry is how long an account IMVU refused is left before trying it again (unless its password changes):
|
||||
// IMVU limits sign-ins.
|
||||
const SignInRetry = time.Hour
|
||||
|
||||
// signedIn is the IMVU account the importer uses: the first of the console's accounts, in order, that signs in.
|
||||
type signedIn struct {
|
||||
mu sync.Mutex
|
||||
session *imvu.Session
|
||||
account string // the hub's ID of its account
|
||||
list []hub.Account // the console's accounts, as last fetched
|
||||
}
|
||||
|
||||
// errNoSession is no account signed in.
|
||||
var errNoSession = errors.New("no IMVU account is signed in")
|
||||
|
||||
// withSession runs f with the signed-in session, signing in first if none is; if IMVU ended the session, it signs
|
||||
// in again and runs f once more.
|
||||
func (im *Importer) withSession(ctx context.Context, f func(*imvu.Session) error) error {
|
||||
for attempt := 0; attempt < 2; attempt++ {
|
||||
s := im.current()
|
||||
if s == nil {
|
||||
im.ensureSignedIn(ctx, nil)
|
||||
if s = im.current(); s == nil {
|
||||
return errNoSession
|
||||
}
|
||||
}
|
||||
err := f(s)
|
||||
if !errors.Is(err, imvu.ErrSignedOut) {
|
||||
return err
|
||||
}
|
||||
im.signedOut(s)
|
||||
}
|
||||
return imvu.ErrSignedOut
|
||||
}
|
||||
|
||||
// current is the signed-in session, or nil.
|
||||
func (im *Importer) current() *imvu.Session {
|
||||
im.signIn.mu.Lock()
|
||||
defer im.signIn.mu.Unlock()
|
||||
return im.signIn.session
|
||||
}
|
||||
|
||||
// signedOut drops a session IMVU no longer takes; the next config poll signs in again.
|
||||
func (im *Importer) signedOut(s *imvu.Session) {
|
||||
im.signIn.mu.Lock()
|
||||
defer im.signIn.mu.Unlock()
|
||||
if im.signIn.session == s {
|
||||
im.signIn.session = nil
|
||||
im.Logger.Printf("IMVU: the session ended; signing in again")
|
||||
}
|
||||
}
|
||||
|
||||
// ensureSignedIn signs in with the first account (in the console's order) that IMVU takes, unless one is signed in
|
||||
// already and still listed. Each sign-in is reported: state ok and whether the account has Access Pass, or the
|
||||
// refusal. A nil list means the last one.
|
||||
func (im *Importer) ensureSignedIn(ctx context.Context, list []hub.Account) {
|
||||
im.signIn.mu.Lock()
|
||||
defer im.signIn.mu.Unlock()
|
||||
if list == nil {
|
||||
list = im.signIn.list
|
||||
} else {
|
||||
im.signIn.list = list
|
||||
}
|
||||
listed := map[string]bool{}
|
||||
for _, a := range list {
|
||||
listed[a.ID] = true
|
||||
}
|
||||
if im.signIn.session != nil {
|
||||
if listed[im.signIn.account] {
|
||||
return
|
||||
}
|
||||
im.Logger.Printf("IMVU: account %s was removed on the console; signing out", im.signIn.account)
|
||||
im.signIn.session.Forget()
|
||||
im.signIn.session = nil
|
||||
}
|
||||
for _, a := range list {
|
||||
kept, ok := im.Accounts.Get(a.ID)
|
||||
if !ok {
|
||||
continue // no password yet (or it needs entering again)
|
||||
}
|
||||
if time.Since(kept.RefusedAt) < SignInRetry {
|
||||
continue // refused with this password lately (a new one clears it)
|
||||
}
|
||||
s := &imvu.Session{URL: im.APIURL, UserAgent: imvu.BrowserUserAgent, Limiter: im.CDN.Limiter, File: filepath.Join(im.Sessions, a.ID+".json")}
|
||||
resumed, err := s.Resume(ctx)
|
||||
if err != nil {
|
||||
im.Logger.Printf("IMVU: resuming %s's session: %v", a.Username, err)
|
||||
return // IMVU unreachable: next time
|
||||
}
|
||||
if !resumed {
|
||||
password, err := im.Key.Open(kept.Sealed, a.IMVUID)
|
||||
if err == nil {
|
||||
err = s.SignIn(ctx, a.Username, password)
|
||||
}
|
||||
var refused *imvu.ErrSignIn
|
||||
if errors.As(err, &refused) {
|
||||
im.Logger.Printf("IMVU: %s: %v", a.Username, err)
|
||||
im.refused(a.ID)
|
||||
s.Forget()
|
||||
im.reportAccount(ctx, a.ID, hub.AccountReport{State: "sign_in_failed", Message: upperFirst(refused.Message)})
|
||||
continue
|
||||
}
|
||||
if err != nil {
|
||||
im.Logger.Printf("IMVU: signing in as %s: %v", a.Username, err)
|
||||
return
|
||||
}
|
||||
}
|
||||
if s.UserID != a.IMVUID {
|
||||
im.Logger.Printf("IMVU: %s signed in as IMVU account %d, not %d", a.Username, s.UserID, a.IMVUID)
|
||||
im.refused(a.ID)
|
||||
s.Forget()
|
||||
im.reportAccount(ctx, a.ID, hub.AccountReport{State: "sign_in_failed", Message: fmt.Sprintf("Signed in as another IMVU account (%d).", s.UserID)})
|
||||
continue
|
||||
}
|
||||
im.signIn.session, im.signIn.account = s, a.ID
|
||||
ap := s.AccessPass
|
||||
how := "signed in"
|
||||
if resumed {
|
||||
how = "resumed the session"
|
||||
}
|
||||
im.Logger.Printf("IMVU: %s as %s (Access Pass: %v)", how, a.Username, ap)
|
||||
im.reportAccount(ctx, a.ID, hub.AccountReport{State: "ok", AccessPass: &ap})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
func (im *Importer) refused(id string) {
|
||||
if err := im.Accounts.Refused(id, time.Now()); err != nil {
|
||||
im.Logger.Printf("account %s: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
func (im *Importer) reportAccount(ctx context.Context, id string, r hub.AccountReport) {
|
||||
if err := im.Hub.ReportAccount(ctx, id, r); err != nil {
|
||||
im.Logger.Printf("account %s: reporting: %v", id, err)
|
||||
}
|
||||
}
|
||||
|
||||
// forgetSessions removes saved sessions of accounts the hub no longer lists.
|
||||
func (im *Importer) forgetSessions(listed map[string]bool) {
|
||||
entries, err := os.ReadDir(im.Sessions)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, e := range entries {
|
||||
id, ok := cutSuffix(e.Name(), ".json")
|
||||
if ok && !listed[id] {
|
||||
os.Remove(filepath.Join(im.Sessions, e.Name()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
func cutSuffix(s, suffix string) (string, bool) {
|
||||
if len(s) < len(suffix) || s[len(s)-len(suffix):] != suffix {
|
||||
return s, false
|
||||
}
|
||||
return s[:len(s)-len(suffix)], true
|
||||
}
|
||||
|
||||
// listShop does a "list" job: the shop as the signed-in account lists it, reported to the hub, which queues its
|
||||
// products.
|
||||
func (im *Importer) listShop(ctx context.Context, j hub.Job) (*hub.Outcome, error) {
|
||||
var products []imvu.Seen
|
||||
err := im.withSession(ctx, func(s *imvu.Session) (err error) {
|
||||
products, err = s.Shop(ctx, j.IMVUCreator)
|
||||
return err
|
||||
})
|
||||
if errors.Is(err, errNoSession) {
|
||||
return nil, errors.New("no IMVU account is signed in to list the shop with")
|
||||
}
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
seen := make([]hub.Seen, len(products))
|
||||
for i, p := range products {
|
||||
seen[i] = hub.Seen(p)
|
||||
}
|
||||
listed, queued, err := im.Hub.Listing(ctx, j.ID, seen)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
im.Logger.Printf("job %s: IMVU shop %d lists %d products signed in; %d queued", j.ID, j.IMVUCreator, listed, queued)
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
// lookUp asks the signed-in account about a product IMVU hides from the hub, tells the hub, and returns its rating.
|
||||
func (im *Importer) lookUp(ctx context.Context, j hub.Job) (string, error) {
|
||||
var p *imvu.Seen
|
||||
accessPass := false
|
||||
err := im.withSession(ctx, func(s *imvu.Session) (err error) {
|
||||
p, err = s.Product(ctx, j.IMVUProduct)
|
||||
accessPass = s.AccessPass
|
||||
return err
|
||||
})
|
||||
if errors.Is(err, errNoSession) {
|
||||
return "", errors.New("IMVU hides this product from the hub (an Access Pass product?), and no IMVU account is signed in to look it up")
|
||||
}
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
var seen *hub.Seen
|
||||
if p != nil {
|
||||
h := hub.Seen(*p)
|
||||
seen = &h
|
||||
}
|
||||
rating, covered, err := im.Hub.Info(ctx, j.ID, seen)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
switch {
|
||||
case rating == "" && !accessPass:
|
||||
return "", errors.New("IMVU doesn't show this product to the importer's account, which has no Access Pass")
|
||||
case rating == "":
|
||||
return "", errors.New("IMVU doesn't show this product to the importer's account either (deleted?)")
|
||||
case !covered && p != nil:
|
||||
return "", fmt.Errorf("the grant doesn't cover this product: IMVU says it's another creator's (%d)", p.CreatorID)
|
||||
case !covered:
|
||||
return "", errors.New("the grant doesn't cover this product")
|
||||
}
|
||||
return rating, nil
|
||||
}
|
||||
Reference in new issue
Block a user