UNPKG

1.9 kBJavaScriptView Raw
1module.exports = (sbot, nameCache) => {
2
3 const chatMessageType = config.chatMessageType;
4
5 function messagesSource(gameId, previousChatId, isPublic) {
6 var linksFromRootMessage = sbot.backlinks.read({
7 query: [{
8 $filter: {
9 dest: gameId
10 }
11 }],
12 index: 'DTA', // use asserted timestamps
13 live: true
14 });
15
16 var typeFilter = pull.filter(msg => {
17 return !msg.sync && msg.value.content.type === chatMessageType
18 });
19
20 var privateOnlyFilter = pull.filter(msg =>
21 isPublic || participants.indexOf(msg.value.author) !== -1
22 )
23
24 var previousChatMessages = !previousChatId ? pull.empty() : sbot.backlinks.read({
25 query: [{
26 $filter: {
27 dest: previousChatId
28 }
29 }],
30 index: 'DTA', // use asserted timestamps
31 })
32
33 var oldChatStream = pull(previousChatMessages, pull(typeFilter, privateOnlyFilter), pull.map(msg => {
34 msg.isOld = true;
35 return msg;
36 }))
37
38
39 var newChatStream = pull(linksFromRootMessage, pull(typeFilter, privateOnlyFilter));
40
41 return pullCat([oldChatStream, newChatStream]);
42 }
43
44 function getNameAndChatMessage(msg, cb) {
45 const message = msg.value.content[chatMessageField];
46 const authorId = msg.value.author;
47
48 nameCache.getDisplayName(authorId, (err, res) => cb(null, {
49 displayName: res,
50 message,
51 isOld: msg.isOld
52 }));
53 }
54
55 function getChatMessages(gameId, previousChatId, isPublic) {
56 return pull(
57 messagesSource(gameId, previousChatId, isPublic),
58 aborter,
59 pull.asyncMap(getNameAndChatMessage)
60 )
61 }
62
63
64 return {
65 getChatMessages
66 }
67}
\No newline at end of file