33from .settings import config
44from .models import App , db
55from .discord import random_party_emoji
6- from .utils import get_app_description , generate_image_url
6+ from .utils import get_app_description , generate_image_url , demand_authed_request
7+ import requests
8+ import json
79
810PLATFORM_EMOJI = {
911 'aplite' : ':pebble-orange:' ,
1618
1719if config ['DISCOURSE_API_KEY' ] is None :
1820 _client = None
21+ print ("== Discourse Integration not configured." )
1922else :
2023 _client = DiscourseClient (host = f"https://{ config ['DISCOURSE_HOST' ]} " , api_username = config ['DISCOURSE_USER' ], api_key = config ['DISCOURSE_API_KEY' ])
2124
@@ -120,6 +123,45 @@ def announce_new_app(app, is_generated, is_new=True):
120123""" )
121124
122125def get_topic_url_for_app (app ):
123- if not _client or not app .discourse_topic_id or app .discourse_topic_id == - 1 :
124- return None
125- return f"https://{ config ['DISCOURSE_HOST' ]} /t/{ app .discourse_topic_id } "
126+ if app .discourse_topic_id > 0 :
127+ return f"https://{ config ['DISCOURSE_HOST' ]} /t/{ app .discourse_topic_id } "
128+ else :
129+ return
130+
131+ def is_valid_topic_url (topic_url ):
132+ topic_url = topic_url .lower ().strip ()
133+
134+ start_string = config ['DISCOURSE_HOST' ].lower ()
135+ if not start_string .startswith ("http://" ):
136+ start_string = "https://" + start_string
137+
138+ return topic_url .startswith (start_string )
139+
140+ def topic_url_to_id (topic_url ):
141+ topic_url = topic_url .lower ().strip ()
142+ if "?" in topic_url :
143+ topic_url = topic_url [:topic_url .index ("?" )]
144+ sections = topic_url .split ("/" )
145+
146+ if len (sections ) == 5 :
147+ # Short url: https://discourse.example.com/t/12345
148+ return int (sections [4 ])
149+ else :
150+ # Long url: https://discourse.example.com/t/topic-title/12345
151+ # Long url with page: https://discourse.example.com/t/topic-title/12345/2
152+ return int (sections [5 ])
153+
154+ def fetch_owner_from_topic_url (topic_url ):
155+ #The py client sucks a bit so we'll just call the JSON
156+ topic = requests .request ("GET" , topic_url + ".json" ).json ()
157+ topic_owner = topic ["details" ]["created_by" ]["username" ]
158+ return topic_owner
159+
160+ def user_owns_discourse_topic (discourse_topic_url ):
161+ discourse_username = fetch_owner_from_topic_url (discourse_topic_url )
162+
163+ auth_result = demand_authed_request ('GET' , f"{ config ['REBBLE_AUTH_URL' ]} /api/v1/me/pebble/appstore" )
164+ me = auth_result .json ()
165+ my_username = me ["rebble_username" ]
166+
167+ return discourse_username == my_username
0 commit comments