You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

chat.py 4.3 KiB

9 years ago
9 years ago
9 years ago
9 years ago
11 years ago
10 years ago
10 years ago
9 years ago
10 years ago
9 years ago
9 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. # Copyright (c) 2015, Frappe Technologies Pvt. Ltd. and Contributors
  2. # MIT License. See license.txt
  3. from __future__ import unicode_literals
  4. import frappe
  5. from frappe.desk.notifications import delete_notification_count_for
  6. from frappe.core.doctype.user.user import STANDARD_USERS
  7. from frappe.utils import cint
  8. @frappe.whitelist()
  9. def get_list(arg=None):
  10. """get list of messages"""
  11. frappe.form_dict['start'] = int(frappe.form_dict['start'])
  12. frappe.form_dict['page_length'] = int(frappe.form_dict['page_length'])
  13. frappe.form_dict['user'] = frappe.session['user']
  14. # set all messages as read
  15. frappe.db.sql("""UPDATE `tabCommunication` set seen = 1
  16. where
  17. communication_type in ('Chat', 'Notification')
  18. and seen = 0
  19. and reference_doctype = 'User'
  20. and reference_name = %s""", frappe.session.user)
  21. delete_notification_count_for("Chat")
  22. frappe.local.flags.commit = True
  23. fields = '''name, owner, modified, content, communication_type,
  24. comment_type, reference_doctype, reference_name'''
  25. if frappe.form_dict.contact == 'Bot':
  26. return frappe.db.sql("""select {0} from `tabCommunication`
  27. where
  28. comment_type = 'Bot'
  29. and reference_doctype = 'User'
  30. and reference_name = %(user)s
  31. order by creation desc
  32. limit %(start)s, %(page_length)s""".format(fields),
  33. frappe.local.form_dict, as_dict=1)
  34. if frappe.form_dict.contact == frappe.session.user:
  35. # return messages
  36. return frappe.db.sql("""select {0} from `tabCommunication`
  37. where
  38. communication_type in ('Chat', 'Notification')
  39. and comment_type != 'Bot'
  40. and reference_doctype ='User'
  41. and (owner=%(contact)s
  42. or reference_name=%(user)s
  43. or owner=reference_name)
  44. order by creation desc
  45. limit %(start)s, %(page_length)s""".format(fields),
  46. frappe.local.form_dict, as_dict=1)
  47. else:
  48. return frappe.db.sql("""select {0} from `tabCommunication`
  49. where
  50. communication_type in ('Chat', 'Notification')
  51. and comment_type != 'Bot'
  52. and reference_doctype ='User'
  53. and ((owner=%(contact)s and reference_name=%(user)s)
  54. or (owner=%(user)s and reference_name=%(contact)s))
  55. order by creation desc
  56. limit %(start)s, %(page_length)s""".format(fields),
  57. frappe.local.form_dict, as_dict=1)
  58. @frappe.whitelist()
  59. def get_active_users():
  60. data = frappe.db.sql("""select name,
  61. (select count(*) from tabSessions where user=tabUser.name
  62. and timediff(now(), lastupdate) < time("01:00:00")) as has_session
  63. from tabUser
  64. where enabled=1 and
  65. ifnull(user_type, '')!='Website User' and
  66. name not in ({})
  67. order by first_name""".format(", ".join(["%s"]*len(STANDARD_USERS))), STANDARD_USERS, as_dict=1)
  68. # make sure current user is at the top, using has_session = 100
  69. users = [d.name for d in data]
  70. if frappe.session.user in users:
  71. data[users.index(frappe.session.user)]["has_session"] = 100
  72. else:
  73. # in case of administrator
  74. data.append({"name": frappe.session.user, "has_session": 100})
  75. if 'System Manager' in frappe.get_roles():
  76. data.append({"name": "Bot", "has_session": 100})
  77. return data
  78. @frappe.whitelist()
  79. def post(txt, contact, parenttype=None, notify=False, subject=None):
  80. """post message"""
  81. comment_type = None
  82. if contact == 'Bot':
  83. contact = frappe.session.user
  84. comment_type = 'Bot'
  85. d = frappe.new_doc('Communication')
  86. d.communication_type = 'Notification' if parenttype else 'Chat'
  87. d.subject = subject
  88. d.content = txt
  89. d.reference_doctype = 'User'
  90. d.reference_name = contact
  91. d.sender = frappe.session.user
  92. if comment_type:
  93. d.comment_type = comment_type
  94. d.insert(ignore_permissions=True)
  95. delete_notification_count_for("Chat")
  96. if notify and cint(notify):
  97. _notify(contact, txt, subject)
  98. return d
  99. @frappe.whitelist()
  100. def delete(arg=None):
  101. frappe.get_doc("Communication", frappe.form_dict['name']).delete()
  102. def _notify(contact, txt, subject=None):
  103. from frappe.utils import get_fullname, get_url
  104. try:
  105. if not isinstance(contact, list):
  106. contact = [frappe.db.get_value("User", contact, "email") or contact]
  107. frappe.sendmail(\
  108. recipients=contact,
  109. sender= frappe.db.get_value("User", frappe.session.user, "email"),
  110. subject=subject or "New Message from " + get_fullname(frappe.session.user),
  111. message=frappe.get_template("templates/emails/new_message.html").render({
  112. "from": get_fullname(frappe.session.user),
  113. "message": txt,
  114. "link": get_url()
  115. }))
  116. except frappe.OutgoingEmailError:
  117. pass