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
@@ -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