Skip to content
This repository
tree: 8dadc8ca98
Fetching contributors…

Cannot retrieve contributors at this time

file 260 lines (215 sloc) 8.927 kb
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260
import re
import logging
from google.appengine.ext import webapp
from google.appengine.ext.webapp import util
from django.utils import simplejson
from model import get_current_youtify_user_model
from model import get_youtify_user_model_by_id_or_nick
from model import get_youtify_user_struct
from model import YoutifyUser
from model import FollowRelation
from model import get_activities_structs
from model import get_display_name_for_youtify_user_model
from model import get_external_user_subscriptions_struct_for_youtify_user_model
from model import get_settings_struct_for_youtify_user_model
from model import get_playlist_structs_for_youtify_user_model
from model import generate_device_token
from activities import create_follow_activity
from mail import send_new_follower_email

BLOCKED_NICKNAMES = [
    'admin',
    'stats',
    'import',
    'export',
    'translations',
    'settings',
    'preferences',
    'yourbrowsersucks',
    'yourdecisionrocks',
    'me',
    'news',
    'feed',
    'newsfeed',
    'activities',
    'toplist',
    'recommendations',
    'queue',
    'search',
    'users',
    'playlists',
    'api',
    'about',
    'support',
    'faq',
]

class ProfileHandler(webapp.RequestHandler):
    def get(self):
        user = get_current_youtify_user_model()
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(simplejson.dumps(get_youtify_user_struct(get_current_youtify_user_model())))

    def post(self):
        user = get_current_youtify_user_model()
        nickname = self.request.get('nickname', user.nickname)
        first_name = self.request.get('first_name', user.first_name)
        last_name = self.request.get('last_name', user.first_name)
        tagline = self.request.get('tagline', user.tagline)

        if nickname and not re.match('^[A-Za-z0-9_]{1,36}$', nickname):
            self.error(400)
            self.response.out.write('Nickname must be 1-36 alphanumerical characters (no whitespace)')
            return

        if nickname and nickname in BLOCKED_NICKNAMES:
            self.error(400)
            self.response.out.write('That nickname is not allowed.')
            return

        for u in YoutifyUser.all().filter('nickname_lower = ', nickname.lower()):
            if str(u.key().id()) != str(user.key().id()):
                self.error(409)
                self.response.out.write('Nickname is already taken')
                return

        user.nickname = nickname
        user.nickname_lower = nickname.lower()
        user.first_name = first_name
        user.last_name = last_name
        user.tagline = tagline

        user.save()

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(get_display_name_for_youtify_user_model(user))

class SettingsHandler(webapp.RequestHandler):
    
    def get(self):
        user = get_current_youtify_user_model()
        settings = get_settings_struct_for_youtify_user_model(user)
        self.response.out.write(simplejson.dumps(settings))

    def post(self):
        user = get_current_youtify_user_model()
        user.send_new_follower_email = self.request.get('send_new_follower_email') == 'true'
        user.send_new_subscriber_email = self.request.get('send_new_subscriber_email') == 'true'
        user.flattr_automatically = self.request.get('flattr_automatically') == 'true'
        user.lastfm_scrobble_automatically = self.request.get('lastfm_scrobble_automatically') == 'true'
        user.save()
        
        logging.info(self.request)
        
        settings = get_settings_struct_for_youtify_user_model(user)
        self.response.out.write(simplejson.dumps(settings))

class FollowingsHandler(webapp.RequestHandler):

    def delete(self, uid):
        me = get_current_youtify_user_model()
        other_user = get_youtify_user_model_by_id_or_nick(uid)

        if other_user is None:
            self.error(400)
            self.response.out.write('Other user not found')
            return

        m = FollowRelation.all().filter('user1 =', me.key().id()).filter('user2 =', int(uid)).get()
        if m:
            m.delete()

        me.nr_of_followings -= 1
        other_user.nr_of_followers -= 1

        me.save()
        other_user.save()

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('ok')

    def post(self, uid):
        other_user = YoutifyUser.get_by_id(int(uid))
        me = get_current_youtify_user_model()

        if other_user is None:
            self.error(400)
            self.response.out.write('Other user not found')
            return

        if me.key().id() == other_user.key().id():
            self.error(400)
            self.response.out.write('You can not follow yourself')
            return

        if FollowRelation.all().filter('user1 =', me).filter('user2 =', other_user).get():
            self.error(400)
            self.response.out.write('You already follow that user')
            return

        me.nr_of_followings += 1
        other_user.nr_of_followers += 1

        me.save()
        other_user.save()

        m = FollowRelation(user1=me.key().id(), user2=other_user.key().id())
        m.put()

        create_follow_activity(me, other_user)
        send_new_follower_email(me, other_user)

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('ok')

class YouTubeUserNameHandler(webapp.RequestHandler):
    def get(self):
        user = get_current_youtify_user_model()
        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write(user.youtube_username)

    def post(self):
        username = self.request.get('username')

        user = get_current_youtify_user_model()
        user.youtube_username = username
        user.save()

        self.response.headers['Content-Type'] = 'text/plain'
        self.response.out.write('ok')

class ExternalUserSubscriptionsHandler(webapp.RequestHandler):
    def get(self):
        user = get_current_youtify_user_model()
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(simplejson.dumps(get_external_user_subscriptions_struct_for_youtify_user_model(user)))

class PlaylistsHandler(webapp.RequestHandler):

    def get(self):
        """Get the users playlists, including private ones"""
        user = get_current_youtify_user_model()
        if user:
            json = get_playlist_structs_for_youtify_user_model(user, include_private_playlists=True)
        else:
            json = []
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(simplejson.dumps(json))

class MeHandler(webapp.RequestHandler):

    def get(self):
        """Get the currnet user, incuding private data"""
        user = get_current_youtify_user_model()
        if user:
            json = get_youtify_user_struct(user, include_private_data=True)
        else:
            json = {
            }
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(simplejson.dumps(json))

class DeviceTokenHandler(webapp.RequestHandler):

    def get(self):
        """Set a new device token for the user"""
        user = get_current_youtify_user_model()
        user.device = generate_device_token()
        user.save()
        json = {
            'device': user.device
        }
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(simplejson.dumps(json))

class LastNotificationSeenTimestampHandler(webapp.RequestHandler):

    def post(self):
        user = get_current_youtify_user_model()
        val = self.request.get('val')
        json = {
            'message': '',
        }
        if user:
            if val > user.last_notification_seen_timestamp:
                user.last_notification_seen_timestamp = val
                user.save()
                json['message'] = 'timestamp updated'
            else:
                json['message'] = 'newer timestamp already set'
        else:
            json['message'] = 'no user found'
        self.response.headers['Content-Type'] = 'application/json'
        self.response.out.write(simplejson.dumps(json))

def main():
    application = webapp.WSGIApplication([
        ('/me', MeHandler),
        ('/me/last-notification-seen-timestamp', LastNotificationSeenTimestampHandler),
        ('/me/external_user_subscriptions', ExternalUserSubscriptionsHandler),
        ('/me/youtube_username', YouTubeUserNameHandler),
        ('/me/profile', ProfileHandler),
        ('/me/playlists', PlaylistsHandler),
        ('/me/request_new_device_token', DeviceTokenHandler),
        ('/me/settings', SettingsHandler),
        ('/me/followings/(.*)', FollowingsHandler),
    ], debug=True)
    util.run_wsgi_app(application)

if __name__ == '__main__':
    main()
Something went wrong with that request. Please try again.