[BREAK] Remove Google Vision features (#23160)
parent
9a89a093bb
commit
c292848bf7
@ -1,7 +0,0 @@ |
||||
For this to properly work, you need to have a Google Service Account; |
||||
https://console.cloud.google.com/apis/credentials |
||||
|
||||
Then you have to authorize that service account access to your buckets; |
||||
https://console.cloud.google.com/storage/browser |
||||
To do that, click on the ellipsis by your bucket's row and Edit object default permissions |
||||
Add user and paste the service account e-mail with owner privileges |
||||
@ -1,70 +0,0 @@ |
||||
const getVisionAttributes = (attachment) => { |
||||
const attributes = {}; |
||||
const labels = []; |
||||
if (attachment.labels && attachment.labels.length > 0) { |
||||
attachment.labels.forEach((label) => { |
||||
labels.push({ label }); |
||||
}); |
||||
} |
||||
if (attachment.safeSearch && attachment.safeSearch && attachment.safeSearch.adult === true) { |
||||
labels.push({ label: 'NSFW', bgColor: 'red', fontColor: 'white' }); |
||||
} |
||||
if (attachment.safeSearch && attachment.safeSearch.violence === true) { |
||||
labels.push({ label: 'Violence', bgColor: 'red', fontColor: 'white' }); |
||||
} |
||||
if (attachment.colors && attachment.colors.length > 0) { |
||||
attributes.color = `#${ attachment.colors[0] }`; |
||||
} |
||||
if (attachment.logos && attachment.logos.length > 0) { |
||||
labels.push({ label: `Logo: ${ attachment.logos[0] }` }); |
||||
} |
||||
if (attachment.faces && attachment.faces.length > 0) { |
||||
let faceCount = 0; |
||||
attachment.faces.forEach((face) => { |
||||
const faceAttributes = []; |
||||
if (face.joy) { |
||||
faceAttributes.push('Joy'); |
||||
} |
||||
if (face.sorrow) { |
||||
faceAttributes.push('Sorrow'); |
||||
} |
||||
if (face.anger) { |
||||
faceAttributes.push('Anger'); |
||||
} |
||||
if (face.surprise) { |
||||
faceAttributes.push('Surprise'); |
||||
} |
||||
if (faceAttributes.length > 0) { |
||||
labels.push({ label: `Face ${ ++faceCount }: ${ faceAttributes.join(', ') }` }); |
||||
} |
||||
}); |
||||
} |
||||
if (labels.length > 0) { |
||||
attributes.labels = labels; |
||||
} |
||||
return attributes; |
||||
}; |
||||
|
||||
export const createGoogleVisionMessageRenderer = () => |
||||
(message) => { |
||||
if (!message.attachments?.length) { |
||||
return message; |
||||
} |
||||
|
||||
message.attachments = message.attachments.map((attachment) => |
||||
Object.assign(attachment, getVisionAttributes(attachment))); |
||||
|
||||
return message; |
||||
}; |
||||
|
||||
export const createGoogleVisionMessageStreamHandler = () => |
||||
(message) => { |
||||
if (!message.attachments?.length) { |
||||
return message; |
||||
} |
||||
|
||||
message.attachments = message.attachments.map((attachment) => |
||||
Object.assign(attachment, getVisionAttributes(attachment))); |
||||
|
||||
return message; |
||||
}; |
||||
@ -1,4 +0,0 @@ |
||||
export { |
||||
createGoogleVisionMessageRenderer, |
||||
createGoogleVisionMessageStreamHandler, |
||||
} from './googlevision'; |
||||
@ -1,160 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { TAPi18n } from 'meteor/rocketchat:tap-i18n'; |
||||
|
||||
import { settings } from '../../settings'; |
||||
import { callbacks } from '../../callbacks'; |
||||
import { Uploads, Settings, Users, Messages } from '../../models'; |
||||
import { FileUpload } from '../../file-upload'; |
||||
import { api } from '../../../server/sdk/api'; |
||||
import { SystemLogger } from '../../../server/lib/logger/system'; |
||||
|
||||
class GoogleVision { |
||||
constructor() { |
||||
this.storage = require('@google-cloud/storage'); |
||||
this.vision = require('@google-cloud/vision'); |
||||
this.storageClient = {}; |
||||
this.visionClient = {}; |
||||
this.enabled = settings.get('GoogleVision_Enable'); |
||||
this.serviceAccount = {}; |
||||
settings.get('GoogleVision_Enable', (key, value) => { |
||||
this.enabled = value; |
||||
}); |
||||
settings.get('GoogleVision_ServiceAccount', (key, value) => { |
||||
try { |
||||
this.serviceAccount = JSON.parse(value); |
||||
this.storageClient = this.storage({ credentials: this.serviceAccount }); |
||||
this.visionClient = this.vision({ credentials: this.serviceAccount }); |
||||
} catch (e) { |
||||
this.serviceAccount = {}; |
||||
} |
||||
}); |
||||
settings.get('GoogleVision_Block_Adult_Images', (key, value) => { |
||||
if (value) { |
||||
callbacks.add('beforeSaveMessage', this.blockUnsafeImages.bind(this), callbacks.priority.MEDIUM, 'googlevision-blockunsafe'); |
||||
} else { |
||||
callbacks.remove('beforeSaveMessage', 'googlevision-blockunsafe'); |
||||
} |
||||
}); |
||||
callbacks.add('afterFileUpload', this.annotate.bind(this), callbacks.priority.MEDIUM, 'GoogleVision'); |
||||
} |
||||
|
||||
incCallCount(count) { |
||||
const currentMonth = new Date().getMonth(); |
||||
const maxMonthlyCalls = settings.get('GoogleVision_Max_Monthly_Calls') || 0; |
||||
if (maxMonthlyCalls > 0) { |
||||
if (settings.get('GoogleVision_Current_Month') !== currentMonth) { |
||||
settings.set('GoogleVision_Current_Month', currentMonth); |
||||
if (count > maxMonthlyCalls) { |
||||
return false; |
||||
} |
||||
} else if (count + (settings.get('GoogleVision_Current_Month_Calls') || 0) > maxMonthlyCalls) { |
||||
return false; |
||||
} |
||||
} |
||||
Settings.update({ _id: 'GoogleVision_Current_Month_Calls' }, { $inc: { value: count } }); |
||||
return true; |
||||
} |
||||
|
||||
blockUnsafeImages(message) { |
||||
if (this.enabled && this.serviceAccount && message && message.file && message.file._id) { |
||||
const file = Uploads.findOne({ _id: message.file._id }); |
||||
if (file && file.type && file.type.indexOf('image') !== -1 && file.store === 'GoogleCloudStorage:Uploads' && file.GoogleStorage) { |
||||
if (this.incCallCount(1)) { |
||||
const bucket = this.storageClient.bucket(settings.get('FileUpload_GoogleStorage_Bucket')); |
||||
const bucketFile = bucket.file(file.GoogleStorage.path); |
||||
const results = Meteor.wrapAsync(this.visionClient.detectSafeSearch, this.visionClient)(bucketFile); |
||||
if (results && results.adult === true) { |
||||
FileUpload.getStore('Uploads').deleteById(file._id); |
||||
const user = Users.findOneById(message.u && message.u._id); |
||||
if (user) { |
||||
api.broadcast('notify.ephemeralMessage', user._id, message.rid, { |
||||
msg: TAPi18n.__('Adult_images_are_not_allowed', {}, user.language), |
||||
}); |
||||
} |
||||
throw new Meteor.Error('GoogleVisionError: Image blocked'); |
||||
} |
||||
} else { |
||||
SystemLogger.error('Google Vision: Usage limit exceeded'); |
||||
} |
||||
return message; |
||||
} |
||||
} |
||||
} |
||||
|
||||
annotate({ message }) { |
||||
const visionTypes = []; |
||||
if (settings.get('GoogleVision_Type_Document')) { |
||||
visionTypes.push('document'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_Faces')) { |
||||
visionTypes.push('faces'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_Landmarks')) { |
||||
visionTypes.push('landmarks'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_Labels')) { |
||||
visionTypes.push('labels'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_Logos')) { |
||||
visionTypes.push('logos'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_Properties')) { |
||||
visionTypes.push('properties'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_SafeSearch')) { |
||||
visionTypes.push('safeSearch'); |
||||
} |
||||
if (settings.get('GoogleVision_Type_Similar')) { |
||||
visionTypes.push('similar'); |
||||
} |
||||
if (this.enabled && this.serviceAccount && visionTypes.length > 0 && message.file && message.file._id) { |
||||
const file = Uploads.findOne({ _id: message.file._id }); |
||||
if (file && file.type && file.type.indexOf('image') !== -1 && file.store === 'GoogleCloudStorage:Uploads' && file.GoogleStorage) { |
||||
if (this.incCallCount(visionTypes.length)) { |
||||
const bucket = this.storageClient.bucket(settings.get('FileUpload_GoogleStorage_Bucket')); |
||||
const bucketFile = bucket.file(file.GoogleStorage.path); |
||||
this.visionClient.detect(bucketFile, visionTypes, Meteor.bindEnvironment((error, results) => { |
||||
if (!error) { |
||||
Messages.setGoogleVisionData(message._id, this.getAnnotations(visionTypes, results)); |
||||
} else { |
||||
SystemLogger.error('GoogleVision error: ', error.stack); |
||||
} |
||||
})); |
||||
} else { |
||||
SystemLogger.error('Google Vision: Usage limit exceeded'); |
||||
} |
||||
} |
||||
} |
||||
} |
||||
|
||||
getAnnotations(visionTypes, visionData) { |
||||
if (visionTypes.length === 1) { |
||||
const _visionData = {}; |
||||
_visionData[`${ visionTypes[0] }`] = visionData; |
||||
visionData = _visionData; |
||||
} |
||||
const results = {}; |
||||
for (const index in visionData) { |
||||
if (visionData.hasOwnProperty(index)) { |
||||
switch (index) { |
||||
case 'faces': |
||||
case 'landmarks': |
||||
case 'labels': |
||||
case 'similar': |
||||
case 'logos': |
||||
results[index] = (results[index] || []).concat(visionData[index] || []); |
||||
break; |
||||
case 'safeSearch': |
||||
results.safeSearch = visionData.safeSearch; |
||||
break; |
||||
case 'properties': |
||||
results.colors = visionData[index].colors; |
||||
break; |
||||
} |
||||
} |
||||
} |
||||
return results; |
||||
} |
||||
} |
||||
|
||||
export default new GoogleVision(); |
||||
@ -1,2 +0,0 @@ |
||||
import './settings'; |
||||
import './googlevision'; |
||||
@ -1,93 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
|
||||
import { settings } from '../../settings'; |
||||
|
||||
Meteor.startup(function() { |
||||
settings.add('GoogleVision_Enable', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
public: true, |
||||
enableQuery: { _id: 'FileUpload_Storage_Type', value: 'GoogleCloudStorage' }, |
||||
}); |
||||
settings.add('GoogleVision_ServiceAccount', '', { |
||||
type: 'string', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
multiline: true, |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
secret: true, |
||||
}); |
||||
settings.add('GoogleVision_Max_Monthly_Calls', 0, { |
||||
type: 'int', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Current_Month', 0, { |
||||
type: 'int', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
hidden: true, |
||||
}); |
||||
settings.add('GoogleVision_Current_Month_Calls', 0, { |
||||
type: 'int', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
blocked: true, |
||||
}); |
||||
settings.add('GoogleVision_Type_Document', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Type_Faces', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Type_Landmarks', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Type_Labels', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Type_Logos', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Type_Properties', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Type_SafeSearch', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
settings.add('GoogleVision_Block_Adult_Images', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: [{ _id: 'GoogleVision_Enable', value: true }, { _id: 'GoogleVision_Type_SafeSearch', value: true }], |
||||
}); |
||||
settings.add('GoogleVision_Type_Similar', false, { |
||||
type: 'boolean', |
||||
group: 'FileUpload', |
||||
section: 'Google Vision', |
||||
enableQuery: { _id: 'GoogleVision_Enable', value: true }, |
||||
}); |
||||
}); |
||||
@ -1,22 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { Tracker } from 'meteor/tracker'; |
||||
|
||||
import { callbacks } from '../../../app/callbacks/client'; |
||||
import { settings } from '../../../app/settings/client'; |
||||
|
||||
Meteor.startup(() => { |
||||
Tracker.autorun(() => { |
||||
const isEnabled = settings.get('GoogleVision_Enable'); |
||||
|
||||
if (!isEnabled) { |
||||
callbacks.remove('renderMessage', 'googlevision'); |
||||
return; |
||||
} |
||||
|
||||
import('../../../app/google-vision/client').then(({ createGoogleVisionMessageRenderer }) => { |
||||
const renderMessage = createGoogleVisionMessageRenderer(); |
||||
callbacks.remove('renderMessage', 'googlevision'); |
||||
callbacks.add('renderMessage', renderMessage, callbacks.priority.HIGH - 3, 'googlevision'); |
||||
}); |
||||
}); |
||||
}); |
||||
@ -1,24 +0,0 @@ |
||||
import { Meteor } from 'meteor/meteor'; |
||||
import { Tracker } from 'meteor/tracker'; |
||||
|
||||
import { callbacks } from '../../../app/callbacks/client'; |
||||
import { settings } from '../../../app/settings/client'; |
||||
|
||||
Meteor.startup(() => { |
||||
Tracker.autorun(() => { |
||||
const isEnabled = settings.get('GoogleVision_Enable'); |
||||
|
||||
if (!isEnabled) { |
||||
callbacks.remove('streamMessage', 'googlevision'); |
||||
return; |
||||
} |
||||
|
||||
import('../../../app/google-vision/client').then( |
||||
({ createGoogleVisionMessageStreamHandler }) => { |
||||
const streamMessage = createGoogleVisionMessageStreamHandler(); |
||||
callbacks.remove('streamMessage', 'googlevision'); |
||||
callbacks.add('streamMessage', streamMessage, callbacks.priority.HIGH - 3, 'googlevision'); |
||||
}, |
||||
); |
||||
}); |
||||
}); |
||||
@ -1 +1 @@ |
||||
{ } |
||||
{} |
||||
@ -1 +1 @@ |
||||
{ } |
||||
{} |
||||
@ -1 +1 @@ |
||||
{ } |
||||
{} |
||||
@ -0,0 +1,28 @@ |
||||
import { Migrations } from '../../../app/migrations/server'; |
||||
import { Settings } from '../../../app/models/server'; |
||||
|
||||
Migrations.add({ |
||||
version: 234, |
||||
up() { |
||||
Settings.remove({ |
||||
_id: { |
||||
$in: [ |
||||
'GoogleVision_Enable', |
||||
'GoogleVision_ServiceAccount', |
||||
'GoogleVision_Max_Monthly_Calls', |
||||
'GoogleVision_Current_Month', |
||||
'GoogleVision_Current_Month_Calls', |
||||
'GoogleVision_Type_Document', |
||||
'GoogleVision_Type_Faces', |
||||
'GoogleVision_Type_Landmarks', |
||||
'GoogleVision_Type_Labels', |
||||
'GoogleVision_Type_Logos', |
||||
'GoogleVision_Type_Properties', |
||||
'GoogleVision_Type_SafeSearch', |
||||
'GoogleVision_Block_Adult_Images', |
||||
'GoogleVision_Type_Similar', |
||||
], |
||||
}, |
||||
}); |
||||
}, |
||||
}); |
||||
Loading…
Reference in new issue