forked from TeamCodeStream/codestream-server
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathread_request.js
More file actions
114 lines (105 loc) · 3.18 KB
/
Copy pathread_request.js
File metadata and controls
114 lines (105 loc) · 3.18 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
// handle the "PUT /read/:streamId" request to indicate the user is "caught up"
// on reading the posts in a particular stream
'use strict';
const RestfulRequest = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/lib/util/restful/restful_request.js');
const ModelSaver = require(process.env.CSSVC_BACKEND_ROOT + '/api_server/lib/util/restful/model_saver');
class ReadRequest extends RestfulRequest {
// authorize the request before processing....
async authorize () {
// they must have access to the stream, unless "all" is specified
const streamId = this.request.params.streamId.toLowerCase();
if (streamId === 'all') {
// all doesn't need authorization, it applies only to the current user
return;
}
const authorized = await this.user.authorizeStream(streamId, this);
if (!authorized) {
throw this.errorHandler.error('updateAuth', { reason: 'user not in stream' });
}
}
// process the request...
async process () {
// unset the lastReads value for the given stream, or simply remove the lastReads
// value completely if "all" specified
this.streamId = this.request.params.streamId.toLowerCase();
let op = {
$set: {
modifiedAt: Date.now()
}
};
if (this.streamId === 'all') {
op = {
'$unset': {
lastReads: true
}
};
}
else {
op = {
'$unset': {
['lastReads.' + this.streamId]: true
}
};
}
this.updateOp = await new ModelSaver({
request: this,
collection: this.data.users,
id: this.user.id
}).save(op);
}
async handleResponse () {
if (this.gotError) {
return await super.handleResponse();
}
this.responseData = { user: this.updateOp };
super.handleResponse();
}
// after the response is returned....
async postProcess () {
// send the lastReads update on the user's me-channel, so other active
// sessions get the message
const channel = 'user-' + this.user.id;
const message = Object.assign({}, this.responseData, { requestId: this.request.id });
Object.assign(message.user, this.op);
try {
await this.api.services.broadcaster.publish(
message,
channel,
{ request: this }
);
}
catch (error) {
// this doesn't break the chain, but it is unfortunate
this.warn(`Unable to publish lastReads message to channel ${channel}: ${JSON.stringify(error)}`);
}
}
// describe this route for help
static describe () {
return {
tag: 'read',
summary: 'Indicates user has read all messages in a stream',
access: 'User must have access to the given stream (for public streams, must be in the team that owns the stream, for private streams, must be in the stream)',
description: 'Indicates user has read all messages in a stream (or all streams, if \'all\' is specified)',
input: 'Specify ID of the stream in the path',
returns: 'Empty object',
publishes: {
summary: 'Publishes a user object, with directives, to the user\'s user channel, indicating how the lastReads attribute for the user object should be updated',
looksLike: {
user: {
id: '<ID of the user>',
$unset: {
lastReads: {
['<streamId>']: true
}
}
}
}
},
errors: [
'notFound',
'updateAuth'
]
};
}
}
module.exports = ReadRequest;