Exemplo n.º 1
0
def upload_msg(msg):
    _time = f"{date.today()}_{datetime.now().time().strftime('%H:%M:%S')}"
    user_name = user_auth.currentUser['displayName']

    def random_char(y):
        return ''.join(random.choice(string.ascii_letters) for _ in range(y))

    try:
        firebaseDB.child('chat_rooms').child(
            user_auth.CHATROOM_NAME).child('chats').child('temp').set({
                'msg':
                msg,
                'user':
                user_name,
                'time':
                _time,
                'date':
                f"{date.today()}",
            })
        firebaseDB.child('chat_rooms').child(user_auth.CHATROOM_NAME).child(
            'chats').child(f"{user_name}_{_time}_{random_char(5)}").set({
                'msg':
                msg,
                'user':
                user_name,
                'time':
                _time,
                'date':
                f"{date.today()}",
            })

    except Exception:
        raise Exception('ERROR_SEND_MSG')
    def leaveFirebaseChat(self):
        from chatroom_gui import ChatroomGUI

        if messagebox.askokcancel("Confirm leave",
                                  "Are you sure you want to leave"):
            temp_chatroom_name = user_auth.CHATROOM_NAME
            user_auth.CHATROOM_NAME = 'chatroom_0'

            self.cont.show_frame(ChatroomGUI)

            firebaseDB.child('chat_rooms').child(temp_chatroom_name).child(
                'attendees').child(user_auth.userID).remove()

            self.ScrolledAttendeesTextBox.delete('1.0', tk.END)
            self.ScrolledChatTextBox.delete('1.0', tk.END)
Exemplo n.º 3
0
def fetch_msg():
    try:
        return firebaseDB.child('chat_rooms').child(
            user_auth.CHATROOM_NAME).child('chats').child('temp').get()

    except Exception:
        raise Exception('ERROR_GET_MSG')
    def changeState(self):
        self.doNotUse()
        Variables.current_status = firebaseDB.child('chat_rooms').child(
            user_auth.CHATROOM_NAME).child('attendees').child(
                user_auth.userID).get().val()['status']

        if Variables.current_status == 'ONLINE':
            firebaseDB.child('chat_rooms').child(
                user_auth.CHATROOM_NAME).child('attendees').child(
                    user_auth.userID).update({'status': 'offline'})
            Variables.current_status = 'offline'

        elif Variables.current_status == 'offline':
            firebaseDB.child('chat_rooms').child(
                user_auth.CHATROOM_NAME).child('attendees').child(
                    user_auth.userID).update({'status': 'ONLINE'})
            Variables.current_status = 'ONLINE'
    def deleteChatroom(self):
        self.doNotUse()
        from chatroom_gui import ChatroomGUI
        created_user_id = Variables.chatroomData['created_userID']

        if user_auth.userID == created_user_id:
            if messagebox.askokcancel("Confirm delete",
                                      "Delete chatroom forever?"):
                temp_chatroom_name = user_auth.CHATROOM_NAME
                user_auth.CHATROOM_NAME = 'chatroom_0'
                self.cont.show_frame(ChatroomGUI)

                firebaseDB.child('chat_rooms').child(
                    temp_chatroom_name).remove()
                self.ScrolledAttendeesTextBox.delete('1.0', tk.END)
                self.ScrolledChatTextBox.delete('1.0', tk.END)

        else:
            messagebox.showerror("Error", "Sorry! You are not the admin")
    def signOutUser(self):
        self.doNotUse()
        from client_gui import LoginGUI
        if messagebox.askokcancel("Confirm Sign out",
                                  "Are you sure you want to\nSign Out"):

            temp_chatroom_name = user_auth.CHATROOM_NAME
            user_auth.CHATROOM_NAME = 'chatroom_0'
            self.cont.show_frame(LoginGUI)

            firebaseDB.child('chat_rooms').child(temp_chatroom_name).child(
                'attendees').child(user_auth.userID).update(
                    {'status': 'offline'})

            firebaseAuth.current_user = None
            user_auth.currentUser = None
            user_auth.userID = None

            self.ScrolledAttendeesTextBox.delete('1.0', tk.END)
            self.ScrolledChatTextBox.delete('1.0', tk.END)
    def getAttendees(self, attendees_box):
        while True:
            if Variables.threadFlag:
                break

            admin = Variables.chatroomData['created_userID']
            attendee_name_str = ''

            all_attendees = firebaseDB.child('chat_rooms').child(
                user_auth.CHATROOM_NAME).child('attendees').get()

            self.attendee = ''
            for usr in all_attendees.each():
                is_admin = False

                if "name" in usr.val():
                    if usr.key() == admin:
                        is_admin = True
                        attendee_name_str = f"{usr.val()['name']} (Admin)"
                    if usr.key() == user_auth.userID:
                        if is_admin:
                            attendee_name_str = f"{usr.val()['name']} (Admin)(You)"
                        else:
                            attendee_name_str = f"{usr.val()['name']} (You)"
                    elif usr.key() != admin:
                        attendee_name_str = f"{usr.val()['name']}"

                    if usr.val(
                    )['name'] != 'dummyName' and Variables.threadFlag is False:
                        user_str = f"{attendee_name_str}\n{usr.val()['email']}\n- - {usr.val()['status']} - - \n\n"
                        self.attendee = self.attendee + user_str

            if self.previous_attendee != self.attendee:
                self.previous_attendee = self.attendee

                attendees_box.configure(state=tk.NORMAL)
                attendees_box.delete('1.0', tk.END)

                attendees_box.insert(tk.END, self.attendee)
                attendees_box.configure(state=tk.DISABLED)
            time.sleep(1)
        print('getAttendees thread broken\n')
    def on_closing(self, del_temp=True):
        if messagebox.askokcancel("Quit", "Do you want to quit?"):
            self.destroy()
            if user_auth.CHATROOM_NAME != 'chatroom_0':
                if del_temp:
                    firebaseDB.child('chat_rooms').child(
                        user_auth.CHATROOM_NAME).child('attendees').child(
                            user_auth.userID).update({'status': 'offline'})
                    firebaseDB.child('chat_rooms').child(
                        user_auth.CHATROOM_NAME).child('chats').child(
                            'temp').remove()
                else:
                    firebaseDB.child('chat_rooms').child(
                        user_auth.CHATROOM_NAME).child('attendees').child(
                            user_auth.userID).update({'status': 'offline'})

                Variables.threadFlag = True
                Variables.firebaseReceive_thread.join()
                print('msg joined')
                # Variables.getAttendees_thread.join()        # TODO not joined
                print('ended')
                sys.exit()