def update_user_interacted_contents(cls, user_id, content_id, action_type, transaction): key = '{0}-{1}'.format(user_id, content_id) user_content = ArangoCore.get_edge_in_collection( ArangoEdge.USER_INTERACTED_CONTENT, key) if not user_content: if action_type in [ ActionType.LIKE, ActionType.COMMENT, ActionType.TIP, ActionType.PLAY ]: ArangoCore.add_interacted_content_to_collection( ArangoEdge.USER_INTERACTED_CONTENT, ArangoVertex.USER, user_id, ArangoVertex.CONTENT, content_id, action_type, transaction) else: user_content['updated_at'] = timezone.now().isoformat() if action_type == ActionType.LIKE: user_content['liked'] = True user_content['liked_date'] = str(timezone.now().date()) elif action_type == ActionType.COMMENT: user_content['commented'] = True elif action_type == ActionType.TIP: user_content['tipped'] = True elif action_type == ActionType.PLAY: user_content['played'] = True elif action_type == ActionType.UNLIKE: user_content['liked'] = False elif action_type == ActionType.UNCOMMENT: user_content['commented'] = False ArangoCore.update_edge_in_collection( ArangoEdge.USER_INTERACTED_CONTENT, user_content, transaction) return user_content
def add_user_channel(cls, user_id, channel): try: ArangoCore.add_channel_to_collection(ArangoEdge.USER_CHANNEL, ArangoVertex.USER, user_id, ArangoVertex.CHANNEL, channel) return True except Exception as exception: raise exception
def update_band(cls, user_id, follow_count, transaction): band_data = ArangoCore.get_vertex_in_collection( ArangoVertex.BAND, user_id) band_data['follow_count'] = follow_count ArangoCore.update_vertex_in_collection(ArangoVertex.BAND, band_data, transaction) band_data['_id'] = '{0}/{1}'.format('rv_users', user_id) ArangoCore.update_vertex_in_collection(ArangoVertex.USER, band_data, transaction)
def delete_download_history(cls, data): try: database = ArangoCore.get_database() collections = [ArangoEdge.USER_DOWNLOAD] with database.transaction(write=collections, commit_on_error=False) as transaction: ArangoCore.delete_edge_from_collection( ArangoEdge.USER_DOWNLOAD, data.get('_key'), transaction) transaction.commit() return True except Exception as exception: raise exception
def add_friend(cls, user_friend_data, activity_data): if '_user_cache' in user_friend_data: del user_friend_data['_user_cache'] try: database = ArangoCore.get_database() collections = ArangoEdge.FRIEND with database.transaction(write=collections, commit_on_error=False) as transaction: ArangoCore.add_edge_to_collection( ArangoEdge.FRIEND, ArangoVertex.USER, user_friend_data['user_id'], ArangoVertex.USER, user_friend_data['friend_user_id'], transaction) transaction.commit() except Exception as exception: raise exception
def get_post_of_friend(cls, user_id): user_id = 'sn_users/' + str(user_id) #query with graph # FOR post IN 1..2 OUTBOUND @user_id sn_friend, sn_user_post # câu query này sẽ select theo 2 cạnh của sn_friend và sn_user_post với giá trị user_id và select hết tất cả các item select đc gồm cả user và post # để chỉ lấy được các post thì t phải filter ra các item có user_type == null vì post ko có user_type chỉ user mới có user_type query_string = "FOR post IN 1..2 OUTBOUND @user_id sn_friend, sn_user_post OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "FILTER post.user_type == NULL " \ "SORT post.created_at DESC " \ "LET user = (FOR user IN sn_users FILTER user._key == TO_STRING(post.user_id) LIMIT 1 " \ "RETURN user)[0] " \ "RETURN merge(post,{user})" #query with vertex and edge # query_string = "LET friends = (" \ # "FOR user IN OUTBOUND @user_id sn_friend OPTIONS {bfs: true, uniqueVertices: 'global'} " \ # "RETURN user._id) " \ # "LET post=(" \ # "FOR friend IN friends " \ # "FOR post IN OUTBOUND friend sn_user_post OPTIONS {bfs: true, uniqueVertices: 'global'}" \ # "SORT post.created_at DESC " \ # "RETURN post) " \ # "RETURN post " parameter = {'user_id': user_id} result = ArangoCore.execute_query(query_string, parameter) return result
def del_user_channel(cls, channel): try: channel_id = '{0}/{1}'.format('rv_channels', channel) database = ArangoCore.get_database() collections = [ArangoEdge.USER_CHANNEL] with database.transaction(write=collections, commit_on_error=False) as transaction: command = ''' function () {{ // Get database var db = require("internal").db; // Delete all edges relate to content_id db._query("For edge IN rv_user_channels FILTER edge._to == '{0}' REMOVE {{_key: edge._key}} IN rv_user_channels"); return true; }}'''.format(channel_id) transaction.execute(command) transaction.commit() return True except Exception as exception: raise exception
def get_data_of_user(cls, user_id): user_id = 'sn_users/' + str(user_id) query_string = "FOR data IN OUTBOUND @user_id sn_user_data OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "SORT data.created_at DESC " \ "RETURN data" parameter = {'user_id': user_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_download_history(cls, download_id): try: query_string = "LET downloads =(FOR download IN sn_user_download FILTER download._key==@download_id LIMIT 1 RETURN download) RETURN downloads[0]" parameter = {'download_id': download_id} result = ArangoCore.execute_query(query_string, parameter) return result[0] if len(result) > 0 else None except Exception as exception: raise exception
def save_download(cls, data): try: database = ArangoCore.get_database() collections = [ ArangoEdge.USER_DOWNLOAD, ArangoVertex.DOWNLOAD, ArangoEdge.DOWNLOAD_DATA ] with database.transaction(write=collections, commit_on_error=False) as transaction: # Add post to graph vertex ArangoCore.add_vertex_to_collection(ArangoVertex.DOWNLOAD, data, transaction) # Add user_download to graph edge ArangoCore.add_user_download_to_collection( ArangoEdge.USER_DOWNLOAD, ArangoVertex.USER, data['user_id'], ArangoVertex.DOWNLOAD, data['id'], transaction) ArangoCore.add_edge_to_collection(ArangoEdge.DOWNLOAD_DATA, ArangoVertex.DOWNLOAD, data['id'], ArangoVertex.DATA, data['data_id'], transaction) transaction.commit() return True except Exception as exception: raise exception
def delete_data(cls, data): try: database = ArangoCore.get_database() collections = [ArangoVertex.DATA, ArangoEdge.USER_DATA] with database.transaction(write=collections, commit_on_error=False) as transaction: data_id = data.get('id') user_id = data.get('user_id') ArangoCore.delete_vertex_from_collection( ArangoVertex.DATA, data_id, transaction) key = '{0}-{1}'.format(user_id, data_id) ArangoCore.delete_edge_from_collection(ArangoEdge.USER_DATA, key, transaction) transaction.commit() return True except Exception as exception: raise exception
def save_post(cls, post_data): try: database = ArangoCore.get_database() collections = POST_COLLECTIONS with database.transaction(write=collections, commit_on_error=False) as transaction: # Add post to graph vertex ArangoCore.add_vertex_to_collection(ArangoVertex.POST, post_data, transaction) # Add user_post to graph edge ArangoCore.add_edge_to_collection(ArangoEdge.USER_POST, ArangoVertex.USER, post_data['user_id'], ArangoVertex.POST, post_data['id'], transaction) transaction.commit() return True except Exception as exception: raise exception
def get_post_of_user(cls, user_id): user_id = 'sn_users/' + str(user_id) query_string = "FOR post IN OUTBOUND @user_id sn_user_post OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "SORT post.created_at DESC " \ "LET user = (FOR user IN sn_users FILTER user._key == TO_STRING(post.user_id) LIMIT 1 " \ "RETURN user)[0] " \ "RETURN merge(post,{user})" parameter = {'user_id': user_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_commnet(cls, post_id): post_id = 'sn_posts/' + str(post_id) query_string = "FOR comment IN OUTBOUND @post_id sn_post_comment OPTIONS {bfs: true, uniqueVertices: 'global'} " \ "SORT comment.created_at ASC " \ "LET user = (FOR user IN sn_users FILTER user._key == TO_STRING(comment.user_id) LIMIT 1 " \ "RETURN user)[0] " \ "RETURN merge(comment,{user})" parameter = {'post_id': post_id} result = ArangoCore.execute_query(query_string, parameter) return result
def get_user_channels_by_user_id(cls, user_id): try: start_id = 'rv_users/{0}'.format(user_id) query_string = "FOR v, e IN OUTBOUND @start_id rv_user_channels OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN e.name " parameter = {'start_id': str(start_id)} return ArangoCore.execute_query(query_string, parameter) except Exception as exception: raise exception
def get_data(cls, data_id, get_name=False): try: if get_name: query_string = "FOR data IN sn_datas FILTER data.id == @data_id LIMIT 1 RETURN data.name" else: query_string = "FOR data IN sn_datas FILTER data.id == @data_id LIMIT 1 RETURN data" parameter = {'data_id': data_id} result = ArangoCore.execute_query(query_string, parameter) return result[0] if len(result) > 0 else None except Exception as exception: raise exception
def create_edge_collection(cls, user_id): try: graph = ArangoCore.get_graph() # Create a new edge definition (and a new edge collection) collection_name = '{0}_{1}'.format(ArangoEdge.USER_POST, user_id) graph.create_edge_definition(name=collection_name, from_collections=[ArangoVertex.USER], to_collections=[ArangoVertex.POST]) except Exception as exception: raise exception
def save_user(cls, user_id, user_data=None, is_new=True): try: user_data = cls.prepare_data_to_save(user_id, user_data, is_new) database = ArangoCore.get_database() collections = [ArangoVertex.USER] with database.transaction(write=collections, commit_on_error=False) as transaction: if is_new: ArangoCore.add_vertex_to_collection( ArangoVertex.USER, user_data, transaction) else: ArangoCore.update_vertex_in_collection( ArangoVertex.USER, user_data, transaction) user_data['_id'] = '{0}/{1}'.format( 'rv_users', user_data['id']) transaction.commit() return True except Exception as exception: raise exception
def unfollow_band(cls, user_id, band_data): try: database = ArangoCore.get_database() collections = ACTIVITY_COLLECTIONS collections += [ArangoVertex.BAND, ArangoEdge.USER_FOLLOW] with database.transaction(write=collections, commit_on_error=False) as transaction: # Remove relation between 2 user _key = '{0}-{1}'.format(user_id, band_data['user_id']) ArangoCore.delete_edge_from_collection(ArangoEdge.USER_FOLLOW, _key) # Update band data cls.update_band(band_data['user_id'], band_data['follow_count'], transaction) transaction.commit() except Exception as exception: raise exception
def save_comment(cls, comment_data): try: database = ArangoCore.get_database() collections = [ArangoVertex.COMMENT] + [ ArangoEdge.POST_COMMENT ] + [ArangoVertex.ACTIVITY] with database.transaction(write=collections, commit_on_error=False) as transaction: # Add post to graph vertex ArangoCore.add_vertex_to_collection(ArangoVertex.COMMENT, comment_data, transaction) # Add user_post to graph edge ArangoCore.add_edge_to_collection(ArangoEdge.POST_COMMENT, ArangoVertex.POST, comment_data['post_id'], ArangoVertex.COMMENT, comment_data['id'], transaction) transaction.commit() return True except Exception as exception: raise exception
def get_download_history_of_user(cls, user_id): try: user_id = 'sn_users/' + user_id query_string = "LET historys = (FOR v,history IN OUTBOUND @user_id sn_user_download " \ "SORT history.created_at DESC " \ "RETURN merge(history,{infor:v})) " \ "FOR history IN historys " \ "FOR data IN sn_datas " \ "FILTER data.id == history.infor.data_id " \ "RETURN merge(history,{data:data}) " parameter = {'user_id': user_id} return ArangoCore.execute_query(query_string, parameter) except Exception as exception: raise exception
def get_user_channels_by_band_id(cls, band_id): try: start_id = 'rv_users/{0}'.format(band_id) query_string = "FOR user IN INBOUND @start_id rv_user_follows OPTIONS {bfs: true, uniqueVertices: 'global'} " query_string += "LET channels = (FOR v, e IN OUTBOUND user._id rv_user_channels OPTIONS {bfs: true, uniqueVertices: 'global'} RETURN e.name) " query_string += "RETURN {'user_id': user.id, 'channels': channels}" parameter = {'start_id': str(start_id)} channels = [] user_channels = ArangoCore.execute_query(query_string, parameter) for user_channel in user_channels: channels += user_channel['channels'] return channels except Exception as exception: raise exception
def prepare_data_to_save(cls, user_id, user_data, is_new=True): data_to_save = {} if is_new else ArangoCore.get_vertex_in_collection( ArangoVertex.USER, user_id) for user_field in USER_FIELDS: if user_data and user_field in user_data: data_to_save[user_field] = user_data[user_field] # if data_to_save['avatar'] != '' and data_to_save['avatar'][:4] != 'http': # data_to_save['avatar_host'] = settings.AWS_URL if data_to_save['is_avatar_uploaded_to_s3'] else settings.API_URL # else: # data_to_save['avatar_host'] = '' # # if data_to_save['background'] != '': # data_to_save['background_host'] = settings.AWS_URL if data_to_save['is_background_uploaded_to_s3'] else settings.API_URL # else: # data_to_save['background_host'] = '' return data_to_save
def update_user_posts(cls, user_id, band_id): try: post_ids = ArangoPostService.get_post_ids_by_band_id(band_id) edge_collection_name = '{0}_{1}'.format(ArangoEdge.USER_POST, user_id) batch = ArangoCore.get_batch() for post_id in post_ids: batch.collection(edge_collection_name).insert({ '_key': '{0}-{1}'.format(user_id, post_id), '_from': '{0}/{1}'.format(ArangoVertex.USER, user_id), '_to': '{0}/{1}'.format(ArangoVertex.POST, post_id) }) batch.commit() except Exception as exception: raise exception
def has_vertex_in_collection(cls, collection_name, condition): return ArangoCore.has_vertex_in_collection(collection_name, condition)