Luke Ross

Colorado

4 releases git clone https://lukeross.name/projects/colorado.git/

Web-based git repository viewer.

Commit f45119a39e08146c001da64e45cad14feee3554e

activitystream the atom

Committed 21 Oct 2018 by Luke Ross

colorado/feed.py

@@ -1,83 +1,121 @@
 from activitystreams_lxml import atom, activitystream
+from copy import deepcopy
 from datetime import datetime
 from dateutil.tz import tzutc
 from flask import url_for
 from functools import partial
 from itertools import chain
+from lxml import etree
 
 
 def make_authors(c):
-    if c.author == c.committer:
-        people = [c.author]
-    else:
-        people = [c.author, c.committer]
+	if c.author == c.committer:
+		people = [c.author]
+	else:
+		people = [c.author, c.committer]
 
-    authors = []
-    for p in people:
-        a = atom.Author()
-        a.name = p.name
-        authors.append(a)
+	authors = []
+	for p in people:
+		a = atom.Author()
+		a.name = p.name
+		authors.append(a)
 
-    return authors
+	return authors
 
 
 def tag_to_entry(repo, tag):
-    return atom.Entry()
+	pass
 
 
-def commit_to_entry(repo, commit):
-    e = atom.Entry()
-    e.title = ("Commit: {}".format(commit.message.split("\n")[0].strip()))
-    e.content = commit.message.strip()
-    e.updated = datetime.fromtimestamp(commit.committed_date).replace(tzinfo=tzutc())
-    e.id = commit.hexsha
-    l = atom.Link()
-    l.rel = "alternate"
-    l.type = "text/html"
-    l.href = url_for(".diff_view",
-      slug=repo.slug, id=commit.hexsha, _external=True)
-    e.append(l)
-    e.extend(make_authors(commit))
-    e.tail = "\n"
-    return e
+def commit_to_entry(repo, commit, branch):
+	e = atom.Entry()
+	e.extend(make_authors(commit))
+	e.content = commit.message.strip()
+	e.id = commit.hexsha
+	l = atom.Link()
+	l.rel = "alternate"
+	l.type = "text/html"
+	l.href = url_for(
+		".diff_view", slug=repo.slug, id=commit.hexsha, _external=True
+	)
+	e.append(l)
+	e.title = ("{} added commit {} to {}: {}".format(
+		commit.committer.name,
+		commit.hexsha,
+		branch.name,
+		commit.message.split("\n")[0].strip())
+	)
+	e.updated = datetime.fromtimestamp(
+		commit.committed_date
+	).replace(tzinfo=tzutc())
 
+	e.verb = "http://lukeross.name/colorado/activity/verb/commit"
+	obj = activitystream.Object()
+	obj.id = commit.hexsha
+	obj.title = "Commit: {}".format(commit.hexsha)
+	obj.summary = commit.message.strip()
+	obj.append(deepcopy(l))
+	obj.object_type = "http://lukeross.name/colorado/activity/type/commit"
+	e.append(obj)
+	target = activitystream.Object()
+	target.tag = etree.QName(activitystream.NS, "target")
+	target.id = branch.name
+	target.title = "Branch: {}".format(branch.name)
+	l = atom.Link()
+	l.rel = "alternate"
+	l.type = "text/html"
+	l.href = url_for(
+		".top_level_view", slug=repo.slug, type="branch", id=branch.name,
+		_external=True
+	)
+	target.append(l)
+	target.object_type = "http://lukeross.name/colorado/activity/type/branch"
+	e.append(target)
 
-def queue_commit(queue, repo, commit):
-    queue.append((commit.committed_date, partial(commit_to_entry, repo, commit)))
-    return commit.parents
+	return e
+
+
+def queue_commit(queue, repo, commit, branch):
+	queue.append((
+		commit.committed_date,
+		partial(commit_to_entry, repo, commit, branch)
+	))
+	return commit.parents
 
 
 def make_feed(record, num_items=10):
-    feed = atom.Feed(nsmap={None: atom.NS})
-    author = atom.Author()
-    author.name = 'Colorado Git Viewer'
-    author.url = url_for('.index', _external=True)
-    feed.append(author)
-    feed.generator = 'Colorado Git Viewer'
-    feed.id = 'fixme'
-    self_link = atom.Link()
-    self_link.href = url_for('.feed_view', slug=record.slug, _external=True)
-    self_link.rel = 'self'
-    self_link.type = 'application/atom+xml'
-    feed.append(self_link)
-    web_link = atom.Link()
-    web_link.href = url_for('.repo_home', slug=record.slug, _external=True)
-    web_link.rel = 'alternate'
-    web_link.type = 'text/html'
-    feed.append(web_link)
-    feed.title = record.name
-    repo = record.repo
-    tidbits = []
-    tidbits.extend(((t.tag.tagged_date, partial(tag_to_entry, record, t.tag)) for t in repo.tags if t.tag))
-    for branch in repo.branches:
-        commits = [branch.commit]
-        for _ in range(num_items):
-            commits = list(chain.from_iterable((queue_commit(tidbits, record, commit) for commit in commits)))
-
-    tidbits = sorted(tidbits, key=lambda i: i[0], reverse=True)
-    tidbits = tidbits[:num_items]
-    feed.updated = datetime.fromtimestamp(tidbits[0][0]).replace(tzinfo=tzutc())
-    for _, handler in tidbits:
-        feed.append(handler())
-
-    return feed
+	feed = activitystream.Feed(
+		nsmap={None: atom.NS, "activity": activitystream.NS}
+	)
+	feed.generator = "Colorado Git Viewer"
+	feed.id = "fixme"
+	self_link = atom.Link()
+	self_link.href = url_for(".feed_view", slug=record.slug, _external=True)
+	self_link.rel = "self"
+	self_link.type = "application/atom+xml"
+	feed.append(self_link)
+	web_link = atom.Link()
+	web_link.href = url_for(".repo_home", slug=record.slug, _external=True)
+	web_link.rel = "alternate"
+	web_link.type = "text/html"
+	feed.append(web_link)
+	feed.title = record.name
+	repo = record.repo
+	tidbits = []
+	# tidbits.extend(
+	# ((t.tag.tagged_date, partial(tag_to_entry, record, t.tag))
+	# for t in repo.tags if t.tag))
+	for branch in repo.branches:
+		commits = [branch.commit]
+		for _ in range(num_items):
+			commits = list(chain.from_iterable(
+				(queue_commit(tidbits, record, commit, branch) for commit in commits)
+			))
+
+	tidbits = sorted(tidbits, key=lambda i: i[0], reverse=True)
+	tidbits = tidbits[:num_items]
+	feed.updated = datetime.fromtimestamp(tidbits[0][0]).replace(tzinfo=tzutc())
+	for _, handler in tidbits:
+		feed.append(handler())
+
+	return feed


colorado/views.py

@@ -672,4 +672,4 @@ def feed_view(slug):
 	from colorado.feed import make_feed
 	from lxml import etree
 	repo = get_repo(slug)
-	return etree.tostring(make_feed(repo))
+	return etree.tostring(make_feed(repo), pretty_print=True)