From 32c3491b54955f3a00c64c833ee96d097ee8a895 Mon Sep 17 00:00:00 2001 From: Jeremy Zhang Date: Tue, 27 Feb 2018 07:22:58 +0000 Subject: [PATCH] Add voting page and track referrals --- ..._added_discordbotsorgtransactions_table.py | 35 +++++++++++++++++++ webapp/titanembeds/app.py | 4 +++ webapp/titanembeds/blueprints/api/api.py | 16 +++++++-- webapp/titanembeds/database/__init__.py | 1 + .../database/discordbotsorg_transactions.py | 18 ++++++++++ webapp/titanembeds/static/js/embed.js | 2 +- .../templates/discordbotsorg_vote.html.j2 | 34 ++++++++++++++++++ .../titanembeds/templates/site_layout.html.j2 | 4 +-- 8 files changed, 109 insertions(+), 5 deletions(-) create mode 100644 webapp/alembic/versions/b18fcc759865_added_discordbotsorgtransactions_table.py create mode 100644 webapp/titanembeds/database/discordbotsorg_transactions.py create mode 100644 webapp/titanembeds/templates/discordbotsorg_vote.html.j2 diff --git a/webapp/alembic/versions/b18fcc759865_added_discordbotsorgtransactions_table.py b/webapp/alembic/versions/b18fcc759865_added_discordbotsorgtransactions_table.py new file mode 100644 index 0000000..8a336a8 --- /dev/null +++ b/webapp/alembic/versions/b18fcc759865_added_discordbotsorgtransactions_table.py @@ -0,0 +1,35 @@ +"""Added DiscordBotsOrgTransactions table + +Revision ID: b18fcc759865 +Revises: dffebf852b41 +Create Date: 2018-02-27 06:14:12.133576 + +""" + +# revision identifiers, used by Alembic. +revision = 'b18fcc759865' +down_revision = 'dffebf852b41' +branch_labels = None +depends_on = None + +from alembic import op +import sqlalchemy as sa + + +def upgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.create_table('discordbotsorg_transactions', + sa.Column('id', sa.Integer(), nullable=False), + sa.Column('user_id', sa.BigInteger(), nullable=False), + sa.Column('timestamp', sa.TIMESTAMP(), nullable=False), + sa.Column('action', sa.String(length=255), nullable=False), + sa.Column('referrer', sa.BigInteger(), nullable=True), + sa.PrimaryKeyConstraint('id') + ) + # ### end Alembic commands ### + + +def downgrade(): + # ### commands auto generated by Alembic - please adjust! ### + op.drop_table('discordbotsorg_transactions') + # ### end Alembic commands ### diff --git a/webapp/titanembeds/app.py b/webapp/titanembeds/app.py index 3bb5cff..3065c73 100644 --- a/webapp/titanembeds/app.py +++ b/webapp/titanembeds/app.py @@ -71,6 +71,10 @@ def terms(): def privacy(): return render_template("privacy_policy.html.j2") +@app.route("/vote") +def vote(): + return render_template("discordbotsorg_vote.html.j2") + @app.before_first_request def before_first_request(): discord_api.init_discordrest() diff --git a/webapp/titanembeds/blueprints/api/api.py b/webapp/titanembeds/blueprints/api/api.py index 538c346..aeea948 100644 --- a/webapp/titanembeds/blueprints/api/api.py +++ b/webapp/titanembeds/blueprints/api/api.py @@ -1,4 +1,4 @@ -from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, GuildMembers, Messages, get_channel_messages, list_all_guild_members, get_guild_member, get_administrators_list, get_badges +from titanembeds.database import db, Guilds, UnauthenticatedUsers, UnauthenticatedBans, AuthenticatedUsers, GuildMembers, Messages, get_channel_messages, list_all_guild_members, get_guild_member, get_administrators_list, get_badges, DiscordBotsOrgTransactions from titanembeds.decorators import valid_session_required, discord_users_only, abort_if_guild_disabled from titanembeds.utils import check_guild_existance, guild_accepts_visitors, guild_query_unauth_users_bool, get_client_ipaddr, discord_api, rate_limiter, channel_ratelimit_key, guild_ratelimit_key, user_unauthenticated, checkUserRevoke, checkUserBanned, update_user_status, check_user_in_guild, get_guild_channels, guild_webhooks_enabled, guild_unauthcaptcha_enabled, get_member_roles, get_online_embed_user_keys, redis_store from titanembeds.oauth import user_has_permission, generate_avatar_url, check_user_can_administrate_guild @@ -6,6 +6,7 @@ from flask import Blueprint, abort, jsonify, session, request, url_for from flask import current_app as app from flask_socketio import emit from sqlalchemy import and_ +from urllib.parse import urlparse, parse_qsl, urlsplit import random import json import datetime @@ -466,16 +467,27 @@ def user_info(guild_id, user_id): usr["badges"].append("discordbotsorgvoted") return jsonify(usr) -@api.route("/webhook/discordbotsorg/vote/{}".format(config.get("discordbotsorg-webhook-secret", "")), methods=["POST"]) +@api.route("/webhook/discordbotsorg/vote", methods=["POST"]) def webhook_discordbotsorg_vote(): incoming = request.get_json() client_id = incoming.get('bot') if config["client-id"] != client_id: abort(401) + if request.headers.get("Authorization", "") != config.get("discordbotsorg-webhook-secret", ""): + abort(403) user_id = incoming.get("user") vote_type = incoming.get("type") + params = dict(parse_qsl(urlsplit(url).query)) if vote_type == "upvote": redis_store.set("DiscordBotsOrgVoted/" + user_id, "voted", 86400) else: redis_store.delete("DiscordBotsOrgVoted/" + user_id) + referrer = None + if "referrer" in params: + try: + referrer = int(float(params["referrer"])) + except: + pass + DBLTrans = DiscordBotsOrgTransactions(int(float(user_id)), vote_type, referrer) + db.session.add(DBLTrans) return ('', 204) \ No newline at end of file diff --git a/webapp/titanembeds/database/__init__.py b/webapp/titanembeds/database/__init__.py index 6c5c6df..79671b8 100644 --- a/webapp/titanembeds/database/__init__.py +++ b/webapp/titanembeds/database/__init__.py @@ -15,6 +15,7 @@ from .titan_tokens import TitanTokens, get_titan_token from .token_transactions import TokenTransactions from .patreon import Patreon from .disabled_guilds import DisabledGuilds, list_disabled_guilds +from .discordbotsorg_transactions import DiscordBotsOrgTransactions def set_titan_token(user_id, amt_change, action): token_count = get_titan_token(user_id) diff --git a/webapp/titanembeds/database/discordbotsorg_transactions.py b/webapp/titanembeds/database/discordbotsorg_transactions.py new file mode 100644 index 0000000..d658f9e --- /dev/null +++ b/webapp/titanembeds/database/discordbotsorg_transactions.py @@ -0,0 +1,18 @@ +from titanembeds.database import db +import datetime +import time + +class DiscordBotsOrgTransactions(db.Model): + __tablename__ = "discordbotsorg_transactions" + id = db.Column(db.Integer, primary_key=True) # Auto increment id + user_id = db.Column(db.BigInteger, nullable=False) # Discord user id of user + timestamp = db.Column(db.TIMESTAMP, nullable=False) # The timestamp of when the action took place + action = db.Column(db.String(255), nullable=False) # Very short description of the action + referrer = db.Column(db.BigInteger, nullable=True) # Discord user id of the referrer + + def __init__(self, user_id, action, referrer=None): + self.user_id = user_id + self.action = action + if referrer: + self.referrer = referrer + self.timestamp = datetime.datetime.fromtimestamp(time.time()).strftime('%Y-%m-%d %H:%M:%S') \ No newline at end of file diff --git a/webapp/titanembeds/static/js/embed.js b/webapp/titanembeds/static/js/embed.js index 78c567e..2c6b92d 100644 --- a/webapp/titanembeds/static/js/embed.js +++ b/webapp/titanembeds/static/js/embed.js @@ -483,7 +483,7 @@ return; } } - var dblAdContents = "close
Loving the Titan, the Discord server widget?

Show your appreciation by voting for Titan daily on Discord Bot List and get a golden name!"; + var dblAdContents = "close
Loving the Titan, the Discord server widget?

Show your appreciation by voting for Titan daily on Discord Bot List and get a golden name and other rewards!"; $(".brand-logo").showBalloon({ html: true, position: "bottom", diff --git a/webapp/titanembeds/templates/discordbotsorg_vote.html.j2 b/webapp/titanembeds/templates/discordbotsorg_vote.html.j2 new file mode 100644 index 0000000..084cec9 --- /dev/null +++ b/webapp/titanembeds/templates/discordbotsorg_vote.html.j2 @@ -0,0 +1,34 @@ +{% extends 'site_layout.html.j2' %} +{% set title="Vote for Titan on Discord Bots List!" %} + +{% block content %} +

Vote for Titan on Discord Bots List!

+
+
+
+

By voting for Titan, we'll be better known on DBL. Let's spread the word about the project!

+

You'll receive a GOLDEN NAME in chat and have a chance to win one of the heroic prizes!

+

Prizes include (but not limited to) Titan Tokens and $4.99 Discord Nitro (Paypal)! Vote often, and daily, to increase your chances of winning!

+
+ Visit Discord Bots! +
+
+
+ +
+ +
+
+
+

It just doesn't end here! Invite your buddies to vote for Titan too!

+

Give your pals a personalized referral link and get rewarded for being a great referrer!

+
+ {% if "user_id" in session and not session["unauthenticated"] %} + + {% else %} +

Please Login to view your personalized referral link.

+ {% endif %} +
+
+
+{% endblock %} \ No newline at end of file diff --git a/webapp/titanembeds/templates/site_layout.html.j2 b/webapp/titanembeds/templates/site_layout.html.j2 index f25c2d3..76c89eb 100644 --- a/webapp/titanembeds/templates/site_layout.html.j2 +++ b/webapp/titanembeds/templates/site_layout.html.j2 @@ -29,9 +29,9 @@
- Hey! Upvote us on Discord Bots List to show your + Hey! Upvote us on Discord Bots List to show your appreciation of the Titan Embeds project! (We'll provide you with a royal - golden name in return for your vote) + golden name and rewards in return for your vote)