2018-05-09 03:47:17 +02:00
|
|
|
from django.http import HttpResponse, Http404
|
2018-08-16 19:10:45 +02:00
|
|
|
from django.conf import settings as django_settings
|
2018-04-24 00:16:22 +02:00
|
|
|
from django.shortcuts import render, redirect
|
2018-08-10 02:58:05 +02:00
|
|
|
from django.views.decorators.cache import never_cache, cache_page
|
2018-05-14 21:35:10 +02:00
|
|
|
from django.urls import reverse
|
2018-05-18 15:22:27 +02:00
|
|
|
from django.core.files.uploadhandler import TemporaryFileUploadHandler
|
2018-05-14 21:35:10 +02:00
|
|
|
from brutaldon.forms import LoginForm, OAuthLoginForm, SettingsForm, PostForm
|
2018-04-24 00:16:22 +02:00
|
|
|
from brutaldon.models import Client, Account
|
|
|
|
from mastodon import Mastodon
|
2018-04-24 20:49:43 +02:00
|
|
|
from urllib import parse
|
2018-06-09 19:49:04 +02:00
|
|
|
from pdb import set_trace
|
2018-04-24 00:16:22 +02:00
|
|
|
|
2018-04-26 02:51:12 +02:00
|
|
|
class NotLoggedInException(Exception):
|
|
|
|
pass
|
|
|
|
|
2018-06-05 14:08:12 +02:00
|
|
|
class Singleton(type):
|
|
|
|
_instances = {}
|
|
|
|
def __call__(cls, *args, **kwargs):
|
|
|
|
if cls not in cls._instances:
|
|
|
|
cls._instances[cls] = super(Singleton, cls).__call__(*args, **kwargs)
|
|
|
|
return cls._instances[cls]
|
|
|
|
|
|
|
|
class MastodonPool(dict, metaclass=Singleton):
|
|
|
|
pass
|
|
|
|
|
2018-04-26 02:51:12 +02:00
|
|
|
def get_mastodon(request):
|
2018-04-24 20:49:43 +02:00
|
|
|
if not (request.session.has_key('instance') and
|
2018-05-14 21:35:10 +02:00
|
|
|
(request.session.has_key('username') or
|
|
|
|
request.session.has_key('access_token'))):
|
2018-04-26 02:51:12 +02:00
|
|
|
raise NotLoggedInException()
|
2018-04-24 20:49:43 +02:00
|
|
|
|
2018-06-05 14:08:12 +02:00
|
|
|
pool = MastodonPool()
|
2018-05-14 21:35:10 +02:00
|
|
|
if request.session.has_key('access_token'):
|
|
|
|
try:
|
|
|
|
client = Client.objects.get(api_base_id=request.session['instance'])
|
|
|
|
except (Client.DoesNotExist, Client.MultipleObjectsReturned):
|
|
|
|
raise NotLoggedInException()
|
2018-06-05 14:08:12 +02:00
|
|
|
if request.session['access_token'] in pool.keys():
|
|
|
|
mastodon = pool[request.session['access_token']]
|
|
|
|
else:
|
|
|
|
mastodon = Mastodon(
|
|
|
|
client_id = client.client_id,
|
|
|
|
client_secret = client.client_secret,
|
|
|
|
api_base_url = client.api_base_id,
|
|
|
|
access_token = request.session['access_token'],
|
|
|
|
ratelimit_method='throw')
|
|
|
|
pool[request.session['access_token']] = mastodon
|
2018-05-14 21:35:10 +02:00
|
|
|
else:
|
|
|
|
try:
|
|
|
|
client = Client.objects.get(api_base_id=request.session['instance'])
|
|
|
|
user = Account.objects.get(username=request.session['username'])
|
|
|
|
except (Client.DoesNotExist, Client.MultipleObjectsReturned,
|
|
|
|
Account.DoesNotExist, Account.MultipleObjectsReturned):
|
|
|
|
raise NotLoggedInException()
|
2018-06-05 14:08:12 +02:00
|
|
|
if user.access_token in pool.keys():
|
|
|
|
mastodon = pool[user.access_token]
|
|
|
|
else:
|
|
|
|
mastodon = Mastodon(
|
|
|
|
client_id = client.client_id,
|
|
|
|
client_secret = client.client_secret,
|
|
|
|
access_token = user.access_token,
|
|
|
|
api_base_url = client.api_base_id,
|
|
|
|
ratelimit_method="throw")
|
2018-04-26 02:51:12 +02:00
|
|
|
return mastodon
|
|
|
|
|
2018-04-26 05:07:58 +02:00
|
|
|
def fullbrutalism_p(request):
|
|
|
|
if request.session.has_key('fullbrutalism'):
|
|
|
|
fullbrutalism = request.session['fullbrutalism']
|
|
|
|
else:
|
|
|
|
fullbrutalism = False
|
|
|
|
return fullbrutalism
|
|
|
|
|
2018-05-18 15:22:27 +02:00
|
|
|
def timeline(request, timeline='home', timeline_name='Home', max_id=None, since_id=None):
|
2018-04-26 02:51:12 +02:00
|
|
|
try:
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
except NotLoggedInException:
|
2018-06-01 15:22:17 +02:00
|
|
|
return redirect(about)
|
2018-05-18 15:22:27 +02:00
|
|
|
data = mastodon.timeline(timeline, limit=100, max_id=max_id, since_id=since_id)
|
2018-05-22 14:57:45 +02:00
|
|
|
form = PostForm(initial={'visibility': request.session['user'].source.privacy})
|
2018-05-18 15:22:27 +02:00
|
|
|
try:
|
|
|
|
prev = data[0]._pagination_prev
|
|
|
|
if len(mastodon.timeline(since_id=prev['since_id'])) == 0:
|
|
|
|
prev = None
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-05-18 15:22:27 +02:00
|
|
|
prev = None
|
|
|
|
try:
|
|
|
|
next = data[-1]._pagination_next
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-05-18 15:22:27 +02:00
|
|
|
next = None
|
|
|
|
return render(request, 'main/%s_timeline.html' % timeline,
|
|
|
|
{'toots': data, 'form': form, 'timeline': timeline,
|
|
|
|
'timeline_name': timeline_name,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-05-18 15:22:27 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request),
|
|
|
|
'prev': prev, 'next': next})
|
2018-04-24 00:16:22 +02:00
|
|
|
|
2018-05-18 15:22:27 +02:00
|
|
|
def home(request, next=None, prev=None):
|
|
|
|
return timeline(request, 'home', 'Home', max_id=next, since_id=prev)
|
2018-04-26 02:06:10 +02:00
|
|
|
|
2018-05-18 15:22:27 +02:00
|
|
|
def local(request, next=None, prev=None):
|
|
|
|
return timeline(request, 'local', 'Local', max_id=next, since_id=prev)
|
2018-04-26 02:06:10 +02:00
|
|
|
|
2018-05-18 15:22:27 +02:00
|
|
|
def fed(request, next=None, prev=None):
|
|
|
|
return timeline(request, 'public', 'Federated', max_id=next, since_id=prev)
|
2018-04-24 00:16:22 +02:00
|
|
|
|
2018-05-07 01:46:51 +02:00
|
|
|
def tag(request, tag):
|
|
|
|
try:
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
except NotLoggedInException:
|
|
|
|
return redirect(login)
|
|
|
|
data = mastodon.timeline_hashtag(tag)
|
|
|
|
return render(request, 'main/timeline.html',
|
2018-06-09 01:02:46 +02:00
|
|
|
{'toots': data, 'timeline_name': '#'+tag,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-05-07 01:46:51 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
|
2018-05-09 03:47:17 +02:00
|
|
|
@never_cache
|
2018-04-24 00:16:22 +02:00
|
|
|
def login(request):
|
2018-05-14 21:35:10 +02:00
|
|
|
# User posts instance name in form.
|
|
|
|
# POST page redirects user to instance, where they log in.
|
|
|
|
# Instance redirects user to oauth_after_login view.
|
|
|
|
# oauth_after_login view saves credential in session, then redirects to home.
|
|
|
|
if request.method == "GET":
|
|
|
|
form = OAuthLoginForm()
|
|
|
|
return render(request, 'setup/login-oauth.html', {'form': form})
|
|
|
|
elif request.method == "POST":
|
|
|
|
form = OAuthLoginForm(request.POST)
|
|
|
|
redirect_uris = request.build_absolute_uri(reverse('oauth_callback'))
|
|
|
|
if form.is_valid():
|
|
|
|
api_base_url = form.cleaned_data['instance']
|
|
|
|
tmp_base = parse.urlparse(api_base_url.lower())
|
|
|
|
if tmp_base.netloc == '':
|
|
|
|
api_base_url = parse.urlunparse(('https', tmp_base.path,
|
|
|
|
'','','',''))
|
|
|
|
else:
|
|
|
|
api_base_url = api_base_url.lower()
|
|
|
|
|
|
|
|
request.session['instance'] = api_base_url
|
|
|
|
try:
|
|
|
|
client = Client.objects.get(api_base_id=api_base_url)
|
|
|
|
except (Client.DoesNotExist, Client.MultipleObjectsReturned):
|
|
|
|
(client_id, client_secret) = Mastodon.create_app('brutaldon',
|
|
|
|
api_base_url=api_base_url,
|
|
|
|
redirect_uris=redirect_uris)
|
|
|
|
client = Client(
|
|
|
|
api_base_id = api_base_url,
|
|
|
|
client_id=client_id,
|
|
|
|
client_secret = client_secret)
|
|
|
|
client.save()
|
|
|
|
|
|
|
|
request.session['client_id'] = client.client_id
|
|
|
|
request.session['client_secret'] = client.client_secret
|
|
|
|
|
|
|
|
mastodon = Mastodon(
|
|
|
|
client_id = client.client_id,
|
|
|
|
client_secret = client.client_secret,
|
|
|
|
api_base_url = api_base_url)
|
|
|
|
return redirect(mastodon.auth_request_url(redirect_uris=redirect_uris))
|
|
|
|
else:
|
|
|
|
return render(request, 'setup/login.html', {'form': form})
|
|
|
|
|
|
|
|
else:
|
|
|
|
return redirect(login)
|
|
|
|
|
|
|
|
@never_cache
|
|
|
|
def oauth_callback(request):
|
|
|
|
code = request.GET.get('code', '')
|
|
|
|
mastodon = Mastodon(client_id=request.session['client_id'],
|
|
|
|
client_secret=request.session['client_secret'],
|
|
|
|
api_base_url=request.session['instance'])
|
|
|
|
redirect_uri = request.build_absolute_uri(reverse('oauth_callback'))
|
|
|
|
access_token = mastodon.log_in(code=code,
|
2018-08-03 02:40:53 +02:00
|
|
|
redirect_uri=redirect_uri)
|
2018-05-14 21:35:10 +02:00
|
|
|
request.session['access_token'] = access_token
|
2018-05-21 02:28:53 +02:00
|
|
|
user = mastodon.account_verify_credentials()
|
2018-05-22 14:57:45 +02:00
|
|
|
request.session['user'] = user
|
2018-05-14 21:35:10 +02:00
|
|
|
return redirect(home)
|
|
|
|
|
|
|
|
|
|
|
|
@never_cache
|
|
|
|
def old_login(request):
|
2018-04-24 00:16:22 +02:00
|
|
|
if request.method == "GET":
|
|
|
|
form = LoginForm()
|
|
|
|
return render(request, 'setup/login.html', {'form': form})
|
|
|
|
elif request.method == "POST":
|
|
|
|
form = LoginForm(request.POST)
|
|
|
|
if form.is_valid():
|
2018-04-24 20:49:43 +02:00
|
|
|
api_base_url = form.cleaned_data['instance']
|
|
|
|
tmp_base = parse.urlparse(api_base_url.lower())
|
|
|
|
if tmp_base.netloc == '':
|
|
|
|
api_base_url = parse.urlunparse(('https', tmp_base.path,
|
|
|
|
'','','',''))
|
|
|
|
else:
|
|
|
|
api_base_url = api_base_url.lower()
|
|
|
|
|
|
|
|
request.session['instance'] = api_base_url
|
2018-04-24 00:16:22 +02:00
|
|
|
username = form.cleaned_data['username']
|
|
|
|
password = form.cleaned_data['password']
|
|
|
|
|
2018-04-24 20:49:43 +02:00
|
|
|
try:
|
|
|
|
client = Client.objects.get(api_base_id=api_base_url)
|
|
|
|
except (Client.DoesNotExist, Client.MultipleObjectsReturned):
|
|
|
|
(client_id, client_secret) = Mastodon.create_app('brutaldon',
|
2018-04-24 00:16:22 +02:00
|
|
|
api_base_url=api_base_url)
|
2018-04-24 20:49:43 +02:00
|
|
|
client = Client(
|
|
|
|
api_base_id = api_base_url,
|
|
|
|
client_id=client_id,
|
|
|
|
client_secret = client_secret)
|
|
|
|
client.save()
|
2018-04-24 00:16:22 +02:00
|
|
|
|
|
|
|
mastodon = Mastodon(
|
2018-04-24 20:49:43 +02:00
|
|
|
client_id = client.client_id,
|
|
|
|
client_secret = client.client_secret,
|
2018-04-24 00:16:22 +02:00
|
|
|
api_base_url = api_base_url)
|
2018-04-24 20:49:43 +02:00
|
|
|
|
|
|
|
try:
|
|
|
|
account = Account.objects.get(username=username, client_id=client.id)
|
|
|
|
except (Account.DoesNotExist, Account.MultipleObjectsReturned):
|
|
|
|
account = Account(
|
|
|
|
username = username,
|
2018-05-01 02:32:50 +02:00
|
|
|
access_token = "",
|
2018-04-24 20:49:43 +02:00
|
|
|
client = client)
|
2018-05-01 02:32:50 +02:00
|
|
|
try:
|
2018-04-25 23:02:59 +02:00
|
|
|
access_token = mastodon.log_in(username,
|
2018-08-03 02:40:53 +02:00
|
|
|
password)
|
2018-05-01 02:32:50 +02:00
|
|
|
account.access_token = access_token
|
2018-04-25 23:02:59 +02:00
|
|
|
account.save()
|
2018-05-01 02:32:50 +02:00
|
|
|
request.session['username'] = username
|
2018-05-21 02:28:53 +02:00
|
|
|
user = mastodon.account_verify_credentials()
|
2018-05-22 14:57:45 +02:00
|
|
|
request.session['user'] = user
|
2018-04-24 00:16:22 +02:00
|
|
|
|
2018-05-01 02:32:50 +02:00
|
|
|
return redirect(home)
|
2018-07-18 19:33:16 +02:00
|
|
|
except Exception as ex:
|
|
|
|
form.add_error('', ex)
|
|
|
|
|
2018-05-01 02:32:50 +02:00
|
|
|
return render(request, 'setup/login.html', {'form': form})
|
2018-04-24 00:16:22 +02:00
|
|
|
else:
|
2018-04-24 20:49:43 +02:00
|
|
|
return render(request, 'setup/login.html', {'form': form})
|
2018-04-24 00:16:22 +02:00
|
|
|
|
2018-05-09 03:47:17 +02:00
|
|
|
@never_cache
|
2018-04-25 01:20:22 +02:00
|
|
|
def logout(request):
|
2018-04-25 23:02:59 +02:00
|
|
|
request.session.flush()
|
2018-06-01 15:22:17 +02:00
|
|
|
return redirect(about)
|
2018-04-25 01:20:22 +02:00
|
|
|
|
2018-04-24 00:16:22 +02:00
|
|
|
def error(request):
|
2018-04-25 01:20:22 +02:00
|
|
|
return render(request, 'error.html', { 'error': "Not logged in yet."})
|
|
|
|
|
2018-06-24 20:19:35 +02:00
|
|
|
def note(request, next=None, prev=None):
|
|
|
|
try:
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
except NotLoggedInException:
|
|
|
|
return redirect(about)
|
|
|
|
notes = mastodon.notifications(limit=100, max_id=next, since_id=prev)
|
|
|
|
try:
|
|
|
|
prev = notes[0]._pagination_prev
|
2018-06-24 21:09:09 +02:00
|
|
|
if len(mastodon.notifications(since_id=prev['since_id'])) == 0:
|
2018-06-24 20:19:35 +02:00
|
|
|
prev = None
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-24 20:19:35 +02:00
|
|
|
prev = None
|
|
|
|
try:
|
|
|
|
next = notes[-1]._pagination_next
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-24 20:19:35 +02:00
|
|
|
next = None
|
2018-04-26 04:50:13 +02:00
|
|
|
return render(request, 'main/notifications.html',
|
2018-04-26 05:07:58 +02:00
|
|
|
{'notes': notes,'timeline': 'Notifications',
|
2018-06-24 20:19:35 +02:00
|
|
|
'timeline_name': 'Notifications',
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-06-24 20:33:44 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request),
|
|
|
|
'prev': prev, 'next': next})
|
2018-04-25 01:20:22 +02:00
|
|
|
|
2018-04-27 00:49:43 +02:00
|
|
|
def thread(request, id):
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
context = mastodon.status_context(id)
|
|
|
|
toot = mastodon.status(id)
|
|
|
|
return render(request, 'main/thread.html',
|
|
|
|
{'context': context, 'toot': toot,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-04-27 00:49:43 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
|
2018-06-24 23:19:39 +02:00
|
|
|
def user(request, username, prev=None, next=None):
|
2018-06-24 23:07:43 +02:00
|
|
|
try:
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
except NotLoggedInException:
|
|
|
|
return redirect(about)
|
2018-05-09 03:47:17 +02:00
|
|
|
try:
|
|
|
|
user_dict = mastodon.account_search(username)[0]
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-05-09 03:47:17 +02:00
|
|
|
raise Http404("The user %s could not be found." % username)
|
2018-07-23 19:37:24 +02:00
|
|
|
data = mastodon.account_statuses(user_dict.id, max_id=next, since_id=prev)
|
2018-06-12 00:16:32 +02:00
|
|
|
relationship = mastodon.account_relationships(user_dict.id)[0]
|
2018-06-24 23:07:43 +02:00
|
|
|
try:
|
|
|
|
prev = data[0]._pagination_prev
|
|
|
|
if len(mastodon.account_statuses(user_dict.id,
|
|
|
|
since_id=prev['since_id'])) == 0:
|
|
|
|
prev = None
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-24 23:07:43 +02:00
|
|
|
prev = None
|
|
|
|
try:
|
|
|
|
next = data[-1]._pagination_next
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-24 23:07:43 +02:00
|
|
|
next = None
|
2018-06-24 23:13:31 +02:00
|
|
|
return render(request, 'main/user.html',
|
2018-05-09 03:47:17 +02:00
|
|
|
{'toots': data, 'user': user_dict,
|
2018-06-12 00:16:32 +02:00
|
|
|
'relationship': relationship,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-06-24 23:07:43 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request),
|
|
|
|
'prev': prev, 'next': next})
|
2018-05-09 03:47:17 +02:00
|
|
|
|
|
|
|
|
2018-05-01 15:28:57 +02:00
|
|
|
@never_cache
|
2018-04-25 22:45:46 +02:00
|
|
|
def settings(request):
|
|
|
|
if request.method == 'POST':
|
|
|
|
form = SettingsForm(request.POST)
|
|
|
|
if form.is_valid():
|
|
|
|
request.session['fullbrutalism'] = form.cleaned_data['fullbrutalism']
|
|
|
|
return redirect(home)
|
|
|
|
else:
|
2018-04-26 05:07:58 +02:00
|
|
|
return render(request, 'setup/settings.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{'form' : form,
|
|
|
|
'own_acct': request.session['user'],
|
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
2018-04-25 22:45:46 +02:00
|
|
|
else:
|
|
|
|
form = SettingsForm(request.session)
|
2018-04-26 05:07:58 +02:00
|
|
|
return render(request, 'setup/settings.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{ 'form': form,
|
|
|
|
'own_acct': request.session['user'],
|
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
2018-04-26 05:07:58 +02:00
|
|
|
|
2018-05-01 15:28:57 +02:00
|
|
|
@never_cache
|
2018-06-12 01:19:22 +02:00
|
|
|
def toot(request, mention=None):
|
2018-04-27 20:12:29 +02:00
|
|
|
if request.method == 'GET':
|
2018-06-12 01:19:22 +02:00
|
|
|
if mention:
|
|
|
|
if not mention.startswith('@'):
|
|
|
|
mention = '@'+mention
|
|
|
|
form = PostForm(initial={'visibility': request.session['user'].source.privacy,
|
|
|
|
'status': mention + '\n' })
|
|
|
|
else:
|
|
|
|
form = PostForm(initial={'visibility': request.session['user'].source.privacy})
|
2018-04-30 14:58:10 +02:00
|
|
|
return render(request, 'main/post.html',
|
2018-04-27 20:12:29 +02:00
|
|
|
{'form': form,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-04-27 20:12:29 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
elif request.method == 'POST':
|
|
|
|
form = PostForm(request.POST, request.FILES)
|
|
|
|
if form.is_valid():
|
|
|
|
mastodon = get_mastodon(request)
|
2018-05-01 20:44:56 +02:00
|
|
|
|
|
|
|
# create media objects
|
|
|
|
media_objects = []
|
|
|
|
for index in range(1,5):
|
|
|
|
if 'media_file_'+str(index) in request.FILES:
|
|
|
|
media_objects.append(
|
|
|
|
mastodon.media_post(request.FILES['media_file_'+str(index)]
|
|
|
|
.temporary_file_path(),
|
|
|
|
description=request.POST.get('media_text_'
|
|
|
|
+str(index),
|
|
|
|
None)))
|
2018-05-22 14:57:45 +02:00
|
|
|
if form.cleaned_data['visibility'] == '':
|
|
|
|
form.cleaned_data['visibility'] = request.session['user'].source.privacy
|
2018-04-27 20:12:29 +02:00
|
|
|
mastodon.status_post(status=form.cleaned_data['status'],
|
|
|
|
visibility=form.cleaned_data['visibility'],
|
2018-05-01 20:44:56 +02:00
|
|
|
spoiler_text=form.cleaned_data['spoiler_text'],
|
|
|
|
media_ids=media_objects)
|
2018-04-27 20:12:29 +02:00
|
|
|
return redirect(home)
|
|
|
|
else:
|
2018-04-30 14:58:10 +02:00
|
|
|
return render(request, 'main/post.html',
|
2018-04-27 20:12:29 +02:00
|
|
|
{'form': form,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-04-27 20:12:29 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
else:
|
|
|
|
return redirect(toot)
|
2018-04-30 20:31:59 +02:00
|
|
|
|
|
|
|
def reply(request, id):
|
|
|
|
if request.method == 'GET':
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
toot = mastodon.status(id)
|
|
|
|
context = mastodon.status_context(id)
|
2018-06-13 19:42:40 +02:00
|
|
|
if toot.account.acct != request.session['user'].acct:
|
|
|
|
initial_text = '@' + toot.account.acct + " "
|
|
|
|
else:
|
|
|
|
initial_text = ""
|
2018-06-04 15:09:34 +02:00
|
|
|
for mention in [x for x in toot.mentions if x.acct != request.session['user'].acct]:
|
2018-05-01 15:34:14 +02:00
|
|
|
initial_text +=('@' + mention.acct + " ")
|
2018-06-22 14:40:46 +02:00
|
|
|
if initial_text != "":
|
|
|
|
initial_text += "\n"
|
2018-05-01 15:34:14 +02:00
|
|
|
form = PostForm({'status': initial_text,
|
|
|
|
'visibility': toot.visibility,
|
|
|
|
'spoiler_text': toot.spoiler_text})
|
2018-04-30 20:31:59 +02:00
|
|
|
return render(request, 'main/reply.html',
|
|
|
|
{'context': context, 'toot': toot, 'form': form, 'reply':True,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-04-30 20:31:59 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
elif request.method == 'POST':
|
|
|
|
form = PostForm(request.POST, request.FILES)
|
2018-05-13 19:16:44 +02:00
|
|
|
mastodon = get_mastodon(request)
|
2018-04-30 20:31:59 +02:00
|
|
|
if form.is_valid():
|
2018-05-01 20:44:56 +02:00
|
|
|
# create media objects
|
|
|
|
media_objects = []
|
|
|
|
for index in range(1,5):
|
|
|
|
if 'media_file_'+str(index) in request.FILES:
|
|
|
|
media_objects.append(
|
|
|
|
mastodon.media_post(request.FILES['media_file_'+str(index)]
|
|
|
|
.temporary_file_path(),
|
|
|
|
description=request.POST.get('media_text_'
|
|
|
|
+str(index),
|
|
|
|
None)))
|
2018-04-30 20:31:59 +02:00
|
|
|
mastodon.status_post(status=form.cleaned_data['status'],
|
|
|
|
visibility=form.cleaned_data['visibility'],
|
|
|
|
spoiler_text=form.cleaned_data['spoiler_text'],
|
2018-05-22 15:05:16 +02:00
|
|
|
media_ids=media_objects,
|
2018-04-30 20:31:59 +02:00
|
|
|
in_reply_to_id=id)
|
|
|
|
return redirect(thread, id)
|
|
|
|
else:
|
2018-05-13 19:16:44 +02:00
|
|
|
toot = mastodon.status(id)
|
|
|
|
context = mastodon.status_context(id)
|
2018-04-30 20:31:59 +02:00
|
|
|
return render(request, 'main/reply.html',
|
|
|
|
{'context': context, 'toot': toot, 'form': form, 'reply': True,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-04-30 20:31:59 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
else:
|
|
|
|
return redirect(reply, id)
|
2018-05-01 00:40:41 +02:00
|
|
|
|
2018-05-01 15:28:57 +02:00
|
|
|
@never_cache
|
2018-05-01 00:40:41 +02:00
|
|
|
def fav(request, id):
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
toot = mastodon.status(id)
|
|
|
|
if request.method == 'POST':
|
2018-05-01 14:39:48 +02:00
|
|
|
if not request.POST.get('cancel', None):
|
2018-05-01 00:53:11 +02:00
|
|
|
if toot.favourited:
|
|
|
|
mastodon.status_unfavourite(id)
|
|
|
|
else:
|
|
|
|
mastodon.status_favourite(id)
|
2018-05-01 00:40:41 +02:00
|
|
|
return redirect(thread, id)
|
|
|
|
else:
|
2018-05-04 16:58:37 +02:00
|
|
|
return render(request, 'main/fav.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{"toot": toot,
|
|
|
|
'own_acct': request.session['user'],
|
|
|
|
"confirm_page": True,
|
2018-05-04 16:58:37 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
2018-05-01 00:49:06 +02:00
|
|
|
|
2018-05-01 16:32:21 +02:00
|
|
|
@never_cache
|
2018-05-01 00:49:06 +02:00
|
|
|
def boost(request, id):
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
toot = mastodon.status(id)
|
|
|
|
if request.method == 'POST':
|
2018-05-01 14:39:48 +02:00
|
|
|
if not request.POST.get('cancel', None):
|
2018-05-01 00:53:11 +02:00
|
|
|
if toot.reblogged:
|
|
|
|
mastodon.status_unreblog(id)
|
|
|
|
else:
|
|
|
|
mastodon.status_reblog(id)
|
2018-05-01 00:49:06 +02:00
|
|
|
return redirect(thread, id)
|
|
|
|
else:
|
2018-05-04 16:58:37 +02:00
|
|
|
return render(request, 'main/boost.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{"toot": toot,
|
|
|
|
'own_acct': request.session['user'],
|
|
|
|
'confirm_page': True,
|
2018-05-04 16:58:37 +02:00
|
|
|
"fullbrutalism": fullbrutalism_p(request)})
|
2018-05-22 15:41:11 +02:00
|
|
|
|
|
|
|
@never_cache
|
|
|
|
def delete(request, id):
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
toot = mastodon.status(id)
|
|
|
|
if request.method == 'POST':
|
|
|
|
if toot.account.acct != request.session['user'].acct:
|
|
|
|
return redirect('home')
|
|
|
|
if not request.POST.get('cancel', None):
|
|
|
|
mastodon.status_delete(id)
|
|
|
|
return redirect(home)
|
|
|
|
else:
|
|
|
|
return render(request, 'main/delete.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{"toot": toot,
|
|
|
|
'own_acct': request.session['user'],
|
|
|
|
'confirm_page': True,
|
2018-05-22 15:41:11 +02:00
|
|
|
"fullbrutalism": fullbrutalism_p(request)})
|
2018-05-26 19:28:13 +02:00
|
|
|
|
2018-06-11 20:34:38 +02:00
|
|
|
@never_cache
|
|
|
|
def follow(request, id):
|
2018-06-12 01:09:12 +02:00
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
try:
|
|
|
|
user_dict = mastodon.account(id)
|
|
|
|
relationship = mastodon.account_relationships(user_dict.id)[0]
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-12 01:09:12 +02:00
|
|
|
raise Http404("The user could not be found.")
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('cancel', None):
|
|
|
|
if relationship.requested or relationship.following:
|
|
|
|
mastodon.account_unfollow(id)
|
|
|
|
else:
|
|
|
|
mastodon.account_follow(id)
|
|
|
|
return redirect(user, user_dict.acct)
|
|
|
|
else:
|
|
|
|
return render(request, 'main/follow.html',
|
2018-06-12 01:38:49 +02:00
|
|
|
{"user": user_dict, "relationship": relationship,
|
|
|
|
"confirm_page": True,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-06-12 01:38:49 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
2018-06-11 20:34:38 +02:00
|
|
|
|
|
|
|
@never_cache
|
|
|
|
def block(request, id):
|
2018-06-12 01:38:49 +02:00
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
try:
|
|
|
|
user_dict = mastodon.account(id)
|
|
|
|
relationship = mastodon.account_relationships(user_dict.id)[0]
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-12 01:38:49 +02:00
|
|
|
raise Http404("The user could not be found.")
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('cancel', None):
|
|
|
|
if relationship.blocking:
|
|
|
|
mastodon.account_unblock(id)
|
|
|
|
else:
|
|
|
|
mastodon.account_block(id)
|
|
|
|
return redirect(user, user_dict.acct)
|
|
|
|
else:
|
|
|
|
return render(request, 'main/block.html',
|
|
|
|
{"user": user_dict, "relationship": relationship,
|
|
|
|
"confirm_page": True,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-06-12 01:38:49 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
|
2018-06-11 20:34:38 +02:00
|
|
|
|
|
|
|
@never_cache
|
|
|
|
def mute(request, id):
|
2018-06-12 01:38:49 +02:00
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
try:
|
|
|
|
user_dict = mastodon.account(id)
|
|
|
|
relationship = mastodon.account_relationships(user_dict.id)[0]
|
2018-08-16 18:59:16 +02:00
|
|
|
except (IndexError, AttributeError):
|
2018-06-12 01:38:49 +02:00
|
|
|
raise Http404("The user could not be found.")
|
|
|
|
if request.method == 'POST':
|
|
|
|
if not request.POST.get('cancel', None):
|
|
|
|
if relationship.muting:
|
|
|
|
mastodon.account_unmute(id)
|
|
|
|
else:
|
|
|
|
mastodon.account_mute(id)
|
|
|
|
return redirect(user, user_dict.acct)
|
|
|
|
else:
|
|
|
|
return render(request, 'main/mute.html',
|
|
|
|
{"user": user_dict, "relationship": relationship,
|
|
|
|
"confirm_page": True,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-06-12 01:38:49 +02:00
|
|
|
'fullbrutalism': fullbrutalism_p(request)})
|
|
|
|
|
2018-06-06 00:36:44 +02:00
|
|
|
|
|
|
|
def search(request):
|
2018-06-09 01:03:02 +02:00
|
|
|
return render(request, 'main/search.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{"fullbrutalism": fullbrutalism_p(request),
|
|
|
|
'own_acct': request.session['user'],
|
|
|
|
})
|
2018-06-06 00:36:44 +02:00
|
|
|
|
|
|
|
def search_results(request):
|
|
|
|
if request.method == 'GET':
|
|
|
|
query = request.GET.get('q', '')
|
|
|
|
elif request.method == 'POST':
|
|
|
|
query = request.POST.get('q', '')
|
|
|
|
else:
|
|
|
|
query = ''
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
results = mastodon.search(query)
|
2018-06-09 01:03:02 +02:00
|
|
|
return render(request, 'main/search_results.html',
|
2018-06-06 00:36:44 +02:00
|
|
|
{"results": results,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session['user'],
|
2018-06-06 00:36:44 +02:00
|
|
|
"fullbrutalism": fullbrutalism_p(request)})
|
|
|
|
|
2018-05-26 19:28:13 +02:00
|
|
|
def about(request):
|
2018-08-16 19:10:45 +02:00
|
|
|
version = django_settings.BRUTALDON_VERSION
|
2018-05-27 19:43:20 +02:00
|
|
|
return render(request, 'about.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{"fullbrutalism": fullbrutalism_p(request),
|
2018-08-16 19:10:45 +02:00
|
|
|
"version": version,
|
2018-07-18 19:33:16 +02:00
|
|
|
'own_acct': request.session.get('user', None),
|
|
|
|
})
|
2018-05-26 19:28:13 +02:00
|
|
|
def privacy(request):
|
2018-05-27 19:43:20 +02:00
|
|
|
return render(request, 'privacy.html',
|
2018-07-18 19:33:16 +02:00
|
|
|
{"fullbrutalism": fullbrutalism_p(request),
|
|
|
|
'own_acct' : request.session['user']})
|
2018-08-10 01:39:23 +02:00
|
|
|
|
|
|
|
@cache_page(60 * 30)
|
|
|
|
def emoji_reference(request):
|
|
|
|
mastodon = get_mastodon(request)
|
|
|
|
emojos = mastodon.custom_emojis()
|
|
|
|
return render(request, 'main/emoji.html',
|
|
|
|
{"fullbrutalism": fullbrutalism_p(request),
|
2018-08-17 03:19:03 +02:00
|
|
|
"emojos": sorted(emojos, key=lambda x: x['shortcode']),
|
2018-08-10 01:39:23 +02:00
|
|
|
'own_acct' : request.session['user']})
|