def draw_chat_object(self, user_id, session_id, type_, **kwargs): with self._session_ as session: user = session.query(model.User).get(user_id) chat_session = session.query(model.Session).get(session_id) if user not in chat_session.users: raise Exception("You cannot add to this session") if type_ == "Text": result = model.Text(**kwargs) elif type_ == "Line": result = model.Line(**kwargs) elif type_ == "Shape": result = model.Shape(**kwargs) else: raise Exception("unknown type: {}".format(type_)) result.owner = user chat_session.chat_objects.append(result) session.add(result) session.commit() msg = Protocol.chat_session_protocol(chat_session) for user in chat_session.users: self.broadcast_user(user.id, "drawn_chat_object", msg) return True
def session_start(self, user_id): with self._session_ as session: chat_session = model.Session() session.add(chat_session) chat_session.users.append(session.query(model.User).get(user_id)) session.commit() return Protocol.chat_session_protocol(chat_session)
def login(self, username, password): with self._session_ as session: user = session.query(model.User).filter_by(username=username, password=password).first() if user is None: raise Exception("No such user or password!") self._set_user_(user) return Protocol.user_protocol(user)
def login(self, username, password): with self._session_ as session: user = session.query(model.User).filter_by( username=username, password=password).first() if user is None: raise Exception("No such user or password!") self._set_user_(user) return Protocol.user_protocol(user)
def get_chat_session(self, user_id, session_id): with self._session_ as session: user = session.query(model.User).get(user_id) chat_session = session.query(model.Session).get(session_id) if user not in chat_session.users: raise Exception("You cannot view this session") return Protocol.chat_session_protocol(chat_session)
def register(self, username, password): with self._session_ as session: user = model.User(username=username, password=password) session.add(user) try: session.commit() self._set_user_(user) except IntegrityError: raise Exception("username already exists") return Protocol.user_protocol(user)
def delete_chat_object(self, user_id, chat_object_id): with self._session_ as session: user = session.query(model.User).get(user_id) chat_object = session.query(model.ChatObject).get(chat_object_id) if user not in chat_object.chat_session.users: raise Exception("You cannot add to this session") chat_session = chat_object.chat_session session.delete(chat_object) session.commit() msg = Protocol.chat_session_protocol(chat_session) for user in chat_session.users: self.broadcast_user(user.id, "deleted_chat_object", msg) return True
def add_user_to_session(self, user_id, session_id, friend_id): with self._session_ as session: user = session.query(model.User).get(user_id) friend = session.query(model.User).get(friend_id) chat_session = session.query(model.Session).get(session_id) session_users = list(chat_session.users) assert user in session_users assert friend not in session_users chat_session.users.append(friend) session.commit() msg = Protocol.chat_session_protocol(chat_session) for user in chat_session.users: self.broadcast_user(user.id, "drawn_chat_object", msg) return True
def _set_user_(self, user): if self._current_client_: self._current_client_.user_id = user.id if user else None if user: self._online[user.id] = Protocol.friend_protocol(user) self.broadcast("online", self._online.values())
def get_friend_by_id(self, user_id, friend_id): with self._session_ as session: friend = session.query(model.User).get(friend_id) return Protocol.friend_protocol(friend);
def session_end(self, user_id, session_id): with self._session_ as session: chat_session = session.query(model.Session).get(session_id) chat_session.ended = True session.commit() return Protocol.chat_session_protocol(chat_session)
def get_friend_by_id(self, user_id, friend_id): with self._session_ as session: friend = session.query(model.User).get(friend_id) return Protocol.friend_protocol(friend)