def like(self,vid,uid,sf): vid = str(vid) uid = str(uid) cql = ''' MATCH (me) WHERE me.uid=%s OPTIONAL MATCH (me)-[r:LIKE_FROM]-(secondlatestupdate) DELETE r CREATE (me)-[:LIKE_FROM]->(latest_update:LIKE {timestamp:%i}) WITH latest_update, collect(secondlatestupdate) AS seconds FOREACH (x IN seconds | CREATE (latest_update)-[:NEXT]->(x)) WITH latest_update MATCH (evt:Event {id: '%s'}) CREATE (latest_update)-[:LIKE_TO]->(evt) RETURN evt.id AS uuid, ID(latest_update)'''%(uid, function.timestamp(), vid) print cql res = db.cypher.execute(cql) temp_id = res[0][0] node_id = int(res[0][1]) cql = ''' MATCH(me) WHERE me.uid=%s MATCH (me)-[r:SUBS_FROM]-(subs)-[:SUBS_TO]-(evt:Event {id:'%s'}) RETURN COUNT(r) '''%(uid, temp_id) res = db.cypher.execute(cql) if res[0][0] == 0: cql = ''' MATCH(me) WHERE me.uid=%s OPTIONAL MATCH (me)-[r:SUBS_FROM]-(secondlatestupdate) DELETE r CREATE (me)-[:SUBS_FROM]->(latest_update:SUBSCRIBLE {timestamp:%i}) WITH latest_update, collect(secondlatestupdate) AS seconds MATCH (evt) WHERE evt.id='%s' WITH latest_update, seconds, evt CREATE (latest_update)-[:SUBS_TO]->(evt) WITH latest_update, seconds FOREACH (x IN seconds | CREATE (latest_update)-[:NEXT]->(x))'''%(uid, function.timestamp(), temp_id) print cql res = db.cypher.execute(cql) cql = ''' MATCH (me) WHERE me.uid=%s MATCH (evt) WHERE evt.id='%s' MATCH (like) WHERE ID(like) = %i MATCH (me)-[:SUBS_FROM]-(sub)-[:SUBS_TO]-(evt)-[:SUBS_TO]-(subs) WITH collect(subs) as subscribers, like FOREACH (x IN subscribers | CREATE (like)-[:NOTIFICATION {read: 0}]->(x)) '''%(uid, temp_id, node_id) print cql res = db.cypher.execute(cql)
def friend_accept(self,init_user,recv_user,sf): init_user = str(init_user) recv_user = str(recv_user) cql = ''' MATCH (usr1:User {uid:%s})-[r:FRIEND_REQ]->(usr2:User {uid:%s}) DELETE r WITH usr1,usr2 CREATE (usr1)-[:FRIEND {timestamp:%i}]->(usr2)'''%(init_user, recv_user, function.timestamp()) res = db.cypher.execute(cql) print '#', res
def create(self,text,uid,sf): uid = str(uid) cql = ''' MATCH (me) WHERE me.uid=%s OPTIONAL MATCH (me)-[r:POST]-(secondlatestupdate) DELETE r CREATE (me)-[:POST]->(latest_update:Event {id:'%s', text:'%s', timestamp:%i}) WITH latest_update, collect(secondlatestupdate) AS seconds, me FOREACH (x IN seconds | CREATE (latest_update)-[:NEXT]->(x)) WITH latest_update, me OPTIONAL MATCH (me)-[r:SUBS_FROM]-(secondsubs) DELETE r CREATE (me)-[:SUBS_FROM]->(latest_subs:SUBSCRIBLE {timestamp:%i}) WITH latest_subs, latest_update, collect(secondsubs) as secs CREATE (latest_subs)-[:SUBS_TO]->(latest_update) WITH secs, latest_subs FOREACH (x IN secs | CREATE (latest_subs)-[:NEXT]->(x)) '''%(uid, uuid.uuid4(), text, function.timestamp(), function.timestamp()) print cql res = db.cypher.execute(cql) print '#', res
def friend_request(self,init_user,recv_user,sf): init_user = str(init_user) recv_user = str(recv_user) cql = ''' MATCH (usr1:User {uid:%s})-[r:FRIEND_REQ]-(usr2:User {uid:%s}) RETURN count(r)'''%(init_user, recv_user) res = db.cypher.execute(cql) if int(res[0][0]) > 0: ret = {'ret': 1, 'err': 'already requested'} sf.write(json_encode(ret)) print ret else: cql = ''' MATCH (usr1:User {uid:%s}), (usr2:User {uid:%s}) CREATE (usr1)-[:FRIEND_REQ {timestamp: %i}]->(usr2)'''%(init_user, recv_user, function.timestamp()) res = db.cypher.execute(cql) print '#', res
def reply(self,vid,text,uid,sf): uid = str(uid) cql = ''' MATCH (me) WHERE me.uid=%s OPTIONAL MATCH (me)-[r:REPLY]-(secondlatestupdate) DELETE r CREATE (me)-[:REPLY]->(latest_update:Event {id:'%s', text:'%s', timestamp:%i}) WITH latest_update, collect(secondlatestupdate) AS seconds FOREACH (x IN seconds | CREATE (latest_update)-[:NEXT]->(x)) WITH latest_update MATCH (evt:Event {id: '%s'}) CREATE (latest_update)-[:REPLY_TO]->(evt) RETURN latest_update.id as uuid, ID(latest_update)'''%(uid, uuid.uuid4(), text, function.timestamp(), vid) res = db.cypher.execute(cql) temp_id = res[0][0] node_id = int(res[0][1]) cql = ''' MATCH(me) WHERE me.uid=%s MATCH (evt:Event {id:'%s'})-[:REPLY_TO*0..]-(evts)-[:REPLY_TO]->(root_evt) WITH root_evt, me ORDER BY root_evt.timestamp LIMIT 1 MATCH (me)-[r:SUBS_FROM]-(subs)-[:SUBS_TO]-(root_evt) RETURN COUNT(r) '''%(uid, temp_id) res = db.cypher.execute(cql) if res[0][0] == 0: cql = ''' MATCH(me) WHERE me.uid=%s MATCH (evt:Event {id:'%s'})-[:REPLY_TO*0..]-(evts)-[:REPLY_TO]->(root_evt) WITH root_evt, me ORDER BY root_evt.timestamp LIMIT 1 OPTIONAL MATCH (me)-[r:SUBS_FROM]-(secondlatestupdate) DELETE r CREATE (me)-[:SUBS_FROM]->(latest_update:SUBSCRIBLE {timestamp:%i}) WITH latest_update, root_evt, collect(secondlatestupdate) AS seconds CREATE (latest_update)-[:SUBS_TO]->(root_evt) WITH latest_update, seconds FOREACH (x IN seconds | CREATE (latest_update)-[:NEXT]->(x))'''%(uid, temp_id, function.timestamp()) res = db.cypher.execute(cql) cql = ''' MATCH (me) WHERE me.uid=%s MATCH (evt:Event {id:'%s'})-[:REPLY_TO*0..]-(evts)-[:REPLY_TO]->(root_evt) WITH root_evt, me ORDER BY root_evt.timestamp LIMIT 1 MATCH (reply) WHERE ID(reply) = %i MATCH (me)-[:SUBS_FROM]->(sub1)-[:NEXT*0..]->(sub2)-[:SUBS_TO]-(root_evt)-[:SUBS_TO]-(subs) WITH collect(subs) as subscribers, reply FOREACH (x IN subscribers | CREATE (reply)-[:NOTIFICATION {read: 0}]->(x)) '''%(uid, temp_id, node_id) print cql res = db.cypher.execute(cql)