def on_create_room(self, raw_attendees, user): chat_room = ChatRoom() chat_room.creator = user for raw_attendee in raw_attendees: attendee_id = raw_attendee.get('id', None) if attendee_id: attendee = User.get(attendee_id) elif raw_attendee['type'] == 'facebook': # TODO race condition may occurs need cacth exceptions attendee = User.get_by_facebook(raw_attendee['uid']) if not attendee: attendee = User() facebook_account = attendee.facebook_account =\ FacebookAccount() facebook_account.name = attendee.nickname =\ raw_attendee['nickname'] attendee.email = raw_attendee.get('email', None) facebook_account.uid = raw_attendee['uid'] db.session.add(attendee) db.session.commit() else: # TODO email account? pass chat_room.users.append(attendee) db.session.add(chat_room) db.session.commit() json = chat_room.to_json() json['message'] = chat_room.messages.first() return None, chat_room.to_json()
def on_new_note(self, note, user): room = ChatRoom.get(note['id']) if not room: return '채팅방을 찾을수 없습니다.', if user not in room.users: return '채팅방에 참여하지 않았습니다.', message = Message() message.user = user message.message = note['message'] message.color = note['color'] for u in room.users: log = MessageLog() log.user = u if u is user: log.status |= MessageLog.READ message.logs.append(log) room.messages.append(message) db.session.commit() print message.logs.all() json = message.to_json() self.emit_to_room(str(room.id), 'new note', json) online_users = self.users_in_room(str(room.id)) q = room.users.filter(User.status==User.NORMAL) if online_users: q = q.filter(not_(User.id.in_(online_users))) for offline_user in q: pass return None, json
def on_list_notes(self, room_id, user): room = ChatRoom.get(room_id) if not room: return '채팅방을 찾을수 없습니다.', if user not in room.users: return '채팅방에 참여하지 않았습니다.', return\ None,\ tuple( n.to_json() \ for n in room.messages.order_by(Message.created.desc()) )
def on_leave_chat_room(self, room_id, user): chat_room = ChatRoom.get(room_id) chat_room.users.remove(user) db.session.commit()