# HG changeset patch # User Marcin Kuzminski # Date 2017-07-26 14:26:35 # Node ID 27a92529d3dba0259b38c401fa23456e42e95bff # Parent ea1add977dea477a63317a716339be4f7527acdd channelstream: add simple method of posting message to notification channel. diff --git a/rhodecode/lib/channelstream.py b/rhodecode/lib/channelstream.py --- a/rhodecode/lib/channelstream.py +++ b/rhodecode/lib/channelstream.py @@ -18,12 +18,15 @@ # RhodeCode Enterprise Edition, including its added features, Support services, # and proprietary license terms, please see https://rhodecode.com/licenses/ +import os import hashlib import itsdangerous import logging -import os import requests +import datetime + from dogpile.core import ReadWriteMutex +from pyramid.threadlocal import get_current_registry import rhodecode.lib.helpers as h from rhodecode.lib.auth import HasRepoPermissionAny @@ -218,3 +221,33 @@ def get_connection_validators(registry): if validator: validators.append(validator) return validators + + +def post_message(channel, message, username, registry=None): + + if not registry: + registry = get_current_registry() + + rhodecode_plugins = getattr(registry, 'rhodecode_plugins', {}) + channelstream_config = rhodecode_plugins.get('channelstream', {}) + if channelstream_config.get('enabled'): + payload = { + 'type': 'message', + 'timestamp': datetime.datetime.utcnow(), + 'user': 'system', + 'exclude_users': [username], + 'channel': channel, + 'message': { + 'message': message, + 'level': 'success', + 'topic': '/notifications' + } + } + + try: + return channelstream_request( + channelstream_config, [payload], '/message', + raise_exc=False) + except ChannelstreamException: + log.exception('Failed to send channelstream data') + raise