seitime-frappe/frappe/chat/util/util.py
Achilles Rasquinha 005cfe3dc8 🎉 NEW Frappe Chat (#4612)
* added doctypes, created frappe chat ui

* added component layout with state-like abilities, added apis

* updated user doctype, moved from state-like feature and component abstraction

* added room component

* fixed publish_realtime with after_commit = True

* created room component and searchbar

* minor fix

* functional message parsing

* update

* Added Chat Profile

* added chat message

* more changes into chat room

* fixed APIs, added client side scripting

* added chat message attachements, more doc updates

* Brand New UI with socket io room integration

* completed socketio integration. off to room subscription and publish

* realtime room update

* raw update

* initialized docs, added p2p connection for call tests

* updated docs

* added coverage, updated api for ease of use

* raw commit

* added test cases

* Chat Room updates and new room creation

* added chat group creation

* added collapsible plugin

* toggable room view

* updated

* [RAW]

* updated UI for chat

* Deleted Previous Chat Page

* moved from frappe.Chat.Widget to frappe.Chat

* modularized frappe-fab

* added more docstrings

* tried adding conversation tones

* Added conversation_tones and refurbished chat popper

* modified frappe.ui.Dialog, moved from AppBar to ActionBar, responsive for Mobile 💃

* moved RoomList item namespace

* Configurable Desktop update, moved profile updates to on_update

* added state change listeners

* removed AppBar to ActionBar customizable 💃

* added destroy method

* removed coverage, refactored group creation

* Successful Chat Rooms and Group creation

* sort rows based on last_message_timestamp or creation

* added frappe._.compare

* removed redundant less variables

* Chat Room back button with custom routing and destroy methods

* Added EmojiPicker

* fixed multiple dialog render

* setup quick access

* added chat chime, functional chat message list updates at room list

* deleted package-lock.json

* realtime date updates

* updated chat message list

* functional message render and updates

* added track seen

* added typing status

* updated typing status

* valid typing statuses and quick search

* Functional Quick Search

* reverted fix

* some more cleanup and promisifed

* fixed hints close on click

* updated fab boldness

* close popper on click panel

* close popper on click panel

* reverted octicon-lg, fixed popper heading click

* new frappe capture

* removed webcamjs

* added uploader and capture

* removed chat FAB, added as notification instead

* on message update
2017-12-28 18:58:43 +05:30

114 lines
No EOL
2.2 KiB
Python

# imports - third-party imports
import requests
# imports - compatibility imports
import six
# imports - standard imports
from collections import MutableSequence, Mapping, MutableMapping
if six.PY2:
from urlparse import urlparse # PY2
else:
from urllib.parse import urlparse # PY3
import json
# imports - module imports
from frappe.model.document import Document
from frappe.exceptions import DuplicateEntryError
from frappe import _dict
import frappe
session = frappe.session
def get_user_doc(user = None):
if isinstance(user, Document):
return user
user = user or session.user
user = frappe.get_doc('User', user)
return user
def squashify(what):
if isinstance(what, MutableSequence) and len(what) == 1:
return what[0]
return what
def safe_json_loads(*args):
results = [ ]
for arg in args:
try:
arg = json.loads(arg)
except Exception as e:
pass
results.append(arg)
return squashify(results)
def filter_dict(what, keys, ignore = False):
copy = dict()
if keys:
for k in keys:
if k not in what and not ignore:
raise KeyError('{key} not in dict.'.format(key = k))
else:
copy.update({
k: what[k]
})
else:
copy = what.copy()
return copy
def assign_if_none(a, b):
if a is None:
a = b
return a
def listify(arg):
if not isinstance(arg, list):
arg = [arg]
return arg
def dictify(arg):
if isinstance(arg, MutableSequence):
for i, a in enumerate(arg):
arg[i] = dictify(a)
elif isinstance(arg, MutableMapping):
arg = _dict(arg)
return arg
def check_url(what, raise_err = False):
if not urlparse(what).scheme:
if raise_err:
raise ValueError('{what} not a valid URL.')
else:
return False
return True
def create_test_user(module):
try:
test_user = frappe.new_doc('User')
test_user.first_name = '{module}'.format(module = module)
test_user.email = 'testuser.{module}@example.com'.format(module = module)
test_user.save()
except DuplicateEntryError:
frappe.log('Test User Chat Profile exists.')
def get_emojis():
redis = frappe.cache()
emojis = redis.hget('frappe_emojis', 'emojis')
if not emojis:
resp = requests.get('http://git.io/frappe-emoji')
if resp.ok:
emojis = resp.json()
redis.hset('frappe_emojis', 'emojis', emojis)
return dictify(emojis)