Skip to content

Commit

Permalink
explore section reworked, fixes #66
Browse files Browse the repository at this point in the history
  • Loading branch information
pmgl committed Apr 21, 2022
1 parent 1cdc4c4 commit d628cb8
Show file tree
Hide file tree
Showing 10 changed files with 339 additions and 332 deletions.
18 changes: 12 additions & 6 deletions server/content/content.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ class @Content

@projects = {}
@tags = {}
@sorted_tags = []

@project_count = 0
@user_count = 0
Expand Down Expand Up @@ -116,6 +117,7 @@ class @Content
if not tag?
tag = new Tag(t)
@tags[t] = tag
@sorted_tags.push tag
tag.add project

return
Expand Down Expand Up @@ -151,17 +153,20 @@ class @Content
time = Date.now()
@top_projects.sort (a,b)-> b.likes-a.likes
@new_projects.sort (a,b)-> b.first_published-a.first_published
@sorted_tags.sort (a,b)-> b.uses+b.num_users*10-a.uses-a.num_users*10

return if @top_projects.length<2 or @new_projects.length<2
return if @top_projects.length<5

now = Date.now()
maxLikes = Math.max(1,@top_projects[0].likes)
maxAge = now-@new_projects[@new_projects.length-1].first_published
maxLikes = Math.max(1,@top_projects[4].likes)

fade = (x)->
1-Math.max(0,Math.min(1,x))

note = (p)->
hours = Math.max(0,(now-p.first_published))/1000/3600
agemark = Math.exp(-hours/24/7)
p.likes/maxLikes*50+50*agemark
recent = fade (now-p.first_published)/1000/3600/24/4
rating = p.likes/maxLikes*(.15+2*fade((now-p.first_published)/1000/3600/24/180))
recent+rating

@hot_projects.sort (a,b)->
note(b)-note(a)
Expand Down Expand Up @@ -207,6 +212,7 @@ class @Content
if not tag?
tag = new Tag(t)
@tags[t] = tag
@sorted_tags.push tag
tag.add project

removeProjectTag:(project,t)->
Expand Down
24 changes: 16 additions & 8 deletions server/content/content.js
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ this.Content = (function() {
this.tokens = {};
this.projects = {};
this.tags = {};
this.sorted_tags = [];
this.project_count = 0;
this.user_count = 0;
this.guest_count = 0;
Expand Down Expand Up @@ -156,6 +157,7 @@ this.Content = (function() {
if (tag == null) {
tag = new Tag(t);
this.tags[t] = tag;
this.sorted_tags.push(tag);
}
tag.add(project);
}
Expand Down Expand Up @@ -204,25 +206,30 @@ this.Content = (function() {
};

Content.prototype.sortPublicProjects = function() {
var maxAge, maxLikes, note, now, time;
var fade, maxLikes, note, now, time;
time = Date.now();
this.top_projects.sort(function(a, b) {
return b.likes - a.likes;
});
this.new_projects.sort(function(a, b) {
return b.first_published - a.first_published;
});
if (this.top_projects.length < 2 || this.new_projects.length < 2) {
this.sorted_tags.sort(function(a, b) {
return b.uses + b.num_users * 10 - a.uses - a.num_users * 10;
});
if (this.top_projects.length < 5) {
return;
}
now = Date.now();
maxLikes = Math.max(1, this.top_projects[0].likes);
maxAge = now - this.new_projects[this.new_projects.length - 1].first_published;
maxLikes = Math.max(1, this.top_projects[4].likes);
fade = function(x) {
return 1 - Math.max(0, Math.min(1, x));
};
note = function(p) {
var agemark, hours;
hours = Math.max(0, now - p.first_published) / 1000 / 3600;
agemark = Math.exp(-hours / 24 / 7);
return p.likes / maxLikes * 50 + 50 * agemark;
var rating, recent;
recent = fade((now - p.first_published) / 1000 / 3600 / 24 / 4);
rating = p.likes / maxLikes * (.15 + 2 * fade((now - p.first_published) / 1000 / 3600 / 24 / 180));
return recent + rating;
};
this.hot_projects.sort(function(a, b) {
return note(b) - note(a);
Expand Down Expand Up @@ -285,6 +292,7 @@ this.Content = (function() {
if (tag == null) {
tag = new Tag(t);
this.tags[t] = tag;
this.sorted_tags.push(tag);
}
return tag.add(project);
};
Expand Down
5 changes: 5 additions & 0 deletions server/content/tag.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,16 @@ class @Tag
constructor:(@tag)->
@targets = {}
@uses = 0
@users = {}
@num_users = 0

add:(target)->
if not @targets[target.id]?
@targets[target.id] = target
@uses++
if target.owner? and target.owner.id? and not @users[target.owner.id]
@users[target.owner.id] = true
@num_users += 1

remove:(target)->
if @targets[target.id]?
Expand Down
8 changes: 7 additions & 1 deletion server/content/tag.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,18 @@ this.Tag = (function() {
this.tag = tag;
this.targets = {};
this.uses = 0;
this.users = {};
this.num_users = 0;
}

Tag.prototype.add = function(target) {
if (this.targets[target.id] == null) {
this.targets[target.id] = target;
return this.uses++;
this.uses++;
if ((target.owner != null) && (target.owner.id != null) && !this.users[target.owner.id]) {
this.users[target.owner.id] = true;
return this.num_users += 1;
}
}
};

Expand Down
38 changes: 36 additions & 2 deletions server/session/session.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -875,9 +875,36 @@ class @Session
source = @content.hot_projects

list = []
for p,i in source
break if list.length>=300
tags = if Array.isArray(data.tags) then data.tags else []
search = if typeof data.search == "string" then data.search else ""
search = search.trim()
offset = data.offset or 0

for i in [offset..source.length-1] by 1
p = source[i]

break if list.length >= 25
offset = i+1

if p.public and not p.deleted and not p.owner.flags.censored
if search
found = false
found |= p.title.toLowerCase().includes(search)
found |= p.description.toLowerCase().includes(search)
found |= p.owner.nick.toLowerCase().includes(search)
for t in p.tags
found |= t.includes(search)

continue if not found

if tags.length > 0
found = false
for t in tags
if p.tags.indexOf(t)>=0
found = true
break
continue if not found

list.push
id: p.id
title: p.title
Expand All @@ -899,9 +926,16 @@ class @Session
libs: p.libs
tabs: p.tabs

tags = []
for t in @content.sorted_tags
tags.push t.tag
break if tags.length>50

@send
name: "public_projects"
list: list
tags: tags
offset: offset
request_id: data.request_id

getPublicProject:(msg)->
Expand Down
49 changes: 46 additions & 3 deletions server/session/session.js
Original file line number Diff line number Diff line change
Expand Up @@ -1377,7 +1377,7 @@ this.Session = (function() {
};

Session.prototype.getPublicProjects = function(data) {
var i, j, len1, list, p, source;
var found, i, j, k, l, len1, len2, len3, list, m, offset, p, ref, ref1, ref2, ref3, search, source, t, tags;
switch (data.ranking) {
case "new":
source = this.content.new_projects;
Expand All @@ -1389,12 +1389,44 @@ this.Session = (function() {
source = this.content.hot_projects;
}
list = [];
for (i = j = 0, len1 = source.length; j < len1; i = ++j) {
tags = Array.isArray(data.tags) ? data.tags : [];
search = typeof data.search === "string" ? data.search : "";
search = search.trim();
offset = data.offset || 0;
for (i = j = ref = offset, ref1 = source.length - 1; j <= ref1; i = j += 1) {
p = source[i];
if (list.length >= 300) {
if (list.length >= 25) {
break;
}
offset = i + 1;
if (p["public"] && !p.deleted && !p.owner.flags.censored) {
if (search) {
found = false;
found |= p.title.toLowerCase().includes(search);
found |= p.description.toLowerCase().includes(search);
found |= p.owner.nick.toLowerCase().includes(search);
ref2 = p.tags;
for (k = 0, len1 = ref2.length; k < len1; k++) {
t = ref2[k];
found |= t.includes(search);
}
if (!found) {
continue;
}
}
if (tags.length > 0) {
found = false;
for (l = 0, len2 = tags.length; l < len2; l++) {
t = tags[l];
if (p.tags.indexOf(t) >= 0) {
found = true;
break;
}
}
if (!found) {
continue;
}
}
list.push({
id: p.id,
title: p.title,
Expand All @@ -1419,9 +1451,20 @@ this.Session = (function() {
});
}
}
tags = [];
ref3 = this.content.sorted_tags;
for (m = 0, len3 = ref3.length; m < len3; m++) {
t = ref3[m];
tags.push(t.tag);
if (tags.length > 50) {
break;
}
}
return this.send({
name: "public_projects",
list: list,
tags: tags,
offset: offset,
request_id: data.request_id
});
};
Expand Down
16 changes: 4 additions & 12 deletions static/js/appstate.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -53,18 +53,10 @@ class @AppState
else
s = location.pathname.split("/")
if s[2]? and s[3]?
if @app.explore.projects?
p = @app.explore.findProject(s[2],s[3])
if p
@app.explore.openProject(p)
@app.appui.setMainSection "explore"
else
@app.explore.update ()=>
p = @app.explore.findProject(s[2],s[3])
if p
@app.explore.openProject(p)
@app.appui.setMainSection "explore"

p = @app.explore.findProject(s[2],s[3])
if p
@app.explore.openProject(p)
@app.appui.setMainSection "explore"

initState:()->
if location.pathname.startsWith("/login/")
Expand Down
20 changes: 4 additions & 16 deletions static/js/appstate.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,22 +74,10 @@ this.AppState = (function() {
} else {
s = location.pathname.split("/");
if ((s[2] != null) && (s[3] != null)) {
if (this.app.explore.projects != null) {
p = this.app.explore.findProject(s[2], s[3]);
if (p) {
this.app.explore.openProject(p);
return this.app.appui.setMainSection("explore");
}
} else {
return this.app.explore.update((function(_this) {
return function() {
p = _this.app.explore.findProject(s[2], s[3]);
if (p) {
_this.app.explore.openProject(p);
return _this.app.appui.setMainSection("explore");
}
};
})(this));
p = this.app.explore.findProject(s[2], s[3]);
if (p) {
this.app.explore.openProject(p);
return this.app.appui.setMainSection("explore");
}
}
}
Expand Down

0 comments on commit d628cb8

Please sign in to comment.