File size: 14,220 Bytes
3533fb9
 
 
 
 
8ad8368
3533fb9
af296c7
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b7698ad
 
 
 
3533fb9
 
 
 
b7698ad
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad8368
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8ad8368
 
3533fb9
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
af296c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3533fb9
af296c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3533fb9
af296c7
3533fb9
af296c7
 
3533fb9
 
af296c7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
3533fb9
 
 
 
 
af296c7
 
 
 
 
3533fb9
 
 
af296c7
3533fb9
 
 
 
 
 
 
 
 
 
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
from sqlalchemy.orm import Session
from sqlalchemy import func
from app.schemas import schemas
from app.core import security
from app.models.models import User, Follow, Video, Like, Post, VerificationCode, ChallengeEntry, Challenge
from app.crud import video as crud_video
from datetime import datetime, timedelta
from typing import Optional
import random
import string
import httpx

def get_country_from_ip(ip: str):
    if not ip or ip in ["127.0.0.1", "localhost"]:
        return "Unknown"
    try:
        response = httpx.get(f"https://ipapi.co/{ip}/country_name/", timeout=2.0)
        if response.status_code == 200:
            return response.text.strip()
    except Exception as e:
        print(f"Geo-location error for {ip}: {e}")
    return "Unknown"

def get_user_by_username(db: Session, username: str):
    return db.query(User).filter(User.username.ilike(username)).first()

def get_user_by_email(db: Session, email: str):
    return db.query(User).filter(User.email.ilike(email)).first()

def create_user(db: Session, user: schemas.UserCreate, google_id: str = None, is_onboarded: bool = False, ip_address: str = None):
    hashed_password = security.get_password_hash(user.password) if user.password else None
    
    # Get user count for admin assignment
    user_count = db.query(func.count(User.id)).scalar() or 0
    is_first_user = user_count == 0
    is_requested_admin = user.username.lower() == "smasduq"
    
    role = "admin" if (is_first_user or is_requested_admin) else "user"

    # Automated country collection
    detected_country = user.country
    if not detected_country and ip_address:
        detected_country = get_country_from_ip(ip_address)

    profile_pic = user.profile_pic
    if not profile_pic and user.username:
        profile_pic = f"https://api.dicebear.com/7.x/adventurer/svg?seed={user.username}"

    db_user = User(
        username=user.username,
        email=user.email,
        full_name=user.full_name,
        profile_pic=profile_pic,
        hashed_password=hashed_password,
        google_id=google_id,
        is_onboarded=is_onboarded,
        role=role,
        country=detected_country
    )
    
    db.add(db_user)
    db.commit()
    db.refresh(db_user)
    return db_user

def update_user_role(db: Session, user_id: int, role: str):
    user = db.query(User).filter(User.id == user_id).first()
    if user:
        user.role = role
        db.commit()
        db.refresh(user)
    return user

def update_password(db: Session, user_id: int, passwords: schemas.PasswordChange):
    user = db.query(User).filter(User.id == user_id).first()
    if not user:
        return None
    
    if not security.verify_password(passwords.current_password, user.hashed_password):
        return False
    
    user.hashed_password = security.get_password_hash(passwords.new_password)
    db.commit()
    db.refresh(user)
    return True

def update_user(db: Session, user_id: int, user_update: schemas.UserUpdate):
    user = db.query(User).filter(User.id == user_id).first()
    if not user:
        return None

    if user_update.username:
        user.username = user_update.username
    if user_update.full_name:
        user.full_name = user_update.full_name
    if user_update.profile_pic:
        user.profile_pic = user_update.profile_pic
    if user_update.bio is not None:
        user.bio = user_update.bio
    if user_update.is_onboarded is not None:
        user.is_onboarded = user_update.is_onboarded
    if user_update.interests is not None:
        user.interests = user_update.interests
    if user_update.referral_source is not None:
        user.referral_source = user_update.referral_source
    if user_update.goals is not None:
        user.goals = user_update.goals
    if user_update.show_wins is not None:
        user.show_wins = user_update.show_wins
    if user_update.show_trophies is not None:
        user.show_trophies = user_update.show_trophies
    
    # Notification Preferences
    if user_update.notif_new_follower is not None:
        user.notif_new_follower = user_update.notif_new_follower
    if user_update.notif_challenge_win is not None:
        user.notif_challenge_win = user_update.notif_challenge_win
    if user_update.notif_comments is not None:
        user.notif_comments = user_update.notif_comments
    if user_update.notif_likes is not None:
        user.notif_likes = user_update.notif_likes
    
    if user_update.email_weekly is not None:
        user.email_weekly = user_update.email_weekly
    if user_update.email_challenges is not None:
        user.email_challenges = user_update.email_challenges
    if user_update.email_payouts is not None:
        user.email_payouts = user_update.email_payouts
    if user_update.email_marketing is not None:
        user.email_marketing = user_update.email_marketing
    
    # Account & Security
    if user_update.payout_method is not None:
        user.payout_method = user_update.payout_method
    if user_update.two_factor_enabled is not None:
        user.two_factor_enabled = user_update.two_factor_enabled

    db.commit()
    db.refresh(user)
    return user

def get_user_by_id(db: Session, user_id: int):
    return db.query(User).filter(User.id == user_id).first()

def get_user_profile(db: Session, username: str, current_user_id: int = None):
    db_user = get_user_by_username(db, username)
    if not db_user:
        return None
    
    user_id = db_user.id
    
    # Counts
    followers_count = db.query(func.count(Follow.follower_id)).filter(Follow.following_id == user_id).scalar() or 0
    following_count = db.query(func.count(Follow.following_id)).filter(Follow.follower_id == user_id).scalar() or 0
    
    # Total Views & Likes
    total_views = db.query(func.sum(Video.views)).filter(Video.owner_id == user_id).scalar() or 0
    total_likes = db.query(func.count(Like.id)).join(Video).filter(Video.owner_id == user_id).scalar() or 0
    
    is_following = False
    if current_user_id:
        is_following = db.query(Follow).filter(Follow.follower_id == current_user_id, Follow.following_id == user_id).first() is not None

    # Videos - Fetch only live (approved) videos for public, but all statuses for the owner
    if current_user_id == user_id:
        all_videos = db.query(Video).filter(Video.owner_id == user_id).all()
    else:
        all_videos = db.query(Video).filter(Video.owner_id == user_id, Video.status == "approved").all()
    videos = [v for v in all_videos if v.video_type == "home"]
    flash_videos = [v for v in all_videos if v.video_type == "flash"]
    upload_counts = crud_video.get_user_upload_counts(db, user_id)

    # Sort videos to find most popular
    sorted_videos = sorted(all_videos, key=lambda x: x.views or 0, reverse=True)
    most_popular = sorted_videos[0] if sorted_videos else None
    
    # Identify Featured Video (Pinned or Popular)
    featured_video = db_user.pinned_video
    
    if not featured_video and most_popular:
        featured_video = most_popular
    
    # Posts
    posts = db.query(Post).filter(Post.owner_id == user_id).all()
    
    # Liked Videos
    liked_videos = db.query(Video).join(Like).filter(Like.user_id == user_id).all()
    
    # Trophies (Won Challenges)
    trophies = db.query(ChallengeEntry).filter(
        ChallengeEntry.user_id == user_id,
        ChallengeEntry.is_winner == True
    ).all()

    profile_data = {
        "id": db_user.id,
        "username": db_user.username,
        "email": db_user.email,
        "full_name": db_user.full_name,
        "profile_pic": db_user.profile_pic,
        "is_premium": db_user.is_premium,
        "is_verified": db_user.is_verified,
        "is_onboarded": db_user.is_onboarded,
        "flash_uploads": upload_counts["flash"],
        "home_uploads": upload_counts["home"],
        "bio": db_user.bio,
        "role": db_user.role,
        "google_id": db_user.google_id,
        "flash_quota_limit": db_user.flash_quota_limit,
        "home_quota_limit": db_user.home_quota_limit,
        "show_wins": db_user.show_wins,
        "show_trophies": db_user.show_trophies,

        "followers_count": followers_count,
        "following_count": following_count,
        "total_views": total_views,
        "total_likes": total_likes,
        "featured_video": featured_video,
        "most_popular_video": most_popular,
        "videos_count": len(videos),
        "flash_videos_count": len(flash_videos),
        "posts_count": len(posts),
        "videos": videos,
        "flash_videos": flash_videos,
        "posts": posts,
        "liked_videos": liked_videos,
        "trophies": trophies,
        "is_following": is_following
    }
    return profile_data

from app.crud import achievement as crud_achievement
from app.crud import notification as crud_notification
from app.utils.push import notify_user_push
from app.schemas.notification import NotificationCreate

def toggle_follow(db: Session, follower_id: int, following_id: int):
    if follower_id == following_id:
        return False
    
    existing = db.query(Follow).filter(Follow.follower_id == follower_id, Follow.following_id == following_id).first()
    
    if existing:
        db.delete(existing)
        db.commit()
        return False
    else:
        new_follow = Follow(follower_id=follower_id, following_id=following_id)
        db.add(new_follow)
        db.commit()
        return True

def handle_follow_side_effects(db: Session, follower_id: int, following_id: int):
    """Secondary tasks after a user follows another."""
    # 1. Check for achievements
    follower_count = db.query(func.count(Follow.follower_id)).filter(Follow.following_id == following_id).scalar()
    
    if follower_count == 1:
        crud_achievement.create_achievement(db, user_id=following_id, milestone_name="FIRST_FOLLOWER")
    elif follower_count == 100:
        crud_achievement.create_achievement(db, user_id=following_id, milestone_name="100_FOLLOWERS")
    elif follower_count == 1000:
        crud_achievement.create_achievement(db, user_id=following_id, milestone_name="1K_FOLLOWERS")
        
    # 2. Notify user
    try:
        follower = get_user_by_id(db, follower_id)
        msg = f"{follower.username} started following you!"
        notify_user_push(db, following_id, "New Follower!", msg, link=f"/profile/{follower.username}", n_type="follower")
    except Exception as e:
        import logging
        logging.getLogger(__name__).error(f"Failed to handle follow side effects: {e}")

def _sync_verification_codes_sequence(db: Session):
    """Repair Postgres serial when it lags behind MAX(id) (common after restores)."""
    from sqlalchemy import text

    db.execute(
        text(
            "SELECT setval("
            "pg_get_serial_sequence('verification_codes', 'id'), "
            "COALESCE((SELECT MAX(id) FROM verification_codes), 1))"
        )
    )
    db.commit()


def get_latest_verification_code(db: Session, email: str):
    return (
        db.query(VerificationCode)
        .filter(VerificationCode.email.ilike(email.strip()))
        .order_by(VerificationCode.created_at.desc())
        .first()
    )


def seconds_since_verification_code(db: Session, email: str) -> Optional[float]:
    """Age of the latest verification/reset row for this email, or None if none."""
    existing = get_latest_verification_code(db, email)
    if not existing or not existing.created_at:
        return None
    created = existing.created_at
    if getattr(created, "tzinfo", None) is not None:
        created = created.replace(tzinfo=None)
    return max(0.0, (datetime.utcnow() - created).total_seconds())


def create_verification_code(db: Session, email: str):
    """
    Create or rotate a verification code for an email.
    Updates the existing row when present so we never fight a stale id sequence on resend.
    """
    from sqlalchemy.exc import IntegrityError

    email_norm = email.strip()
    code = "".join(random.choices(string.digits, k=6))
    expires_at = datetime.utcnow() + timedelta(minutes=10)
    now = datetime.utcnow()

    existing = get_latest_verification_code(db, email_norm)
    if existing:
        existing.email = email_norm
        existing.code = code
        existing.expires_at = expires_at
        existing.created_at = now
        db.commit()
        return code

    ver_code = VerificationCode(
        email=email_norm,
        code=code,
        expires_at=expires_at,
        created_at=now,
    )
    db.add(ver_code)
    try:
        db.commit()
        return code
    except IntegrityError:
        db.rollback()
        _sync_verification_codes_sequence(db)
        # Another row may exist now, or sequence was the only problem — retry safely.
        existing = get_latest_verification_code(db, email_norm)
        if existing:
            existing.email = email_norm
            existing.code = code
            existing.expires_at = expires_at
            existing.created_at = now
            db.commit()
            return code
        ver_code = VerificationCode(
            email=email_norm,
            code=code,
            expires_at=expires_at,
            created_at=now,
        )
        db.add(ver_code)
        db.commit()
        return code


def verify_code(db: Session, email: str, code: str):
    db_code = db.query(VerificationCode).filter(VerificationCode.email.ilike(email), VerificationCode.code == code).first()
    if not db_code:
        return False

    expires = db_code.expires_at
    if getattr(expires, "tzinfo", None) is not None:
        expires = expires.replace(tzinfo=None)
    if expires < datetime.utcnow():
        db.delete(db_code)
        db.commit()
        return False

    # Success
    db.delete(db_code)
    db.commit()
    return True

def get_followers(db: Session, user_id: int, skip: int = 0, limit: int = 100):
    return db.query(User).join(Follow, User.id == Follow.follower_id).filter(Follow.following_id == user_id).offset(skip).limit(limit).all()

def get_following(db: Session, user_id: int, skip: int = 0, limit: int = 100):
    return db.query(User).join(Follow, User.id == Follow.following_id).filter(Follow.follower_id == user_id).offset(skip).limit(limit).all()