-
Notifications
You must be signed in to change notification settings - Fork 122
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
159 additions
and
138 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,72 +1,71 @@ | ||
import { TaggedNostrEvent } from "@snort/system"; | ||
|
||
export function getHost(ev?: TaggedNostrEvent) { | ||
return ev?.tags.find(a => a[0] === "p" && a[3] === "host")?.[1] ?? ev?.pubkey ?? ""; | ||
return ev?.tags.find(a => a[0] === "p" && a[3] === "host")?.[1] ?? ev?.pubkey ?? ""; | ||
} | ||
|
||
export type StreamState = "live" | "ended" | "planned"; | ||
|
||
export interface StreamInfo { | ||
id?: string; | ||
title?: string; | ||
summary?: string; | ||
image?: string; | ||
thumbnail?: string; | ||
status?: StreamState; | ||
stream?: string; | ||
recording?: string; | ||
contentWarning?: string; | ||
tags: Array<string>; | ||
goal?: string; | ||
participants?: string; | ||
starts?: string; | ||
ends?: string; | ||
service?: string; | ||
host?: string; | ||
gameId?: string; | ||
id?: string; | ||
title?: string; | ||
summary?: string; | ||
image?: string; | ||
thumbnail?: string; | ||
status?: StreamState; | ||
stream?: string; | ||
recording?: string; | ||
contentWarning?: string; | ||
tags: Array<string>; | ||
goal?: string; | ||
participants?: string; | ||
starts?: string; | ||
ends?: string; | ||
service?: string; | ||
host?: string; | ||
gameId?: string; | ||
} | ||
|
||
const gameTagFormat = /^[a-z-]+:[a-z0-9-]+$/i; | ||
export function extractStreamInfo(ev?: TaggedNostrEvent) { | ||
const ret = { | ||
host: getHost(ev), | ||
} as StreamInfo; | ||
const matchTag = (tag: Array<string>, k: string, into: (v: string) => void) => { | ||
if (tag[0] === k) { | ||
into(tag[1]); | ||
} | ||
}; | ||
const ret = { | ||
host: getHost(ev), | ||
} as StreamInfo; | ||
const matchTag = (tag: Array<string>, k: string, into: (v: string) => void) => { | ||
if (tag[0] === k) { | ||
into(tag[1]); | ||
} | ||
}; | ||
|
||
for (const t of ev?.tags ?? []) { | ||
matchTag(t, "d", v => (ret.id = v)); | ||
matchTag(t, "title", v => (ret.title = v)); | ||
matchTag(t, "summary", v => (ret.summary = v)); | ||
matchTag(t, "image", v => (ret.image = v)); | ||
matchTag(t, "thumbnail", v => (ret.thumbnail = v)); | ||
matchTag(t, "status", v => (ret.status = v as StreamState)); | ||
if (t[0] === "streaming") { | ||
matchTag(t, "streaming", v => (ret.stream = v)); | ||
} | ||
matchTag(t, "recording", v => (ret.recording = v)); | ||
matchTag(t, "url", v => (ret.recording = v)); | ||
matchTag(t, "content-warning", v => (ret.contentWarning = v)); | ||
matchTag(t, "current_participants", v => (ret.participants = v)); | ||
matchTag(t, "goal", v => (ret.goal = v)); | ||
matchTag(t, "starts", v => (ret.starts = v)); | ||
matchTag(t, "ends", v => (ret.ends = v)); | ||
matchTag(t, "service", v => (ret.service = v)); | ||
for (const t of ev?.tags ?? []) { | ||
matchTag(t, "d", v => (ret.id = v)); | ||
matchTag(t, "title", v => (ret.title = v)); | ||
matchTag(t, "summary", v => (ret.summary = v)); | ||
matchTag(t, "image", v => (ret.image = v)); | ||
matchTag(t, "thumbnail", v => (ret.thumbnail = v)); | ||
matchTag(t, "status", v => (ret.status = v as StreamState)); | ||
if (t[0] === "streaming") { | ||
matchTag(t, "streaming", v => (ret.stream = v)); | ||
} | ||
const { regularTags } = sortStreamTags(ev?.tags ?? []); | ||
ret.tags = regularTags; | ||
matchTag(t, "recording", v => (ret.recording = v)); | ||
matchTag(t, "url", v => (ret.recording = v)); | ||
matchTag(t, "content-warning", v => (ret.contentWarning = v)); | ||
matchTag(t, "current_participants", v => (ret.participants = v)); | ||
matchTag(t, "goal", v => (ret.goal = v)); | ||
matchTag(t, "starts", v => (ret.starts = v)); | ||
matchTag(t, "ends", v => (ret.ends = v)); | ||
matchTag(t, "service", v => (ret.service = v)); | ||
} | ||
const { regularTags } = sortStreamTags(ev?.tags ?? []); | ||
ret.tags = regularTags; | ||
|
||
return ret; | ||
return ret; | ||
} | ||
|
||
|
||
export function sortStreamTags(tags: Array<string | Array<string>>) { | ||
const plainTags = tags.filter(a => (Array.isArray(a) ? a[0] === "t" : true)).map(a => (Array.isArray(a) ? a[1] : a)); | ||
const plainTags = tags.filter(a => (Array.isArray(a) ? a[0] === "t" : true)).map(a => (Array.isArray(a) ? a[1] : a)); | ||
|
||
const regularTags = plainTags.filter(a => !a.match(gameTagFormat)) ?? []; | ||
const prefixedTags = plainTags.filter(a => !regularTags.includes(a)); | ||
return { regularTags, prefixedTags }; | ||
} | ||
const regularTags = plainTags.filter(a => !a.match(gameTagFormat)) ?? []; | ||
const prefixedTags = plainTags.filter(a => !regularTags.includes(a)); | ||
return { regularTags, prefixedTags }; | ||
} |