forked from Trac-Systems/intercom
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprotocol.js
More file actions
166 lines (146 loc) · 5.06 KB
/
protocol.js
File metadata and controls
166 lines (146 loc) · 5.06 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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
'use strict'
/**
* TracPoll Protocol
* Bridges incoming /tx commands to Contract methods and
* broadcasts real-time vote events over Intercom sidechannels.
*
* Sidechannels used:
* tracpoll-votes — live vote stream (anonymous, option index only)
* tracpoll-results — periodic result snapshots after each vote
*/
export default class Protocol {
constructor ({ contract, peer }) {
this.contract = contract
this.peer = peer
}
// ─── Entry point — all /tx calls land here ─────────────────────────────────
async execute ({ address, command }) {
let cmd
try {
cmd = typeof command === 'string' ? JSON.parse(command) : command
} catch (e) {
return this._err('invalid JSON command')
}
const { op } = cmd
if (!op) return this._err('missing "op" field')
switch (op) {
case 'poll_create': {
const result = await this.contract.poll_create({
address,
question: cmd.question,
options: cmd.options,
duration_minutes: cmd.duration_minutes ?? 60
})
if (result.ok) {
// Broadcast poll creation to the live-votes channel
await this._broadcast('tracpoll-votes', {
event: 'poll_created',
poll_id: result.poll_id,
question: result.poll.question,
options: result.poll.options,
closes_at: result.poll.closes_at,
duration_minutes: result.poll.duration_minutes
})
this._log(`📋 Poll #${result.poll_id} created: "${result.poll.question}"`)
}
return result
}
case 'poll_vote': {
const result = await this.contract.poll_vote({
address,
poll_id: cmd.poll_id,
option_index: cmd.option_index
})
if (result.ok) {
// Broadcast anonymous vote event (no address)
await this._broadcast('tracpoll-votes', {
event: 'vote_cast',
poll_id: result.poll_id,
option_index: result.option_index,
new_tally: result.new_tally,
total_votes: result.total_votes
})
// Also push live results snapshot
const res = await this.contract.poll_results({ poll_id: result.poll_id })
if (res.ok) {
await this._broadcast('tracpoll-results', {
event: 'results_update',
poll_id: res.poll_id,
question: res.question,
total_votes: res.total_votes,
results: res.results,
status: res.status
})
}
this._log(`🗳️ Vote on poll #${result.poll_id} → option ${result.option_index} (total: ${result.total_votes})`)
}
return result
}
case 'poll_results': {
return await this.contract.poll_results({ poll_id: cmd.poll_id })
}
case 'poll_list': {
return await this.contract.poll_list({
limit: cmd.limit ?? 10,
include_closed: cmd.include_closed ?? false
})
}
case 'poll_close': {
const result = await this.contract.poll_close({
address,
poll_id: cmd.poll_id
})
if (result.ok) {
await this._broadcast('tracpoll-votes', {
event: 'poll_closed',
poll_id: result.poll_id
})
// Final results
const res = await this.contract.poll_results({ poll_id: result.poll_id })
if (res.ok) {
await this._broadcast('tracpoll-results', {
event: 'final_results',
poll_id: res.poll_id,
question: res.question,
total_votes: res.total_votes,
results: res.results,
status: 'closed'
})
this._log(`🔒 Poll #${result.poll_id} closed. Total votes: ${res.total_votes}`)
this._logResults(res)
}
}
return result
}
default:
return this._err(`unknown op: "${op}"`)
}
}
// ─── Helpers ──────────────────────────────────────────────────────────────
_err (msg) {
return { ok: false, error: msg }
}
_log (msg) {
console.log(`[TracPoll] ${msg}`)
}
_logResults (res) {
console.log(`[TracPoll] Results for "${res.question}":`)
for (const r of res.results) {
const bar = '█'.repeat(Math.round(r.pct / 5))
console.log(` [${r.index}] ${r.option.padEnd(30)} ${bar} ${r.pct}% (${r.votes} votes)`)
}
}
async _broadcast (channel, payload) {
try {
if (this.peer && typeof this.peer.sc_send === 'function') {
await this.peer.sc_send({
channel,
message: JSON.stringify({ ...payload, ts: Date.now() })
})
}
} catch (e) {
// Sidechannel broadcast is best-effort — don't crash on failure
this._log(`[warn] sc broadcast failed on "${channel}": ${e.message}`)
}
}
}