Skip to content

Commit

Permalink
fix: message publishing
Browse files Browse the repository at this point in the history
  • Loading branch information
akurilov committed Aug 8, 2024
1 parent 18237ef commit c440dcd
Show file tree
Hide file tree
Showing 8 changed files with 22 additions and 22 deletions.
5 changes: 1 addition & 4 deletions web/api/events.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,7 @@ const Events = {

// uuidv4
Events.newId = function () {
return 'xxxxxxxx-xxxx-4xxx-yxxx-xxxxxxxxxxxx'.replace(/[xy]/g, function(c) {
var r = Math.random() * 16 | 0, v = c == 'x' ? r : (r & 0x3 | 0x8);
return v.toString(16);
});
return Ksuid.generate();
}

Events.publishInternal = function (payload, headers) {
Expand Down
1 change: 1 addition & 0 deletions web/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@
<script src="auth.js"></script>
<script src="donation.js"></script>
<script src="api/events.js"></script>
<script src="xksuid.js"></script>
<script src="intcomm.js"></script>
<script src="jquery-3.7.1.min.js"></script>
<script src="api/response.js"></script>
Expand Down
7 changes: 2 additions & 5 deletions web/intcomm.js
Original file line number Diff line number Diff line change
Expand Up @@ -195,19 +195,16 @@ async function reportPublicationInappropriate(srcAddr, evtLink, evtId) {
ce_string: reason,
},
action: {
ce_string: "report",
ce_string: "Flag", // see for example: https://docs.joinmastodon.org/spec/activitypub/#Flag
},
object: {
ce_string: evtId,
},
objecturl: {
ce_uri: evtLink,
},
subject: {
ce_string: userIdCurrent,
},
},
text_data: `User ${userIdCurrent} reports the inappropriate message from ${srcAddr}, reason: ${reason}`,
text_data: `User ${userIdCurrent} reports the inappropriate message ${evtId} from ${srcAddr}, reason: ${reason}`,
}
const headers = getAuthHeaders();
if (await Events.publishInternal(payload, headers)) {
Expand Down
1 change: 1 addition & 0 deletions web/pub-msg.html
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
<script src="api/status.js"></script>
<script src="attrs.js"></script>
<script src="api/events.js"></script>
<script src="xksuid.js"></script>
<script src="intcomm.js"></script>
<script src="pub-msg.js"></script>

Expand Down
1 change: 1 addition & 0 deletions web/pub-src-details.html
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@

<script src="api/response.js"></script>
<script src="auth.js"></script>
<script src="xksuid.js"></script>
<script src="api/events.js"></script>
<script src="intcomm.js"></script>
<script src="api/sources.js"></script>
Expand Down
1 change: 1 addition & 0 deletions web/pub.html
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@
<script src="api/limits.js"></script>
<script src="api/sources.js"></script>
<script src="pub.js"></script>
<script src="xksuid.js"></script>
<script src="api/events.js"></script>
<script src="intcomm.js"></script>
<script>
Expand Down
1 change: 1 addition & 0 deletions web/sub.html
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@

<script src="auth.js?v=1"></script>
<script src="api/response.js"></script>
<script src="xksuid.js"></script>
<script src="api/events.js"></script>
<script src="intcomm.js"></script>
<script src="api/subscriptions.js"></script>
Expand Down
27 changes: 14 additions & 13 deletions web/xksuid.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const Ksuid = {}
//@ts-check

/**
Expand All @@ -11,7 +12,7 @@ const BASE62 = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'
* @param {DataView} view
* @returns {string}
*/
export function base62(view) {
Ksuid.base62 = function (view) {
if (view.byteLength !== 20) {
throw new Error("incorrect buffer size")
}
Expand Down Expand Up @@ -58,7 +59,7 @@ export function base62(view) {
* @param {string} str
* @returns {Uint8Array} buffer
*/
export function debase62(str) {
Ksuid.debase62 = function (str) {
if (str.length !== 27) throw new Error('Expected 27 characters long base62 string')
const srcBase = 62n
const dstBase = 4294967296n
Expand Down Expand Up @@ -120,7 +121,7 @@ export function debase62(str) {
* @param {boolean|undefined} desc order, `true` indicates xKSUID
* @returns {number} seconds
*/
export function toEpoch(timestamp, desc) {
Ksuid.toEpoch = function (timestamp, desc) {
if (!desc) {
return Math.round(timestamp / 1000) - 14e8
}
Expand All @@ -134,7 +135,7 @@ export function toEpoch(timestamp, desc) {
* @param {boolean|undefined} desc
* @returns {number} ms
*/
export function fromEpoch(timestamp, desc) {
Ksuid.fromEpoch = function (timestamp, desc) {
if (!desc) {
return (14e8 + timestamp) * 1000
}
Expand All @@ -145,7 +146,7 @@ export function fromEpoch(timestamp, desc) {
* Generates cryptographically strong random buffer
* @returns {Uint8Array} 16 bytes of random binary values
*/
export function randomBytes() {
Ksuid.randomBytes = function () {
return crypto.getRandomValues(new Uint8Array(16))
}

Expand All @@ -155,37 +156,37 @@ export function randomBytes() {
* @param {number} timestamp ms
* @returns {string} 27 chars KSUID or 28 chars for xKSUID
*/
export function generate(desc = false, timestamp = Date.now()) {
Ksuid.generate = function (desc = false, timestamp = Date.now()) {
const buf = new ArrayBuffer(20)
const view = new DataView(buf)
const ts = toEpoch(timestamp, desc)
const ts = Ksuid.toEpoch(timestamp, desc)
let offset = 0
view.setUint32(offset, ts, false)
offset += 4
const rnd = randomBytes()
const rnd = Ksuid.randomBytes()
for (const b of rnd) {
view.setUint8(offset++, b)
}
if (desc) return 'z' + base62(view)
return base62(view)
if (desc) return 'z' + Ksuid.base62(view)
return Ksuid.base62(view)
}

/**
* Parses (x)KSUID string to timestamp and random part
* @param {string} ksuid
* @return {{ts:Date,rnd:ArrayBuffer}} parsed value
*/
export function parse(ksuid) {
Ksuid.parse = function (ksuid) {
if (ksuid.length > 28 || ksuid.length < 27) {
throw new Error(`Incorrect length: ${ksuid.length}, expected 27 or 28`)
}
const desc = ksuid.length === 28 && ksuid[0] == 'z'
if (ksuid.length === 28 && ksuid[0] != 'z') {
throw new Error(`KSUID is 28 symbol, but first char is not "z"`)
}
const buf = debase62(desc ? ksuid.slice(1, 28) : ksuid)
const buf = Ksuid.debase62(desc ? ksuid.slice(1, 28) : ksuid)
const view = new DataView(buf.buffer)
const tsValue = view.getUint32(0, false)
const ts = new Date(fromEpoch(tsValue, desc))
const ts = new Date(Ksuid.fromEpoch(tsValue, desc))
return { ts, rnd: buf.buffer.slice(4) }
}

0 comments on commit c440dcd

Please sign in to comment.