-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.coffee
More file actions
executable file
·108 lines (93 loc) · 2.93 KB
/
Copy pathserver.coffee
File metadata and controls
executable file
·108 lines (93 loc) · 2.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
Db = require 'db'
Event = require 'event'
Http = require 'http'
Metatags = require 'metatags'
OAuth = require 'oauth'
Photo = require 'photo'
App = require 'app'
# The `secrets.server.coffee` file cannot be added to the git repo, for
# obvious reasons. The function should return Yahoo credentials like:
# `["verylongid09ves4u5w9serawevwa9r9ws8erus8fv9ws8ers--", "passworddfsnsd3ufgnsidu5fndsufg"]`
yahooConsumerPair = require('secrets').yahooConsumerPair()
exports.client_search = (text,cb) !->
request =
url: 'https://yboss.yahooapis.com/ysearch/limitedweb?format=json&q=' + encodeURIComponent(text)
cb: ['onSearchResults', cb]
OAuth.sign request, yahooConsumerPair
Http.get request
exports.onSearchResults = (cb, resp) !->
metas = {_MODE_:'replace'}
if resp.body and (data = JSON.parse(resp.body)) and (results = data?.bossresponse?.limitedweb?.results)
i = cnt = 0
while cnt<3 and i<results.length
url = results[i++].url
if prevUrl and url.substr(0,prevUrl.length)==prevUrl
continue
metas[cnt++] = {url}
prevUrl = url
# Immediately show url search results
cb.reply metas
# Asynchronously fetch meta info for these pages
for pos in [0...cnt] by 1
url = metas[pos].url
Http.get
url: url
cb: ['onSearchMeta', cb, url, pos]
exports.onSearchMeta = (cb, url, pos, resp) !->
if resp.body and meta = Metatags.fromHtml(resp.body)
meta.url = url
else
meta = {url}
#log 'onSearchMeta', url, pos, JSON.stringify meta
push = {}
push[pos] = meta
cb.reply push
exports.client_add = (text) !->
if typeof text is 'object'
if text.photoguid
text.by = App.userId()
Photo.claim text.photoguid, text
else
addTopic App.userId(), text # not used for search results anymore
else if (text.toLowerCase().indexOf('http') is 0 or text.toLowerCase().indexOf('www.') is 0) and text.split(' ').length is 1
Http.get
url: text
cb: ['httpTags', App.userId(), text]
memberId: App.userId()
else
addTopic App.userId(), title: text
exports.onPhoto = (info, data) !->
#log 'info > ' + JSON.stringify(info)
#log 'data > ' + JSON.stringify(data)
if info.key and data.by
data.photo = info.key
addTopic data.by, data
exports.httpTags = (userId, url, resp) !->
if resp.body and meta = Metatags.fromHtml(resp.body)
meta.url = url
addTopic userId, meta
else
# url was probably malformed, just add as title
addTopic userId, title:url
addTopic = (userId, data) !->
topic =
title: data.title
description: data.description||''
url: data.url
time: 0|(new Date()/1000)
by: userId
if data.image
topic.image = data.image
if data.imageThumb
topic.imageThumb = data.imageThumb
if data.photo
topic.photo = data.photo
maxId = Db.shared.incr('maxId')
Db.shared.set(maxId, topic)
name = App.userName(userId)
Event.create
text: "#{name} added topic: #{topic.title}"
sender: userId
exports.client_remove = (id) !->
return if App.userId() isnt Db.shared.get(id, 'by') and !App.userIsAdmin()
Db.shared.remove(id)