github.com/cs3org/reva/v2@v2.27.7/pkg/storage/favorite/memory/memory.go (about) 1 // Copyright 2018-2021 CERN 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 // 15 // In applying this license, CERN does not waive the privileges and immunities 16 // granted to it by virtue of its status as an Intergovernmental Organization 17 // or submit itself to any jurisdiction. 18 19 package memory 20 21 import ( 22 "context" 23 "sync" 24 25 user "github.com/cs3org/go-cs3apis/cs3/identity/user/v1beta1" 26 provider "github.com/cs3org/go-cs3apis/cs3/storage/provider/v1beta1" 27 "github.com/cs3org/reva/v2/pkg/storage/favorite" 28 "github.com/cs3org/reva/v2/pkg/storage/favorite/registry" 29 ) 30 31 func init() { 32 registry.Register("memory", New) 33 } 34 35 type mgr struct { 36 sync.RWMutex 37 favorites map[string]map[string]*provider.ResourceId 38 } 39 40 // New returns an instance of the in-memory favorites manager. 41 func New(m map[string]interface{}) (favorite.Manager, error) { 42 return &mgr{favorites: make(map[string]map[string]*provider.ResourceId)}, nil 43 } 44 45 func (m *mgr) ListFavorites(_ context.Context, userID *user.UserId) ([]*provider.ResourceId, error) { 46 m.RLock() 47 defer m.RUnlock() 48 favorites := make([]*provider.ResourceId, 0, len(m.favorites[userID.OpaqueId])) 49 for _, id := range m.favorites[userID.OpaqueId] { 50 favorites = append(favorites, id) 51 } 52 return favorites, nil 53 } 54 55 func (m *mgr) SetFavorite(_ context.Context, userID *user.UserId, resourceInfo *provider.ResourceInfo) error { 56 m.Lock() 57 defer m.Unlock() 58 if m.favorites[userID.OpaqueId] == nil { 59 m.favorites[userID.OpaqueId] = make(map[string]*provider.ResourceId) 60 } 61 m.favorites[userID.OpaqueId][resourceInfo.Id.OpaqueId] = resourceInfo.Id 62 return nil 63 } 64 65 func (m *mgr) UnsetFavorite(_ context.Context, userID *user.UserId, resourceInfo *provider.ResourceInfo) error { 66 m.Lock() 67 defer m.Unlock() 68 delete(m.favorites[userID.OpaqueId], resourceInfo.Id.OpaqueId) 69 return nil 70 }