build / windows (push) Skipped
build / other_os (push) Skipped
build / mac_amd64 (push) Skipped
build / mac_arm64 (push) Skipped
build / linux (push) Skipped
build / go1.26 (push) Skipped
build / linux_386 (push) Skipped
build / lint (push) Skipped
build / android-all (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/386 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/amd64 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v6 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm/v7 (push) Skipped
Build & Push Docker Images / Build Docker Image for linux/arm64 (push) Skipped
Build & Push Docker Images / Merge & Push Final Docker Image (push) Waiting for needed jobs
Replace Proton-API-Bridge and go-proton-api with a client in backend/protondrive written against Proton's current Drive API, following the behaviour of Proton's official Drive SDK: - volume-based and v2 routes only - upload block verification, signed manifests and extended attributes - current content encryption (v4 node keys, v6 PKESK, SEIPDv2) - browser (session fork) sign-in, with username and password still supported - the volume's change events, used to keep the metadata cache current and for ChangeNotify, polled at most every 30 s - an in-memory fake Proton server (protontest) so the tests run without an account Uses ProtonMail/gopenpgp/v3 and ProtonMail/go-srp directly; no cgo. Written with heavy AI assistance (Claude Code). Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
209 lines
5.9 KiB
Go
209 lines
5.9 KiB
Go
package protontest
|
|
|
|
import (
|
|
"net/http"
|
|
|
|
"github.com/rclone/rclone/backend/protondrive/api"
|
|
)
|
|
|
|
// Volume event types as the API encodes them.
|
|
const (
|
|
EventDelete = 0
|
|
EventCreate = 1
|
|
EventUpdate = 2
|
|
EventUpdateMetadata = 3
|
|
)
|
|
|
|
// defaultEventsPageSize is the number of events per page unless changed
|
|
// by SetEventsPageSize.
|
|
const defaultEventsPageSize = 50
|
|
|
|
// Event is a change to a link as the volume events route reports it.
|
|
// Every change through the API or a test mutator such as SeedFile or
|
|
// SetParent appends one, as Proton does: Create when a link becomes
|
|
// active (an upload committed, a folder made or copied), Update when a
|
|
// new revision is committed or a test mutator changes the link's
|
|
// content or keys, UpdateMetadata when the link is renamed, moved or
|
|
// trashed, and Delete for every link emptying the trash removes,
|
|
// descendants included.
|
|
//
|
|
// As on Proton, ParentLinkID, IsShared and IsTrashed are the link's
|
|
// state when the events are read, not when the change was made: a link
|
|
// deleted for good has no parent and isn't trashed. Events added by
|
|
// InjectEvent keep their own fields.
|
|
type Event struct {
|
|
Type int
|
|
LinkID string
|
|
ParentLinkID string // empty for none
|
|
IsShared bool
|
|
IsTrashed bool
|
|
}
|
|
|
|
// loggedEvent is an Event with its ID.
|
|
type loggedEvent struct {
|
|
id string
|
|
Event
|
|
injected bool // whether Event is reported as given, not with the link's state
|
|
}
|
|
|
|
// eventLog is a volume's events, oldest first.
|
|
type eventLog struct {
|
|
entries []loggedEvent
|
|
latest string // ID of the newest event, or the initial ID
|
|
cursors map[string]int // events covered by each ID still known
|
|
}
|
|
|
|
// newEventLog returns an empty log with an initial ID.
|
|
func newEventLog() *eventLog {
|
|
id := randomID()
|
|
return &eventLog{latest: id, cursors: map[string]int{id: 0}}
|
|
}
|
|
|
|
// add appends e with a new ID.
|
|
func (l *eventLog) add(e loggedEvent) {
|
|
id := randomID()
|
|
e.id = id
|
|
l.entries = append(l.entries, e)
|
|
l.latest = id
|
|
l.cursors[id] = len(l.entries)
|
|
}
|
|
|
|
// logLink appends an event of type typ for link l.
|
|
func (d *drive) logLink(typ int, l *link) {
|
|
d.events.add(loggedEvent{Event: Event{Type: typ, LinkID: l.id}})
|
|
}
|
|
|
|
// reported returns e as the events route reports it now.
|
|
func (d *drive) reported(e loggedEvent) Event {
|
|
if e.injected {
|
|
return e.Event
|
|
}
|
|
out := Event{Type: e.Type, LinkID: e.LinkID}
|
|
if l := d.links[e.LinkID]; l != nil {
|
|
out.ParentLinkID = l.parentID
|
|
out.IsTrashed = l.state == linkStateTrashed
|
|
}
|
|
return out
|
|
}
|
|
|
|
type latestEventResponse struct {
|
|
EventID string
|
|
Code int
|
|
}
|
|
|
|
type eventLinkJSON struct {
|
|
LinkID string
|
|
ParentLinkID *string
|
|
IsShared bool
|
|
IsTrashed bool
|
|
}
|
|
|
|
type eventJSON struct {
|
|
EventID string
|
|
EventType int
|
|
Link eventLinkJSON
|
|
}
|
|
|
|
type invitationJSON struct {
|
|
LinkID string
|
|
}
|
|
|
|
type eventsResponse struct {
|
|
Events []eventJSON
|
|
ConvertibleExternalInvitations []invitationJSON
|
|
EventID string
|
|
More bool
|
|
Refresh bool
|
|
DisableSdkQuotaChecks bool
|
|
Code int
|
|
}
|
|
|
|
// registerEvents registers the volume events routes.
|
|
func (s *Server) registerEvents() {
|
|
s.handleDrive("GET", "/drive/volumes/{volumeID}/events/latest", s.handleLatestEvent)
|
|
s.handleDrive("GET", "/drive/v2/volumes/{volumeID}/events/{eventID}", s.handleEvents)
|
|
}
|
|
|
|
// handleLatestEvent returns the ID of the volume's newest event.
|
|
func (s *Server) handleLatestEvent(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
|
|
writeJSON(w, http.StatusOK, &latestEventResponse{EventID: d.events.latest, Code: api.CodeOK})
|
|
}
|
|
|
|
// handleEvents returns a page of the volume's events after an event
|
|
// ID, or Refresh with the newest ID if that ID is unknown or forgotten.
|
|
func (s *Server) handleEvents(w http.ResponseWriter, r *http.Request, u *User, d *drive) {
|
|
log := d.events
|
|
resp := &eventsResponse{
|
|
Events: []eventJSON{},
|
|
ConvertibleExternalInvitations: []invitationJSON{},
|
|
EventID: log.latest,
|
|
Code: api.CodeOK,
|
|
}
|
|
start, ok := log.cursors[r.PathValue("eventID")]
|
|
if !ok {
|
|
resp.Refresh = true
|
|
writeJSON(w, http.StatusOK, resp)
|
|
return
|
|
}
|
|
end := min(start+s.eventsPageSize, len(log.entries))
|
|
for _, logged := range log.entries[start:end] {
|
|
e := d.reported(logged)
|
|
resp.Events = append(resp.Events, eventJSON{
|
|
EventID: logged.id,
|
|
EventType: e.Type,
|
|
Link: eventLinkJSON{
|
|
LinkID: e.LinkID,
|
|
ParentLinkID: nullable(e.ParentLinkID),
|
|
IsShared: e.IsShared,
|
|
IsTrashed: e.IsTrashed,
|
|
},
|
|
})
|
|
}
|
|
if end > start {
|
|
resp.EventID = log.entries[end-1].id
|
|
}
|
|
resp.More = end < len(log.entries)
|
|
writeJSON(w, http.StatusOK, resp)
|
|
}
|
|
|
|
// SetEventsPageSize sets the number of events per page of the volume
|
|
// events route; n of 0 or less restores the default.
|
|
func (s *Server) SetEventsPageSize(n int) {
|
|
if n <= 0 {
|
|
n = defaultEventsPageSize
|
|
}
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
s.eventsPageSize = n
|
|
}
|
|
|
|
// ExpireEvents forgets every event ID of u's volume but the newest, so
|
|
// a client polling from an older one is told to refresh.
|
|
func (s *Server) ExpireEvents(u *User) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
log := u.mustDriveLocked().events
|
|
log.cursors = map[string]int{log.latest: len(log.entries)}
|
|
}
|
|
|
|
// InjectEvent appends e to the events of u's volume. It is reported
|
|
// with its own fields, whatever the state of its link when read.
|
|
func (s *Server) InjectEvent(u *User, e Event) {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
u.mustDriveLocked().events.add(loggedEvent{Event: e, injected: true})
|
|
}
|
|
|
|
// Events returns every event of u's volume, oldest first, including
|
|
// those ExpireEvents forgot, as the events route would report them now.
|
|
func (s *Server) Events(u *User) []Event {
|
|
s.mu.Lock()
|
|
defer s.mu.Unlock()
|
|
d := u.mustDriveLocked()
|
|
out := make([]Event, 0, len(d.events.entries))
|
|
for _, e := range d.events.entries {
|
|
out = append(out, d.reported(e))
|
|
}
|
|
return out
|
|
}
|