Repository: owncloud/ocis
Pull request #4012 (2022-06-22 09:43:23)

Handle file touched events
## Description
This PR makes the search provider listen and react to `FileTouched` events.

## Related Issue
- Fixes https://github.com/owncloud/ocis/issues/3154


update-docs[bot]: Thanks for opening this pull request! The maintainers of this repository would appreciate it if you would create a [changelog](https://github.com/owncloud/ocis/blob/master/changelog/README.md) item based on your changes.

sonarcloud[bot]: Kudos, SonarCloud Quality Gate passed!&nbsp; &nbsp; [![Quality Gate passed](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/QualityGateBadge/passed-16px.png 'Quality Gate passed')](https://sonarcloud.io/dashboard?id=owncloud_ocis&pullRequest=4012)

[![Bug](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/bug-16px.png 'Bug')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=BUG) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=BUG) [0 Bugs](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=BUG)  
[![Vulnerability](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/vulnerability-16px.png 'Vulnerability')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=VULNERABILITY) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=VULNERABILITY) [0 Vulnerabilities](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=VULNERABILITY)  
[![Security Hotspot](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/security_hotspot-16px.png 'Security Hotspot')](https://sonarcloud.io/project/security_hotspots?id=owncloud_ocis&pullRequest=4012&resolved=false&types=SECURITY_HOTSPOT) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/security_hotspots?id=owncloud_ocis&pullRequest=4012&resolved=false&types=SECURITY_HOTSPOT) [0 Security Hotspots](https://sonarcloud.io/project/security_hotspots?id=owncloud_ocis&pullRequest=4012&resolved=false&types=SECURITY_HOTSPOT)  
[![Code Smell](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/common/code_smell-16px.png 'Code Smell')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=CODE_SMELL) [![A](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/RatingBadge/A-16px.png 'A')](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=CODE_SMELL) [1 Code Smell](https://sonarcloud.io/project/issues?id=owncloud_ocis&pullRequest=4012&resolved=false&types=CODE_SMELL)

[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/CoverageChart/0-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=4012&metric=new_coverage&view=list) [0.0% Coverage](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=4012&metric=new_coverage&view=list)  
[![0.0%](https://sonarsource.github.io/sonarcloud-github-static-resources/v2/checks/Duplications/3-16px.png '0.0%')](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=4012&metric=new_duplicated_lines_density&view=list) [0.0% Duplication](https://sonarcloud.io/component_measures?id=owncloud_ocis&pullRequest=4012&metric=new_duplicated_lines_density&view=list)



Patch:
diff --git a/extensions/search/pkg/search/provider/events.go b/extensions/search/pkg/search/provider/events.go
index 641c3010a99..37c62c237f9 100644
--- a/extensions/search/pkg/search/provider/events.go
+++ b/extensions/search/pkg/search/provider/events.go
@@ -91,6 +91,11 @@ func (p *Provider) handleEvent(ev interface{}) {
 		owner = &user.User{
 			Id: e.Executant,
 		}
+	case events.FileTouched:
+		ref = e.Ref
+		owner = &user.User{
+			Id: e.Executant,
+		}
 	case events.FileVersionRestored:
 		ref = e.Ref
 		owner = &user.User{
diff --git a/extensions/search/pkg/search/provider/searchprovider.go b/extensions/search/pkg/search/provider/searchprovider.go
index 067d9665eba..5b0301c8e62 100644
--- a/extensions/search/pkg/search/provider/searchprovider.go
+++ b/extensions/search/pkg/search/provider/searchprovider.go
@@ -14,6 +14,7 @@ import (
 	provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1"
 	ctxpkg "github.com/cs3org/reva/v2/pkg/ctx"
 	"github.com/cs3org/reva/v2/pkg/errtypes"
+	"github.com/cs3org/reva/v2/pkg/events"
 	sdk "github.com/cs3org/reva/v2/pkg/sdk/common"
 	"github.com/cs3org/reva/v2/pkg/storage/utils/walker"
 	"github.com/cs3org/reva/v2/pkg/storagespace"
@@ -26,6 +27,16 @@ import (
 	searchsvc "github.com/owncloud/ocis/v2/protogen/gen/ocis/services/search/v0"
 )
 
+var ListenEvents = []events.Unmarshaller{
+	events.ItemTrashed{},
+	events.ItemRestored{},
+	events.ItemMoved{},
+	events.ContainerCreated{},
+	events.FileUploaded{},
+	events.FileTouched{},
+	events.FileVersionRestored{},
+}
+
 type Provider struct {
 	logger            log.Logger
 	gwClient          gateway.GatewayAPIClient
diff --git a/extensions/search/pkg/service/v0/service.go b/extensions/search/pkg/service/v0/service.go
index ef589ec5ea4..235b5610191 100644
--- a/extensions/search/pkg/service/v0/service.go
+++ b/extensions/search/pkg/service/v0/service.go
@@ -14,7 +14,6 @@ import (
 	"go-micro.dev/v4/metadata"
 	grpcmetadata "google.golang.org/grpc/metadata"
 
-	"github.com/owncloud/ocis/v2/extensions/audit/pkg/types"
 	"github.com/owncloud/ocis/v2/extensions/search/pkg/config"
 	"github.com/owncloud/ocis/v2/extensions/search/pkg/search"
 	"github.com/owncloud/ocis/v2/extensions/search/pkg/search/index"
@@ -38,7 +37,7 @@ func NewHandler(opts ...Option) (searchsvc.SearchProviderHandler, error) {
 	if err != nil {
 		return nil, err
 	}
-	evts, err := events.Consume(client, evtsCfg.ConsumerGroup, types.RegisteredEvents()...)
+	evts, err := events.Consume(client, evtsCfg.ConsumerGroup, searchprovider.ListenEvents...)
 	if err != nil {
 		return nil, err
 	}
diff --git a/go.mod b/go.mod
index a19301e9788..56c48f7f9d7 100644
--- a/go.mod
+++ b/go.mod
@@ -10,7 +10,7 @@ require (
 	github.com/blevesearch/bleve_index_api v1.0.2
 	github.com/coreos/go-oidc/v3 v3.2.0
 	github.com/cs3org/go-cs3apis v0.0.0-20220512100524-551800f020d8
-	github.com/cs3org/reva/v2 v2.6.0
+	github.com/cs3org/reva/v2 v2.6.1-0.20220622100918-317fb93c2990
 	github.com/disintegration/imaging v1.6.2
 	github.com/go-chi/chi/v5 v5.0.7
 	github.com/go-chi/cors v1.2.1
diff --git a/go.sum b/go.sum
index 7195052f275..ce117946914 100644
--- a/go.sum
+++ b/go.sum
@@ -294,10 +294,8 @@ github.com/crewjam/httperr v0.2.0/go.mod h1:Jlz+Sg/XqBQhyMjdDiC+GNNRzZTD7x39Gu3p
 github.com/crewjam/saml v0.4.6 h1:XCUFPkQSJLvzyl4cW9OvpWUbRf0gE7VUpU8ZnilbeM4=
 github.com/crewjam/saml v0.4.6/go.mod h1:ZBOXnNPFzB3CgOkRm7Nd6IVdkG+l/wF+0ZXLqD96t1A=
 github.com/cs3org/cato v0.0.0-20200828125504-e418fc54dd5e/go.mod h1:XJEZ3/EQuI3BXTp/6DUzFr850vlxq11I6satRtz0YQ4=
-github.com/cs3org/reva/v2 v2.5.2-0.20220621133128-d90c8aa60a15 h1:isQbNMNY9PSWy0CWT1KQIdCyD85XUdu6Ww1HjIuR6uc=
-github.com/cs3org/reva/v2 v2.5.2-0.20220621133128-d90c8aa60a15/go.mod h1:zAHqzr36X4lIalonDQeNbwrIXjn66C38lp5A+MTRS1c=
-github.com/cs3org/reva/v2 v2.6.0 h1:h+QYJWhDFqONsfgLXjHXKRyPJLhyNUR6oYaTkR5I3gg=
-github.com/cs3org/reva/v2 v2.6.0/go.mod h1:zAHqzr36X4lIalonDQeNbwrIXjn66C38lp5A+MTRS1c=
+github.com/cs3org/reva/v2 v2.6.1-0.20220622100918-317fb93c2990 h1:xjbf0G+Xq3fYkv7eJYwMBIPI3N+tr1nqOz7Kn2/dkAQ=
+github.com/cs3org/reva/v2 v2.6.1-0.20220622100918-317fb93c2990/go.mod h1:zAHqzr36X4lIalonDQeNbwrIXjn66C38lp5A+MTRS1c=
 github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8 h1:Z9lwXumT5ACSmJ7WGnFl+OMLLjpz5uR2fyz7dC255FI=
 github.com/cubewise-code/go-mime v0.0.0-20200519001935-8c5762b177d8/go.mod h1:4abs/jPXcmJzYoYGF91JF9Uq9s/KL5n1jvFDix8KcqY=
 github.com/cyberdelia/templates v0.0.0-20141128023046-ca7fffd4298c/go.mod h1:GyV+0YP4qX0UQ7r2MoYZ+AvYDp12OF5yg4q8rGnyNh4=


Test patch:
diff --git a/extensions/search/pkg/search/provider/events_test.go b/extensions/search/pkg/search/provider/events_test.go
index a36b36aef99..f90b9b0e269 100644
--- a/extensions/search/pkg/search/provider/events_test.go
+++ b/extensions/search/pkg/search/provider/events_test.go
@@ -95,6 +95,23 @@ var _ = Describe("Searchprovider", func() {
 			}, "2s").Should(BeTrue())
 		})
 
+		It("triggers an index update when a file has been touched", func() {
+			called := false
+			indexClient.On("Add", mock.Anything, mock.MatchedBy(func(riToIndex *sprovider.ResourceInfo) bool {
+				return riToIndex.Id.OpaqueId == ri.Id.OpaqueId
+			})).Return(nil).Run(func(args mock.Arguments) {
+				called = true
+			})
+			eventsChan <- events.FileTouched{
+				Ref:       ref,
+				Executant: user.Id,
+			}
+
+			Eventually(func() bool {
+				return called
+			}, "2s").Should(BeTrue())
+		})
+
 		It("removes an entry from the index when the file has been deleted", func() {
 			called := false
 			gwClient.On("Stat", mock.Anything, mock.Anything).Return(&sprovider.StatResponse{