def subscribe(id, userId): """ Subscribe a user to a stream. Parameters: id - a stream id. userId - a user id. """ db = core.connect() theUser = db[userId] theStream = db[id] allowed = not theStream["private"] if not allowed: perms = permission.joinableStreams(userId) allowed = id in perms if allowed and (not id in theUser["streams"]): theUser["streams"].append(id) db[userId] = theUser if theStream["private"]: perm = permission.permissionForUser(userId, id) permission.update(perm["_id"], 1)
def canSubscribe(id, userId): """ Return true if: 1. User is admin. 2. User created the stream. 3. The stream is public. 4. User has join permissions. Parameters: id - a stream id. userId - a user id. Returns: bool. """ if user.isAdmin(userId): return True theStream = read(id) if theStream["createdBy"] == userId: return True if not theStream["private"]: return True joinable = permission.joinableStreams(userId) return id in joinable