use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use time::OffsetDateTime;
pub const GUIDE: &str = include_str!("../guide.md");
pub const DELETED_RETENTION_DAYS: i64 = 7;
pub const SUBPROTOCOL: &str = "semora.v1";
pub const API_PREFIX: &str = "/api/v1";
pub const MAX_WS_MESSAGE: usize = 64 * 1024;
pub const MAX_PAGE_EVENT_DATA: usize = 64 * 1024;
pub const MAX_ATTACHMENT: usize = 5 * 1024 * 1024;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum DocKind {
Md,
Html,
Bundle,
}
impl DocKind {
pub fn as_str(self) -> &'static str {
match self {
DocKind::Md => "md",
DocKind::Html => "html",
DocKind::Bundle => "bundle",
}
}
}
impl fmt::Display for DocKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for DocKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"md" => Ok(DocKind::Md),
"html" => Ok(DocKind::Html),
"bundle" => Ok(DocKind::Bundle),
other => Err(format!("unknown document kind `{other}`")),
}
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
#[serde(rename_all = "lowercase")]
pub enum Level {
Viewer,
Reviewer,
Editor,
Owner,
}
impl Level {
pub fn as_str(self) -> &'static str {
match self {
Level::Viewer => "viewer",
Level::Reviewer => "reviewer",
Level::Editor => "editor",
Level::Owner => "owner",
}
}
pub fn label(self) -> &'static str {
match self {
Level::Viewer => "Viewer",
Level::Reviewer => "Reviewer",
Level::Editor => "Editor",
Level::Owner => "Owner",
}
}
}
impl fmt::Display for Level {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for Level {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"viewer" => Ok(Level::Viewer),
"reviewer" => Ok(Level::Reviewer),
"editor" => Ok(Level::Editor),
"owner" => Ok(Level::Owner),
other => Err(format!("unknown level `{other}`")),
}
}
}
pub fn owner_level() -> Level {
Level::Owner
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Person {
pub id: i64,
pub name: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Scope {
#[serde(rename = "docs:write")]
DocsWrite,
#[serde(rename = "docs:read")]
DocsRead,
#[serde(rename = "events")]
Events,
}
impl Scope {
pub fn all() -> Vec<Scope> {
vec![Scope::DocsWrite, Scope::DocsRead, Scope::Events]
}
pub fn as_str(self) -> &'static str {
match self {
Scope::DocsWrite => "docs:write",
Scope::DocsRead => "docs:read",
Scope::Events => "events",
}
}
pub fn parse_list(s: &str) -> Result<Vec<Scope>, String> {
s.split(',')
.map(str::trim)
.filter(|p| !p.is_empty())
.map(Scope::from_str)
.collect()
}
pub fn join(scopes: &[Scope]) -> String {
scopes
.iter()
.map(|s| s.as_str())
.collect::<Vec<_>>()
.join(",")
}
}
impl FromStr for Scope {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"docs:write" => Ok(Scope::DocsWrite),
"docs:read" => Ok(Scope::DocsRead),
"events" => Ok(Scope::Events),
other => Err(format!("unknown scope `{other}`")),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct BrokenLink {
pub from: String,
pub href: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PushResponse {
pub id: String,
pub rev: i64,
pub kind: DocKind,
pub url: String,
pub ws_url: String,
pub files: usize,
pub title: String,
#[serde(default)]
pub unchanged: bool,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub broken_links: Vec<BrokenLink>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub warnings: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path_url: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DocSummary {
pub id: String,
pub title: String,
pub kind: DocKind,
pub tags: Vec<String>,
pub rev: i64,
pub revision_count: i64,
pub url: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub project: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path_url: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub level: Option<Level>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ListResponse {
pub docs: Vec<DocSummary>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub next_cursor: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProjectInfo {
pub slug: String,
pub name: String,
pub url: String,
pub documents: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(with = "time::serde::rfc3339")]
pub updated_at: OffsetDateTime,
#[serde(default = "owner_level")]
pub level: Level,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub owner: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateProject {
pub slug: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub name: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub join: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct JoinNeeded {
pub error: String,
pub message: String,
pub owner: String,
pub level: Level,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct PatchProject {
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub slug: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProjectList {
pub projects: Vec<ProjectInfo>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Mapping {
pub pattern: String,
pub folder: String,
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct MappingList {
pub mappings: Vec<Mapping>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct FileInfo {
pub path: String,
pub size: u64,
pub mime: String,
pub hash: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct RevisionInfo {
pub rev: i64,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub message: Option<String>,
pub files: usize,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub created_by: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub pushed_by: Option<Person>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub origin: Option<String>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DocDetail {
#[serde(flatten)]
pub summary: DocSummary,
pub entry_path: String,
pub ws_url: String,
pub files: Vec<FileInfo>,
pub revisions: Vec<RevisionInfo>,
#[serde(default)]
pub meta: Value,
}
#[derive(Debug, Clone, Default, PartialEq, Serialize, Deserialize)]
pub struct PatchDoc {
#[serde(default, skip_serializing_if = "Option::is_none")]
pub title: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub tags: Option<Vec<String>>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub add_tags: Vec<String>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub remove_tags: Vec<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub meta: Option<Value>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct ViewResponse {
pub content_url: String,
pub rev: i64,
pub path: String,
#[serde(with = "time::serde::rfc3339")]
pub expires_at: OffsetDateTime,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct UserInfo {
pub id: i64,
pub email: String,
pub display_name: String,
pub is_admin: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct KeyInfo {
pub id: i64,
pub name: String,
pub prefix: String,
pub scopes: Vec<Scope>,
pub created_via: String,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(
default,
with = "time::serde::rfc3339::option",
skip_serializing_if = "Option::is_none"
)]
pub last_used_at: Option<OffsetDateTime>,
#[serde(
default,
with = "time::serde::rfc3339::option",
skip_serializing_if = "Option::is_none"
)]
pub expires_at: Option<OffsetDateTime>,
#[serde(
default,
with = "time::serde::rfc3339::option",
skip_serializing_if = "Option::is_none"
)]
pub revoked_at: Option<OffsetDateTime>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct WhoAmI {
pub server: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub server_id: Option<String>,
pub user: UserInfo,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub key: Option<KeyInfo>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreateKeyRequest {
pub name: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub scopes: Option<Vec<Scope>>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub expires_in_days: Option<u32>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CreatedKey {
pub key: KeyInfo,
pub secret: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CliDeviceRequest {
#[serde(default)]
pub name: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CliDeviceResponse {
pub device_code: String,
pub user_code: String,
pub verification_uri: String,
pub verification_uri_complete: String,
pub expires_in: u64,
pub interval: u64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct CliTokenRequest {
pub device_code: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "status", rename_all = "snake_case")]
pub enum CliTokenResponse {
Pending,
Approved {
api_key: String,
name: String,
user: UserInfo,
},
Denied,
Expired,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ApiErrorBody {
pub error: String,
pub message: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum EventKind {
Comment,
Drawing,
PageEvent,
Message,
Say,
}
impl EventKind {
pub fn as_str(self) -> &'static str {
match self {
EventKind::Comment => "comment",
EventKind::Drawing => "drawing",
EventKind::PageEvent => "page_event",
EventKind::Message => "message",
EventKind::Say => "say",
}
}
pub fn feedback() -> [EventKind; 4] {
[
EventKind::Comment,
EventKind::Drawing,
EventKind::PageEvent,
EventKind::Message,
]
}
pub fn parse_list(s: &str) -> Result<Vec<EventKind>, String> {
s.split(',')
.map(str::trim)
.filter(|p| !p.is_empty())
.map(EventKind::from_str)
.collect()
}
}
impl FromStr for EventKind {
type Err = String;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"comment" => Ok(EventKind::Comment),
"drawing" => Ok(EventKind::Drawing),
"page_event" => Ok(EventKind::PageEvent),
"message" => Ok(EventKind::Message),
"say" => Ok(EventKind::Say),
other => Err(format!("unknown event kind `{other}`")),
}
}
}
impl fmt::Display for EventKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, Default, PartialEq, Eq, Serialize, Deserialize)]
pub struct Anchor {
pub path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub quote: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub prefix: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub suffix: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub selector: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub heading: Option<String>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Size {
pub width: u32,
pub height: u32,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub struct Point {
pub x: i64,
pub y: i64,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EventOut {
pub id: i64,
pub doc_id: String,
pub kind: EventKind,
pub rev: i64,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub path: Option<String>,
pub payload: Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub attachment_url: Option<String>,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from: Option<Person>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SayOut {
pub id: i64,
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reply_to: Option<i64>,
#[serde(with = "time::serde::rfc3339")]
pub created_at: OffsetDateTime,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub from: Option<Person>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub backlog: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct NoteOut {
pub event: EventOut,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub delivered_to: Option<String>,
#[serde(default, skip_serializing_if = "std::ops::Not::not")]
pub backlog: bool,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct SayRequest {
pub text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reply_to: Option<i64>,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct EventsResponse {
pub events: Vec<EventOut>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct AttachmentResponse {
pub attachment_id: String,
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ServerMsg {
Hello {
doc_id: String,
rev: i64,
agents: usize,
browsers: usize,
},
Revision {
rev: i64,
changed: Vec<String>,
},
Presence {
agents: usize,
browsers: usize,
},
Event(EventOut),
Say(SayOut),
Note(NoteOut),
Delivered {
id: i64,
to: String,
},
Error {
message: String,
},
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum ClientMsg {
Comment {
anchor: Anchor,
text: String,
},
Drawing {
path: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
note: Option<String>,
attachment_id: String,
viewport: Size,
scroll: Point,
},
PageEvent {
name: String,
#[serde(default)]
data: Value,
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
},
Message {
text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
path: Option<String>,
},
Ack {
id: i64,
},
Say {
text: String,
#[serde(default, skip_serializing_if = "Option::is_none")]
reply_to: Option<i64>,
},
}