git ssb

30+

cel / git-ssb-web



Commit a623136687667b0fb73dfbb8432d287868cede02

Implement naming repos

Charles Lehner committed on 3/23/2016, 4:36:45 AM
Parent: 6afe4bb561793e9e92fa37a07a07e36f43af45df

Files changed

about.jschanged
index.jschanged
static/styles.csschanged
about.jsView
@@ -35,43 +35,67 @@
3535 }
3636
3737 // Get About info (name and icon) for a feed.
3838 function getAboutFull(sbot, source, dest, cb) {
39- var name, image
39+ var info = {}
40+ var target = dest.target || dest
41+ var owner = dest.owner || dest
42+
4043 pull(
4144 cat([
4245 // First get About info that we gave them.
4346 sbot.links({
4447 source: source,
45- dest: dest,
48+ dest: target,
4649 rel: 'about',
4750 values: true,
4851 reverse: true
4952 }),
5053 // If that isn't enough, then get About info that they gave themselves.
5154 sbot.links({
52- source: dest,
53- dest: dest,
55+ source: owner,
56+ dest: target,
5457 rel: 'about',
5558 values: true,
5659 reverse: true
5760 }),
5861 ]),
5962 pull.filter(function (msg) {
60- return msg && msg.value.content && (!name || !image)
63+ return msg && msg.value.content
6164 }),
6265 pull.drain(function (msg) {
66+ if (info.name && info.image) return false
6367 var c = msg.value.content
64- if (!name) {
65- name = c.name
68+ if (!info.name && c.name)
69+ info.name = c.name
70+ if (!info.image && c.image)
71+ info.image = c.image.link
72+ }, function (err) {
73+ if (err && err !== true) return cb(err)
74+ if (!info.name) info.name = truncate(target, 20)
75+ cb(null, info)
76+ })
77+ )
78+
79+ // Keep updated as changes are made
80+ pull(
81+ sbot.links({
82+ dest: target,
83+ rel: 'about',
84+ live: true,
85+ values: true,
86+ gte: Date.now()
87+ }),
88+ pull.drain(function (msg) {
89+ var c = msg.value.content
90+ if (msg.value.author == source || msg.value.author == owner) {
91+ // TODO: give about from source (self) priority over about from owner
92+ if (c.name)
93+ info.name = c.name
94+ if (c.image)
95+ info.image = c.image
6696 }
67- if (!image) {
68- image = c.image ? c.image.link : c.image
69- // var imgLink = mlib.link(c.image, 'blob')
70- // image = imgLink && imgLink.link
71- }
7297 }, function (err) {
73- if (err) return cb(err)
74- cb(null, {name: name || truncate(id, 8), image: image})
98+ if (err) console.error(err)
7599 })
76100 )
77101 }
index.jsView
@@ -133,11 +133,17 @@
133133 return str
134134 }
135135 }
136136
137-function getRepoName(repoId, cb) {
138- // TODO: use petnames
139- cb(null, repoId.substr(0, 20) + '…')
137+function getRepoName(about, ownerId, repoId, cb) {
138+ about.getName({
139+ owner: ownerId,
140+ target: repoId,
141+ toString: function () {
142+ // hack to fit two parameters into asyncmemo
143+ return ownerId + '/' + repoId
144+ }
145+ }, cb)
140146 }
141147
142148 var hasOwnProp = Object.prototype.hasOwnProperty
143149
@@ -347,16 +353,16 @@
347353 pull.once('<hr/></article></body></html>')
348354 ])
349355 }
350356
351- function serveError(err) {
357+ function serveError(err, status) {
352358 if (err.message == 'stream is closed')
353359 reconnect()
354360 return pull(
355361 pull.once(
356362 '<h2>' + err.name + '</h3>' +
357363 '<pre>' + escapeHTML(err.stack) + '</pre>'),
358- serveTemplate(err.name, 500)
364+ serveTemplate(err.name, status || 500)
359365 )
360366 }
361367
362368 /* Feed */
@@ -387,21 +393,30 @@
387393
388394 function renderRepoCreated(msg, authorName, cb) {
389395 var msgLink = link([msg.key],
390396 new Date(msg.value.timestamp).toLocaleString())
391- var repoLink = link([msg.key])
392397 var authorLink = link([msg.value.author], authorName)
393- cb(null, '<section class="collapse">' + msgLink + '<br>' +
394- authorLink + ' created repo ' + repoLink + '</section>')
398+ var author = msg.value.author
399+ getRepoName(about, author, msg.key, function (err, repoName) {
400+ if (err) return cb(err)
401+ var repoLink = link([msg.key], repoName)
402+ cb(null, '<section class="collapse">' + msgLink + '<br>' +
403+ authorLink + ' created repo ' + repoLink + '</section>')
404+ })
395405 }
396406
397407 function renderUpdate(msg, authorName, cb) {
398408 var msgLink = link([msg.key],
399409 new Date(msg.value.timestamp).toLocaleString())
400- var repoLink = link([msg.value.content.repo])
401410 var authorLink = link([msg.value.author], authorName)
402- cb(null, '<section class="collapse">' + msgLink + '<br>' +
403- authorLink + ' pushed to ' + repoLink + '</section>')
411+ var repoId = msg.value.content.repo
412+ var author = msg.value.author
413+ getRepoName(about, author, repoId, function (err, repoName) {
414+ if (err) return cb(err)
415+ var repoLink = link([msg.value.content.repo], repoName)
416+ cb(null, '<section class="collapse">' + msgLink + '<br>' +
417+ authorLink + ' pushed to ' + repoLink + '</section>')
418+ })
404419 }
405420
406421 /* Index */
407422
@@ -466,14 +481,24 @@
466481
467482 if (req.method == 'POST') {
468483 return readNext(function (cb) {
469484 readReqJSON(req, function (err, data) {
470- if (data && data.vote != null) {
485+ if (err) return cb(null, serveError(err, 400))
486+ if (!data) return cb(null, serveError(new Error('No data'), 400))
487+ if (data.vote != null) {
471488 var voteValue = +data.vote || 0
472489 ssb.publish(schemas.vote(repo.id, voteValue), function (err) {
473490 if (err) return cb(null, serveError(err))
474491 cb(null, serveRedirect(req.url))
475492 })
493+ } else if ('repo-name' in data) {
494+ var name = data['repo-name']
495+ if (!name) return cb(null, serveRedirect(req.url))
496+ var msg = schemas.name(repo.id, name)
497+ ssb.publish(msg, function (err) {
498+ if (err) return cb(null, serveError(err))
499+ cb(null, serveRedirect(req.url))
500+ })
476501 } else {
477502 cb(null, servePlainError(400, 'What are you trying to do?'))
478503 }
479504 })
@@ -519,9 +544,9 @@
519544 'onclick="this.select()"/>'
520545 var digsPath = [repo.id, 'digs']
521546
522547 var done = multicb({ pluck: 1, spread: true })
523- getRepoName(repo.id, done())
548+ about.getName(repo.id, done())
524549 about.getName(repo.feed, done())
525550 getVotes(repo.id, done())
526551
527552 return readNext(function (cb) {
@@ -540,10 +565,18 @@
540565 (upvoted ? 'Undig' : 'Dig') +
541566 '</button>') + ' ' +
542567 '<strong>' + link(digsPath, votes.upvotes) + '</strong>' +
543568 '</form>' +
544- '<h2>' + link([repo.feed], authorName) + ' / ' +
569+ '<form class="petname" action="" method="post">' +
570+ '<input name="repo-name" id="repo-name" value="' +
571+ escapeHTML(repoName) + '" />' +
572+ '<label class="repo-name-toggle" for="repo-name" ' +
573+ 'title="Rename the repo"><i>✍</i></label>' +
574+ '<input class="repo-name-btn" type="submit" value="Rename">' +
575+ '<h2 class="left">' + link([repo.feed], authorName) + ' / ' +
545576 link([repo.id], repoName) + '</h2>' +
577+ '</form>' +
578+ '<br clear="all" \>' +
546579 '</div><div class="repo-nav">' + link([repo.id], 'Code') +
547580 link([repo.id, 'activity'], 'Activity') +
548581 link([repo.id, 'commits', branch || ''], 'Commits') +
549582 gitLink +
static/styles.cssView
@@ -68,8 +68,12 @@
6868 }
6969
7070 /* repo page */
7171
72+.left {
73+ float: left;
74+}
75+
7276 .repo-title h2 {
7377 color: #888;
7478 }
7579
@@ -92,8 +96,34 @@
9296 .upvotes button:hover {
9397 background-color: #f6f6f6;
9498 }
9599
100+.petname {
101+ display: inline-block;
102+}
103+
104+#repo-name {
105+ font-size: inherit;
106+ width: 0;
107+ opacity: 0;
108+}
109+
110+#repo-name:focus {
111+ margin-left: 1ex;
112+ width: 20em;
113+ opacity: 1;
114+}
115+
116+#repo-name:focus ~ h2 :last-child,
117+#repo-name:not(:focus) ~ .repo-name-btn:not(:active) {
118+ display: none;
119+}
120+
121+.repo-name-toggle {
122+ font-size: 1.3em;
123+ cursor: pointer;
124+}
125+
96126 .clone-url {
97127 font-size: small;
98128 color: #666;
99129 padding: .3em 1ex;

Built with git-ssb-web