2014-07-18 20:34:52 +00:00
|
|
|
from app import app, notification_queue
|
|
|
|
from data import model
|
2014-10-22 23:01:56 +00:00
|
|
|
from auth.auth_context import get_authenticated_user, get_validated_oauth_token
|
2014-07-18 20:34:52 +00:00
|
|
|
|
|
|
|
import json
|
|
|
|
|
2014-07-29 17:39:26 +00:00
|
|
|
def build_event_data(repo, extra_data={}, subpage=None):
|
2014-09-24 22:01:35 +00:00
|
|
|
repo_string = '%s/%s' % (repo.namespace_user.username, repo.name)
|
2014-07-29 17:39:26 +00:00
|
|
|
homepage = '%s://%s/repository/%s' % (app.config['PREFERRED_URL_SCHEME'],
|
|
|
|
app.config['SERVER_HOSTNAME'],
|
|
|
|
repo_string)
|
|
|
|
|
2014-07-18 20:34:52 +00:00
|
|
|
if subpage:
|
2014-07-29 17:39:26 +00:00
|
|
|
if not subpage.startswith('/'):
|
|
|
|
subpage = '/' + subpage
|
|
|
|
|
2014-07-18 20:34:52 +00:00
|
|
|
homepage = homepage + subpage
|
|
|
|
|
|
|
|
event_data = {
|
|
|
|
'repository': repo_string,
|
2014-09-24 22:01:35 +00:00
|
|
|
'namespace': repo.namespace_user.username,
|
2014-07-18 20:34:52 +00:00
|
|
|
'name': repo.name,
|
2014-07-29 17:39:26 +00:00
|
|
|
'docker_url': '%s/%s' % (app.config['SERVER_HOSTNAME'], repo_string),
|
2014-07-18 20:34:52 +00:00
|
|
|
'homepage': homepage,
|
|
|
|
'visibility': repo.visibility.name
|
|
|
|
}
|
|
|
|
|
|
|
|
event_data.update(extra_data)
|
2014-07-29 17:39:26 +00:00
|
|
|
return event_data
|
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
def build_notification_data(notification, event_data, performer_data=None):
|
|
|
|
if not performer_data:
|
|
|
|
performer_data = {}
|
|
|
|
|
|
|
|
oauth_token = get_validated_oauth_token()
|
|
|
|
if oauth_token:
|
|
|
|
performer_data['oauth_token_id'] = oauth_token.id
|
|
|
|
performer_data['oauth_token_application_id'] = oauth_token.application.client_id
|
|
|
|
performer_data['oauth_token_application'] = oauth_token.application.name
|
|
|
|
|
|
|
|
performer_user = get_authenticated_user()
|
|
|
|
if performer_user:
|
|
|
|
performer_data['entity_id'] = performer_user.id
|
|
|
|
performer_data['entity_name'] = performer_user.username
|
|
|
|
|
2014-07-31 17:30:54 +00:00
|
|
|
return {
|
|
|
|
'notification_uuid': notification.uuid,
|
2014-09-24 22:01:35 +00:00
|
|
|
'repository_namespace': notification.repository.namespace_user.username,
|
2014-07-31 17:30:54 +00:00
|
|
|
'repository_name': notification.repository.name,
|
2014-10-22 23:01:56 +00:00
|
|
|
'event_data': event_data,
|
|
|
|
'performer_data': performer_data
|
2014-07-31 17:30:54 +00:00
|
|
|
}
|
|
|
|
|
2014-07-29 17:39:26 +00:00
|
|
|
|
2014-10-22 23:01:56 +00:00
|
|
|
def spawn_notification(repo, event_name, extra_data={}, subpage=None, pathargs=[],
|
|
|
|
performer_data=None):
|
2014-07-29 17:39:26 +00:00
|
|
|
event_data = build_event_data(repo, extra_data=extra_data, subpage=subpage)
|
2014-07-18 20:34:52 +00:00
|
|
|
|
2014-09-24 22:01:35 +00:00
|
|
|
notifications = model.list_repo_notifications(repo.namespace_user.username, repo.name,
|
|
|
|
event_name=event_name)
|
2014-10-22 23:01:56 +00:00
|
|
|
for notification in list(notifications):
|
|
|
|
notification_data = build_notification_data(notification, event_data, performer_data)
|
2014-09-24 22:01:35 +00:00
|
|
|
path = [repo.namespace_user.username, repo.name, event_name] + pathargs
|
2014-07-18 20:34:52 +00:00
|
|
|
notification_queue.put(path, json.dumps(notification_data))
|