@@ -17,7 +17,7 @@ class UserNeo4j
1717
1818 attr_accessor :skip_get_website_titles
1919
20- has_many :both , :friends , type : :FRIEND , model_class : self , labels : false , unique : true
20+ has_many :out , :friends , type : :FRIEND , model_class : self , labels : false , unique : true
2121
2222 validates :slug , exclusion : { in : [ 'admin' , 'root' ] + RouteRecognizer . initial_path_segments } , uniqueness : true
2323 validates :first_name , presence : true
@@ -40,37 +40,30 @@ def references
4040 end
4141
4242 def reload_titles!
43+ return unless persisted?
44+
4345 # TODO: Use async_perform with Sidekiq
4446 WebsiteScrapperWorker . new . perform ( uuid , self . class )
4547 end
4648
4749 def friendsBySearch ( term )
48- byebug
4950 friends_path = { }
50- uuids = friends ( :l , :r , rel_length : { max : 5 } ) .
51- where ( "ANY(title IN l.titles WHERE toLower(title) CONTAINS toLower({title}))" ) .
52- params ( { title : term } ) .
53- pluck ( :uuid )
5451
55- uuids . each do |uuid |
52+ friends = as ( :user ) . friends ( :l , :r , rel_length : { max : 5 } ) .
53+ where ( 'ANY(title IN l.titles WHERE toLower(title) CONTAINS toLower({title})) OR
54+ ANY(subtitle IN l.subtitles WHERE toLower(subtitle) CONTAINS toLower({title}))' ) .
55+ where ( 'l <> user' ) .
56+ params ( { title : term } )
57+
58+ friends . each do |friend |
5659 friends_path [ uuid ] = Neo4j ::ActiveBase . current_session .
5760 query ( "MATCH (r:UserNeo4j { uuid: '#{ self . uuid } ' }),
58- (l:UserNeo4j { uuid: '#{ uuid } ' }),
61+ (l:UserNeo4j { uuid: '#{ friend . uuid } ' }),
5962 p = shortestPath((r)-[FRIEND*..5]->(l))
60- RETURN p" , limit : 1 ) . first . p . nodes .
61- map { |node | User . find_by ( { neo4j_uuid : node . properties [ :uuid ] } ) }
63+ RETURN p" , limit : 1 ) . first . p . nodes
6264 end
6365
64- return uuids , friends_path
65-
66- # FIXME: May it works
67- # as(:user).
68- # friends(:friends).
69- # query.
70- # with('shortestPath((user)-[:FRIEND*..5]->(friends)) AS shortest_path').
71- # where("ANY(title IN friends.titles WHERE toLower(title) CONTAINS toLower({title}))").
72- # params({ title: term }).
73- # pluck(:shortest_path)
66+ return friends , friends_path
7467 end
7568
7669 protected
0 commit comments