Example #1
0
 def process_op(self, update, command, params):
     chat_id = update.message.chat_id
     if params:
         params = params.replace(" ", "").lower()
         moderator = Moderator.get_or_create(username=params)
         self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
         self.bot.sendMessage(chat_id=chat_id, text="Added permissions for %s" % params)
     else:
         moderators = Moderator.select()
         moderators = map(lambda x: x.username, moderators)
         moderators = "\r\n".join(moderators)
         self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
         self.bot.sendMessage(chat_id=chat_id, text="Ops:\r\n%s" % (moderators or "-----"))
Example #2
0
 def _wrapper(self, update, command, params):
     chat_id = update.message.chat_id
     request_username = update.message.from_user.username
     if request_username == MAIN_ADMIN or Moderator.select().where(Moderator.username==request_username).count():
         func(self, update, command, params)
     else:
         self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
         self.bot.sendMessage(chat_id=chat_id, text="You don't have permissions for this command")
Example #3
0
 def process_deop(self, update, command, params):
     chat_id = update.message.chat_id
     params = params.replace(" ", "").lower()
     moderator = Moderator.select().where(Moderator.username == params)
     if moderator.count():
         moderator[0].delete_instance()
     self.bot.sendChatAction(chat_id=chat_id, action=telegram.ChatAction.TYPING)
     self.bot.sendMessage(chat_id=chat_id, text="Removed permissions for %s" % params)
Example #4
0
def new_topic(message):
    print("Added")
    print(message['data']['user'])
    user = User.query.filter_by(email=message['data']['user']).first()
    room = Topic.query.filter_by(topicname=session.get('room')).first()
    mod = Moderator(user.uid, room.uid)
    db.session.add(mod)
    db.session.commit()
Example #5
0
def new_interest_group(message):
    print("message = ", message)
    print(message['data']['message'])
    myEmail = session.get('email')
    user = User.query.filter_by(email=myEmail).first()
    others = User.query.filter(User.email != myEmail).all()
    interests = user.interests
    interests_set = set()
    for i in interests:
        print(i.interest_name)
        interests_set.add(i.interest_name.lower())

    same_interests = []
    interest_to_users = {}

    topic_name = ""

    for i in interests_set:
        count = 0
        friends = []
        for other in others:
            for other_i in other.interests:
                if other_i.interest_name.lower() == i:
                    count += 1
                    friends.append(other.username)
        if count > 0:
            heappush(same_interests, (-count, i))
            interest_to_users[i] = friends

    print(same_interests)

    interest = ""
    while len(same_interests) > 0:
        interest = heappop(same_interests)[1]
        print("interest" + interest)
        if Topic.query.filter_by(topicname=interest).first() != None:
            continue
        topic_name = interest
        room = Topic(topic_name, user.uid)
        db.session.add(room)
        db.session.commit()
        room = Topic.query.filter_by(topicname=topic_name).first()
        mod = Moderator(user.uid, room.uid)
        db.session.add(mod)
        db.session.commit()
        break

    if topic_name == "":
        emit('redirect', '/random_setting')
    else:
        emit('redirect', '/chat/' + room.topicname)
        emit('alert', {
            'topic': room.topicname,
            'users': json.dumps(interest_to_users[interest])
        },
             namespace='/',
             broadcast=True)
Example #6
0
def login(user_id, pin_number, user_role):
    if user_role == ROLES.get("admin"):
        administrator = Administrator.get_or_none(Administrator.user_id == user_id)
        if administrator and (administrator.pin_number == pin_number):
            session['user_id'] = administrator.id
            return True
    elif user_role == ROLES.get("moderator"):
        moderator = Moderator.get_or_none(Moderator.user_id == user_id)
        if moderator and (moderator.pin_number == pin_number):
            session['user_id'] = moderator.id
            return True
    elif user_role == ROLES.get("faculty"):
        faculty = Faculty.get_or_none(Faculty.user_id == user_id)
        if faculty and(faculty.pin_number == pin_number):
            session['user_id'] = faculty.id
            return True
    else:
        return False
Example #7
0
def show_chatroom(chatroom_title):
    form = TopicForm()
    topics = Topic.query.all()
    users = User.query.all()
    messages = Message.query.all()
    banned_from = []
    moderated_rooms = []
    banned_users = []

    topic = Topic.query.filter_by(topicname=chatroom_title).first()

    user = User.query.filter_by(email=session['email']).first()

    if BannedUser.query.filter_by(user_id=user.uid).first() is not None:
        flagged_rooms = BannedUser.query.filter_by(user_id=user.uid).all()
        for room in flagged_rooms:
            if room.times_flagged >= 5 and room.topic_id != 1:
                print("banned from:")
                print(room.topic_id)
                banned_from.append(room.topic_id)

    if BannedUser.query.filter_by(topic_id=topic.uid).first() is not None:
        users = BannedUser.query.filter_by(topic_id=topic.uid).all()
        for u in users:
            if u.times_flagged >= 5 and u.topic_id != 1:
                print(u.topic_id)
                banned_users.append(
                    User.query.filter_by(uid=u.user_id).first().username)

    if Moderator.query.filter_by(user_id=user.uid).first() is not None:
        print("mod exists")
        mod_for = Moderator.query.filter_by(user_id=user.uid).all()
        for room in mod_for:
            print("mod")
            print(room.topic_id)
            if room.topic_id != 1:
                moderated_rooms.append(
                    Topic.query.filter_by(uid=room.topic_id).first().topicname)

    if topic is None:
        return redirect(url_for('chat'))

    if topic.uid in banned_from:
        return redirect(url_for('chat'))

    session['room'] = topic.topicname

    if 'email' not in session:
        return redirect(url_for('signin'))

    user = User.query.filter_by(email=session['email']).first()

    if user is None:
        return redirect(url_for('signin'))
    else:
        if request.method == 'POST':
            if form.validate() == False:
                return render_template('chat.html',
                                       form=form,
                                       topics=topics,
                                       users=users,
                                       messages=messages,
                                       banned_from=banned_from,
                                       moderated_rooms=moderated_rooms,
                                       banned_users=banned_users)
            else:
                uid = user.uid
                newtopic = Topic(form.topicname.data, uid)
                db.session.add(newtopic)
                db.session.commit()
                session['topic'] = newtopic.topicname
                newtopic = Topic.query.filter_by(
                    topicname=form.topicname.data).first()
                mod = Moderator(user.uid, newtopic.uid)
                db.session.add(mod)
                db.session.commit()
                return redirect('/chat/' + newtopic.topicname)

        if request.method == 'GET':
            return render_template('chat.html',
                                   form=form,
                                   topics=topics,
                                   users=users,
                                   messages=messages,
                                   banned_from=banned_from,
                                   moderated_rooms=moderated_rooms,
                                   banned_users=banned_users)