-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathapps.osl
More file actions
212 lines (178 loc) · 5.56 KB
/
apps.osl
File metadata and controls
212 lines (178 loc) · 5.56 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
def handleAppInfo(*gin.Context c) (
string appname = c.Param("appname").toLower()
object app = getAppInfo(appname)
if app.ok == false (
c.JSON(404, app)
return
)
app.info.authors = migrateAuthorNamesToUsernames(app.info.authors.toArray())
app.info.author = app.info.authors.join(", ")
object stats = fs.ReadFile("./stats.json").JsonParse()
object views = stats.views
views[appname] = views[appname].toNum() + 1
stats.views = views
fs.WriteFile("./stats.json", stats.JsonFormat())
c.JSON(200, app)
)
def handleAppIcon(*gin.Context c) (
c.File(fs.JoinPath("./apps", c.Param("appname").toLower(), "icon.icn"))
)
def handleOwnedApps(*gin.Context c) (
string userId = c.MustGet("userId")
if userId == "" (
c.JSON(401, {"error": "Unauthorized"})
return
)
object ownerships = readOwnerships()
array appNames = ownerships.getKeys()
array ownedApps = []
for i appNames.len (
string name = appNames[i]
if ownerships[name].contains(userId) (
ownedApps.append(name)
)
)
c.JSON(200, {"ownedApps": ownedApps.sort()})
)
def handleAppFile(*gin.Context c) (
string appname = c.Param("appname").toLower()
string fileType = c.Param("filetype")
string path = fs.JoinPath("./apps", appname, "script." ++ fileType.toLower())
boolean exists = fs.Exists(path)
app := getAppInfo(appname)
if !exists or app.ok == false (
c.JSON(404, {
ok: false,
error: "app not found",
})
return
)
info := app.info
string userId = c.MustGet("userId").toStr().toLower()
string token = c.Query("auth")
object ownerships = readOwnerships()
rawOwners := ownerships[appname]
if rawOwners == null (
rawOwners = []
)
array owners = rawOwners
boolean alreadyOwns = false
if userId != "" (
alreadyOwns = owners.contains(userId.toLower())
)
array authors = info.authors
if authors.len == 0 (
c.JSON(400, { ok: false, error: "app has no authors" })
return
)
for i authors.len (
string author = authors[i]
if author == userId (
alreadyOwns = true
)
)
if info.price != null and info.price > 0 and alreadyOwns == false (
if token == "" (
c.JSON(401, { ok: false, error: "missing auth token" })
return
)
string authorUsername = getUsernameFromUserId(info.authors[1].toStr())
if authorUsername == "" (
authorUsername = info.authors[1].toStr()
)
object resp = requests.Post("https://api.rotur.dev/me/transfer?auth=" ++ token, {
body: {
amount: info.price.toNum(),
to: authorUsername,
note: "App store: " ++ info.title.toStr() ++ " purchase",
}
})
if resp.success == false or resp.status != 200 (
string errorMsg = resp.body.toStr().JsonParse().error.toStr()
if errorMsg.contains("insufficient funds") (
errorMsg = "insufficient funds"
)
c.JSON(400, { ok: false, error: "failed to purchase: " ++ errorMsg })
return
)
)
if userId != "" and alreadyOwns == false (
owners.append(userId.toLower())
ownerships[appname] = owners
writeOwnerships(ownerships)
)
object stats = fs.ReadFile("./stats.json").JsonParse()
object downloads = stats.downloads
downloads[appname] = downloads[appname].toNum() + 1
stats.downloads = downloads
fs.WriteFile("./stats.json", stats.JsonFormat())
c.File(path)
)
def handleAllApps(*gin.Context c) (
string token = c.Query("auth")
string userId = ""
log token
if token != "" (
array parts = token.split(",")
if parts.len >= 2 (
userId = parts[2].toStr().toLower()
)
)
object resp = getAllApps(false)
if resp.ok == false (
c.JSON(500, resp)
return
)
if userId != "" (
object ownerships = readOwnerships()
object apps = resp.apps
array names = apps.getKeys()
for i names.len (
string n = names[i]
array owners = ownerships[n]
boolean owns = owners != null and owners.contains(userId)
apps[n].owns = owns
)
resp.apps = apps
)
c.JSON(200, resp)
)
def handleAppScreenshot(*gin.Context c) (
string appname = c.Param("appname").toLower()
string screenshot = c.Param("screenshot")
string path = fs.JoinPath("./apps", appname, "screenshots", screenshot)
boolean exists = fs.Exists(path)
if !exists (
c.JSON(404, {
ok: false,
error: "app not found",
})
return
)
c.File(path)
)
def handleFeatured(*gin.Context c) (
object featured = loadConfig("featured.json")
if featured.featured == null (
c.JSON(404, {
ok: false,
error: "featured app not found",
})
return
)
c.JSON(200, featured)
)
def handleGetAppReviews(*gin.Context c) (
string appname = c.Param("appname").toLower()
c.JSON(200, getReviewsWithUsernames(appname))
)
def handleAppReview(*gin.Context c) (
string appname = c.Param("appname").toLower()
string userId = c.MustGet("userId")
string review = c.Query("content")
if review == "" (
c.JSON(400, { ok: false, error: "Missing content" })
)
writeReview(appname, userId, review)
c.JSON(200, { ok: true, error: "Successfully added review" })
)