forked from djyde/cusdis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdata.service.ts
138 lines (127 loc) · 3.14 KB
/
data.service.ts
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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
import { prisma } from '../utils.server'
const parser = require('xml2json')
import TurndownService from 'turndown'
import { statService } from './stat.service'
const turndownService = new TurndownService()
export type DataSchema = {
pages: Array<{
uniqueId: string
pageId: string
url?: string
title?: string
}>
comments: Array<{
id: string
content: string
createdAt: string
by_nickname: string
by_email?: string
pageUniqueId: string
parentId: string
}>
}
export class DataService {
disqusAdapter(xmlData: string): DataSchema {
const parsed = JSON.parse(parser.toJson(xmlData)).disqus
const threads = (parsed.thread.filter(
(_) => typeof _.id === 'string' && _.isDeleted === 'false',
) as Array<{
'dsq:id': string
id: string
link: string
title: string
createdAt: string
isDeleted: string
}>).map((_) => {
return {
uniqueId: _['dsq:id'],
pageId: _.id,
url: _.link,
title: _.title,
} as DataSchema['pages'][0]
})
const posts = (parsed.post as Array<{
'dsq:id': string
message: string
createdAt: string
isDeleted: string
thread: {
'dsq:id': string
}
author: {
name: string
isAnonymous: string
username: string
}
parent?: {
'dsq:id': string
}
}>)
.map((_) => {
return {
pageUniqueId: _.thread['dsq:id'],
by_nickname: _.author.name,
content: turndownService.turndown(_.message),
id: _['dsq:id'],
createdAt: _.createdAt,
pageId: _.thread['dsq:id'],
parentId: _.parent?.['dsq:id'],
} as DataSchema['comments'][0]
})
.filter(
(post) =>
threads.findIndex((_) => _.uniqueId === post.pageUniqueId) !==
-1,
)
return {
pages: threads,
comments: posts,
}
}
async import(projectId: string, schema: DataSchema) {
const pages = await prisma.$transaction(
schema.pages.map((thread) => {
return prisma.page.upsert({
where: {
id: thread.uniqueId,
},
create: {
id: thread.uniqueId,
projectId,
slug: thread.pageId,
url: thread.url,
title: thread.title,
},
update: {},
})
}),
)
const upsertedPosts = await prisma.$transaction(
schema.comments.map((post) => {
return prisma.comment.upsert({
where: {
id: post.id,
},
create: {
id: post.id,
content: post.content,
createdAt: post.createdAt,
by_nickname: post.by_nickname,
pageId: post.pageUniqueId,
parentId: post.parentId,
},
update: {},
})
}),
)
return {
threads: pages,
posts: upsertedPosts
}
}
async importFromDisqus(projectId: string, xmlData: string) {
const result = await this.import(projectId, this.disqusAdapter(xmlData))
statService.capture('import_disqus')
return result
}
}