forked from ubc/canvas-discussion
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
67 lines (61 loc) · 2.03 KB
/
index.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
const capi = require('node-canvas-api')
const { flatten } = require('./util')
const writeToCSV = require('./writeToCSV')
const getDiscussionTopicIds = courseId => capi.getDiscussionTopics(courseId)
.then(discussions => discussions.map(x => x.id))
// recursively get nested replies and flatten result
const getNestedReplies = (replyObj, participants, topicId) => {
const replies = replyObj.hasOwnProperty('replies')
? flatten(
replyObj.replies.map(replyObj => getNestedReplies(replyObj, participants))
) : []
const authorName = participants.find(x => x.id === replyObj.user_id)
? participants.find(x => x.id === replyObj.user_id).display_name
: ''
return [{
authorId: replyObj.user_id,
authorName: authorName,
message: replyObj.message,
likes: replyObj.rating_sum,
timestamp: replyObj.created_at,
parentId: replyObj.parent_id || topicId,
id: replyObj.id
}, ...replies]
}
const getDiscussions = async courseId => {
const discussionTopicIds = await getDiscussionTopicIds(courseId)
const discussionAndTopic = await Promise.all(
discussionTopicIds
.map(topicId => Promise.all([
capi.getFullDiscussion(courseId, topicId),
capi.getDiscussionTopic(courseId, topicId)
]))
)
return discussionAndTopic.map(([discussion, topic]) => {
const topicTitle = topic.title
const topicMessage = topic.message
const author = topic.author
const timestamp = topic.created_at
const topicId = topic.id
const participants = discussion.participants
const replies = discussion.view.length > 0
? discussion.view
.filter(x => !x.deleted)
.map(reply => getNestedReplies(reply, participants, topicId))
: []
return {
topicTitle,
topicMessage,
id: topicId,
authorId: author.id || '',
authorName: author.display_name || '',
timestamp,
replies
}
})
}
Promise.all([
//{course id} add course ID here!
].map(courseId => getDiscussions(courseId)
.then(discussions => writeToCSV(courseId, discussions))
))