def deleteGroupChatByName(self, gname):
     dao = GroupChatsDAO()
     if not dao.getGroupChatByName(gname):
         return jsonify(Error="Group Chat not found."), 404
     else:
         dao.deleteGroupChatByName(gname)
         return jsonify(DeleteStatus="OK"), 200
def deleteReply(self, uid, gid, mid, rpid):
    dao = GroupChatsDAO()
    if not dao.getReplyById(uid, gid, mid, rpid):
        return jsonify(Error="Group Chat not found."), 404
    else:
        dao.deleteReply(rpid)
        return jsonify(DeleteStatus="OK"), 200
 def deleteGroupChatById(self, uid, gid):
     dao = GroupChatsDAO()
     if not dao.getGroupChatByGroupChatIdAndUserId(gid, uid):
         return jsonify(Error="Group Chat not found."), 404
     else:
         dao.deleteGroupChatById(gid)
         return jsonify(DeleteStatus="OK"), 200
def deleteReaction(self, gid, mid, rid):
    dao = GroupChatsDAO()
    if not dao.getReactionById(rid):
        return jsonify(Error="Group Chat not found."), 404
    else:
        dao.deleteReaction(rid)
        return jsonify(DeleteStatus="OK"), 200
def deleteUserFromGroupChat(self, userid1, userid2, gid):
    dao = GroupChatsDAO()
    if dao.getMemberFromGroupChat(gid, userid2) is None:
        return jsonify(Error="Member not found in Group Chat."), 404
    else:
        dao.deleteUserFromGroupChat(userid2, gid)
        return jsonify(DeleteStatus="OK"), 200
def deleteMessage(self, uid, gid, mid):
    dao = GroupChatsDAO()
    if not dao.getMessageFromGroupChatById(uid, gid, mid):
        return jsonify(Error="Group Chat not found."), 404
    else:
        dao.deleteMessage(mid)
        return jsonify(DeleteStatus="OK"), 200
 def getGroupChatInfoById(self, groupchatid):
     dao = GroupChatsDAO()
     row = dao.getGroupChatInfoById(groupchatid)
     if not row:
         return jsonify(Error="Group chat not found"), 404
     else:
         groupchat = self.build_groupChats_dict(row)
         return jsonify(GroupChat=groupchat)
 def getAvailableGroupChatsByUserId(self, userid):
     dao = GroupChatsDAO()
     chat_list = dao.getAvailableGroupChatsByUserId(userid)
     result_map = []
     for row in chat_list:
         result = self.build_groupChats_dict(row)
         result_map.append(result)
     return jsonify(GroupChats=result_map), 201
 def getMessageFromGroupChatById(self, gid, uid, mid):
     dao = GroupChatsDAO()
     row = dao.getMessageFromGroupChatById(gid, uid, mid)
     if not row:
         return jsonify(Error="Message Not Found"), 404
     else:
         message = self.build_message_dict(row)
         return jsonify(Message=message)
 def getMessageReactionById(self, uid, gid, mid, rpid):
     dao = GroupChatsDAO()
     row = dao.getReactionById(rpid)
     if not row:
         return jsonify(Error="Reply not found"), 404
     else:
         groupchat = self.build_reaction_update_dict(row)
         return jsonify(GroupChat=groupchat)
 def getReplyById(self, uid, gid, mid, rpid):
     dao = GroupChatsDAO()
     row = dao.getReplyById(uid, gid, mid, rpid)
     if not row:
         return jsonify(Error="Reply not found"), 404
     else:
         groupchat = self.build_reply_dict1(row)
         return jsonify(GroupChat=groupchat)
 def getGroupChatById(self, gid):
     dao = GroupChatsDAO()
     chat_list = dao.getGroupChatById(gid)
     result_map = []
     if not chat_list:
         return jsonify(Error="GroupChat Not Found"), 404
     else:
         result_map = self.build_groupChats_dict(chat_list)
         return jsonify(GroupChat=result_map), 201
 def getMessagesFromGroupChatByUserIdAndGroupChatId(self, userid,
                                                    groupchatid):
     dao = GroupChatsDAO()
     result = dao.getMessagesFromGroupChatByUserIdAndGroupChatId(
         userid, groupchatid)
     result_map = []
     for r in result:
         result_map.append(self.build_message_dict(r))
     return jsonify(Messages=result_map), 201
 def getMessagesByHashtagStringInGroupChat(self, userid, groupchatid,
                                           hashtagstring):
     dao = GroupChatsDAO()
     result_map = []
     temp_hash = "#" + hashtagstring
     result = dao.getMessagesByHashtagStringInGroupChat(
         userid, groupchatid, temp_hash)
     for r in result:
         result_map.append(self.build_message_dict(r))
     return jsonify(Messages=result_map), 201
 def getAllGroupChats(self):
     dao = GroupChatsDAO()
     groupChats = dao.getAllGroupChats()
     result_map = []
     if not groupChats:
         return jsonify(Error="No group chats to show")
     else:
         for g in groupChats:
             result_map.append(self.build_groupChats_dict(g))
         return jsonify(GroupChats=result_map)
 def getAllMessages(self):
     dao = GroupChatsDAO()
     messages = dao.getAllMessages()
     result_map = []
     if not messages:
         return jsonify(Error="No messages to show")
     else:
         for m in messages:
             result_map.append(self.build_message_dict(m))
         return jsonify(Messages=result_map)
 def getMessageDislikesInGroupChatByUserIdGroupChatIdAndMessageId(
         self, userid, messageid, groupchatid):
     dao = GroupChatsDAO()
     result = dao.getMessageDislikesInGroupChatByUserIdGroupChatIdAndMessageId(
         userid, groupchatid, messageid)
     result_map = []
     if result is None:
         return jsonify(Error="Unable to get reactions")
     else:
         for r in result:
             result_map.append(self.build_reactions_dict(r))
         return jsonify(Reactions=result_map)
 def getRepliesFromMessageInGroupChatByUserIdAndGroupChatIdAndMessageId(
         self, userid, groupchatid, messageid):
     dao = GroupChatsDAO()
     result = dao.getRepliesFromMessageInGroupChatByUserIdAndGroupChatIdandMessageId(
         userid, groupchatid, messageid)
     result_map = []
     if result is None:
         return jsonify(Error="Unable to get replies")
     else:
         for r in result:
             result_map.append(self.build_reply_dict(r))
         return jsonify(Replies=result_map)
 def addUserToGroupChat(self, userid, groupchatid, json):
     dao = GroupChatsDAO()
     if len(json) != 1:
         return jsonify(Error="Malformed update request"), 400
     else:
         userid2 = json['uid']
         if userid2:
             gid = dao.addUserToGroupChat(userid2, groupchatid)
             result = self.build_ismember_attributes(gid, userid2)
             return jsonify(Membership=result), 201
         else:
             return jsonify(
                 Error="Unexpected attributes in post request"), 400
def updateMessage(self, userid, groupchatid, mid, json):
    dao = GroupChatsDAO()
    if not dao.getMessageFromGroupChatById(userid, groupchatid, mid):
        return jsonify(Error="Message not found"), 404
    else:
        if len(json) != 7:
            return jsonify(Error="Malformed update request"), 400
        else:
            mmessage = json['mmessage']
            mupload_date = json['mupload_date']
            msize = json['msize']
            mlength = json['mlength']
            mtype = json['mtype']
            mpath = json['mpath']
            mhashtag = json['mhashtag']
            uid = userid
            if mmessage and msize and mlength and mtype and mpath:
                dao.updateMessage(mid, mmessage, msize, mlength, mtype, mpath)
                result = self.build_messages_attributes(
                    mid, mmessage, mupload_date, msize, mlength, mtype, mpath,
                    mhashtag, uid)
                for value in mhashtag:
                    if not dao.getHashtagByHashtag(value):
                        entry = dao.insertHashtagAndContainsFromMessage(
                            mid, value)
                    else:
                        entry = dao.insertContainsFromMessage(mid, value)

                return jsonify(Message=result), 201
            else:
                return jsonify(
                    Error="Unexpected attributes in update request"), 400
def reactToReplyInGroupChatByUserIdAndGroupChatIdAndMessageId(
        self, userid, groupchatid, messageid, json):
    dao = GroupChatsDAO()
    if len(form) != 1:
        return jsonify(Error="Malformed update request"), 400
    else:
        rtype = json['rtype']
        rupload_date = dt.datetime.now().date().strftime("%m/%d/%Y")
        if rtype:
            rid = dao.reactToReplyInGroupChatByUserIdAndGroupChatIdAndMessageId(
                userid, groupchatid, messageid, rtype)
            result = self.build_reactions_attributes(rid, rtype, rupload_date)
            return jsonify(Reaction=result), 201
        else:
            return jsonify(Error="Unexpected attributes in post request"), 400
def updateReply(self, userid, groupchatid, messageid, rpid, json):
    dao = GroupChatsDAO()
    if not dao.getReplyByIdOnly(rpid):
        return jsonify(Error="Reply not found"), 404
    else:
        if len(json) != 7:
            return jsonify(Error="Malformed update request"), 400
        else:
            rpreply = json['rpreply']
            rpupdate_date = json['rpupload_date']
            rpsize = json['rpsize']
            rplength = json['rplength']
            rptype = json['rptype']
            rppath = json['rppath']
            rphashtag = json['rphashtag']
            uid = userid
            if rpreply:
                rpid = dao.updateReply(rpid, rpreply, rpsize, rplength, rptype,
                                       rppath)
                result = self.build_reply_attributes(rpid, rpupdate_date,
                                                     rpreply, rpsize, rplength,
                                                     rptype, rppath, messageid,
                                                     uid)
                for value in rphashtag:
                    if dao.getHashtagByHashtag(value) is None:
                        entry = dao.insertHashtagAndContainsFromReply(
                            rpid, value)
                    else:
                        entry = dao.insertContainsFromReply(rpid, value)
                return jsonify(Update=result), 200
            else:
                return jsonify(
                    Error="Unexpected attributes in update request"), 400
def updateReaction(self, gid, mid, rid, json):
    dao = GroupChatsDAO()
    if not dao.getReactionById(rid):
        return jsonify(Error="Reaction not found"), 404
    else:
        if len(json) != 1:
            return jsonify(Error="Malformed update request"), 400
        else:
            rtype = json['rtype']
            rupdate_date = dt.datetime.now().date().strftime("%m/%d/%Y")
            if rtype is not None:
                rid = dao.updateReaction(rid, rtype)
                result = self.build_reaction_update_attributes(
                    rid, rtype, rupdate_date)
                return jsonify(Update=result), 200
            else:
                return jsonify(
                    Error="Unexpected attributes in update request"), 400
    def createGroupChat(self, userid, form):
        if len(form) != 2:
            return jsonify(Error="Malformed Post Request"), 400
        else:
            gname = form['gname']
            gpicture_id = form['gpicture_id']
            gcreation_date = dt.datetime.now().date().strftime("%m/%d/%Y")
            if gname and gpicture_id and userid:
                dao = GroupChatsDAO()
                gid = dao.createGroupChat(userid, gname, gpicture_id)
                result = self.build_groupChat_attributes(
                    gid, gname, gcreation_date, gpicture_id, userid)
                if result is None:
                    return jsonify(Error="Unable to create group chat")
                else:
                    return jsonify(GroupChat=result)

            else:
                return jsonify(Error="Malformed Post Request"), 400
 def updateGroupChat(self, uid, gid, json):
     dao = GroupChatsDAO()
     if not dao.getGroupChatById(gid):
         return jsonify(Error="GroupChat not found"), 404
     else:
         if len(json) != 2:
             return jsonify(Error="Malformed update request"), 400
         else:
             gname = json['gname']
             gpicture_id = json['gpicture_id']
             gcreation_date = dt.datetime.now().date().strftime("%m/%d/%Y")
             if gname and gpicture_id:
                 gid = dao.updateGroupChat(gid, gname, gpicture_id)
                 result = self.build_groupChat_attributes(
                     gid, gname, gcreation_date, gpicture_id, uid)
                 return jsonify(GroupChat=result), 200
             else:
                 return jsonify(
                     Error="Unexpected attributes in update request"), 400
 def replyToMessageInGroupChatByUserIdAndGroupChatIdAndMessageId(
         self, userid, groupchatid, messageid, json):
     dao = GroupChatsDAO()
     if len(json) != 6:
         return jsonify(Error="Malformed update request"), 400
     else:
         rpreply = json['rpreply']
         rpupload_date = dt.datetime.now().date().strftime("%m/%d/%Y")
         rpsize = json['rpsize']
         rplength = json['rplength']
         rptype = json['rptype']
         rppath = json['rppath']
         rphashtag = json['rphashtag']
         if rptype and rpupload_date:
             rpid = dao.replyToMessageInGroupChatByUserIdAndGroupChatIdAndMessageId(
                 userid, groupchatid, messageid, rpreply, rpsize, rplength,
                 rptype, rppath)
             result = self.build_reply_attributes(rpid, rpreply,
                                                  rpupload_date, rpsize,
                                                  rplength, rppath, rptype,
                                                  messageid, userid)
             for value in rphashtag:
                 if dao.getHashtagByHashtag(value) is None:
                     entry = dao.insertHashtagAndContainsFromReply(
                         rpid, value)
                 else:
                     entry = dao.insertContainsFromReply(rpid, value)
             return jsonify(Reply=result), 201
         else:
             return jsonify(
                 Error="Unexpected attributes in post request"), 400
    def insertMessage(self, userid, groupchatid, json):
        dao = GroupChatsDAO()
        image = json['image']
        if len(image) != 0:
            name = str(uuid.uuid4())
            temp = image[0]['url']
            filetype = guess_extension(guess_type(temp)[0])
            path = 'src/static/' + name + filetype
            b64 = re.sub(r'^data:image\/[a-zA-Z]+;base64,', '', temp)
            with open(path, "wb") as fh:
                fh.write(base64.decodebytes((b64).encode('utf-8')))
            mpath = 'http://127.0.0.1:5000/static/' + name + filetype
            mtype = 'image/png'

        else:
            mpath = ''
            mtype = 'text'

        mmessage = json['mmessage']
        mupload_date = json['mupload_date']
        msize = json['msize']
        mlength = json['mlength']
        mhashtag = json['mhashtag']
        uid = userid
        mid = dao.insertMessage(uid, groupchatid, mmessage, msize, mlength,
                                mtype, mpath)
        result = self.build_messages_attributes(mid, mmessage, mupload_date,
                                                msize, mlength, mtype, mpath,
                                                mhashtag, uid)
        for value in mhashtag:
            value = value.lower()
            if dao.getHashtagByHashtag(value) is None:
                entry = dao.insertHashtagAndContainsFromMessage(mid, value)
            else:
                entry = dao.insertContainsFromMessage(mid, value)
        return jsonify(Message=result), 201