-
Notifications
You must be signed in to change notification settings - Fork 2
/
client.ts
294 lines (274 loc) · 6.58 KB
/
client.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
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
import {
gql,
request,
} from "https://deno.land/x/[email protected]/mod.ts";
import { Event, SolaGroup } from "./types.ts";
export class SocialLayerClient {
graphUrl = "https://graph.sola.day/v1/graphql";
async getEventsRaw(
groupIds: number[],
startISO: string,
endISO: string,
limit: number,
offset: number,
): Promise<{ events: Event[]; hasNextPage: boolean }> {
// Debugging logs
console.log(startISO, endISO);
const query = gql`
query ($groupIds: [Int!], $limit: Int, $offset: Int, $start: timestamp, $end: timestamp) {
events(
where: {
start_time: { _lte: $end }
end_time: { _gte: $start }
group_id: { _in: $groupIds }
status: { _in: ["open", "new", "normal"] }
}
order_by: { start_time: asc }
limit: $limit
offset: $offset
) {
id
title
content
cover_url
tags
start_time
end_time
location
max_participant
min_participant
host_info
meeting_url
event_site {
id
title
location
}
group_id
owner {
username
}
notes
category
recurring_event {
id
interval
start_time
end_time
timezone
}
timezone
geo_lng
geo_lat
participants {
id
profile {
username
}
}
external_url
}
}
`;
const variables = {
groupIds,
limit: limit + 1,
offset,
start: startISO,
end: endISO,
};
try {
const response = await request(
this.graphUrl,
query,
variables,
);
const events = response.events;
const hasNextPage = events.length > limit;
if (hasNextPage) {
events.pop();
}
return { events, hasNextPage };
} catch (error) {
console.error("Error fetching events:", error);
return { events: [], hasNextPage: false };
}
}
async getEvents(
groupIds: number[],
startDateInput: string,
endDateInput: string,
limit: number,
offset: number,
): Promise<{ events: Event[]; hasNextPage: boolean }> {
const groupInfo = await this.getGroupTimestamp(groupIds);
const timezone = groupInfo.length === 0 ? "Asia/Shanghai" : groupInfo[0];
// Parse date inputs
const startDate = parseDate(startDateInput, timezone);
const endDate = parseDate(endDateInput, timezone);
const startISO = startDate.toISOString();
const endISO = endDate.toISOString();
const res = await this.getEventsRaw(
groupIds,
startISO,
endISO,
limit,
offset,
);
return res;
}
async getTodaysEvents(
groupIds: number[],
limit: number,
offset: number,
): Promise<{ events: Event[]; hasNextPage: boolean }> {
const res = await this.getEvents(
groupIds,
"today",
"tomorrow",
limit,
offset,
);
return res;
}
async queryGroup(name: string): Promise<number | undefined> {
const query = gql`
query($name: String) {
groups(where: {username: {_eq: $name}}) {
id
}
}
`;
const variables = {
name,
};
try {
const response = await request<{ groups: { id: number }[] }>(
this.graphUrl,
query,
variables,
);
const group = response.groups.pop()?.id;
return group;
} catch (error) {
console.error(`Error query group ${name} with:`, error);
return undefined;
}
}
async listGroups(): Promise<
SolaGroup[]
> {
const query = gql`{
groups(where: {events_count: {_gt: 0}}, order_by: {events_count: asc}) {
username
id
events_count
}
}`;
try {
const response = await request<
{ groups: { username: string; id: number; events_count: number }[] }
>(
this.graphUrl,
query,
);
const groups = response.groups.map((value) => {
const group: SolaGroup = {
name: value.username,
events_count: value.events_count,
id: value.id,
};
return group;
});
return groups;
} catch (error) {
console.error(`Error list group with:`, error);
return [];
}
}
async getGroupInfos(
ids: number[],
): Promise<{ username: string; timezone: string }[]> {
const query = gql`
query($ids:[bigint!]) {
groups(where: {id: {_in: $ids}}, order_by: {events_count: asc}) {
username
timezone
}
}
`;
const variables = {
ids,
};
try {
const response = await request<
{ groups: { username: string; timezone: string }[] }
>(
this.graphUrl,
query,
variables,
);
return response.groups;
} catch (error) {
console.error(`Error get group names with:`, error);
return [];
}
}
async getGroupTimestamp(ids: number[]): Promise<string[]> {
const query = gql`
query($ids:[bigint!]) {
groups(where: {id: {_in: $ids}}, order_by: {events_count: asc}) {
timezone
}
}
`;
const variables = {
ids,
};
try {
const response = await request<
{ groups: { timezone: string }[] }
>(
this.graphUrl,
query,
variables,
);
const groups = response.groups.map((value) => {
return value.timezone;
});
return groups;
} catch (error) {
console.error(`Error get group timestamps with:`, error);
return [];
}
}
}
// Helper function to parse natural language dates
export function parseDate(input: string, timezone: string): Date {
const now = new Date();
const today = new Date(now.toLocaleString("en-US", { timeZone: timezone }));
let date: Date;
switch (input.toLowerCase()) {
case "today":
date = today;
break;
case "tomorrow":
date = new Date(today);
date.setDate(today.getDate() + 1);
break;
default: {
// Handle specific date inputs
const parsedDate = new Date(input);
if (!isNaN(parsedDate.getTime())) {
date = parsedDate;
// If no year is provided, use the current year
if (input.match(/^\d{1,2}-\d{1,2}$/)) {
date.setFullYear(today.getFullYear());
}
} else {
throw new Error("Invalid date input");
}
}
}
return date;
}