<commit_before>from __future__ import unicode_literals

# imports - standard imports
import json

# imports - module imports
from   frappe.model.document import Document
from   frappe import _
import frappe

# imports - frappe module imports
from frappe.chat 						 import authenticate
from frappe.core.doctype.version.version import get_diff
from frappe.chat.doctype.chat_message	 import chat_message
from frappe.chat.util import (
	safe_json_loads,
	dictify,
	listify,
	squashify,
	get_if_empty
)

session = frappe.session

def is_direct(owner, other, bidirectional = False):
	def get_room(owner, other):
		room = frappe.get_all('Chat Room', filters = [
			['Chat Room', 	   'type' , 'in', ('Direct', 'Visitor')],
			['Chat Room', 	   'owner', '=' , owner],
			['Chat Room User', 'user' , '=' , other]
		], distinct = True)

		return room

	exists = len(get_room(owner, other)) == 1
	if bidirectional:
		exists = exists or len(get_room(other, owner)) == 1

	return exists

def get_chat_room_user_set(users, filter_ = None):
	seen, uset = set(), list()

	for u in users:
		if filter_(u) and u.user not in seen:
			uset.append(u)
			seen.add(u.user)

	return uset

class ChatRoom(Document):
	def validate(self):
		if self.is_new():
			users = get_chat_room_user_set(self.users, filter_ = lambda u: u.user != session.user)
			self.update(dict(
				users = users
			))

		if self.type == "Direct":
			if len(self.users) != 1:
				frappe.throw(_('{0} room must have atmost one user.').format(self.type))

			other = squashify(self.users)

			if self.is_new():
				if is_direct(self.owner, other.user, bidirectional = True):
					frappe.throw(_('Direct room with {0} already exists.').format(other.user))

		if self.type == "Group" and not self.room_name:
			frappe.throw(_('Group name cannot be empty.'))

	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:<commit_msg>fix: chat room creation<commit_after>from __future__ import unicode_literals

# imports - standard imports
import json

# imports - module imports
from   frappe.model.document import Document
from   frappe import _
import frappe

# imports - frappe module imports
from frappe.chat 						 import authenticate
from frappe.core.doctype.version.version import get_diff
from frappe.chat.doctype.chat_message	 import chat_message
from frappe.chat.util import (
	safe_json_loads,
	dictify,
	listify,
	squashify,
	get_if_empty
)

session = frappe.session

def is_direct(owner, other, bidirectional = False):
	def get_room(owner, other):
		room = frappe.get_all('Chat Room', filters = [
			['Chat Room', 	   'type' , 'in', ('Direct', 'Visitor')],
			['Chat Room', 	   'owner', '=' , owner],
			['Chat Room User', 'user' , '=' , other]
		], distinct = True)

		return room

	exists = len(get_room(owner, other)) == 1
	if bidirectional:
		exists = exists or len(get_room(other, owner)) == 1

	return exists

def get_chat_room_user_set(users, filter_ = None):
	seen, uset = set(), list()

	for u in users:
		if filter_(u) and u.user not in seen:
			uset.append(u)
			seen.add(u.user)

	return uset

class ChatRoom(Document):
	def validate(self):
		if self.is_new():
			users = get_chat_room_user_set(self.users, filter_ = lambda u: u.user != session.user)
			self.update(dict(
				users = users
			))

		if self.type == "Direct":
			if len(self.users) != 1:
				frappe.throw(_('{0} room must have atmost one user.').format(self.type))

			other = squashify(self.users)

			if self.is_new():
				if is_direct(self.owner, other.user, bidirectional = True):
					frappe.throw(_('Direct room with {0} already exists.').format(other.user))

		if self.type == "Group" and not self.room_name:
			frappe.throw(_('Group name cannot be empty.'))

	def on_update(self):
		if not self.is_new():
			before = self.get_doc_before_save()
			after  = self
			diff = None

			if before:
				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:
{'fraction_non_alphanumeric': 0.12666516143599008, 'fraction_numerical': 0.002257846014901784, 'mean_word_length': 3.57172342621259, 'pattern_counts': {'":': 2, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7047530', 'n_tokens_mistral': 1716, 'n_tokens_neox': 1515, 'n_words': 418}