예제 #1
0
def update(user, data):
	authenticate(user)

	data  = safe_json_loads(data)

	dprof = dataent.get_doc('Chat Profile', user)
	dprof.update(data)
	dprof.save(ignore_permissions = True)
예제 #2
0
def history(room, user, fields=None, limit=10, start=None, end=None):
    if dataent.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)
예제 #3
0
def get(user, rooms=None, fields=None, filters=None):
    # There is this horrible bug out here.
    # Looks like if dataent.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 = dataent.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 = dataent.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 = dataent.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
예제 #4
0
def create(user, exists_ok = False, fields = None):
	authenticate(user)

	exists_ok, fields = safe_json_loads(exists_ok, fields)

	try:
		dprof = dataent.new_doc('Chat Profile')
		dprof.user = user
		dprof.save(ignore_permissions = True)
	except dataent.DuplicateEntryError:
		if not exists_ok:
			dataent.throw(_('Chat Profile for User {0} exists.').format(user))

	profile = get(user, fields = fields)

	return profile
예제 #5
0
def get(name, rooms=None, fields=None):
    rooms, fields = safe_json_loads(rooms, fields)

    has_message = dataent.db.exists('Chat Message', name)

    if has_message:
        dmess = dataent.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
예제 #6
0
def settings(fields = None):
    fields    = safe_json_loads(fields)

    dsettings = dataent.get_single('Website Settings')
    response  = dict(
        socketio         = dict(
            port         = dataent.conf.socketio_port
        ),
        enable           = bool(dsettings.chat_enable),
        enable_from      = dsettings.chat_enable_from,
        enable_to        = dsettings.chat_enable_to,
        room_name        = dsettings.chat_room_name,
        welcome_message  = dsettings.chat_welcome_message,
        operators        = [
            duser.user for duser in dsettings.chat_operators
        ]
    )

    if fields:
        response = filter_dict(response, fields)

    return response
예제 #7
0
    def test_safe_json_loads(self):
        number = safe_json_loads("1")
        self.assertEqual(type(number), int)

        number = safe_json_loads("1.0")
        self.assertEqual(type(number), float)

        string = safe_json_loads("foobar")
        self.assertEqual(type(string), six.text_type)

        array = safe_json_loads('[{ "foo": "bar" }]')
        self.assertEqual(type(array), list)

        objekt = safe_json_loads('{ "foo": "bar" }')
        self.assertEqual(type(objekt), dict)

        true, null = safe_json_loads("true", "null")
        self.assertEqual(true, True)
        self.assertEqual(null, None)
예제 #8
0
def create(kind, owner, users=None, name=None):
    authenticate(owner)

    users = safe_json_loads(users)
    create = True

    if kind == 'Visitor':
        room = squashify(
            dataent.db.sql("""
			SELECT name
			FROM   `tabChat Room`
			WHERE  owner = "{owner}"
		""".format(owner=owner),
                           as_dict=True))

        if room:
            room = dataent.get_doc('Chat Room', room.name)
            create = False

    if create:
        room = dataent.new_doc('Chat Room')
        room.type = kind
        room.owner = owner
        room.room_name = name

    dusers = []

    if kind != 'Visitor':
        if users:
            users = listify(users)
            for user in users:
                duser = dataent.new_doc('Chat Room User')
                duser.user = user
                dusers.append(duser)

            room.users = dusers
    else:
        dsettings = dataent.get_single('Website Settings')
        room.room_name = dsettings.chat_room_name

        users = [user for user in room.users] if hasattr(room, 'users') else []

        for user in dsettings.chat_operators:
            if user.user not in users:
                # appending user to room.users will remove the user from chat_operators
                # this is undesirable, create a new Chat Room User instead
                chat_room_user = {
                    "doctype": "Chat Room User",
                    "user": user.user
                }
                room.append('users', chat_room_user)

    room.save(ignore_permissions=True)

    room = get(owner, rooms=room.name)
    users = [room.owner] + [u for u in room.users]

    for u in users:
        dataent.publish_realtime('dataent.chat.room:create',
                                 room,
                                 user=u,
                                 after_commit=True)

    return room