From 6618dc5edbb729c120ce8ca988b400f83c94393a Mon Sep 17 00:00:00 2001 From: Ryan Lerch Date: Feb 05 2018 11:36:10 +0000 Subject: add a mailing list widget for teams This adds a simple mailing list widget for use on teams page. It shows a list of the 5 most recent mailing list threads. --- diff --git a/hubs/default_config.py b/hubs/default_config.py index a230584..546342a 100644 --- a/hubs/default_config.py +++ b/hubs/default_config.py @@ -66,6 +66,7 @@ WIDGETS = [ 'hubs.widgets.halp:Halp', 'hubs.widgets.irc:IRC', 'hubs.widgets.meetings:Meetings', + 'hubs.widgets.mailinglist:MailingList', 'hubs.widgets.memberships:Memberships', 'hubs.widgets.my_hubs:MyHubs', 'hubs.widgets.pagure_pr:PagurePRs', diff --git a/hubs/utils/hyperkitty.py b/hubs/utils/hyperkitty.py new file mode 100644 index 0000000..3991a05 --- /dev/null +++ b/hubs/utils/hyperkitty.py @@ -0,0 +1,26 @@ +from __future__ import unicode_literals + +import requests +import arrow + +HYPERKITTY_URL = "https://lists.fedoraproject.org/archives" + + +def last_ten_threads(listname): + url = "/".join([HYPERKITTY_URL, "api", "list", listname, "threads"]) + response = requests.get(url) + data = response.json() + name = listname.split("@")[0] + for thread in data["results"]: + subject = thread["subject"] + subject = subject.lower().replace("["+name+"]", "") + + # the hyperkitty api returns a URL with the /api/ still in it + # here we strip it out. + url = thread["url"].replace(HYPERKITTY_URL+"/api", HYPERKITTY_URL) + yield dict( + thread_subject=subject, + thread_replies=thread["replies_count"], + thread_url=url, + thread_active_date=arrow.get(thread["date_active"]) + ) diff --git a/hubs/widgets/mailinglist/__init__.py b/hubs/widgets/mailinglist/__init__.py new file mode 100644 index 0000000..7d70c65 --- /dev/null +++ b/hubs/widgets/mailinglist/__init__.py @@ -0,0 +1,66 @@ +from __future__ import unicode_literals + +from hubs.utils import hyperkitty +from hubs.utils.fedmsg import get_fedmsg_config +from hubs.widgets.base import Widget +from hubs.widgets.view import RootWidgetView +from hubs.widgets.caching import CachedFunction + +fedmsg_config = get_fedmsg_config() + + +class MailingList(Widget): + + name = "mailinglist" + label = "Latest Mailing List Threads" + position = "right" + hub_types = ["team"] + + def get_template_environment(self): + env = super(MailingList, self).get_template_environment() + # Add a filter + env.filters['humanize'] = lambda d: d.humanize() + return env + + +class BaseView(RootWidgetView): + + def get_context(self, instance, *args, **kwargs): + if not instance.hub.config["mailing_list"]: + return dict(no_list_defined=True) + get_threads = GetMailingListThreads(instance) + return get_threads() + + +class GetMailingListThreads(CachedFunction): + def execute(self): + threads = list() + listname = self.instance.hub.config["mailing_list"] + for t in hyperkitty.last_ten_threads(listname): + threads.append(t) + new_thread_url = "/".join([hyperkitty.HYPERKITTY_URL, + "list", + listname, + "message", + "new"]) + return dict(threads=threads, + new_thread_url=new_thread_url) + + def should_invalidate(self, message): + if ".mailman.receive" in message["topic"]: + configlistname = self.instance.hub.config["mailing_list"] + configlistname = configlistname.split("@")[0] + try: + listname = message['msg']['mlist']['listname'] + except KeyError: + return False + return (configlistname == listname) + elif message["topic"].endswith('.hubs.hub.updated'): + if "mailing_list" in message["msg"]["changed_keys"]: + hub_id = message["msg"]["hub_id"] + if hub_id == self.instance.hub.id: + return True + else: + return False + else: + return False diff --git a/hubs/widgets/mailinglist/templates/root.html b/hubs/widgets/mailinglist/templates/root.html new file mode 100644 index 0000000..bdf28af --- /dev/null +++ b/hubs/widgets/mailinglist/templates/root.html @@ -0,0 +1,30 @@ + +{% if no_list_defined %} +

+ You must configure a mailinglist in the hub configuration in order to use this widget. +

+{% else %} + {% for thread in threads[:5] %} +
+
+ +
+ {{thread["thread_replies"]}} replies +
last active {{thread["thread_active_date"]|humanize}}
+
+
+
+ {% endfor %} + Start a new thread +{% endif %} + +