@@ -32,7 +32,7 @@ export class WebhookController {
32
32
const schemaId = req . body . schemaId ;
33
33
const globalId = req . body . id ;
34
34
const mapping = Object . values ( this . adapter . mapping ) . find (
35
- ( m ) => m . schemaId === schemaId ,
35
+ ( m ) => m . schemaId === schemaId
36
36
) ;
37
37
this . adapter . addToLockedIds ( globalId ) ;
38
38
@@ -47,20 +47,38 @@ export class WebhookController {
47
47
let localId = await this . adapter . mappingDb . getLocalId ( globalId ) ;
48
48
49
49
if ( mapping . tableName === "users" ) {
50
- const { user } = await this . userService . findOrCreateUser (
51
- req . body . w3id ,
52
- ) ;
53
- for ( const key of Object . keys ( local . data ) ) {
54
- // @ts -ignore
55
- user [ key ] = local . data [ key ] ;
50
+ if ( localId ) {
51
+ const user = await this . userService . findById ( localId ) ;
52
+ for ( const key of Object . keys ( local . data ) ) {
53
+ // @ts -ignore
54
+ user [ key ] = local . data [ key ] ;
55
+ }
56
+ if ( ! user ) throw new Error ( ) ;
57
+ user . name = req . body . data . displayName ;
58
+ await this . userService . userRepository . save ( user ) ;
59
+ await this . adapter . mappingDb . storeMapping ( {
60
+ localId : user . id ,
61
+ globalId : req . body . id ,
62
+ } ) ;
63
+ this . adapter . addToLockedIds ( user . id ) ;
64
+ this . adapter . addToLockedIds ( globalId ) ;
65
+ } else {
66
+ const { user } = await this . userService . findOrCreateUser (
67
+ req . body . w3id
68
+ ) ;
69
+ for ( const key of Object . keys ( local . data ) ) {
70
+ // @ts -ignore
71
+ user [ key ] = local . data [ key ] ;
72
+ }
73
+ user . name = req . body . data . displayName ;
74
+ await this . userService . userRepository . save ( user ) ;
75
+ await this . adapter . mappingDb . storeMapping ( {
76
+ localId : user . id ,
77
+ globalId : req . body . id ,
78
+ } ) ;
79
+ this . adapter . addToLockedIds ( user . id ) ;
80
+ this . adapter . addToLockedIds ( globalId ) ;
56
81
}
57
- user . name = req . body . data . displayName ;
58
- await this . userService . userRepository . save ( user ) ;
59
- await this . adapter . mappingDb . storeMapping ( {
60
- localId : user . id ,
61
- globalId : req . body . id ,
62
- } ) ;
63
- this . adapter . addToLockedIds ( user . id ) ;
64
82
} else if ( mapping . tableName === "posts" ) {
65
83
let author : User | null = null ;
66
84
if ( local . data . author ) {
@@ -79,10 +97,10 @@ export class WebhookController {
79
97
return await this . userService . findById ( userId ) ;
80
98
}
81
99
return null ;
82
- } ,
100
+ }
83
101
) ;
84
102
likedBy = ( await Promise . all ( likedByPromises ) ) . filter (
85
- ( user ) : user is User => user !== null ,
103
+ ( user ) : user is User => user !== null
86
104
) ;
87
105
}
88
106
@@ -100,13 +118,13 @@ export class WebhookController {
100
118
comment . author = author as User ;
101
119
comment . post = parent as Post ;
102
120
await this . commentService . commentRepository . save (
103
- comment ,
121
+ comment
104
122
) ;
105
123
} else {
106
124
const comment = await this . commentService . createComment (
107
125
parent ?. id as string ,
108
126
author ?. id as string ,
109
- local . data . text as string ,
127
+ local . data . text as string
110
128
) ;
111
129
localId = comment . id ;
112
130
await this . adapter . mappingDb . storeMapping ( {
@@ -128,14 +146,14 @@ export class WebhookController {
128
146
. split ( "(" ) [ 1 ]
129
147
. split ( ")" ) [ 0 ] ;
130
148
return await this . userService . findById (
131
- userId ,
149
+ userId
132
150
) ;
133
151
}
134
152
return null ;
135
- } ,
153
+ }
136
154
) ;
137
155
likedBy = ( await Promise . all ( likedByPromises ) ) . filter (
138
- ( user ) : user is User => user !== null ,
156
+ ( user ) : user is User => user !== null
139
157
) ;
140
158
}
141
159
@@ -160,7 +178,7 @@ export class WebhookController {
160
178
{
161
179
...local . data ,
162
180
likedBy,
163
- } ,
181
+ }
164
182
) ;
165
183
166
184
this . adapter . addToLockedIds ( post . id ) ;
@@ -192,7 +210,7 @@ export class WebhookController {
192
210
return await this . userService . findById ( userId ) ;
193
211
}
194
212
return null ;
195
- } ,
213
+ }
196
214
) ;
197
215
participants = (
198
216
await Promise . all ( participantPromises )
@@ -212,7 +230,7 @@ export class WebhookController {
212
230
} else {
213
231
const chat = await this . chatService . createChat (
214
232
local . data . name as string ,
215
- participants . map ( ( p ) => p . id ) ,
233
+ participants . map ( ( p ) => p . id )
216
234
) ;
217
235
218
236
this . adapter . addToLockedIds ( chat . id ) ;
@@ -262,7 +280,7 @@ export class WebhookController {
262
280
const message = await this . chatService . sendMessage (
263
281
chat . id ,
264
282
sender . id ,
265
- local . data . text as string ,
283
+ local . data . text as string
266
284
) ;
267
285
268
286
this . adapter . addToLockedIds ( message . id ) ;
0 commit comments