-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathAppwriteService.ts
228 lines (202 loc) Β· 5.11 KB
/
AppwriteService.ts
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
import { ID, type Models } from "appwrite";
import {
Account,
Client,
Databases,
Functions,
Query,
Storage,
} from "appwrite";
// Manually kept in sync with Appwrite collection attributes
export type Project = {
platform: string;
name: string;
tagline: string;
description: string;
upvotes: number;
framework?: string;
uiLibrary?: string;
useCase: string;
urlWebsite?: string;
urlArticle?: string;
urlTwitter?: string;
urlGitHub?: string;
urlWindows?: string;
urlMacOs?: string;
urlLinux?: string;
urlAppStore?: string;
urlGooglePlay?: string;
imageId: string;
hasAuthentication: boolean;
hasMessaging: boolean;
hasStorage: boolean;
hasRealtime: boolean;
hasFunctions: boolean;
hasDatabases: boolean;
} & Models.Document;
export type ProjectService = {
projectId: string;
service: string;
} & Models.Document;
export type ProjectUpvote = {
projectId: string;
userId: string;
} & Models.Document;
const client = new Client();
client
.setEndpoint("https://cloud.appwrite.io/v1")
.setProject("builtWithAppwrite");
const account = new Account(client);
const storage = new Storage(client);
const databases = new Databases(client);
const functions = new Functions(client);
export const AppwriteService = {
signIn: () => {
const redirectUrl = window.location.href;
account.createOAuth2Session("github", redirectUrl, redirectUrl);
},
signOut: async () => {
await account.deleteSession("current");
},
getAccount: async () => {
try {
return await account.get();
} catch (err) {
// console.log(err);
return null;
}
},
countProjects: async (queries: string[]) => {
const hasIsPublished = queries.find((query) =>
query.startsWith('equal("isPublished')
);
const hasCreatedAtSort = queries.find(
(query) =>
query.startsWith('orderDesc("$createdAt') ||
query.startsWith('orderAsc("$createdAt')
);
if (!hasIsPublished) {
queries.push(Query.equal("isPublished", true));
}
if (!hasCreatedAtSort) {
queries.push(Query.orderDesc("$createdAt"));
}
const response = await databases.listDocuments<Project>(
"main",
"projects",
queries
);
return response.total;
},
getProject: async (projectId: string) => {
const project = await databases.getDocument<Project>(
"main",
"projects",
projectId
);
return project;
},
listProjects: async (queries: string[]) => {
const hasIsPublished = queries.find((query) =>
query.startsWith('equal("isPublished')
);
const hasCreatedAtSort = queries.find(
(query) =>
query.startsWith('orderDesc("$createdAt') ||
query.startsWith('orderAsc("$createdAt')
);
if (!hasIsPublished) {
queries.push(Query.equal("isPublished", true));
}
if (!hasCreatedAtSort) {
queries.push(Query.orderDesc("$createdAt"));
}
const { documents: projects } = await databases.listDocuments<Project>(
"main",
"projects",
queries
);
return projects;
},
searchProjects: async (searchQuery: string) => {
const { documents: projects } = await databases.listDocuments<Project>(
"main",
"projects",
[Query.search("search", searchQuery)]
);
return projects.filter((project) => project.isPublished);
},
getProjectThumbnail: (fileId: string, width = 1280) => {
return storage
.getFilePreview(
"thumbnails",
fileId,
width,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
undefined,
"webp"
)
.toString();
},
upvoteProject: async (projectId: string) => {
const execution = await functions.createExecution(
"upvoteProject",
projectId
);
if (!execution.response) {
throw new Error("Unexpected error occured.");
}
const json = JSON.parse(execution.response);
if (json.ok === false) {
throw new Error(json.msg);
}
return json;
},
listUpvotes: async (queries: string[]) => {
return (
await databases.listDocuments<ProjectUpvote>(
"main",
"projectUpvotes",
queries
)
).documents;
},
listUserUpvotes: async (userId: string, queries: string[] = []) => {
const defaultQueries = [
Query.equal("userId", userId),
Query.orderDesc("$createdAt"),
];
queries = [...queries, ...defaultQueries];
return (
await databases.listDocuments<ProjectUpvote>(
"main",
"projectUpvotes",
queries
)
).documents;
},
uploadThumbnail: async (file: File) => {
return await storage.createFile("thumbnails", ID.unique(), file);
},
submitProject: async (data: any) => {
const execution = await functions.createExecution(
"submitProject",
JSON.stringify(data)
);
if (!execution.response) {
throw new Error("Unexpected error occured.");
}
const json = JSON.parse(execution.response);
if (json.ok === false) {
throw new Error(json.msg);
}
return json;
},
};