Exemplo n.º 1
0
def get(user, fields=None):
    duser = frappe.get_doc('User', user)

    if frappe.db.exists('Chat Profile', user):
        dprof = frappe.get_doc('Chat Profile', user)

        # If you're adding something here, make sure the client recieves it.
        profile = dict(
            # User
            name=duser.name,
            email=duser.email,
            first_name=duser.first_name,
            last_name=duser.last_name,
            username=duser.username,
            avatar=duser.user_image,
            bio=duser.bio,
            # Chat Profile
            status=dprof.status,
            chat_background=dprof.chat_background,
            message_preview=bool(dprof.message_preview),
            notification_tones=bool(dprof.notification_tones),
            conversation_tones=bool(dprof.conversation_tones),
            enable_chat=bool(dprof.enable_chat))
        profile = filter_dict(profile, fields)

        return dictify(profile)
Exemplo n.º 2
0
    def on_update(self):
        if not self.is_new():
            before = self.get_doc_before_save()
            if not before: return

            after = self
            diff = dictify(get_diff(before, after))
            if diff:
                update = {}
                for changed in diff.changed:
                    field, old, new = changed

                    if field == 'last_message':
                        new = chat_message.get(new)

                    update.update({field: new})

                if diff.added or diff.removed:
                    update.update(dict(users=[u.user for u in self.users]))

                update = dict(room=self.name, data=update)

                frappe.publish_realtime('frappe.chat.room:update',
                                        update,
                                        room=self.name,
                                        after_commit=True)
Exemplo n.º 3
0
    def on_update(self):
        if not self.is_new():
            b, a = self.get_doc_before_save(), self
            diff = dictify(get_diff(a, b))
            if diff:
                user = session.user

                fields = [changed[0] for changed in diff.changed]

                if 'status' in fields:
                    rooms = chat_room.get(
                        user, filters=['Chat Room', 'type', '=', 'Direct'])
                    update = dict(user=user, data=dict(status=self.status))

                    for room in rooms:
                        frappe.publish_realtime('frappe.chat.profile:update',
                                                update,
                                                room=room.name,
                                                after_commit=True)

                if 'enable_chat' in fields:
                    update = dict(
                        user=user,
                        data=dict(enable_chat=bool(self.enable_chat)))
                    frappe.publish_realtime('frappe.chat.profile:update',
                                            update,
                                            user=user,
                                            after_commit=True)
Exemplo n.º 4
0
def get(user, fields = None):
    duser   = frappe.get_doc('User', user)
    dprof   = frappe.get_doc('Chat Profile', user)

    # If you're adding something here, make sure the client recieves it.
    profile = dict(
        # User
        name       = duser.name,
        email      = duser.email,
        first_name = duser.first_name,
        last_name  = duser.last_name,
        username   = duser.username,
        avatar     = duser.user_image,
        bio        = duser.bio,
        # Chat Profile
        status             = dprof.status,
        chat_background    = dprof.chat_background,
        message_preview    = bool(dprof.message_preview),
        notification_tones = bool(dprof.notification_tones),
        conversation_tones = bool(dprof.conversation_tones),
        enable_chat        = bool(dprof.enable_chat)
    )
    profile = filter_dict(profile, fields)

    return dictify(profile)
Exemplo n.º 5
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
Exemplo n.º 6
0
def history(room, user, fields=None, limit=10, start=None, end=None):
    authenticate(user)

    fields = safe_json_loads(fields)

    mess = chat_message.history(room, limit=limit, start=start, end=end)
    mess = squashify(mess)

    return dictify(mess)
Exemplo n.º 7
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
Exemplo n.º 8
0
def history(room, user, fields=None, limit=10, start=None, end=None):
    if frappe.get_doc('Chat Room', room).type != 'Visitor':
        authenticate(user)

    fields = safe_json_loads(fields)

    mess = chat_message.history(room, limit=limit, start=start, end=end)
    mess = squashify(mess)

    return dictify(mess)
Exemplo n.º 9
0
def history(room, user, fields = None, limit = 10, start = None, end = None):
	if frappe.get_doc('Chat Room', room).type != 'Visitor':
		authenticate(user)

	fields = safe_json_loads(fields)

	mess   = chat_message.history(room, limit = limit, start = start, end = end)
	mess   = squashify(mess)

	return dictify(mess)
Exemplo n.º 10
0
def get_version(doctype, name, limit = None, head = False, raise_err = True):
	'''
	Returns a list of version information of a given DocType (Applicable only if DocType has changes tracked).

	Example
	>>> frappe.get_version('User', '*****@*****.**')
	>>>
	[
		{
			 "version": [version.data], 	 # Refer Version DocType get_diff method and data attribute
			    "user": "******"    # User that created this version
			"creation": <datetime.datetime>  # Creation timestamp of that object.
		}
	]
	'''
	meta  = get_meta(doctype)
	if meta.track_changes:
		names = db.sql("""
			SELECT name from tabVersion
			WHERE  ref_doctype = '{doctype}' AND docname = '{name}'
			{order_by}
			{limit}
		""".format(
			doctype  = doctype,
			name     = name,
			order_by = 'ORDER BY creation'	 			     if head  else '',
			limit    = 'LIMIT {limit}'.format(limit = limit) if limit else ''
		))

		from frappe.chat.util import squashify, dictify, safe_json_loads

		versions = [ ]

		for name in names:
			name = squashify(name)
			doc  = get_doc('Version', name)

			data = doc.data
			data = safe_json_loads(data)
			data = dictify(dict(
				version  = data,
				user 	 = doc.owner,
				creation = doc.creation
			))

			versions.append(data)

		return versions
	else:
		if raise_err:
			raise ValueError('{doctype} has no versions tracked.'.format(
				doctype = doctype
			))
Exemplo n.º 11
0
def get_version(doctype, name, limit = None, head = False, raise_err = True):
	'''
	Returns a list of version information of a given DocType (Applicable only if DocType has changes tracked).

	Example
	>>> frappe.get_version('User', '*****@*****.**')
	>>>
	[
		{
			 "version": [version.data], 	 # Refer Version DocType get_diff method and data attribute
			    "user": "******"    # User that created this version
			"creation": <datetime.datetime>  # Creation timestamp of that object.
		}
	]
	'''
	meta  = get_meta(doctype)
	if meta.track_changes:
		names = db.sql("""
			SELECT name from tabVersion
			WHERE  ref_doctype = '{doctype}' AND docname = '{name}'
			{order_by}
			{limit}
		""".format(
			doctype  = doctype,
			name     = name,
			order_by = 'ORDER BY creation'	 			     if head  else '',
			limit    = 'LIMIT {limit}'.format(limit = limit) if limit else ''
		))

		from frappe.chat.util import squashify, dictify, safe_json_loads

		versions = [ ]

		for name in names:
			name = squashify(name)
			doc  = get_doc('Version', name)

			data = doc.data
			data = safe_json_loads(data)
			data = dictify(dict(
				version  = data,
				user 	 = doc.owner,
				creation = doc.creation
			))

			versions.append(data)

		return versions
	else:
		if raise_err:
			raise ValueError('{doctype} has no versions tracked.'.format(
				doctype = doctype
			))
Exemplo n.º 12
0
    def on_update(self):
        if not self.is_new():
            b, a = self.get_doc_before_save(), self
            diff = dictify(get_diff(a, b))
            if diff:
                user   = session.user

                fields = [changed[0] for changed in diff.changed]

                if 'status' in fields:
                    rooms  = chat_room.get(user, filters = ['Chat Room', 'type', '=', 'Direct'])
                    update = dict(user = user, data = dict(status = self.status))

                    for room in rooms:
                        frappe.publish_realtime('frappe.chat.profile:update', update, room = room.name, after_commit = True)

                if 'enable_chat' in fields:
                    update = dict(user = user, data = dict(enable_chat = bool(self.enable_chat)))
                    frappe.publish_realtime('frappe.chat.profile:update', update, user = user, after_commit = True)
Exemplo n.º 13
0
	def on_update(self):
		if not self.is_new():
			before = self.get_doc_before_save()
			after  = self

			diff   = dictify(get_diff(before, after))
			if diff:
				update = { }
				for changed in diff.changed:
					field, old, new = changed

					if field == 'last_message':
						new = chat_message.get(new)

					update.update({ field: new })

				if diff.added or diff.removed:
					update.update(dict(users = [u.user for u in self.users]))

				update = dict(room = self.name, data = update)

				frappe.publish_realtime('frappe.chat.room:update', update, room = self.name, after_commit = True)
Exemplo n.º 14
0
def history(room, user = None, pagination = 20):
	mess = chat_message.get_messages(room, pagination = pagination)

	mess = squashify(mess)
	
	return dictify(mess)