Example #1
0
def subscribe(update: Update, context: CallbackContext):
    user = update.effective_user
    chat = update.effective_chat
    message = update.effective_message

    if not BotTelegramCore.instance().is_admin(user.id, chat.id):
        message.reply_text(ADMIN_RESTRICTED)
        return

    try:
        observer = UserObserver.objects.get(chat_id=f"{chat.id}")
    except DoesNotExist:
        observer = UserObserver(f"{chat.username}", f"{chat.id}").save()

    subscribed_subjects = Subject.objects(observers=observer)
    avaliable_subjects = [
        subject for subject in Subject.objects()
        if subject not in subscribed_subjects
    ]

    buttons_list = [
        InlineKeyboardButton(subject.header,
                             callback_data=json.dumps({
                                 "type": "subscribe",
                                 "id": f"{subject.id}"
                             })) for subject in avaliable_subjects
    ]
    menu = InlineKeyboardMarkup(build_menu(buttons_list, n_cols=1))

    message.reply_text("<b>Avaliable subjects</b>",
                       parse_mode='HTML',
                       reply_markup=menu)
Example #2
0
 def setUp(self) -> None:
     self.brasil = Subject(
         "🇧🇷", "Decred Brasil",
         "wss://split-ticket-svc.stake.decredbrasil"
         ".com:8477/watchWaitingList", "dcrbr1").save()
     self.voting = Subject(
         "🇺🇸", "Decred Voting", "wss://matcher.decredvoting.com:8477/"
         "watchWaitingList", "decredvoting1").save()
Example #3
0
    def test_unique_header(self):
        Subject(
            "🇧🇷", "Decred Brasil",
            "wss://split-ticket-svc.stake.decredbrasil"
            ".com:8477/watchWaitingList", "dcrbr1").save()
        self.assertEqual(Subject.objects.count(), 1)

        sub_2 = Subject("🇧🇷", "Decred Brasil",
                        "wss://asdasdtbrasil.com:8477/watchWaitingList",
                        "asdasd")
        self.assertRaises(NotUniqueError, sub_2.save)
        self.assertEqual(Subject.objects.count(), 1)
Example #4
0
def subscriptions(update: Update, context: CallbackContext):
    user = update.effective_user
    chat = update.effective_chat
    message = update.effective_message

    if not BotTelegramCore.instance().is_admin(user.id, chat.id):
        message.reply_text(ADMIN_RESTRICTED)
        return

    try:
        observer = UserObserver.objects.get(chat_id=f"{chat.id}")
    except DoesNotExist:
        message.reply_text(f"Observer with chat_id {chat.id} doesn't exist!\n"
                           f"Call /subscribe before")
        return

    subscribed_subjects = Subject.objects(observers=observer)

    text = "<b>Subscribed subjects</b>\n\n"
    for index, subject in enumerate(subscribed_subjects):
        text += '-'
        text += subject.__str__()
        text += '\n' if index != len(subscribed_subjects) else ''

    message.reply_text(text, parse_mode='HTML')
Example #5
0
def unsubscribe(update: Update, context: CallbackContext):
    user = update.effective_user
    chat = update.effective_chat
    message = update.effective_message

    if not BotTelegramCore.instance().is_admin(user.id, chat.id):
        message.reply_text(ADMIN_RESTRICTED)
        return

    try:
        observer = UserObserver.objects.get(chat_id=f"{chat.id}")
    except DoesNotExist:
        message.reply_text(f"Observer with chat_id {chat.id} doesn't exist!\n"
                           f"Call /subscribe before")
        return

    subscribed_subjects = Subject.objects(observers=observer)

    buttons_list = [
        InlineKeyboardButton(subject.header,
                             callback_data=json.dumps({
                                 "type": "unsubscribe",
                                 "id": f"{subject.id}"
                             })) for subject in subscribed_subjects
    ]

    menu = InlineKeyboardMarkup(build_menu(buttons_list, n_cols=1))

    message.reply_text("<b>Subscribed subjects</b>",
                       parse_mode='HTML',
                       reply_markup=menu)
Example #6
0
 def test_header(self):
     Subject(
         "🇧🇷", "Decred Brasil",
         "wss://split-ticket-svc.stake.decredbrasil"
         ".com:8477/watchWaitingList", "dcrbr1").save()
     sub: Subject = Subject.objects.first()
     self.assertEqual(sub.header, "🇧🇷 Decred Brasil")
Example #7
0
 def test_create(self):
     Subject(
         "🇧🇷", "Decred Brasil",
         "wss://split-ticket-svc.stake.decredbrasil"
         ".com:8477/watchWaitingList", "dcrbr1").save()
     self.assertEqual(Subject.objects.count(), 1)
     sub: Subject = Subject.objects.first()
     self.assertEqual(sub.emoji, "🇧🇷")
     self.assertEqual(sub.name, "Decred Brasil")
     self.assertEqual(
         sub.uri, "wss://split-ticket-svc.stake."
         "decredbrasil.com:8477/watchWaitingList")
     self.assertEqual(sub.default_session, "dcrbr1")
Example #8
0
 def setUp(self) -> None:
     self.subject = Subject(
         "🇧🇷", "Decred Brasil",
         "wss://split-ticket-svc.stake.decredbrasil"
         ".com:8477/watchWaitingList", "dcrbr1").save()
Example #9
0
 def create_all(cls):
     subjects = Subject.objects()
     for subject in subjects:
         cls(subject)
Example #10
0
from mongoengine.errors import NotUniqueError

from db.subject import Subject
from db.observer import Observer
from db.ticket import Status

try:
    brasil = Subject(
        "🇧🇷", "Decred Brasil", "wss://split-ticket-svc.stake.decredbrasil"
        ".com:8477/watchWaitingList", "dcrbr1").save()
except NotUniqueError:
    brasil = Subject.objects.get(name="Decred Brasil")

try:
    voting = Subject("🇺🇸", "Decred Voting",
                     "wss://matcher.decredvoting.com:8477/"
                     "watchWaitingList", "decredvoting1").save()
except NotUniqueError:
    voting = Subject.objects.get(name="Decred Voting")

try:
    split = Subject("🏴‍☠️", "99split", "wss://split.99split.com:8477/"
                    "watchWaitingList", "99split").save()
except NotUniqueError:
    split = Subject.objects.get(name="99split")

try:
    channel = Observer("@ticketsplitting", "@ticketsplitting").save()
except NotUniqueError:
    channel = Observer.objects.get(chat_id="@ticketsplitting")
Example #11
0
class SessionWebSocketTestCase(TestCase):
    def setUp(self) -> None:
        self.brasil = Subject(
            "🇧🇷", "Decred Brasil",
            "wss://split-ticket-svc.stake.decredbrasil"
            ".com:8477/watchWaitingList", "dcrbr1").save()
        self.voting = Subject(
            "🇺🇸", "Decred Voting", "wss://matcher.decredvoting.com:8477/"
            "watchWaitingList", "decredvoting1").save()

    def tearDown(self):
        SessionWebSocket.sessions = {}

    def test_create(self):
        sws = SessionWebSocket(self.brasil)

        self.assertEqual(len(SessionWebSocket.sessions), 1)

        self.assertEqual(sws.subject, self.brasil)
        self.assertIsNone(sws.ws)

        self.assertEqual(sws.uri, self.brasil.uri)
        self.assertEqual(sws.name, self.brasil.name)
        self.assertEqual(sws.header, self.brasil.header)
        self.assertEqual(sws.__str__(), self.brasil.__str__())

    def test_set_ws(self):
        sws = SessionWebSocket(self.brasil)

        self.assertIsNone(sws.ws)
        sws.set_ws()

        self.assertIsInstance(sws.ws, WebSocketApp)
        self.assertEqual(sws.ws.url, self.brasil.uri)

    def test_get_sws(self):
        SessionWebSocket(self.brasil)

        self.assertEqual(len(SessionWebSocket.sessions), 1)

        sws = SessionWebSocket.get_sws(
            "wss://split-ticket-svc.stake."
            "decredbrasil.com:8477/watchWaitingList")
        self.assertIsInstance(sws, SessionWebSocket)

        self.assertRaises(SessionWebSocketNotFoundError,
                          SessionWebSocket.get_sws, "url_teste")

    def test_create_all(self):
        self.assertEqual(len(SessionWebSocket.sessions), 0)

        SessionWebSocket.create_all()

        self.assertEqual(len(SessionWebSocket.sessions), 2)

        sws = SessionWebSocket.get_sws(
            "wss://split-ticket-svc.stake."
            "decredbrasil.com:8477/watchWaitingList")
        self.assertIsNone(sws.ws)

        self.assertEqual(
            sws.uri, "wss://split-ticket-svc.stake.decredbrasil"
            ".com:8477/watchWaitingList")
        self.assertEqual(sws.name, "Decred Brasil")
        self.assertEqual(sws.header, "🇧🇷 Decred Brasil")

        sws = SessionWebSocket.get_sws("wss://matcher.decredvoting"
                                       ".com:8477/watchWaitingList")
        self.assertIsNone(sws.ws)

        self.assertEqual(
            sws.uri, "wss://matcher.decredvoting.com:"
            "8477/watchWaitingList")
        self.assertEqual(sws.name, "Decred Voting")
        self.assertEqual(sws.header, "🇺🇸 Decred Voting")