1
1
import { LoggerBase , logExecutionTime } from '@crowd/logging'
2
2
import { Blob } from 'buffer'
3
+ import vader from 'crowd-sentiment'
3
4
import { Transaction } from 'sequelize/types'
4
5
import { PlatformType } from '@crowd/types'
5
- import { IS_DEV_ENV , IS_TEST_ENV } from '../conf'
6
+ import { IS_DEV_ENV , IS_TEST_ENV , GITHUB_CONFIG } from '../conf'
6
7
import ActivityRepository from '../database/repositories/activityRepository'
7
8
import MemberAttributeSettingsRepository from '../database/repositories/memberAttributeSettingsRepository'
8
9
import MemberRepository from '../database/repositories/memberRepository'
@@ -20,6 +21,8 @@ import MemberService from './memberService'
20
21
import SegmentRepository from '../database/repositories/segmentRepository'
21
22
import SegmentService from './segmentService'
22
23
24
+ const IS_GITHUB_COMMIT_DATA_ENABLED = GITHUB_CONFIG . isCommitDataEnabled === 'true'
25
+
23
26
export default class ActivityService extends LoggerBase {
24
27
options : IServiceOptions
25
28
@@ -94,10 +97,24 @@ export default class ActivityService extends LoggerBase {
94
97
const toUpdate = merge ( existing , data , {
95
98
// eslint-disable-next-line @typescript-eslint/no-unused-vars
96
99
timestamp : ( oldValue , _newValue ) => oldValue ,
100
+ attributes : ( oldValue , newValue ) => {
101
+ if ( oldValue && newValue ) {
102
+ const out = { ...oldValue , ...newValue }
103
+ // If either of the two has isMainBranch set to true, then set it to true
104
+ if ( oldValue . isMainBranch || newValue . isMainBranch ) {
105
+ out . isMainBranch = true
106
+ }
107
+ return out
108
+ }
109
+ return newValue
110
+ } ,
97
111
// eslint-disable-next-line @typescript-eslint/no-unused-vars
98
112
organizationId : ( oldValue , _newValue ) => oldValue ,
99
113
} )
100
114
record = await ActivityRepository . update ( id , toUpdate , repositoryOptions )
115
+ if ( data . parent ) {
116
+ await this . addToConversation ( record . id , data . parent , transaction )
117
+ }
101
118
} else {
102
119
if ( ! data . sentiment ) {
103
120
const sentiment = await this . getSentiment ( data )
@@ -226,6 +243,31 @@ export default class ActivityService extends LoggerBase {
226
243
}
227
244
}
228
245
246
+ // When we implement Kern.ais's sentiment, we will get rid of this. In the meantime, we use Vader
247
+ // because we don't have an agreement with LF for comprehend.
248
+ if ( IS_GITHUB_COMMIT_DATA_ENABLED ) {
249
+ const text = data . sourceParentId ? data . body : `${ data . title } ${ data . body } `
250
+ const sentiment = vader . SentimentIntensityAnalyzer . polarity_scores ( text )
251
+ const compound = Math . round ( ( ( sentiment . compound + 1 ) / 2 ) * 100 )
252
+ // Some activities are inherently different, we might want to dampen their sentiment
253
+
254
+ let label = 'neutral'
255
+ if ( compound < 33 ) {
256
+ label = 'negative'
257
+ } else if ( compound > 66 ) {
258
+ label = 'positive'
259
+ }
260
+
261
+ return {
262
+ positive : Math . round ( sentiment . pos * 100 ) ,
263
+ negative : Math . round ( sentiment . neg * 100 ) ,
264
+ neutral : Math . round ( sentiment . neu * 100 ) ,
265
+ mixed : Math . round ( sentiment . neu * 100 ) ,
266
+ sentiment : compound ,
267
+ label,
268
+ }
269
+ }
270
+
229
271
try {
230
272
data . body = data . body ?? ''
231
273
data . title = data . title ?? ''
0 commit comments