Example #1
0
def removeUserFromGroup(userId, groupId):
    # check manager exists
    if not users.userExists(userId):
        return {'result' : 'failure', 'error' : 'User doesn\'t exist'}

    # validate group
    group = groupsCollection.where('group_id', groupId)
    if not group.exists():
        return {'result' : 'failure', 'error' : 'Group doesn\'t exist'}

    members = group['members'].get()
    if userId not in members:
        return {'result' : 'failure', 'error' : 'User not in group'}
    
    # remove user from group
    members.remove(userId)
    group['members'] = members
    
    # possibly remove user from manager list
    managers = group['managers'].get()
    if userId in managers:
        managers.remove(userId)
        group['managers'] = managers

    # remove group from user's group list
    users.getUserDocument(userId)['groups'].remove(groupId)

    return {'result' : 'success'}
def invite(leaderId, userId, partyId):
    user = users.getUserDocument(userId)
    if not user.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    leader = users.getUserDocument(leaderId)
    if not leader.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    party = partiesCollection.where('party_id', partyId)
    if not party.exists():
        return {'result': 'failure', 'error': 'Party doesn\'t exist'}

    if party['leader'].get() != leaderId:
        return {'result': 'failure', 'error': 'Not party leader'}

    if userId in party['members'].get():
        return {'result': 'failure', 'error': 'User already a member'}

    if userId in party['invited'].get():
        return {'result': 'failure', 'error': 'User already invited'}

    if userId in party['requests'].get():
        return {'result': 'failure', 'error': 'User already requested'}

    party['invited'].append(userId)
    user['party_invites'].append(partyId)

    # TODO - notify user

    return {'result': 'success'}
Example #3
0
def removeUserFromAllGroups(userId):
    
    if not users.userExists(userId):
        return {'result' : 'failure', 'error' : 'User doesn\'t exist'}

    groupIds = users.getUserDocument(userId)['groups'].get()
    for groupId in groupIds:
        removeUserFromGroup(userId, groupId)

    for groupId in users.getUserDocument(userId)['invited'].get():
        group = groupsCollection.where('group_id', groupId)
        group['invited'].remove(userId)
def createParty(
        leaderId,
        groups_,
        location,
        time_=None,  # a unix timestamp; if None, will replace with current time
        public=False,
        autoJoin=False,
        name="Party"):

    if time_ is None:
        time_ = int(time.time())

    partyId = secrets.token_hex(16)
    partiesCollection.new({
        'party_id': partyId,
        'name': name,
        'leader': leaderId,
        'groups': groups_,
        'location': location,
        'time': time_,
        'public': public,
        'auto_join': autoJoin,
        'members': [leaderId],
        'invited': [],
        'requests': [],
        'messages': []
    })

    leader = users.getUserDocument(leaderId)
    leader['parties'].append(partyId)

    notification = {
        'party_id': partyId,
        'name': name,
        'leader': leaderId,
        'groups': groups_,
        'location': location,
        'time': time_,
        'public': public,
        'auto_join': autoJoin,
        'n_members': 1
    }

    if public:
        if _messages:
            from user import messaging
            messaging.sendMessage('party_created', notification)

    else:
        if _messages:
            usersToNotify = set()
            for groupId in groups_:
                group = groups.getGroupDocument(groupId)
                if group.exists():
                    usersToNotify |= set(group['members'].get())

            from user import messaging
            messaging.sendMessage('party_created', notification, usersToNotify)

    return {'result': 'success', 'party_id': partyId}
Example #5
0
def searchGroups(groupName, userId = None, limit = 20):
    projection = {
        '_id' : 0,
        'group_id' : 1,
        'name' : 1,
        'public_joinable' : 1,
        'required_email' : 1
    }

    results1 = list(groupsCollection.raw.find(
        {'public_visible' : True},
        projection
    ))

    if userId:
        user = users.getUserDocument(userId)
        results2 = [
            groupsCollection.raw.find_one(
                {'group_id' : groupId},
                projection
            ) for groupId in (user['groups'].get() + user['invited'].get())
        ]

        resultsDict = {}
        for group in (results1 + results2):
            resultsDict[group['group_id']] = group
        
        results = list(resultsDict.values())

    else:
        results = results1
    
    return search(results, groupName, 'name', limit)
Example #6
0
def deleteGroup(manager, groupId):
    # check manager exists
    if not users.userExists(manager):
        return {'result' : 'failure', 'error' : 'User doesn\'t exist'}

    # validate group
    group = groupsCollection.where('group_id', groupId)
    if not group.exists():
        return {'result' : 'failure', 'error' : 'Group doesn\'t exist'}

    # validate manager
    if manager not in group['managers'].get():
        return {'result' : 'failure', 'error' : 'User not a group manager'}
    
    notification = {
        'group_id' : groupId,
        'name' : group['name'].get()
    }

    # update user records and notify users
    for userId in group['members'].get():
        user = users.getUserDocument(userId)

        user['groups'].remove(groupId)
        #messaging.addNotification(userId, 'group_delete', notification)

    # delete the group
    group.drop()

    return {'result' : 'success'}
Example #7
0
def joinGroup(userId, groupId):

    # check manager exists
    user = users.getUserDocument(userId)
    if not users.userExists(userId):
        return {'result' : 'failure', 'error' : 'User doesn\'t exist'}

    # validate group
    group = groupsCollection.where('group_id', groupId)
    if not group.exists():
        return {'result' : 'failure', 'error' : 'Group doesn\'t exist'}

    # validate that the user can join the group
    invited = group['invited'].get()
    if userId in invited:
        invited.remove(userId)
        group['invited'] = invited
            
    else:
        requiredEmail = group['required_email'].get()
        userEmail = user['email'].get()

        if requiredEmail is not None and userEmail.endswith(requiredEmail):
            pass
        else:
            return {
                'result' : 'failure',
                'error' : 'User does not have permission to join group'
            }
    
    if groupId in user['invited'].get():
        user['invited'].remove(groupId)
    
    members = group['members'].get()
    if userId in members:
        return {
            'result' : 'failure',
            'error' : 'User already a member of group'
        }
    
    group['members'].append(userId)
    users.getUserDocument(userId)['groups'].append(groupId)

    return {'result' : 'success'}
def acceptJoinParty(leaderId, userId, partyId, decline=False):
    user = users.getUserDocument(userId)
    if not user.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    leader = users.getUserDocument(leaderId)
    if not leader.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    party = partiesCollection.where('party_id', partyId)
    if not party.exists():
        return {'result': 'failure', 'error': 'Party doesn\'t exist'}

    if party['leader'].get() != leaderId:
        return {'result': 'failure', 'error': 'Not party leader'}

    if userId not in party['requests'].get():
        return {
            'result': 'failure',
            'error': 'User has not requested to join party'
        }

    party['requests'].remove(userId)
    user['party_requests'].remove(partyId)
    if decline:
        if _messages:
            from user import messaging

            messaging.sendMessage('party_decline', {'party_id': partyId},
                                  [userId])

        return {'result': 'success'}

    party['members'].append(userId)
    user['parties'].append(partyId)

    if _messages:
        from user import messaging

        messaging.sendMessage('party_accept', {'party_id': partyId}, [userId])

    notifyUserJoined(userId, partyId)
    return {'result': 'success'}
Example #9
0
def createGroup(name, manager, requiredEmail = None, publicVisible = False, publicJoinable = False):

    if not users.userExists(manager):
        return {'result' : 'failure'}

    groupId = secrets.token_hex(16)

    groupsCollection.new({
        "group_id" : groupId,
        "name" : name,
        "members" : [manager],
        "managers" : [manager],
        "invited" : [],
        "required_email" : requiredEmail,
        "public_joinable" : publicJoinable,
        "public_visible" : publicVisible
    })

    users.getUserDocument(manager)['groups'].append(groupId)

    return {'result' : 'success', 'group_id' : groupId}
Example #10
0
def getUserGroups(userId):
    user = users.getUserDocument(userId)

    userGroups = []
    groupIds = user['groups'].get()
    for groupId in groupIds:
        group = getGroupDocument(groupId)
        userGroups.append(
            {
                'group_id' : group['group_id'].get(),
                'name' : group['name'].get(),
                'n_members' : len(group['members'].get())
            }
        )
    
    return userGroups
Example #11
0
def partyChat(userId, partyId, message):

    party = partiesCollection.where('party_id', partyId)
    if not party.exists():
        return {'result': 'failure', 'error': 'Party doesn\'t exist'}

    members = party['members'].get()
    if userId not in members:
        return {'result': 'failure', 'error': 'User not in group'}

    if _messages:
        from user import messaging

        messaging.sendMessage(
            'party_chat', {
                'user_id': userId,
                'name': users.getUserDocument(userId)['name'].get(),
                'message': message
            }, members)
Example #12
0
def getJoinableParties(userId, location=None):
    projection = {
        '_id': 0,
        'name': 1,
        'party_id': 1,
        'groups': 1,
        'location': 1,
        'time': 1,
        'public': 1,
        'auto_join': 1,
        'members': 1
    }

    user = users.getUserDocument(userId)
    groups = set(user['groups'].get())
    userParties = user['parties'].get()
    parties = partiesCollection.raw.find({}, projection=projection)

    newParties = []
    for party in parties:
        party['n_members'] = len(party['members'])
        del party['members']

        joinable = False
        if party['public']:
            joinable = True
        if len(set(party['groups']) & groups) != 0:
            joinable = True
        elif party['party_id'] in userParties:
            joinable = True

        if party['party_id'] in userParties:
            party['joined'] = True
        else:
            party['joined'] = False

        del party['groups']

        if joinable:
            newParties.append(party)

    return newParties
Example #13
0
def deleteParty(leaderId, partyId):

    leader = users.getUserDocument(leaderId)
    if not leader.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    party = partiesCollection.where('party_id', partyId)
    if not party.exists():
        return {'result': 'failure', 'error': 'Party doesn\'t exist'}

    if party['leader'].get() != leaderId:
        return {'result': 'failure', 'error': 'Not party leader'}

    members = party['members'].get()
    requests = party['requests'].get()
    invited = party['invited'].get()

    for userId in members:
        users.getUserDocument(userId)['parties'].remove(partyId)

    for userId in requests:
        users.getUserDocument(userId)['party_requests'].remove(partyId)

    for userId in invited:
        users.getUserDocument(userId)['party_invites'].remove(partyId)

    if _messages:
        from user import messaging

        if party['public'].get():
            usersToNotify = None
        else:
            usersToNotify = set(members) | set(requests) | set(invited)
            for groupId in party['groups'].get():
                group = groups.getGroupDocument(groupId)
                if group.exists():
                    usersToNotify |= set(group['members'].get())

        messaging.sendMessage('party_leave', {'party_id': partyId},
                              usersToNotify)

    party.drop()

    return {'result': 'success'}
Example #14
0
def declineInvite(userId, groupId):
    # check manager exists
    user = users.getUserDocument(userId)
    if not user.exists():
        return {'result' : 'failure', 'error' : 'User doesn\'t exist'}

    # validate group
    group = groupsCollection.where('group_id', groupId)
    if not group.exists():
        return {'result' : 'failure', 'error' : 'Group doesn\'t exist'}

    # validate that the user can join the group
    invited = group['invited'].get()
    if userId in invited:
        invited.remove(userId)
        group['invited'] = invited
        user['invited'].remove(groupId)
        return {'result' : 'success'}

    else:
        return {'result' : 'failure', 'error' : 'User not invited to group'}
Example #15
0
def removeUserFromParties(userId):

    user = users.getUserDocument(userId)
    if not user.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    for partyId in user['parties'].get():
        party = partiesCollection.where('party_id', partyId)
        if userId == party['leader'].get():
            deleteParty(userId, partyId)
        else:
            party['members'].remove(userId)
            notifyUserJoined(userId, partyId)

            if _messages:
                from user import messaging
                messaging.sendMessage('party_leave', {'party_id': partyId},
                                      [userId])

    for partyId in user['party_invites'].get():
        partiesCollection.where('party_id', partyId)['invited'].remove(userId)

        if _messages:
            from user import messaging
            messaging.sendMessage('party_leave', {'party_id': partyId},
                                  [userId])

    for partyId in user['party_requests'].get():
        partiesCollection.where('party_id', partyId)['requests'].remove(userId)

        if _messages:
            from user import messaging
            messaging.sendMessage('leave_group', {'party_id': partyId},
                                  [userId])

    user['parties'] = []
    user['party_invites'] = []
    user['party_requests'] = []

    return {'result': 'success'}
Example #16
0
def joinPartyRequest(userId, partyId):

    user = users.getUserDocument(userId)
    if not user.exists():
        return {'result': 'failure', 'error': 'User doesn\'t exist'}

    party = partiesCollection.where('party_id', partyId)
    if not party.exists():
        return {'result': 'failure', 'error': 'Party doesn\'t exist'}

    if userId in party['members'].get():
        return {'result': 'failure', 'error': 'User has already joined party'}

    # validate that user can request to join party
    invited = False
    if userId in party['invited'].get():
        party['invited'].remove(userId)
        user['party_invites'].remove(partyId)
        invited = True
    elif party['public'].get():
        pass
    else:
        userGroups = set(user['groups'].get())
        partyGroups = set(party['groups'].get())

        if len(userGroups & partyGroups) == 0:
            return {
                'result': 'failure',
                'error': 'User cannot request to join party'
            }

    if invited or party['auto_join'].get():
        party['members'].append(userId)
        user['parties'].append(partyId)

        if _messages:
            from user import messaging

            messaging.sendMessage('party_accept', {'party_id': partyId},
                                  [userId])

        notifyUserJoined(userId, partyId)

        return {'result': 'success', 'message': 'User joined party'}

    else:
        if userId in party['requests'].get():
            return {
                'result': 'failure',
                'error': 'User has already requested to join party'
            }

        party['requests'].append(userId)
        user['party_requests'].append(partyId)

        if _messages:
            from user import messaging

            messaging.sendMessage('party_request', {
                'user_id': userId,
                'name': user['name'].get()
            }, [party['leader'].get()])

        return {
            'result': 'success',
            'message': 'Join request made. Waiting on party leader'
        }