File size: 8,654 Bytes
b63786a
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
b63786a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
b63786a
 
 
 
 
4c888b7
 
b63786a
4c888b7
b63786a
 
 
 
 
 
 
 
 
4c888b7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
b63786a
 
4c888b7
b63786a
 
 
 
 
 
 
 
 
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
const User = require('../models/User');
const GalleryPost = require('../models/GalleryPost');
const { shouldUseMemoryStore } = require('../config/dbMode');
const memoryStore = require('../store/memoryStore');
const { isValidAvatarInput } = require('../utils/imageValidation');
const { normalizeId, normalizeIdList, serializeUser } = require('../utils/userSerializer');
const {
  FOLLOW_REWARD,
  addRewardMarker,
  addTokensToUser,
  ensureUserTokenState,
  generateReferralCode,
  hasRewardMarker,
  normalizeReferralCode,
} = require('../utils/tokenUtils');

const findUserRecordByReferralCode = async (referralCode = '') => {
  const normalizedReferralCode = normalizeReferralCode(referralCode);
  if (!normalizedReferralCode) {
    return null;
  }

  if (shouldUseMemoryStore()) {
    return memoryStore.findUserRecordByReferralCode(normalizedReferralCode);
  }

  return User.findOne({ referralCode: normalizedReferralCode });
};

const ensureUserReferralCode = async (user) => {
  if (!user || user.referralCode) {
    return user;
  }

  for (let attempt = 0; attempt < 12; attempt += 1) {
    const referralCode = generateReferralCode();
    const existingUser = await findUserRecordByReferralCode(referralCode);
    if (!existingUser) {
      user.referralCode = referralCode;

      if (shouldUseMemoryStore()) {
        await memoryStore.persistStore();
      } else {
        await user.save();
      }
      break;
    }
  }

  return user;
};

const serializePublicUser = (user) => {
  const serializedUser = serializeUser(user);

  if (!serializedUser) {
    return null;
  }

  return {
    _id: serializedUser.id,
    id: serializedUser.id,
    username: serializedUser.username,
    avatarUrl: serializedUser.avatarUrl,
    createdAt: serializedUser.createdAt,
    followersCount: serializedUser.followersCount,
    followingCount: serializedUser.followingCount,
  };
};

const serializePublicGalleryPost = (post, viewerUserId = '') => {
  const normalizedViewerUserId = normalizeId(viewerUserId);
  const likedBy = normalizeIdList(post.likedBy);
  const authorId = normalizeId(post.userId);

  return {
    _id: normalizeId(post._id),
    userId: authorId,
    username: post.username,
    userAvatarUrl: post.userAvatarUrl || '',
    title: post.title,
    description: post.description || '',
    prompt: post.prompt || '',
    imageUrl: post.imageUrl,
    source: post.source || 'upload',
    createdAt: post.createdAt,
    likesCount: likedBy.length,
    likedByCurrentUser: normalizedViewerUserId ? likedBy.includes(normalizedViewerUserId) : false,
    shareCount: Number(post.shareCount || 0),
    authorFollowersCount: Number(post.authorFollowersCount || 0),
  };
};

// Get User Profile
exports.getUserProfile = async (req, res) => {
  try {
    const userId = req.userId;

    const user = shouldUseMemoryStore()
      ? await memoryStore.findUserById(userId)
      : await User.findById(userId).select('-password');

    if (!user) {
      return res.status(404).json({ error: 'User not found' });
    }

    await ensureUserReferralCode(user);

    res.json({ user: serializeUser(user) });
  } catch (error) {
    console.error('Profile retrieval error:', error);
    res.status(500).json({ error: 'Failed to retrieve profile' });
  }
};

// Update User Profile
exports.updateUserProfile = async (req, res) => {
  try {
    const userId = req.userId;
    const { username, avatarUrl } = req.body;
    const updates = {};

    if (typeof username === 'string') {
      if (username.trim().length === 0) {
        return res.status(400).json({ error: 'Username is required' });
      }
      updates.username = username;
    }

    if (avatarUrl !== undefined) {
      if (!isValidAvatarInput(avatarUrl)) {
        return res.status(400).json({ error: 'Please upload a valid profile image' });
      }
      updates.avatarUrl = avatarUrl || '';
    }

    if (Object.keys(updates).length === 0) {
      return res.status(400).json({ error: 'No profile changes provided' });
    }

    const user = shouldUseMemoryStore()
      ? await memoryStore.updateUser(userId, updates)
      : await User.findByIdAndUpdate(
          userId,
          updates,
          { new: true, runValidators: true }
        ).select('-password');

    res.json({ message: 'Profile updated successfully', user: serializeUser(user) });
  } catch (error) {
    console.error('Profile update error:', error);
    if (error.code === 11000) {
      return res.status(409).json({ error: 'Username already exists' });
    }
    res.status(500).json({ error: 'Failed to update profile' });
  }
};

exports.getPublicUserProfile = async (req, res) => {
  try {
    const targetUserId = req.params.userId;
    const viewerUserId = req.userId || '';

    if (shouldUseMemoryStore()) {
      const result = await memoryStore.getPublicUserProfile({
        targetUserId,
        viewerUserId,
      });

      if (!result) {
        return res.status(404).json({ error: 'User not found' });
      }

      return res.json(result);
    }

    const [targetUser, viewer] = await Promise.all([
      User.findById(targetUserId).select('-password'),
      viewerUserId ? User.findById(viewerUserId).select('following').lean() : null,
    ]);

    if (!targetUser) {
      return res.status(404).json({ error: 'User not found' });
    }

    const viewerFollowingIds = normalizeIdList(viewer?.following);
    const normalizedTargetUserId = normalizeId(targetUser);
    const posts = await GalleryPost.find({ userId: targetUser._id })
      .sort({ createdAt: -1 })
      .lean();

    res.json({
      user: serializePublicUser(targetUser),
      posts: posts.map((post) => serializePublicGalleryPost(post, viewerUserId)),
      totalPublicPosts: posts.length,
      isCurrentUser: viewerUserId === normalizedTargetUserId,
      isFollowing: viewerFollowingIds.includes(normalizedTargetUserId),
      canFollow: Boolean(viewerUserId && viewerUserId !== normalizedTargetUserId),
    });
  } catch (error) {
    console.error('Public profile retrieval error:', error);

    if (error.name === 'CastError') {
      return res.status(404).json({ error: 'User not found' });
    }

    res.status(500).json({ error: 'Failed to retrieve public profile' });
  }
};

exports.toggleFollowUser = async (req, res) => {
  try {
    const currentUserId = req.userId;
    const targetUserId = req.params.userId;

    if (currentUserId === targetUserId) {
      return res.status(400).json({ error: 'You cannot follow yourself' });
    }

    const [currentUser, targetUser] = shouldUseMemoryStore()
      ? await Promise.all([
          memoryStore.getUserRecordById(currentUserId),
          memoryStore.getUserRecordById(targetUserId),
        ])
      : await Promise.all([
          User.findById(currentUserId).select('-password'),
          User.findById(targetUserId).select('-password'),
        ]);

    if (!currentUser || !targetUser) {
      return res.status(404).json({ error: 'User not found' });
    }

    ensureUserTokenState(currentUser);

    const isFollowing = currentUser.following.some((id) => id.toString() === targetUserId);
    let message = isFollowing ? 'User unfollowed successfully' : 'User followed successfully';

    currentUser.following = isFollowing
      ? currentUser.following.filter((id) => id.toString() !== targetUserId)
      : [...currentUser.following, targetUser._id];

    targetUser.followers = isFollowing
      ? targetUser.followers.filter((id) => id.toString() !== currentUserId)
      : [...targetUser.followers, currentUser._id];

    if (!isFollowing && !hasRewardMarker(currentUser, 'rewardedFollowUserIds', targetUserId)) {
      addRewardMarker(currentUser, 'rewardedFollowUserIds', targetUserId);
      addTokensToUser(currentUser, FOLLOW_REWARD, 'follow-user', 'First follow reward for a creator');
      message = `${message} +${FOLLOW_REWARD} tokens awarded.`;
    }

    if (shouldUseMemoryStore()) {
      memoryStore.syncGalleryPostsForAuthorFollowers(targetUserId, targetUser.followers.length);
      await memoryStore.persistStore();
    } else {
      await Promise.all([
        currentUser.save(),
        targetUser.save(),
        GalleryPost.updateMany(
          { userId: targetUser._id },
          { authorFollowersCount: targetUser.followers.length }
        ),
      ]);
    }

    res.json({
      message,
      following: !isFollowing,
      currentUser: serializeUser(currentUser),
      targetUser: serializeUser(targetUser),
    });
  } catch (error) {
    console.error('Follow toggle error:', error);
    res.status(500).json({ error: 'Failed to update follow status' });
  }
};