Hub: accounts, IMVU linking by tagline, proxy sign-in, presence, console
CI / test (push) Failing after 42s
CI / test (push) Failing after 42s
People sign up on the website and link IMVU accounts by putting a code in the account's tagline, which the hub reads from IMVU's public API (the most recent proof wins; link caps per person). IMVU's age verification or Access Pass make an account adult. T5DE's proxy signs in with a device code and keeps a WebSocket open, reporting the IMVU account in use and the community products it wears; /online shows those who opted in. Moderators get a console: who's online, people, link caps, bans, roles, and the audit log. Tests run the website and API against an embedded Postgres and a fake IMVU API. Co-Authored-By: Claude Opus 5.5 <[email protected]>
This commit is contained in:
1 parent
54e569bea3
commit
b9dc3f0ec1
36 files changed
+3548
-5
No files matched your search
@@ -0,0 +1,144 @@
|
||||
// Package imvu reads IMVU's public user API: who an account is, its tagline (for linking) and whether it's age
|
||||
// verified or has Access Pass. The hub calls it rarely (linking, and age checks at most daily) and never depends on
|
||||
// it for anything else.
|
||||
package imvu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"net/url"
|
||||
"strconv"
|
||||
"strings"
|
||||
"sync"
|
||||
"time"
|
||||
)
|
||||
|
||||
// User is an IMVU account, as the public API shows it.
|
||||
type User struct {
|
||||
ID int64
|
||||
Username string
|
||||
Tagline string
|
||||
AgeVerified bool
|
||||
AccessPass bool
|
||||
}
|
||||
|
||||
// ErrNoSuchUser means IMVU has no account by that name or ID.
|
||||
var ErrNoSuchUser = errors.New("no such IMVU account")
|
||||
|
||||
// Client reads IMVU's API.
|
||||
type Client struct {
|
||||
// Base is the API's address; tests point it elsewhere.
|
||||
Base string
|
||||
HTTP *http.Client
|
||||
|
||||
mu sync.Mutex
|
||||
last time.Time
|
||||
}
|
||||
|
||||
// New is a client for api.imvu.com.
|
||||
func New() *Client {
|
||||
return &Client{Base: "https://api.imvu.com", HTTP: &http.Client{Timeout: 15 * time.Second}}
|
||||
}
|
||||
|
||||
// minInterval spaces out calls to IMVU.
|
||||
const minInterval = 200 * time.Millisecond
|
||||
|
||||
func (c *Client) wait(ctx context.Context) error {
|
||||
c.mu.Lock()
|
||||
next := c.last.Add(minInterval)
|
||||
now := time.Now()
|
||||
if next.Before(now) {
|
||||
next = now
|
||||
}
|
||||
c.last = next
|
||||
c.mu.Unlock()
|
||||
select {
|
||||
case <-time.After(time.Until(next)):
|
||||
return nil
|
||||
case <-ctx.Done():
|
||||
return ctx.Err()
|
||||
}
|
||||
}
|
||||
|
||||
// ByUsername looks an account up by name.
|
||||
func (c *Client) ByUsername(ctx context.Context, username string) (*User, error) {
|
||||
username = strings.TrimSpace(username)
|
||||
if username == "" || len(username) > 64 {
|
||||
return nil, ErrNoSuchUser
|
||||
}
|
||||
return c.get(ctx, "/user?username="+url.QueryEscape(username))
|
||||
}
|
||||
|
||||
// ByID reads an account (fresh, for checks).
|
||||
func (c *Client) ByID(ctx context.Context, id int64) (*User, error) {
|
||||
return c.get(ctx, "/user/user-"+strconv.FormatInt(id, 10))
|
||||
}
|
||||
|
||||
func (c *Client) get(ctx context.Context, path string) (*User, error) {
|
||||
if err := c.wait(ctx); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req, err := http.NewRequestWithContext(ctx, "GET", c.Base+path, nil)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
req.Header.Set("User-Agent", "t5de-hub (https://git.t5de.dev/t5de/t5de-hub)")
|
||||
resp, err := c.HTTP.Do(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer resp.Body.Close()
|
||||
body, err := io.ReadAll(io.LimitReader(resp.Body, 1<<20))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if resp.StatusCode == http.StatusNotFound {
|
||||
return nil, ErrNoSuchUser
|
||||
}
|
||||
if resp.StatusCode != http.StatusOK {
|
||||
return nil, fmt.Errorf("IMVU answered %d", resp.StatusCode)
|
||||
}
|
||||
return parse(body)
|
||||
}
|
||||
|
||||
// parse reads IMVU's "denormalized" answer: the user is the entry the answer's id points to, or the only user in it.
|
||||
func parse(body []byte) (*User, error) {
|
||||
var answer struct {
|
||||
Status string `json:"status"`
|
||||
ID string `json:"id"`
|
||||
Denormalized map[string]struct {
|
||||
Data json.RawMessage `json:"data"`
|
||||
} `json:"denormalized"`
|
||||
}
|
||||
if err := json.Unmarshal(body, &answer); err != nil {
|
||||
return nil, fmt.Errorf("IMVU's answer: %w", err)
|
||||
}
|
||||
var found []json.RawMessage
|
||||
for key, entry := range answer.Denormalized {
|
||||
if strings.Contains(key, "/user/user-") {
|
||||
found = append(found, entry.Data)
|
||||
}
|
||||
}
|
||||
if len(found) != 1 {
|
||||
return nil, ErrNoSuchUser
|
||||
}
|
||||
var data struct {
|
||||
LegacyCID json.Number `json:"legacy_cid"`
|
||||
Username string `json:"username"`
|
||||
Tagline string `json:"tagline"`
|
||||
AgeVerified bool `json:"is_ageverified"`
|
||||
AP bool `json:"is_ap"`
|
||||
}
|
||||
if err := json.Unmarshal(found[0], &data); err != nil {
|
||||
return nil, fmt.Errorf("IMVU's user: %w", err)
|
||||
}
|
||||
id, err := data.LegacyCID.Int64()
|
||||
if err != nil || id <= 0 || data.Username == "" {
|
||||
return nil, ErrNoSuchUser
|
||||
}
|
||||
return &User{ID: id, Username: data.Username, Tagline: data.Tagline, AgeVerified: data.AgeVerified, AccessPass: data.AP}, nil
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
package imvu
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"net/http/httptest"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestByUsername(t *testing.T) {
|
||||
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
||||
if r.URL.Query().Get("username") == "Nobody" {
|
||||
w.Write([]byte(`{"status":"success","id":"x","denormalized":{}}`))
|
||||
return
|
||||
}
|
||||
w.Write([]byte(`{"status":"success","id":"https://api.imvu.com/user?username=Someone","denormalized":{
|
||||
"https://api.imvu.com/user/user-42":{"data":{"legacy_cid":42,"username":"Someone","tagline":"hi t5de-ABCD",
|
||||
"is_ageverified":true,"is_ap":false}}}}`))
|
||||
}))
|
||||
defer srv.Close()
|
||||
c := &Client{Base: srv.URL, HTTP: srv.Client()}
|
||||
u, err := c.ByUsername(context.Background(), "Someone")
|
||||
if err != nil || u.ID != 42 || u.Username != "Someone" || u.Tagline != "hi t5de-ABCD" || !u.AgeVerified || u.AccessPass {
|
||||
t.Fatalf("%+v %v", u, err)
|
||||
}
|
||||
if _, err := c.ByUsername(context.Background(), "Nobody"); !errors.Is(err, ErrNoSuchUser) {
|
||||
t.Fatalf("got %v", err)
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user