redo default home screen, show recently tipped memes

master
lza_menace 3 years ago
parent 9ec4d37448
commit be5bf5af76

@ -12,7 +12,7 @@ from suchwow import config
from suchwow.models import Post, Profile, Comment, Notification, db, Moderator
from suchwow.routes import auth, comment, post, profile, leaderboard, api
from suchwow.utils.decorators import login_required, moderator_required
from suchwow.utils.helpers import post_webhook, get_activity
from suchwow.utils.helpers import post_webhook, get_latest_tipped_posts
from suchwow.reddit import make_post
from suchwow.discord import post_discord_webhook
from suchwow import wownero, filters
@ -38,6 +38,14 @@ def index():
submitter = request.args.get("submitter", None)
content = request.args.get("content", None)
if content != 'feed':
posts = get_latest_tipped_posts()
return render_template(
"index.html",
posts=posts[0:30],
title="Latest Tipped Memes"
)
try:
page = int(page)
except:
@ -54,9 +62,11 @@ def index():
"index.html",
posts=paginated_posts,
page=page,
total_pages=total_pages
total_pages=total_pages,
title="Latest Memes"
)
@app.route("/mod")
@moderator_required
def mod_queue():

@ -4,7 +4,13 @@
<div class="container" style="text-align:center;">
<h1 class="title">Latest Memes</h1>
<h1 class="title">{% if title %}{{ title }}{% else %}Latest Memes{% endif %}</h1>
{% if request.args.content != 'feed' %}
<a href="/?content=feed">View Latest Memes</a>
{% else %}
<a href="/">View Latest Tipped Posts</a>
{% endif %}
<section class="section">
{% if posts %}
@ -25,7 +31,7 @@
<p class="title is-4">
<a href="{{ url_for('post.read', id=post.id) }}">{{ post.title }}</a>
</p>
<p class="subtitle is-6"><a href="/?submitter={{ post.submitter }}">{{ post.submitter }}</a></p>
<p class="subtitle is-6"><a href="/?content=feed&submitter={{ post.submitter }}">{{ post.submitter }}</a></p>
</div>
</div>
@ -50,7 +56,7 @@
<nav class="pagination is-centered pb-4" role="navigation" aria-label="pagination">
<ul class="pagination-list">
{% for p in range(1, total_pages + 1) %}
<a href="{% if request.args.submitter %}/?submitter={{ request.args.submitter }}&{% else %}/?{% endif %}page={{ p }}" class="pagination-link {% if p == page %}current-page-btn{% endif %}">{{ p }}</a>
<a href="/?content=feed&{% if request.args.submitter %}submitter={{ request.args.submitter }}&{% endif %}page={{ p }}" class="pagination-link {% if p == page %}current-page-btn{% endif %}">{{ p }}</a>
{% endfor %}
</ul>
</nav>

@ -24,7 +24,7 @@
<a href="{{ url_for('post.approve', id=post.id) }}"><button type="button" name="button">Approve</button></a>
<a href="{{ url_for('post.delete', id=post.id) }}"><button type="button" name="button">Reject</button></a>
{% endif %}
<p>Submitted by <i><u><a href="/?submitter={{ post.submitter }}">{{ post.submitter }}</a></u></i> at <i>{{ post.timestamp }}</i></p>
<p>Submitted by <i><u><a href="/?content=feed&submitter={{ post.submitter }}">{{ post.submitter }}</a></u></i> at <i>{{ post.timestamp }}</i></p>
<img src="{{ url_for('post.uploaded_file', filename=post.image_name) }}" width=600/ style="border-radius:4px;">
</div>
</section>

@ -41,26 +41,36 @@ def post_webhook(msg):
except:
return False
def get_activity():
posts = Post.select()
w = Wallet()
data = {}
for p in posts:
data[p.timestamp] = {'type': 'post', 'post': p}
for tx in w.incoming_transfers(p.account_index):
if 'timestamp' in tx:
data[tx['timestamp']] = {'type': 'tx', 'post': p}
def get_latest_tipped_posts():
key_name = 'latest_tips'
tipped_posts = rw_cache(key_name, None, 1200)
dates = sorted(data, reverse=True)
new_data = []
for d in dates:
new_data.append(data[d]['post'])
if not tipped_posts:
new_data = []
w = Wallet()
data = {}
for acc in w.accounts():
txes = w.transfers(acc)
if 'in' in txes:
for tx in txes['in']:
p = Post.select().where(
Post.account_index==acc
).first()
if p:
data[tx['timestamp']] = p
return new_data[0:20]
dates = sorted(data, reverse=True)
for d in dates:
if not data[d] in new_data:
new_data.append(data[d])
tipped_posts = rw_cache(key_name, new_data, 1200)
return tipped_posts
# Use hacky filesystem cache since i dont feel like shipping redis
def rw_cache(key_name, data=None):
def rw_cache(key_name, data=None, diff_seconds=3600):
pickle_file = path.join(config.DATA_FOLDER, f'{key_name}.pkl')
try:
if path.isfile(pickle_file):
@ -70,7 +80,7 @@ def rw_cache(key_name, data=None):
diff = now - mtime
# If pickled data file is less than an hour old, load it and render page
# Otherwise, determine balances, build json, store pickled data, and render page
if diff.seconds < 3600:
if diff.seconds < diff_seconds:
print(f'unpickling {key_name}')
with open(pickle_file, 'rb') as f:
pickled_data = pickle.load(f)

@ -53,15 +53,8 @@ class Wallet(object):
return self.make_wallet_rpc('query_key', {'key_type': 'mnemonic'})['key']
def accounts(self):
accounts = []
_accounts = self.make_wallet_rpc('get_accounts')
idx = 0
self.master_address = _accounts['subaddress_accounts'][0]['base_address']
for _acc in _accounts['subaddress_accounts']:
assert idx == _acc['account_index']
accounts.append(_acc['account_index'])
idx += 1
return accounts
return [i['account_index'] for i in _accounts['subaddress_accounts']]
def new_account(self, label=None):
_account = self.make_wallet_rpc('create_account', {'label': label})

Loading…
Cancel
Save