diff --git a/pkg/consts/views.go b/pkg/consts/views.go index 72eeefbd28b890ad64d7931493b3063e0d2b4a87..bf79a3950d2f6d40bf4ce02527b057c206dc366b 100644 --- a/pkg/consts/views.go +++ b/pkg/consts/views.go @@ -7,7 +7,7 @@ import ( // IndexViewsVersion is the version of current definition of views & indexes. // This number should be incremented when this file changes. -const IndexViewsVersion int = 19 +const IndexViewsVersion int = 18 // globalIndexes is the index list required on the global databases to run // properly. @@ -195,7 +195,6 @@ function(doc) { }); } }`, - Reduce: "_count", } // SharingsByDocTypeView is the view for fetching a list of sharings diff --git a/pkg/sharing/upload.go b/pkg/sharing/upload.go index cc1105bf377a7c5d288d02e94f451ac25af36418..5292c899791f41d4feb5898e4288a08dbd2e8a58 100644 --- a/pkg/sharing/upload.go +++ b/pkg/sharing/upload.go @@ -9,7 +9,6 @@ import ( "net/url" "os" "strings" - "time" "github.com/cozy/cozy-stack/client/request" "github.com/cozy/cozy-stack/pkg/consts" @@ -560,15 +559,23 @@ func (s *Sharing) UploadNewFile(inst *instance.Instance, target *FileDocWithRevi // countReceivedFiles counts the number of files received during the initial // sync, and pushs an event to the real-time system with this count func (s *Sharing) countReceivedFiles(inst *instance.Instance) { - // Let CouchDB updates its index - time.Sleep(1 * time.Second) - count := 0 - var resCount couchdb.ViewResponse - reqCount := &couchdb.ViewRequest{Key: s.SID, Reduce: true} - err := couchdb.ExecView(inst, consts.FilesReferencedByView, reqCount, &resCount) - if err == nil && len(resCount.Rows) > 0 { - count = int(resCount.Rows[0].Value.(float64)) + var req = &couchdb.ViewRequest{ + Key: s.SID, + IncludeDocs: true, + } + var res couchdb.ViewResponse + err := couchdb.ExecView(inst, consts.SharedDocsBySharingID, req, &res) + if err == nil { + for _, row := range res.Rows { + var doc SharedRef + if err = json.Unmarshal(row.Doc, &doc); err != nil { + continue + } + if doc.Infos[s.SID].Binary { + count++ + } + } } if count >= s.NbFiles { diff --git a/tests/sharing/Gemfile b/tests/sharing/Gemfile index 50d374933f253faf9af4c3a9890bea55d8764b85..5cfd9d5c96861bee5443b1b5e61891143cd66bbe 100644 --- a/tests/sharing/Gemfile +++ b/tests/sharing/Gemfile @@ -2,11 +2,10 @@ source 'https://rubygems.org' gem "awesome_print" gem "faker" +gem "faye-websocket" gem "mimemagic" gem "minitest" gem "pry" gem "pry-rescue" +gem "pry-stack_explorer" gem "rest-client" - -# Added at 2018-05-21 09:30:04 +0200 by nono: -gem "pry-stack_explorer", "~> 0.4.9" diff --git a/tests/sharing/Gemfile.lock b/tests/sharing/Gemfile.lock index 3139cf5ffae075963e7adc69939ff4610a67b75d..ed300ae124d143df2c544d48c94c891e83612d57 100644 --- a/tests/sharing/Gemfile.lock +++ b/tests/sharing/Gemfile.lock @@ -9,8 +9,12 @@ GEM debug_inspector (0.0.3) domain_name (0.5.20170404) unf (>= 0.0.5, < 1.0.0) + eventmachine (1.2.7) faker (1.8.7) i18n (>= 0.7) + faye-websocket (0.10.7) + eventmachine (>= 0.12.0) + websocket-driver (>= 0.5.1) http-cookie (1.0.3) domain_name (~> 0.5) i18n (1.0.0) @@ -39,6 +43,9 @@ GEM unf (0.1.4) unf_ext unf_ext (0.0.7.5) + websocket-driver (0.7.0) + websocket-extensions (>= 0.1.0) + websocket-extensions (0.1.3) PLATFORMS ruby @@ -46,11 +53,12 @@ PLATFORMS DEPENDENCIES awesome_print faker + faye-websocket mimemagic minitest pry pry-rescue - pry-stack_explorer (~> 0.4.9) + pry-stack_explorer rest-client BUNDLED WITH diff --git a/tests/sharing/tests/sync.rb b/tests/sharing/tests/sync.rb index 2e061bbdae2fdaddd64e727d691f811492137a87..f6b5223d6c3fee6fc39bec5ed227b3e1cf3f6ec8 100644 --- a/tests/sharing/tests/sync.rb +++ b/tests/sharing/tests/sync.rb @@ -1,5 +1,7 @@ require_relative '../boot' require 'minitest/autorun' +require 'faye/websocket' +require 'eventmachine' require 'pry-rescue/minitest' unless ENV['CI'] def make_xor_key @@ -52,6 +54,9 @@ describe "A folder" do file = "../fixtures/wet-cozy_20160910__©M4Dz.jpg" opts = CozyFile.options_from_fixture(file, dir_id: folder.couch_id) file = CozyFile.create inst, opts + zip = "../fixtures/logos.zip" + opts = CozyFile.options_from_fixture(zip, dir_id: child1.couch_id) + CozyFile.create inst, opts # Create the sharing of a folder contact = Contact.create inst, given_name: recipient_name @@ -70,9 +75,39 @@ describe "A folder" do sleep 1 inst_recipient.accept sharing doc = Helpers.couch.get_doc inst_recipient.domain, Sharing.doctype, sharing.couch_id - assert_equal 1, doc["initial_number_of_files_to_sync"] + assert_equal 2, doc["initial_number_of_files_to_sync"] + + # Check the realtime events + EM.run do + ws = Faye::WebSocket::Client.new("ws://#{inst_recipient.domain}/realtime/") + + ws.on :open do + ws.send({ + method: "AUTH", + payload: inst_recipient.token_for("io.cozy.files") + }.to_json) + ws.send({ + method: "SUBSCRIBE", + payload: { type: "io.cozy.sharings.initial-sync", id: sharing.couch_id } + }.to_json) + end + + ws.on :message do |event| + msg = JSON.parse(event.data) + if msg["event"] == "DELETED" + ws.close + else + assert_equal 1, msg.dig("payload", "doc", "count") + end + end + + ws.on :close do + EM.stop + end + end + doc = Helpers.couch.get_doc inst_recipient.domain, Sharing.doctype, sharing.couch_id + assert_nil doc["initial_number_of_files_to_sync"] - sleep 7 # Check the folders are the same child1_path = CGI.escape "/#{Helpers::SHARED_WITH_ME}/#{folder.name}/#{child1.name}" child1_recipient = Folder.find_by_path inst_recipient, child1_path diff --git a/web/realtime/realtime.go b/web/realtime/realtime.go index 5c30ab137b4b811de6b223bb2a2d45c0b1b4cff1..ae7ed42f9c17afe2264481d9a22b5c582e2ba4a1 100644 --- a/web/realtime/realtime.go +++ b/web/realtime/realtime.go @@ -7,6 +7,7 @@ import ( "strings" "time" + "github.com/cozy/cozy-stack/pkg/consts" "github.com/cozy/cozy-stack/pkg/instance" "github.com/cozy/cozy-stack/pkg/logger" "github.com/cozy/cozy-stack/pkg/permissions" @@ -171,7 +172,10 @@ func readPump(ctx context.Context, c echo.Context, i *instance.Instance, ws *web sendErr(ctx, errc, missingType(cmd)) continue } - if withAuthentication && !pdoc.Permissions.AllowWholeType(permissions.GET, cmd.Payload.Type) { + // XXX: no permissions are required for io.cozy.sharings.initial-sync + if withAuthentication && + cmd.Payload.Type != consts.SharingsInitialSync && + !pdoc.Permissions.AllowWholeType(permissions.GET, cmd.Payload.Type) { sendErr(ctx, errc, forbidden(cmd)) continue }