-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofile.go
More file actions
426 lines (371 loc) · 10.6 KB
/
profile.go
File metadata and controls
426 lines (371 loc) · 10.6 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
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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
package main
import (
"fmt"
"io"
"net/http"
"net/url"
"regexp"
"slices"
"sort"
"strconv"
"strings"
"time"
"github.com/gin-gonic/gin"
)
type profileResp struct {
Username Username `json:"username"`
DiscordID string `json:"discord_id"`
Avatar string `json:"pfp"`
Banned bool `json:"sys.banned"`
Private bool `json:"private"`
Bio string `json:"bio"`
Banner string `json:"banner"`
Followers int `json:"followers"`
Following int `json:"following"`
Pronouns string `json:"pronouns"`
System string `json:"system"`
Created int64 `json:"created"`
Badges []Badge `json:"badges"`
Subscription string `json:"subscription"`
MaxSize string `json:"max_size"`
Currency float64 `json:"currency"`
Index int `json:"index"`
PrivateData any `json:"private_data,omitempty"`
Balance any `json:"balance,omitempty"`
Blocked []string `json:"blocked,omitempty"`
Posts []NetPost `json:"posts,omitempty"`
Status *UserStatus `json:"status,omitempty"`
Theme map[string]any `json:"theme,omitempty"`
Followed bool `json:"followed,omitempty"`
FollowsMe bool `json:"follows_me,omitempty"`
Id UserId `json:"id"`
}
func renderBioRegex(bio string, profile User, otherKeys profileResp) string {
safeProfile := map[string]string{}
for k, v := range profile {
if k == "key" || k == "password" {
continue
}
switch val := v.(type) {
case string:
safeProfile[k] = val
case int:
safeProfile[k] = strconv.Itoa(val)
case float64:
safeProfile[k] = fmt.Sprintf("%g", val)
case bool:
safeProfile[k] = strconv.FormatBool(val)
}
}
safeProfile["bio"] = otherKeys.Bio
safeProfile["followers"] = fmt.Sprint(otherKeys.Followers)
safeProfile["following"] = fmt.Sprint(otherKeys.Following)
re := regexp.MustCompile(`{{\s*([a-zA-Z0-9_]+)(?:\s+([^}]+?))?\s*}}`)
result := re.ReplaceAllStringFunc(bio, func(match string) string {
sub := re.FindStringSubmatch(match)
if len(sub) < 2 {
return ""
}
prefix := sub[1]
key := ""
if len(sub) >= 3 {
key = sub[2]
}
switch prefix {
case "user":
if val, ok := safeProfile[key]; ok {
return val
}
return ""
case "time":
tier := profile.GetSubscription().Tier
if !hasTierOrHigher(tier, "drive") {
return "{{ Error, time only available to Drive users }}"
}
format := strings.TrimSpace(key)
if format == "" {
format = "15:04"
}
format = normalizeUserTimeLayout(format)
tzStr := getStringOrEmpty(profile.Get("timezone"))
offsetHours, ok := parseUTCOffsetHours(tzStr)
if !ok {
offsetHours = 0
}
loc := time.FixedZone(fmt.Sprintf("UTC%+d", offsetHours), offsetHours*60*60)
return time.Now().UTC().In(loc).Format(format)
case "url":
tier := profile.GetSubscription().Tier
if !hasTierOrHigher(tier, "Pro") {
return "{{ Error, url only available to Pro users }}"
}
client := &http.Client{
Timeout: 3 * time.Second,
}
resp, err := client.Get("https://proxy.mistium.com?url=" + url.QueryEscape(key))
if err != nil {
return ""
}
defer resp.Body.Close()
limited := io.LimitReader(resp.Body, 1000)
body, err := io.ReadAll(limited)
if err != nil {
return ""
}
if len(body) > 1000 {
body = body[:1000]
}
return string(body)
case "flex":
if key == "economy%" {
currencyData := getUserCreditData()
total := 0.0
for _, amount := range currencyData {
total += amount
}
if total == 0 {
return "0%"
}
current := getFloatOrDefault(safeProfile["sys.currency"], 0.0)
return fmt.Sprintf("%.2f%%", (current/total)*100)
}
}
return ""
})
return result
}
func getProfile(c *gin.Context) {
nameRaw := Username(c.Query("username"))
if nameRaw == "" {
nameRaw = Username(c.Query("name"))
}
discord_id := c.Query("discord_id")
id := UserId(c.Query("id"))
if nameRaw == "" && discord_id == "" && id == "" {
c.JSON(400, gin.H{"error": "Name, Discord ID, or ID is required"})
return
}
if id != "" {
idToUserMutex.RLock()
foundUser, ok := idToUser[id]
idToUserMutex.RUnlock()
if !ok {
c.JSON(404, gin.H{"error": "User not found"})
return
}
nameRaw = foundUser.GetUsername()
}
authKey := c.Query("auth")
includePosts := c.DefaultQuery("include_posts", "1") == "1"
// Convert the name to lowercase for case-insensitive comparison
name := Username(nameRaw)
nameLower := name.ToLower()
// Find user with case-insensitive matching
var foundUser User
if discord_id != "" {
foundUsers, err := getAccountsBy("discord_id", discord_id, 1)
if err != nil {
c.JSON(404, gin.H{"error": "User not found"})
return
}
foundUser = foundUsers[0]
name = foundUser.GetUsername()
nameLower = name.ToLower()
} else {
user, err := getAccountByUsername(name)
if err != nil {
fmt.Println("Error", err.Error())
c.JSON(404, gin.H{"error": "User not found"})
return
}
foundUser = user
}
userIndex := getIdxOfAccountBy("username", string(nameLower))
userId := foundUser.GetId()
if foundUser.IsBanned() {
c.JSON(200, profileResp{
Username: foundUser.GetUsername(),
Badges: []Badge{},
Currency: 0,
MaxSize: "0",
Avatar: "https://avatars.rotur.dev/.banned_user",
Bio: "This user is banned.",
Banner: "https://avatars.rotur.dev/.banners/.banned_user",
Index: 0,
Followers: 0,
Following: 0,
Posts: []NetPost{},
Created: time.Now().UnixMilli(),
Private: true,
Banned: true,
Id: "",
})
return
}
// Get user posts
pinnedPosts := make([]NetPost, 0)
regularPosts := make([]NetPost, 0)
if includePosts {
postsMutex.RLock()
for _, post := range posts {
if post.User == userId {
if post.Pinned {
pinnedPosts = append(pinnedPosts, post.ToNet())
} else {
regularPosts = append(regularPosts, post.ToNet())
}
}
}
postsMutex.RUnlock()
}
// Sort posts by timestamp (newest first)
sort.Slice(pinnedPosts, func(i, j int) bool {
return pinnedPosts[i].Timestamp > pinnedPosts[j].Timestamp
})
sort.Slice(regularPosts, func(i, j int) bool {
return regularPosts[i].Timestamp > regularPosts[j].Timestamp
})
// Get follower count
followersMutex.RLock()
followerCount := 0
if data, exists := followersData[userId]; exists {
followerCount = len(data.Followers)
}
// Get following count
followingCount := 0
for _, data := range followersData {
if slices.Contains(data.Followers, userId) {
followingCount++
}
}
followersMutex.RUnlock()
// Initialize follow relationship info
var isFollowing *bool
var isFollowedBy *bool
if authKey != "" {
// Authenticate the user with the provided auth key
authenticatedUser := authenticateWithKey(authKey)
if authenticatedUser != nil {
authenticatedId := authenticatedUser.GetId()
// Check if the authenticated user is following the target user
followersMutex.RLock()
if data, exists := followersData[userId]; exists {
if slices.Contains(data.Followers, authenticatedId) {
following := true
isFollowing = &following
}
}
if isFollowing == nil {
notFollowing := false
isFollowing = ¬Following
}
// Check if the target user is following the authenticated user
if data, exists := followersData[authenticatedId]; exists {
if slices.Contains(data.Followers, userId) {
followedBy := true
isFollowedBy = &followedBy
}
}
if isFollowedBy == nil {
notFollowedBy := false
isFollowedBy = ¬FollowedBy
}
followersMutex.RUnlock()
}
}
maxSizeStr := foundUser.GetMaxSize()
sub := foundUser.GetSubscription().Tier
// Calculate dynamic badges
calculatedBadges := calculateUserBadges(foundUser)
st, err := loadUserStatus(foundUser.GetUsername())
if err != nil {
st = nil
}
profileData := profileResp{
Username: foundUser.GetUsername(),
Avatar: "https://avatars.rotur.dev/" + string(foundUser.GetUsername()),
Followers: followerCount,
Following: followingCount,
Pronouns: getStringOrEmpty(foundUser.Get("pronouns")),
System: foundUser.GetSystem(),
Created: foundUser.GetCreated(),
Badges: calculatedBadges,
Subscription: sub,
Theme: foundUser.GetTheme(),
MaxSize: maxSizeStr,
Currency: foundUser.GetCredits(),
Index: userIndex + 1,
Private: foundUser.IsPrivate(),
Status: st,
Id: foundUser.GetId(),
}
benefits := foundUser.GetSubscriptionBenefits()
bio := getStringOrEmpty(foundUser.Get("bio"))
if bio != "" && benefits.Has_Bio_templating {
bio = renderBioRegex(bio, foundUser, profileData)
}
if len(bio) > benefits.Bio_Length {
bio = bio[:benefits.Bio_Length]
}
profileData.Bio = bio
if foundUser.Get("sys.banner") != nil || foundUser.Get("banner") != nil {
profileData.Banner = "https://avatars.rotur.dev/.banners/" + string(foundUser.GetUsername())
}
if includePosts {
// Combine pinned and regular posts (pinned first, then regular, both in reverse chronological order)
allUserPosts := make([]NetPost, 0, len(pinnedPosts)+len(regularPosts))
for i := 0; i < len(pinnedPosts); i++ {
allUserPosts = append(allUserPosts, pinnedPosts[i])
}
for i := 0; i < len(regularPosts); i++ {
allUserPosts = append(allUserPosts, regularPosts[i])
}
profileData.Posts = allUserPosts
}
// Add follow relationship info if available
if authKey != "" && isFollowing != nil {
profileData.Followed = *isFollowing
profileData.FollowsMe = *isFollowedBy
}
c.JSON(200, profileData)
}
func getExists(c *gin.Context) {
type resp struct {
Exists bool `json:"exists"`
}
username := Username(c.Query("username")).ToLower()
if username == "" {
c.JSON(400, gin.H{"error": "Username is required"})
return
}
usersMutex.RLock()
defer usersMutex.RUnlock()
for _, user := range users {
if user.GetUsername().ToLower() == username {
c.JSON(200, resp{Exists: true})
return
}
}
c.JSON(200, resp{Exists: false})
}
func getSupporters(c *gin.Context) {
// anyone who isnt a free user is considered a supporter
type resp struct {
Username Username `json:"username"`
Subscription string `json:"subscription"`
}
supporters := make([]resp, 0)
usersMutex.RLock()
defer usersMutex.RUnlock()
for _, user := range users {
subscriptionName := user.GetSubscription().Tier
if subscriptionName == "Free" {
continue
}
supporters = append(supporters, resp{
Username: user.GetUsername(),
Subscription: subscriptionName,
})
}
c.JSON(200, supporters)
}