Exemplo n.º 1
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 assign_if_empty (previous assign_if_none)
	# - Achilles Rasquinha [email protected]
	authenticate(user)

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

	rooms   = listify(assign_if_empty(rooms,  [ ]))
	fields  = listify(assign_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,
		debug    = bool(frappe.conf.get('developer_mode'))
	)

	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
Exemplo n.º 2
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,
                content=dmess.content,
                urls=dmess.urls,
                mentions=dmess.mentions,
                creation=dmess.creation,
                seen=assign_if_empty(dmess._seen, []))

    return data