forked from outline/outline
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathevents.js
123 lines (113 loc) · 2.75 KB
/
events.js
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
// @flow
import Queue from 'bull';
import services from './services';
export type UserEvent =
| {
name: | 'users.create' // eslint-disable-line
| 'users.update'
| 'users.suspend'
| 'users.activate'
| 'users.delete',
userId: string,
teamId: string,
actorId: string,
}
| {
name: 'users.invite',
teamId: string,
actorId: string,
data: {
email: string,
name: string,
},
};
export type DocumentEvent =
| {
name: | 'documents.create' // eslint-disable-line
| 'documents.publish'
| 'documents.delete'
| 'documents.pin'
| 'documents.unpin'
| 'documents.archive'
| 'documents.unarchive'
| 'documents.restore'
| 'documents.star'
| 'documents.unstar',
documentId: string,
collectionId: string,
teamId: string,
actorId: string,
}
| {
name: 'documents.move',
documentId: string,
collectionId: string,
teamId: string,
actorId: string,
data: {
collectionIds: string[],
documentIds: string[],
},
}
| {
name: 'documents.update',
documentId: string,
collectionId: string,
teamId: string,
actorId: string,
data: {
autosave: boolean,
done: boolean,
},
};
export type CollectionEvent =
| {
name: | 'collections.create' // eslint-disable-line
| 'collections.update'
| 'collections.delete',
collectionId: string,
teamId: string,
actorId: string,
}
| {
name: 'collections.add_user' | 'collections.remove_user',
userId: string,
collectionId: string,
teamId: string,
actorId: string,
};
export type IntegrationEvent = {
name: 'integrations.create' | 'integrations.update',
modelId: string,
teamId: string,
actorId: string,
};
export type Event =
| UserEvent
| DocumentEvent
| CollectionEvent
| IntegrationEvent;
const globalEventsQueue = new Queue('global events', process.env.REDIS_URL);
const serviceEventsQueue = new Queue('service events', process.env.REDIS_URL);
// this queue processes global events and hands them off to service hooks
globalEventsQueue.process(async job => {
const names = Object.keys(services);
names.forEach(name => {
const service = services[name];
if (service.on) {
serviceEventsQueue.add(
{ service: name, ...job.data },
{ removeOnComplete: true }
);
}
});
});
// this queue processes an individual event for a specific service
serviceEventsQueue.process(async job => {
const event = job.data;
const service = services[event.service];
if (service.on) {
service.on(event);
}
});
export default globalEventsQueue;