forked from jschirrmacher/netvis-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathRoomInfoReader.js
105 lines (96 loc) · 3.21 KB
/
RoomInfoReader.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
/* eslint-env node */
const filterWords = [
...require('stopwords-de'),
...require('stopwords-en')
]
module.exports = function ({db, client, logger}) {
async function getRoomWords(rooms) {
const roomWords = Object.assign({}, ...rooms.map(room => ({[room._id]: {}})))
const cursor = await db.rocketchat_message.findWithCursor({}, {msg: 1, rid: 1})
while (await cursor.hasNext()) {
const doc = await cursor.next()
if (doc.msg && roomWords[doc.rid] && doc.t !== 'uj') {
doc.msg.split(' ')
.filter(word => !word.match(/^@/)) // filter user names
.filter(word => !word.match(/^http?s:\/\//))
.map(word => word.replace(/[^A-Za-z\u00C0-\u017F]/g, '').toLowerCase()) // remove non-alpha-chars
.filter(word => word.length > 1)
.filter(word => !word.match(/^\d*$/)) // filter numbers
.filter(word => !filterWords.includes(word)) // filter stopwords
.map(word => {
roomWords[doc.rid] = roomWords[doc.rid] || {}
roomWords[doc.rid][word] = roomWords[doc.rid][word] || 0
roomWords[doc.rid][word]++
})
}
}
return roomWords;
}
return {
async getRoomsWithTopics() {
const rooms = await db.rocketchat_room.find({t: 'c'})
const roomWords = await getRoomWords(rooms)
logger.debug(rooms.map(r => r.name))
const words = {}
Object.keys(roomWords).forEach(roomId => {
roomWords[roomId] = Object.keys(roomWords[roomId])
.map(word => ({word, num: roomWords[roomId][word]}))
.sort((a, b) => b.num - a.num)
.slice(0, 20)
roomWords[roomId].forEach(e => {
words[e.word] = (words[e.word] || 0) + e.num
})
})
const nodes = []
Object.keys(words).forEach(word => {
nodes.push({
id: 'w_' + word,
name: word,
type: 'topic',
links: {
rooms: []
}
})
})
rooms.forEach(room => {
const topics = roomWords[room._id].map(info => {
const id = 'w_' + info.word;
const node = nodes.find(n => n.id === id)
if (node) {
node.links.rooms.push(room._id)
}
return id
})
const radius = Math.sqrt(room.usersCount * 10) + 20
if (room.parentRoomId && !rooms.find(r => r._id === room.parentRoomId)) {
delete room.parentRoomId
}
const node = {
id: room._id,
name: room.name,
type: 'room',
shape: 'circle',
radius,
url: 'https://' + client + '.assistify.noncd.db.de/channel/' + room.name,
links: {topics},
visibility: Math.sqrt(room.usersCount)
}
if (room.parentRoomId) {
node.links.parents = [room.parentRoomId]
}
nodes.push(node)
})
nodes.forEach(n => {
if (n.type === 'topic') {
const size = Math.sqrt(n.links.rooms.length * 200) + 100
n.width = size
n.height = size * 0.7
n.fontSize = Math.sqrt(n.links.rooms.length * 3) / 2
n.visibility = n.links.rooms.length
n.shape = 'rect'
}
})
return nodes
}
}
}