Ejemplo n.º 1
0
def get(user=None, token=None, rooms=None, fields=None, filters=None):
	# There is this horrible bug out here.
	# Looks like if frappe.call sends optional arguments (not in right order),
	# the argument turns to an empty string.
	# I'm not even going to think searching for it.
	# Hence, the hack was get_if_empty (previous assign_if_none)
	# - Achilles Rasquinha [email protected]
	data = user or token
	authenticate(data)

	rooms, fields, filters = safe_json_loads(rooms, fields, filters)

	rooms = listify(get_if_empty(rooms, []))
	fields = listify(get_if_empty(fields, []))

	const = []  # constraints
	if rooms:
		const.append(['Chat Room', 'name', 'in', rooms])
	if filters:
		if isinstance(filters[0], list):
			const = const + filters
		else:
			const.append(filters)

	default = ['name', 'type', 'room_name', 'creation', 'owner', 'avatar']
	handle = ['users', 'last_message']

	param = [f for f in fields if f not in handle]

	rooms = frappe.get_all('Chat Room',
			or_filters=[
				['Chat Room', 'owner', '=', frappe.session.user],
				['Chat Room User', 'user', '=', frappe.session.user]
			],
			filters=const,
			fields=param + ['name'] if param else default,
			distinct=True
		)

	if not fields or 'users' in fields:
		for i, r in enumerate(rooms):
			droom = frappe.get_doc('Chat Room', r.name)
			rooms[i]['users'] = []

			for duser in droom.users:
				rooms[i]['users'].append(duser.user)

	if not fields or 'last_message' in fields:
		for i, r in enumerate(rooms):
			droom = frappe.get_doc('Chat Room', r.name)
			if droom.last_message:
				rooms[i]['last_message'] = chat_message.get(droom.last_message)
			else:
				rooms[i]['last_message'] = None

	rooms = squashify(dictify(rooms))

	return rooms
Ejemplo n.º 2
0
def get(user, rooms = None, fields = None, filters = None):
	# There is this horrible bug out here.
	# Looks like if frappe.call sends optional arguments (not in right order), the argument turns to an empty string.
	# I'm not even going to think searching for it.
	# Hence, the hack was get_if_empty (previous assign_if_none)
	# - Achilles Rasquinha [email protected]
	authenticate(user)

	rooms, fields, filters = safe_json_loads(rooms, fields, filters)

	rooms   = listify(get_if_empty(rooms,  [ ]))
	fields  = listify(get_if_empty(fields, [ ]))

	const   = [ ] # constraints
	if rooms:
		const.append(['Chat Room', 'name', 'in', rooms])
	if filters:
		if isinstance(filters[0], list):
			const = const + filters
		else:
			const.append(filters)

	default = ['name', 'type', 'room_name', 'creation', 'owner', 'avatar']
	handle  = ['users', 'last_message']

	param   = [f for f in fields if f not in handle]

	rooms   = frappe.get_all('Chat Room',
		or_filters = [
			['Chat Room', 	   'owner', '=', user],
			['Chat Room User', 'user',  '=', user]
		],
		filters  = const,
		fields   = param + ['name'] if param else default,
		distinct = True
	)

	if not fields or 'users' in fields:
		for i, r in enumerate(rooms):
			droom = frappe.get_doc('Chat Room', r.name)
			rooms[i]['users'] = [ ]

			for duser in droom.users:
				rooms[i]['users'].append(duser.user)

	if not fields or 'last_message' in fields:
		for i, r in enumerate(rooms):
			droom = frappe.get_doc('Chat Room', r.name)
			if droom.last_message:
				rooms[i]['last_message'] = chat_message.get(droom.last_message)
			else:
				rooms[i]['last_message'] = None

	rooms = squashify(dictify(rooms))

	return rooms
Ejemplo n.º 3
0
def get(name, rooms=None, fields=None):
    rooms, fields = safe_json_loads(rooms, fields)

    dmess = frappe.get_doc('Chat Message', name)
    data = dict(name=dmess.name,
                user=dmess.user,
                room=dmess.room,
                room_type=dmess.room_type,
                content=json.loads(dmess.content)
                if dmess.type in ["File"] else dmess.content,
                type=dmess.type,
                urls=dmess.urls,
                mentions=dmess.mentions,
                creation=dmess.creation,
                seen=get_if_empty(dmess._seen, []))

    return data
Ejemplo n.º 4
0
def get(name, rooms = None, fields = None):
	rooms, fields = safe_json_loads(rooms, fields)

	dmess = frappe.get_doc('Chat Message', name)
	data  = dict(
		name      = dmess.name,
		user      = dmess.user,
		room      = dmess.room,
		room_type = dmess.room_type,
		content   = json.loads(dmess.content) if dmess.type in ["File"] else dmess.content,
		type      = dmess.type,
		urls      = dmess.urls,
		mentions  = dmess.mentions,
		creation  = dmess.creation,
		seen      = get_if_empty(dmess._seen, [ ])
	)

	return data