Compare commits
7 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| f2273b984c | |||
| 7be397b76e | |||
| d82a7674ce | |||
| 38723a4d20 | |||
| 1fc3b3dd4e | |||
| d2a684650a | |||
| 82659391f9 |
@@ -10,7 +10,7 @@ executors:
|
||||
type: boolean
|
||||
default: false
|
||||
docker:
|
||||
- image: plumeorg/plume-buildenv:v0.0.9
|
||||
- image: plumeorg/plume-buildenv:v0.0.7
|
||||
- image: <<#parameters.postgres>>circleci/postgres:9.6-alpine<</parameters.postgres>><<^parameters.postgres>>alpine:latest<</parameters.postgres>>
|
||||
environment:
|
||||
POSTGRES_USER: postgres
|
||||
@@ -82,7 +82,7 @@ commands:
|
||||
type: string
|
||||
steps:
|
||||
- run: zip -0 ccov.zip `find . -name 'plume*.gc*' -o -name 'plm*.gc*'`
|
||||
- run: grcov ccov.zip -s . -t lcov --llvm --branch --ignore-not-existing --ignore '/*' -o lcov.info
|
||||
- run: grcov ccov.zip -s . -t lcov --llvm --branch --ignore-not-existing --ignore-dir '/*' -o lcov.info
|
||||
- run: bash <(curl -s https://codecov.io/bash) -f lcov.info -F <<parameters.type>>
|
||||
- run: find . -name 'plume*.gc*' -delete -o -name 'plm*.gc*' -delete
|
||||
- run: rm ccov.zip lcov.info
|
||||
@@ -226,7 +226,6 @@ jobs:
|
||||
steps:
|
||||
- restore_env:
|
||||
cache: none
|
||||
- run: cargo build
|
||||
- run: crowdin upload -b master
|
||||
|
||||
workflows:
|
||||
|
||||
@@ -4,11 +4,11 @@ ENV PATH="/root/.cargo/bin:${PATH}"
|
||||
#install native/circleci/build dependancies
|
||||
RUN apt update &&\
|
||||
apt install -y --no-install-recommends git ssh tar gzip ca-certificates default-jre&&\
|
||||
apt install -y --no-install-recommends binutils-dev build-essential cmake curl gcc gettext git libcurl4-openssl-dev libdw-dev libelf-dev libiberty-dev libpq-dev libsqlite3-dev libssl-dev make openssl pkg-config postgresql postgresql-contrib python zlib1g-dev python3-pip zip unzip libclang-dev&&\
|
||||
apt install -y --no-install-recommends binutils-dev build-essential cmake curl gcc gettext git libcurl4-openssl-dev libdw-dev libelf-dev libiberty-dev libpq-dev libsqlite3-dev libssl-dev make openssl pkg-config postgresql postgresql-contrib python zlib1g-dev python3-pip zip unzip &&\
|
||||
rm -rf /var/lib/apt/lists/*
|
||||
|
||||
#install and configure rust
|
||||
RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly-2020-01-15 -y &&\
|
||||
RUN curl https://sh.rustup.rs -sSf | sh -s -- --default-toolchain nightly-2019-03-23 -y &&\
|
||||
rustup component add rustfmt clippy &&\
|
||||
rustup component add rust-std --target wasm32-unknown-unknown
|
||||
|
||||
|
||||
-47
@@ -1,47 +0,0 @@
|
||||
# This file contains your instance configuration
|
||||
# Some documentation about these variables is available here:
|
||||
# https://docs.joinplu.me/environment/
|
||||
|
||||
## GENERAL SETTINGS ##
|
||||
|
||||
# The directory containing database migrations
|
||||
# For Postgres: migrations/postgres
|
||||
# For SQlite: migrations/sqlite
|
||||
MIGRATION_DIRECTORY=migrations/postgres
|
||||
|
||||
# The URL of your database (or its path for SQlite databases)
|
||||
DATABASE_URL=postgres://plume:plume@localhost/plume
|
||||
|
||||
# The domain of your instance
|
||||
BASE_URL=plu.me
|
||||
|
||||
# The secret key for private cookies and CSRF protection
|
||||
# You can generate one with `openssl rand -base64 32`
|
||||
ROCKET_SECRET_KEY=
|
||||
|
||||
# Port and address which Plume will use
|
||||
ROCKET_PORT=7878
|
||||
ROCKET_ADDRESS=127.0.0.1
|
||||
|
||||
## MAIL CONFIG ##
|
||||
#MAIL_SERVER=smtp.plu.me
|
||||
#MAIL_ADDRESS=no-reply@plu.me
|
||||
#MAIL_USER=plume
|
||||
#MAIL_PASSWORD=
|
||||
#MAIL_HELO_NAME=no-reply@plu.me
|
||||
|
||||
## ADVANCED OPTIONS ##
|
||||
#MEDIA_UPLOAD_DIRECTORY=static/media
|
||||
#SEARCH_INDEX=search_index
|
||||
|
||||
# Sample logo configuration
|
||||
#PLUME_LOGO=icons/trwnh/paragraphs/plumeParagraphs.svg
|
||||
#PLUME_LOGO_FAVICON=icons/trwnh/paragraphs/plumeParagraphs32.png
|
||||
#PLUME_LOGO_48=icons/trwnh/paragraphs/plumeParagraphs48.png
|
||||
#PLUME_LOGO_72=icons/trwnh/paragraphs/plumeParagraphs72.png
|
||||
#PLUME_LOGO_96=icons/trwnh/paragraphs/plumeParagraphs96.png
|
||||
#PLUME_LOGO_144=icons/trwnh/paragraphs/plumeParagraphs144.png
|
||||
#PLUME_LOGO_160=icons/trwnh/paragraphs/plumeParagraphs160.png
|
||||
#PLUME_LOGO_192=icons/trwnh/paragraphs/plumeParagraphs192.png
|
||||
#PLUME_LOGO_256=icons/trwnh/paragraphs/plumeParagraphs256.png
|
||||
#PLUME_LOGO_512=icons/trwnh/paragraphs/plumeParagraphs512.png
|
||||
+4
-2
@@ -7,7 +7,7 @@ translations
|
||||
.env
|
||||
Rocket.toml
|
||||
!.gitkeep
|
||||
static
|
||||
static/media
|
||||
docker-compose.yml
|
||||
*.db
|
||||
*.sqlite
|
||||
@@ -16,6 +16,8 @@ docker-compose.yml
|
||||
tags.*
|
||||
!tags.rs
|
||||
search_index
|
||||
main.css
|
||||
*.wasm
|
||||
*.js
|
||||
.buildconfig
|
||||
__pycache__
|
||||
.vscode/
|
||||
|
||||
+46
-2
@@ -1,2 +1,46 @@
|
||||
You can read our Code of Conduct [here](https://docs.joinplu.me/organization/code-of-conduct).
|
||||
By contributing to this repository, you agree to be bound by this Code of Conduct.
|
||||
# Contributor Covenant Code of Conduct
|
||||
|
||||
## Our Pledge
|
||||
|
||||
In the interest of fostering an open and welcoming environment, we as contributors and maintainers pledge to making participation in our project and our community a harassment-free experience for everyone, regardless of age, body size, disability, ethnicity, gender identity and expression, level of experience, nationality, personal appearance, race, religion, sexual identity and orientation, etc.
|
||||
|
||||
## Our Standards
|
||||
|
||||
Examples of behavior that contributes to creating a positive environment include:
|
||||
|
||||
* Using welcoming and inclusive language
|
||||
* Being respectful of differing viewpoints and experiences
|
||||
* Gracefully accepting constructive criticism
|
||||
* Focusing on what is best for the community
|
||||
* Showing empathy towards other community members
|
||||
|
||||
Examples of unacceptable behavior by participants include:
|
||||
|
||||
* The use of sexualized language or imagery and unwelcome sexual attention or advances
|
||||
* Trolling, insulting/derogatory comments, and personal or political attacks
|
||||
* Public or private harassment
|
||||
* Publishing others' private information, such as a physical or electronic address, without explicit permission
|
||||
* Other conduct which could reasonably be considered inappropriate in a professional setting
|
||||
|
||||
## Our Responsibilities
|
||||
|
||||
Project maintainers are responsible for clarifying the standards of acceptable behavior and are expected to take appropriate and fair corrective action in response to any instances of unacceptable behavior.
|
||||
|
||||
Project maintainers have the right and responsibility to remove, edit, or reject comments, commits, code, wiki edits, issues, and other contributions that are not aligned to this Code of Conduct, or to ban temporarily or permanently any contributor for other behaviors that they deem inappropriate, threatening, offensive, or harmful.
|
||||
|
||||
## Scope
|
||||
|
||||
This Code of Conduct applies both within project spaces and in public spaces when an individual is representing the project or its community. Examples of representing a project or community include using an official project e-mail address, posting via an official social media account, or acting as an appointed representative at an online or offline event. Representation of a project may be further defined and clarified by project maintainers.
|
||||
|
||||
## Enforcement
|
||||
|
||||
Instances of abusive, harassing, or otherwise unacceptable behavior may be reported by contacting the project team at baptiste@gelez.xyz. The project team will review and investigate all complaints, and will respond in a way that it deems appropriate to the circumstances. The project team is obligated to maintain confidentiality with regard to the reporter of an incident. Further details of specific enforcement policies may be posted separately.
|
||||
|
||||
Project maintainers who do not follow or enforce the Code of Conduct in good faith may face temporary or permanent repercussions as determined by other members of the project's leadership.
|
||||
|
||||
## Attribution
|
||||
|
||||
This Code of Conduct is adapted from the [Contributor Covenant][homepage], version 1.4, available at [http://contributor-covenant.org/version/1/4][version]
|
||||
|
||||
[homepage]: http://contributor-covenant.org
|
||||
[version]: http://contributor-covenant.org/version/1/4/
|
||||
|
||||
Generated
+1817
-2268
File diff suppressed because it is too large
Load Diff
+18
-23
@@ -1,16 +1,13 @@
|
||||
[package]
|
||||
authors = ["Plume contributors"]
|
||||
name = "plume"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
repository = "https://github.com/Plume-org/Plume"
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
activitypub = "0.1.3"
|
||||
askama_escape = "0.1"
|
||||
async-trait = "*"
|
||||
atom_syndication = "0.6"
|
||||
clap = "2.33"
|
||||
colored = "1.8"
|
||||
dotenv = "0.14"
|
||||
gettext = { git = "https://github.com/Plume-org/gettext/", rev = "294c54d74c699fbc66502b480a37cc66c1daa7f3" }
|
||||
@@ -18,22 +15,22 @@ gettext-macros = { git = "https://github.com/Plume-org/gettext-macros/", rev = "
|
||||
gettext-utils = { git = "https://github.com/Plume-org/gettext-macros/", rev = "a7c605f7edd6bfbfbfe7778026bfefd88d82db10" }
|
||||
guid-create = "0.1"
|
||||
heck = "0.3.0"
|
||||
lettre = "0.9.2"
|
||||
lettre_email = "0.9.2"
|
||||
num_cpus = "1.10"
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" }
|
||||
rocket_contrib = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" , features = ["json"] }
|
||||
rpassword = "4.0"
|
||||
scheduled-thread-pool = "0.2.2"
|
||||
lettre = { git = "https://github.com/lettre/lettre", rev = "c988b1760ad8179d9e7f3fb8594d2b86cf2a0a49" }
|
||||
lettre_email = { git = "https://github.com/lettre/lettre", rev = "c988b1760ad8179d9e7f3fb8594d2b86cf2a0a49" }
|
||||
num_cpus = "1.0"
|
||||
rocket = "0.4.0"
|
||||
rocket_contrib = { version = "0.4.0", features = ["json"] }
|
||||
rocket_i18n = { git = "https://github.com/Plume-org/rocket_i18n", rev = "e922afa7c366038b3433278c03b1456b346074f2" }
|
||||
rpassword = "3.0"
|
||||
runtime-fmt = "0.3.0"
|
||||
scheduled-thread-pool = "0.2.0"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
serde_qs = "0.5"
|
||||
serde_qs = "0.4"
|
||||
shrinkwraprs = "0.2.1"
|
||||
syntect = "3.3"
|
||||
tokio = "0.2"
|
||||
validator = "0.10"
|
||||
validator_derive = "0.10"
|
||||
webfinger = { git = "https://github.com/Plume-org/webfinger", rev = "4e8f12810c4a7ba7a07bbcb722cd265fdff512b6", features = ["async"] }
|
||||
validator = "0.8"
|
||||
validator_derive = "0.8"
|
||||
webfinger = "0.3.1"
|
||||
|
||||
[[bin]]
|
||||
name = "plume"
|
||||
@@ -65,14 +62,12 @@ path = "plume-common"
|
||||
[dependencies.plume-models]
|
||||
path = "plume-models"
|
||||
|
||||
[dependencies.rocket_i18n]
|
||||
git = "https://github.com/Plume-org/rocket_i18n"
|
||||
branch = "go-async"
|
||||
default-features = false
|
||||
features = ["rocket"]
|
||||
[dependencies.rocket_csrf]
|
||||
git = "https://github.com/fdb-hiroshima/rocket_csrf"
|
||||
rev = "4a72ea2ec716cb0b26188fb00bccf2ef7d1e031c"
|
||||
|
||||
[build-dependencies]
|
||||
ructe = "0.9.0"
|
||||
ructe = "0.6.2"
|
||||
rsass = "0.9"
|
||||
|
||||
[features]
|
||||
|
||||
+1
-2
@@ -10,8 +10,7 @@ RUN apt-get update && apt-get install -y --no-install-recommends \
|
||||
gcc \
|
||||
make \
|
||||
openssl \
|
||||
libssl-dev \
|
||||
clang
|
||||
libssl-dev
|
||||
|
||||
WORKDIR /scratch
|
||||
COPY script/wasm-deps.sh .
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
<!--
|
||||
If your issue is not purely technical, it should be debated on Loomio first
|
||||
|
||||
https://framavox.org/g/WK40YHMA/plume
|
||||
-->
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Plume version (and/or commit):
|
||||
@@ -1,55 +1,29 @@
|
||||
<h1 align="center">
|
||||
<img src="https://raw.githubusercontent.com/Plume-org/Plume/master/assets/icons/trwnh/feather/plumeFeather64.png" alt="Plume's logo">
|
||||
<img src="https://raw.githubusercontent.com/Plume-org/Plume/master/static/icons/trwnh/feather/plumeFeather64.png" alt="Plume's logo">
|
||||
Plume
|
||||
</h1>
|
||||
<p align="center">
|
||||
<a href="https://github.com/Plume-org/Plume/"><img alt="CircleCI" src="https://img.shields.io/circleci/build/gh/Plume-org/Plume.svg"></a>
|
||||
<a href="https://codecov.io/gh/Plume-org/Plume"><img src="https://codecov.io/gh/Plume-org/Plume/branch/master/graph/badge.svg" alt="Code coverage"></a>
|
||||
<a title="Crowdin" target="_blank" href="https://crowdin.com/project/plume"><img src="https://d322cqt584bo4o.cloudfront.net/plume/localized.svg"></a>
|
||||
<a href="https://hub.docker.com/r/plumeorg/plume"><img alt="Docker Pulls" src="https://img.shields.io/docker/pulls/plumeorg/plume.svg"></a>
|
||||
<a href="https://liberapay.com/Plume"><img alt="Liberapay patrons" src="https://img.shields.io/liberapay/patrons/Plume.svg"></a>
|
||||
<a href="https://travis-ci.org/Plume-org/Plume"><img src="https://travis-ci.org/Plume-org/Plume.svg?branch=master" alt="Build status"></a>
|
||||
<a href="hhttps://codecov.io/gh/Plume-org/Plume"><img src="https://codecov.io/gh/Plume-org/Plume/branch/master/graph/badge.svg" alt="Code coverage"></a>
|
||||
</p>
|
||||
<p align="center">
|
||||
<a href="https://joinplu.me/">Website</a>
|
||||
—
|
||||
<a href="https://docs.joinplu.me/">Documentation</a>
|
||||
—
|
||||
<a href="https://docs.joinplu.me/contribute">Contribute</a>
|
||||
<a href="https://contribute.joinplu.me/">Contribute</a>
|
||||
—
|
||||
<a href="https://joinplu.me/#instances">Instances list</a>
|
||||
</p>
|
||||
|
||||
Plume is a **federated blogging engine**, based on *ActivityPub*. It is written in *Rust*, with the *Rocket* framework, and *Diesel* to interact with the database.
|
||||
The front-end uses *Ructe* templates, *WASM* and *SCSS*.
|
||||
Plume is a federated blogging engine, based on ActivityPub. It uses the Rocket framework, and Diesel to interact with the database.
|
||||
|
||||
## Features
|
||||
It is not yet ready for production use, but we have all the basic features (account management, blogs, articles, comments, etc) and a basic federation.
|
||||
|
||||
A lot of features are still missing, but what is already here should be quite stable. Current and planned features include:
|
||||
Feel free to join our Matrix room: `#plume:disroot.org` to discuss about the project!
|
||||
|
||||
- **A blog-centric approach**: you can create as much blogs as you want with your account, to keep your different publications separated.
|
||||
- **Media management**: you can upload pictures to illustrate your articles, but also audio files if you host a podcast, and manage them all from Plume.
|
||||
- **Federation**: Plume is part of a network of interconnected websites called the Fediverse. Each of these websites (often called *instances*) have their own
|
||||
rules and thematics, but they can all communicate with each other.
|
||||
- **Collaborative writing**: invite other people to your blogs, and write articles together.
|
||||
We also have a [Loomio group](https://framavox.org/g/WK40YHMA/plume) to debate and vote and features. Feel free to join it to take part in Plume's decision process.
|
||||
|
||||
## Get involved
|
||||
For more informations on the different ways to contribute, check out our [contribution page](https://contribute.joinplu.me). We are welcoming any kind of contribution, and you can probably find a way to help.
|
||||
|
||||
If you want to have regular news about the project, the best place is probably [our blog](https://fediverse.blog/~/PlumeDev), or our Matrix room: [`#plume:disroot.org`](https://riot.im/app/#/room/#plume:disroot.org).
|
||||
|
||||
If you want to contribute more, a good first step is to read [our contribution guides](https://docs.joinplu.me/contribute). We accept all kind of contribution:
|
||||
|
||||
- [Back-end or front-end development](https://docs.joinplu.me/contribute/development/)
|
||||
- [Translations](https://docs.joinplu.me/contribute/translations/)
|
||||
- [Documentation](https://docs.joinplu.me/contribute/documentation/)
|
||||
- UI and/or UX design (we don't have a dedicated guide yet, but [we can talk](https://docs.joinplu.me/contribute/discussion/) to see how we can work together!)
|
||||
- [Taking part in discussions](https://docs.joinplu.me/contribute/discussion/)
|
||||
- [Financial support](https://docs.joinplu.me/contribute/donations/)
|
||||
|
||||
But this list is not exhaustive and if you want to contribute differently you are welcome too!
|
||||
|
||||
As we want the various spaces related to the project (GitHub, Matrix, Loomio, etc) to be as safe as possible for everyone, we adopted [a code of conduct](https://docs.joinplu.me/organization/code-of-conduct). Please read it and make sure you accept it before contributing.
|
||||
|
||||
## Starting your own instance
|
||||
|
||||
We provide various way to install Plume: from source, with pre-built binaries, with Docker or with YunoHost.
|
||||
For detailed explanations, please refer to [the documentation](https://docs.joinplu.me/installation/).
|
||||
As we want the various spaces related to the project (GitHub, Matrix, Loomio, etc) to be as safe as possible for everyone, we adopted [a code of conduct](https://github.com/Plume-org/Plume/blob/master/CODE_OF_CONDUCT.md). Please read it and make sure you accept it before contributing.
|
||||
|
||||
@@ -1,3 +0,0 @@
|
||||
* {
|
||||
font-family: monospace;
|
||||
}
|
||||
@@ -1,28 +0,0 @@
|
||||
@import '_variables';
|
||||
|
||||
/* Color Scheme */
|
||||
$gray: #1a3854;
|
||||
$black: #102e4a;
|
||||
$white: #F8F8F8;
|
||||
$purple: #7765E3;
|
||||
$lightpurple: #c2bbee;
|
||||
$red: #d16666;
|
||||
$yellow: #ff934f;
|
||||
$blue: #7f96ff;
|
||||
|
||||
$background: $black;
|
||||
$form-input-background: $gray;
|
||||
$form-input-border: $white;
|
||||
$text-color: $white;
|
||||
$primary: $purple;
|
||||
$primary-text-color: $white; // text color on primary background (buttons for instance)
|
||||
$success-color: $blue;
|
||||
|
||||
//Code Highlighting
|
||||
|
||||
$code-keyword-color: #f79ac1;
|
||||
$code-source-color: #a6f0ab;
|
||||
$code-constant-color: #dfec84;
|
||||
$code-operator-color: #eddf95;
|
||||
$code-string-color: #f2ae60;
|
||||
$code-comment-color: #a3b4f9;
|
||||
@@ -1,37 +0,0 @@
|
||||
/* Color Scheme */
|
||||
$gray: #F3F3F3;
|
||||
$black: #242424;
|
||||
$white: #F8F8F8;
|
||||
$purple: #7765E3;
|
||||
$lightpurple: #c2bbee;
|
||||
$red: #E92F2F;
|
||||
$yellow: #ffe347;
|
||||
$green: #23f0c7;
|
||||
|
||||
$background: $white;
|
||||
$form-input-background: white;
|
||||
$form-input-border: $black;
|
||||
$text-color: $black;
|
||||
$primary: $purple;
|
||||
$primary-text-color: $white; // text color on primary background (buttons for instance)
|
||||
$success-color: $green;
|
||||
|
||||
/* Dimensions */
|
||||
|
||||
$article-width: 70ch;
|
||||
$horizontal-margin: 20%;
|
||||
$margin: 0 $horizontal-margin;
|
||||
|
||||
/* Fonts */
|
||||
|
||||
$route159: "Route159", serif;
|
||||
$playfair: "Playfair Display", serif;
|
||||
$lora: "Lora", serif;
|
||||
|
||||
//Code Highlighting
|
||||
$code-keyword-color: #45244a;
|
||||
$code-source-color: #4c588c;
|
||||
$code-constant-color: scale-color(magenta,$lightness:-5%);
|
||||
$code-operator-color: scale-color($code-source-color,$lightness:-5%);
|
||||
$code-string-color: #8a571c;
|
||||
$code-comment-color: #1c4c8a;
|
||||
@@ -1,12 +0,0 @@
|
||||
/* color palette: https://coolors.co/23f0c7-ef767a-7765e3-6457a6-ffe347 */
|
||||
|
||||
@import url('./feather.css');
|
||||
@import url('./fonts/Route159/Route159.css');
|
||||
@import url('./fonts/Lora/Lora.css');
|
||||
@import url('./fonts/Playfair_Display/PlayfairDisplay.css');
|
||||
|
||||
@import 'dark_variables';
|
||||
@import 'global';
|
||||
@import 'header';
|
||||
@import 'article';
|
||||
@import 'forms';
|
||||
@@ -1,12 +0,0 @@
|
||||
/* color palette: https://coolors.co/23f0c7-ef767a-7765e3-6457a6-ffe347 */
|
||||
|
||||
@import url('./feather.css');
|
||||
@import url('./fonts/Route159/Route159.css');
|
||||
@import url('./fonts/Lora/Lora.css');
|
||||
@import url('./fonts/Playfair_Display/PlayfairDisplay.css');
|
||||
|
||||
@import 'variables';
|
||||
@import 'global';
|
||||
@import 'header';
|
||||
@import 'article';
|
||||
@import 'forms';
|
||||
@@ -1,6 +1,8 @@
|
||||
use ructe::Ructe;
|
||||
extern crate rsass;
|
||||
extern crate ructe;
|
||||
use ructe::*;
|
||||
use std::process::{Command, Stdio};
|
||||
use std::{ffi::OsStr, fs::*, io::Write, path::*};
|
||||
use std::{env, fs::*, io::Write, path::PathBuf};
|
||||
|
||||
fn compute_static_hash() -> String {
|
||||
//"find static/ -type f ! -path 'static/media/*' | sort | xargs stat -c'%n %Y' | openssl dgst -r"
|
||||
@@ -35,17 +37,25 @@ fn compute_static_hash() -> String {
|
||||
}
|
||||
|
||||
fn main() {
|
||||
Ructe::from_env()
|
||||
.expect("This must be run with cargo")
|
||||
.compile_templates("templates")
|
||||
.expect("compile templates");
|
||||
let out_dir = PathBuf::from(env::var("OUT_DIR").unwrap());
|
||||
let in_dir = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join("templates");
|
||||
compile_templates(&in_dir, &out_dir).expect("compile templates");
|
||||
|
||||
compile_themes().expect("Theme compilation error");
|
||||
recursive_copy(&Path::new("assets").join("icons"), &Path::new("static"))
|
||||
.expect("Couldn't copy icons");
|
||||
recursive_copy(&Path::new("assets").join("images"), &Path::new("static"))
|
||||
.expect("Couldn't copy images");
|
||||
create_dir_all(&Path::new("static").join("media")).expect("Couldn't init media directory");
|
||||
println!("cargo:rerun-if-changed=static/css/_article.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_forms.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_global.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_header.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/_variables.scss");
|
||||
println!("cargo:rerun-if-changed=static/css/main.scss");
|
||||
let mut out = File::create("static/css/main.css").expect("Couldn't create main.css");
|
||||
out.write_all(
|
||||
&rsass::compile_scss_file(
|
||||
"static/css/main.scss".as_ref(),
|
||||
rsass::OutputStyle::Compressed,
|
||||
)
|
||||
.expect("Error during SCSS compilation"),
|
||||
)
|
||||
.expect("Couldn't write CSS output");
|
||||
|
||||
let cache_id = &compute_static_hash()[..8];
|
||||
println!("cargo:rerun-if-changed=target/deploy/plume-front.wasm");
|
||||
@@ -64,91 +74,3 @@ fn main() {
|
||||
|
||||
println!("cargo:rustc-env=CACHE_ID={}", cache_id)
|
||||
}
|
||||
|
||||
fn compile_themes() -> std::io::Result<()> {
|
||||
let input_dir = Path::new("assets").join("themes");
|
||||
let output_dir = Path::new("static").join("css");
|
||||
|
||||
let themes = find_themes(input_dir)?;
|
||||
|
||||
for theme in themes {
|
||||
compile_theme(&theme, &output_dir)?;
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn find_themes(path: PathBuf) -> std::io::Result<Vec<PathBuf>> {
|
||||
let ext = path.extension().and_then(OsStr::to_str);
|
||||
if metadata(&path)?.is_dir() {
|
||||
Ok(read_dir(&path)?.fold(vec![], |mut themes, ch| {
|
||||
if let Ok(ch) = ch {
|
||||
if let Ok(mut new) = find_themes(ch.path()) {
|
||||
themes.append(&mut new);
|
||||
}
|
||||
}
|
||||
themes
|
||||
}))
|
||||
} else if (ext == Some("scss") || ext == Some("sass"))
|
||||
&& !path.file_name().unwrap().to_str().unwrap().starts_with('_')
|
||||
{
|
||||
Ok(vec![path.clone()])
|
||||
} else {
|
||||
Ok(vec![])
|
||||
}
|
||||
}
|
||||
|
||||
fn compile_theme(path: &Path, out_dir: &Path) -> std::io::Result<()> {
|
||||
let name = path
|
||||
.components()
|
||||
.skip_while(|c| *c != Component::Normal(OsStr::new("themes")))
|
||||
.skip(1)
|
||||
.filter_map(|c| {
|
||||
c.as_os_str()
|
||||
.to_str()
|
||||
.unwrap_or_default()
|
||||
.splitn(2, '.')
|
||||
.next()
|
||||
})
|
||||
.collect::<Vec<_>>()
|
||||
.join("-");
|
||||
|
||||
let dir = path.parent().unwrap();
|
||||
|
||||
let out = out_dir.join(name);
|
||||
create_dir_all(&out)?;
|
||||
|
||||
// copy files of the theme that are not scss
|
||||
for ch in read_dir(&dir)? {
|
||||
recursive_copy(&ch?.path(), &out)?;
|
||||
}
|
||||
|
||||
// compile the .scss/.sass file
|
||||
let mut out = File::create(out.join("theme.css"))?;
|
||||
out.write_all(
|
||||
&rsass::compile_scss_file(path, rsass::OutputStyle::Compressed)
|
||||
.expect("SCSS compilation error"),
|
||||
)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn recursive_copy(path: &Path, out_dir: &Path) -> std::io::Result<()> {
|
||||
if metadata(path)?.is_dir() {
|
||||
let out = out_dir.join(path.file_name().unwrap());
|
||||
create_dir_all(out.clone())?;
|
||||
|
||||
for ch in read_dir(path)? {
|
||||
recursive_copy(&ch?.path(), &out)?;
|
||||
}
|
||||
} else {
|
||||
println!("cargo:rerun-if-changed={}", path.display());
|
||||
|
||||
let ext = path.extension().and_then(OsStr::to_str);
|
||||
if ext != Some("scss") && ext != Some("sass") {
|
||||
copy(path, out_dir.join(path.file_name().unwrap()))?;
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -0,0 +1,6 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
--#!|_conn, path: &Path| {
|
||||
--#! let mut pb = path.to_path_buf();
|
||||
--#! pb.push("search_index");
|
||||
--#! std::fs::remove_dir_all(pb).map_err(Error::from)
|
||||
--#!}
|
||||
@@ -0,0 +1,10 @@
|
||||
-- Your SQL goes here
|
||||
--#!|conn: &Connection, path: &Path| {
|
||||
--#! let mut pb = path.to_path_buf();
|
||||
--#! pb.push("search_index");
|
||||
--#! let searcher = super::search::Searcher::create(&pb)?;
|
||||
--#! searcher.fill(conn)?;
|
||||
--#! searcher.commit();
|
||||
--#! Ok(())
|
||||
--#!}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE password_reset_requests;
|
||||
@@ -1,9 +0,0 @@
|
||||
CREATE TABLE password_reset_requests (
|
||||
id SERIAL PRIMARY KEY,
|
||||
email VARCHAR NOT NULL,
|
||||
token VARCHAR NOT NULL,
|
||||
expiration_date TIMESTAMP NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX password_reset_requests_token ON password_reset_requests (token);
|
||||
CREATE UNIQUE INDEX password_reset_requests_email ON password_reset_requests (email);
|
||||
@@ -1,4 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
ALTER TABLE users ADD COLUMN is_admin BOOLEAN NOT NULL DEFAULT 'f';
|
||||
UPDATE users SET is_admin = 't' WHERE role = 0;
|
||||
ALTER TABLE users DROP COLUMN role;
|
||||
@@ -1,4 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
ALTER TABLE users ADD COLUMN role INTEGER NOT NULL DEFAULT 2;
|
||||
UPDATE users SET role = 0 WHERE is_admin = 't';
|
||||
ALTER TABLE users DROP COLUMN is_admin;
|
||||
@@ -1,4 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
ALTER TABLE blogs DROP COLUMN theme;
|
||||
ALTER TABLE users DROP COLUMN preferred_theme;
|
||||
ALTER TABLE users DROP COLUMN hide_custom_css;
|
||||
@@ -1,4 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
ALTER TABLE blogs ADD COLUMN theme VARCHAR;
|
||||
ALTER TABLE users ADD COLUMN preferred_theme VARCHAR;
|
||||
ALTER TABLE users ADD COLUMN hide_custom_css BOOLEAN NOT NULL DEFAULT 'f';
|
||||
@@ -1,6 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
DROP TABLE timeline;
|
||||
DROP TABLE timeline_definition;
|
||||
DROP TABLE list_elems;
|
||||
DROP TABLE lists;
|
||||
@@ -1,31 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
|
||||
CREATE TABLE timeline_definition(
|
||||
id SERIAL PRIMARY KEY,
|
||||
user_id integer REFERENCES users ON DELETE CASCADE,
|
||||
name VARCHAR NOT NULL,
|
||||
query VARCHAR NOT NULL,
|
||||
CONSTRAINT timeline_unique_user_name UNIQUE(user_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE timeline(
|
||||
id SERIAL PRIMARY KEY,
|
||||
post_id integer NOT NULL REFERENCES posts ON DELETE CASCADE,
|
||||
timeline_id integer NOT NULL REFERENCES timeline_definition ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE lists(
|
||||
id SERIAL PRIMARY KEY,
|
||||
name VARCHAR NOT NULL,
|
||||
user_id integer REFERENCES users ON DELETE CASCADE,
|
||||
type integer NOT NULL,
|
||||
CONSTRAINT list_unique_user_name UNIQUE(user_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE list_elems(
|
||||
id SERIAL PRIMARY KEY,
|
||||
list_id integer NOT NULL REFERENCES lists ON DELETE CASCADE,
|
||||
user_id integer REFERENCES users ON DELETE CASCADE,
|
||||
blog_id integer REFERENCES blogs ON DELETE CASCADE,
|
||||
word VARCHAR
|
||||
);
|
||||
@@ -1,4 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DELETE FROM timeline_definition WHERE name = 'Your feed';
|
||||
DELETE FROM timeline_definition WHERE name = 'Local feed' AND query = 'local';
|
||||
DELETE FROM timeline_definition WHERE name = 'Federared feed' AND query = 'all';
|
||||
@@ -1,6 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
INSERT INTO timeline_definition (name, query) VALUES
|
||||
('Local feed', 'local'),
|
||||
('Federated feed', 'all');
|
||||
INSERT INTO timeline_definition (user_id,name,query)
|
||||
select id,'Your feed',CONCAT('followed or [',fqn,']') from users;
|
||||
@@ -1,8 +0,0 @@
|
||||
DELETE FROM timeline WHERE id IN
|
||||
(
|
||||
SELECT timeline.id FROM timeline
|
||||
INNER JOIN timeline_definition ON timeline.timeline_id = timeline_definition.id
|
||||
WHERE timeline_definition.query LIKE 'followed or [%]' OR
|
||||
timeline_definition.query = 'local' OR
|
||||
timeline_definition.query = 'all'
|
||||
);
|
||||
@@ -1,18 +0,0 @@
|
||||
INSERT INTO timeline (post_id, timeline_id)
|
||||
SELECT posts.id,timeline_definition.id FROM posts,timeline_definition
|
||||
WHERE timeline_definition.query = 'all';
|
||||
|
||||
INSERT INTO timeline (post_id, timeline_id)
|
||||
SELECT posts.id,timeline_definition.id FROM posts
|
||||
CROSS JOIN timeline_definition
|
||||
INNER JOIN blogs ON posts.blog_id = blogs.id
|
||||
INNER JOIN instances ON blogs.instance_id = instances.id
|
||||
WHERE timeline_definition.query = 'local' and instances.local = true;
|
||||
|
||||
INSERT INTO timeline (post_id, timeline_id)
|
||||
SELECT posts.id,timeline_definition.id FROM posts
|
||||
INNER JOIN blog_authors ON posts.blog_id = blog_authors.blog_id
|
||||
LEFT JOIN follows ON blog_authors.author_id = follows.following_id
|
||||
INNER JOIN timeline_definition ON follows.follower_id = timeline_definition.user_id
|
||||
or blog_authors.author_id = timeline_definition.user_id
|
||||
WHERE timeline_definition.query LIKE 'followed or [%]';
|
||||
@@ -1,3 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
drop table email_blocklist;
|
||||
@@ -1,6 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TABLE email_blocklist(id SERIAL PRIMARY KEY,
|
||||
email_address TEXT UNIQUE,
|
||||
note TEXT,
|
||||
notify_user BOOLEAN DEFAULT FALSE,
|
||||
notification_text TEXT);
|
||||
@@ -0,0 +1,6 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
--#!|_conn, path: &Path| {
|
||||
--#! let mut pb = path.to_path_buf();
|
||||
--#! pb.push("search_index");
|
||||
--#! std::fs::remove_dir_all(pb).map_err(Error::from)
|
||||
--#!}
|
||||
@@ -0,0 +1,10 @@
|
||||
-- Your SQL goes here
|
||||
--#!|conn: &Connection, path: &Path| {
|
||||
--#! let mut pb = path.to_path_buf();
|
||||
--#! pb.push("search_index");
|
||||
--#! let searcher = super::search::Searcher::create(&pb)?;
|
||||
--#! searcher.fill(conn)?;
|
||||
--#! searcher.commit();
|
||||
--#! Ok(())
|
||||
--#!}
|
||||
|
||||
@@ -1 +0,0 @@
|
||||
DROP TABLE password_reset_requests;
|
||||
@@ -1,9 +0,0 @@
|
||||
CREATE TABLE password_reset_requests (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
email VARCHAR NOT NULL,
|
||||
token VARCHAR NOT NULL,
|
||||
expiration_date DATETIME NOT NULL
|
||||
);
|
||||
|
||||
CREATE INDEX password_reset_requests_token ON password_reset_requests (token);
|
||||
CREATE UNIQUE INDEX password_reset_requests_email ON password_reset_requests (email);
|
||||
@@ -1,72 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
CREATE TABLE IF NOT EXISTS "users_without_role" (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
username VARCHAR NOT NULL,
|
||||
display_name VARCHAR NOT NULL DEFAULT '',
|
||||
outbox_url VARCHAR NOT NULL UNIQUE,
|
||||
inbox_url VARCHAR NOT NULL UNIQUE,
|
||||
is_admin BOOLEAN NOT NULL DEFAULT 'f',
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
email TEXT,
|
||||
hashed_password TEXT,
|
||||
instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL,
|
||||
creation_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ap_url TEXT NOT NULL default '' UNIQUE,
|
||||
private_key TEXT,
|
||||
public_key TEXT NOT NULL DEFAULT '',
|
||||
shared_inbox_url VARCHAR,
|
||||
followers_endpoint VARCHAR NOT NULL DEFAULT '' UNIQUE,
|
||||
avatar_id INTEGER REFERENCES medias(id) ON DELETE CASCADE,
|
||||
last_fetched_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
fqn TEXT NOT NULL DEFAULT '',
|
||||
summary_html TEXT NOT NULL DEFAULT '',
|
||||
FOREIGN KEY (avatar_id) REFERENCES medias(id) ON DELETE SET NULL,
|
||||
CONSTRAINT blog_authors_unique UNIQUE (username, instance_id)
|
||||
);
|
||||
|
||||
INSERT INTO users_without_role SELECT
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
outbox_url,
|
||||
inbox_url,
|
||||
't',
|
||||
summary,
|
||||
email,
|
||||
hashed_password,
|
||||
instance_id,
|
||||
creation_date,
|
||||
ap_url,
|
||||
private_key,
|
||||
public_key,
|
||||
shared_inbox_url,
|
||||
followers_endpoint,
|
||||
avatar_id,
|
||||
last_fetched_date,
|
||||
fqn,
|
||||
summary
|
||||
FROM users WHERE role = 0;
|
||||
INSERT INTO users_without_role SELECT
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
outbox_url,
|
||||
inbox_url,
|
||||
'f',
|
||||
summary,
|
||||
email,
|
||||
hashed_password,
|
||||
instance_id,
|
||||
creation_date,
|
||||
ap_url,
|
||||
private_key,
|
||||
public_key,
|
||||
shared_inbox_url,
|
||||
followers_endpoint,
|
||||
avatar_id,
|
||||
last_fetched_date,
|
||||
fqn,
|
||||
summary
|
||||
FROM users WHERE role != 0;
|
||||
DROP TABLE users;
|
||||
ALTER TABLE users_without_role RENAME TO users;
|
||||
@@ -1,74 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
|
||||
CREATE TABLE IF NOT EXISTS "users_with_role" (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
username VARCHAR NOT NULL,
|
||||
display_name VARCHAR NOT NULL DEFAULT '',
|
||||
outbox_url VARCHAR NOT NULL UNIQUE,
|
||||
inbox_url VARCHAR NOT NULL UNIQUE,
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
email TEXT,
|
||||
hashed_password TEXT,
|
||||
instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL,
|
||||
creation_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ap_url TEXT NOT NULL default '' UNIQUE,
|
||||
private_key TEXT,
|
||||
public_key TEXT NOT NULL DEFAULT '',
|
||||
shared_inbox_url VARCHAR,
|
||||
followers_endpoint VARCHAR NOT NULL DEFAULT '' UNIQUE,
|
||||
avatar_id INTEGER REFERENCES medias(id) ON DELETE CASCADE,
|
||||
last_fetched_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
fqn TEXT NOT NULL DEFAULT '',
|
||||
summary_html TEXT NOT NULL DEFAULT '',
|
||||
role INTEGER NOT NULL DEFAULT 2,
|
||||
FOREIGN KEY (avatar_id) REFERENCES medias(id) ON DELETE SET NULL,
|
||||
CONSTRAINT blog_authors_unique UNIQUE (username, instance_id)
|
||||
);
|
||||
|
||||
|
||||
INSERT INTO users_with_role SELECT
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
outbox_url,
|
||||
inbox_url,
|
||||
summary,
|
||||
email,
|
||||
hashed_password,
|
||||
instance_id,
|
||||
creation_date,
|
||||
ap_url,
|
||||
private_key,
|
||||
public_key,
|
||||
shared_inbox_url,
|
||||
followers_endpoint,
|
||||
avatar_id,
|
||||
last_fetched_date,
|
||||
fqn,
|
||||
summary,
|
||||
0
|
||||
FROM users WHERE is_admin = 't';
|
||||
INSERT INTO users_with_role SELECT
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
outbox_url,
|
||||
inbox_url,
|
||||
summary,
|
||||
email,
|
||||
hashed_password,
|
||||
instance_id,
|
||||
creation_date,
|
||||
ap_url,
|
||||
private_key,
|
||||
public_key,
|
||||
shared_inbox_url,
|
||||
followers_endpoint,
|
||||
avatar_id,
|
||||
last_fetched_date,
|
||||
fqn,
|
||||
summary,
|
||||
2
|
||||
FROM users WHERE is_admin = 'f';
|
||||
DROP TABLE users;
|
||||
ALTER TABLE users_with_role RENAME TO users;
|
||||
@@ -1,88 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
CREATE TABLE blogs_before_themes (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
actor_id VARCHAR NOT NULL,
|
||||
title VARCHAR NOT NULL,
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
outbox_url VARCHAR NOT NULL UNIQUE,
|
||||
inbox_url VARCHAR NOT NULL UNIQUE,
|
||||
instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL,
|
||||
creation_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ap_url text not null default '' UNIQUE,
|
||||
private_key TEXT,
|
||||
public_key TEXT NOT NULL DEFAULT '',
|
||||
fqn TEXT NOT NULL DEFAULT '',
|
||||
summary_html TEXT NOT NULL DEFAULT '',
|
||||
icon_id INTEGER REFERENCES medias(id) ON DELETE SET NULL DEFAULT NULL,
|
||||
banner_id INTEGER REFERENCES medias(id) ON DELETE SET NULL DEFAULT NULL,
|
||||
CONSTRAINT blog_unique UNIQUE (actor_id, instance_id)
|
||||
);
|
||||
INSERT INTO blogs_before_themes SELECT
|
||||
id,
|
||||
actor_id,
|
||||
title,
|
||||
summary,
|
||||
outbox_url,
|
||||
inbox_url,
|
||||
instance_id,
|
||||
creation_date,
|
||||
ap_url,
|
||||
private_key,
|
||||
public_key,
|
||||
fqn,
|
||||
summary_html,
|
||||
icon_id,
|
||||
banner_id
|
||||
FROM blogs;
|
||||
DROP TABLE blogs;
|
||||
ALTER TABLE blogs_before_themes RENAME TO blogs;
|
||||
|
||||
CREATE TABLE users_before_themes (
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
username VARCHAR NOT NULL,
|
||||
display_name VARCHAR NOT NULL DEFAULT '',
|
||||
outbox_url VARCHAR NOT NULL UNIQUE,
|
||||
inbox_url VARCHAR NOT NULL UNIQUE,
|
||||
summary TEXT NOT NULL DEFAULT '',
|
||||
email TEXT,
|
||||
hashed_password TEXT,
|
||||
instance_id INTEGER REFERENCES instances(id) ON DELETE CASCADE NOT NULL,
|
||||
creation_date DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
ap_url TEXT NOT NULL default '' UNIQUE,
|
||||
private_key TEXT,
|
||||
public_key TEXT NOT NULL DEFAULT '',
|
||||
shared_inbox_url VARCHAR,
|
||||
followers_endpoint VARCHAR NOT NULL DEFAULT '' UNIQUE,
|
||||
avatar_id INTEGER REFERENCES medias(id) ON DELETE CASCADE,
|
||||
last_fetched_date TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
|
||||
fqn TEXT NOT NULL DEFAULT '',
|
||||
summary_html TEXT NOT NULL DEFAULT '',
|
||||
role INTEGER NOT NULL DEFAULT 2,
|
||||
FOREIGN KEY (avatar_id) REFERENCES medias(id) ON DELETE SET NULL,
|
||||
CONSTRAINT blog_authors_unique UNIQUE (username, instance_id)
|
||||
);
|
||||
INSERT INTO users_before_themes SELECT
|
||||
id,
|
||||
username,
|
||||
display_name,
|
||||
outbox_url,
|
||||
inbox_url,
|
||||
summary,
|
||||
email,
|
||||
hashed_password,
|
||||
instance_id,
|
||||
creation_date,
|
||||
ap_url,
|
||||
private_key,
|
||||
public_key,
|
||||
shared_inbox_url,
|
||||
followers_endpoint,
|
||||
avatar_id,
|
||||
last_fetched_date,
|
||||
fqn,
|
||||
summary_html,
|
||||
role
|
||||
FROM users;
|
||||
DROP TABLE users;
|
||||
ALTER TABLE users_before_themes RENAME TO users;
|
||||
@@ -1,4 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
ALTER TABLE blogs ADD COLUMN theme VARCHAR;
|
||||
ALTER TABLE users ADD COLUMN preferred_theme VARCHAR;
|
||||
ALTER TABLE users ADD COLUMN hide_custom_css BOOLEAN NOT NULL DEFAULT 'f';
|
||||
@@ -1,6 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
DROP TABLE timeline;
|
||||
DROP TABLE timeline_definition;
|
||||
DROP TABLE list_elems;
|
||||
DROP TABLE lists;
|
||||
@@ -1,31 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
|
||||
CREATE TABLE timeline_definition(
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
user_id INTEGER REFERENCES users(id) ON DELETE CASCADE,
|
||||
name VARCHAR NOT NULL,
|
||||
query VARCHAR NOT NULL,
|
||||
CONSTRAINT timeline_unique_user_name UNIQUE(user_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE timeline(
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
post_id integer NOT NULL REFERENCES posts(id) ON DELETE CASCADE,
|
||||
timeline_id integer NOT NULL REFERENCES timeline_definition(id) ON DELETE CASCADE
|
||||
);
|
||||
|
||||
CREATE TABLE lists(
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
name VARCHAR NOT NULL,
|
||||
user_id integer REFERENCES users(id) ON DELETE CASCADE,
|
||||
type integer NOT NULL,
|
||||
CONSTRAINT timeline_unique_user_name UNIQUE(user_id, name)
|
||||
);
|
||||
|
||||
CREATE TABLE list_elems(
|
||||
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||
list_id integer NOT NULL REFERENCES lists(id) ON DELETE CASCADE,
|
||||
user_id integer REFERENCES users(id) ON DELETE CASCADE,
|
||||
blog_id integer REFERENCES blogs(id) ON DELETE CASCADE,
|
||||
word VARCHAR
|
||||
);
|
||||
@@ -1,4 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
DELETE FROM timeline_definition WHERE name = 'Your feed';
|
||||
DELETE FROM timeline_definition WHERE name = 'Local feed' AND query = 'local';
|
||||
DELETE FROM timeline_definition WHERE name = 'Federared feed' AND query = 'all';
|
||||
@@ -1,6 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
INSERT INTO timeline_definition (name, query) VALUES
|
||||
('Local feed', 'local'),
|
||||
('Federated feed', 'all');
|
||||
INSERT INTO timeline_definition (user_id,name,query)
|
||||
select id,'Your feed','followed or ['||fqn||']' from users;
|
||||
@@ -1,8 +0,0 @@
|
||||
DELETE FROM timeline WHERE id IN
|
||||
(
|
||||
SELECT timeline.id FROM timeline
|
||||
INNER JOIN timeline_definition ON timeline.timeline_id = timeline_definition.id
|
||||
WHERE timeline_definition.query LIKE 'followed or [%]' OR
|
||||
timeline_definition.query = 'local' OR
|
||||
timeline_definition.query = 'all'
|
||||
);
|
||||
@@ -1,17 +0,0 @@
|
||||
INSERT INTO timeline (post_id, timeline_id)
|
||||
SELECT posts.id,timeline_definition.id FROM posts,timeline_definition
|
||||
WHERE timeline_definition.query = 'all';
|
||||
|
||||
INSERT INTO timeline (post_id, timeline_id)
|
||||
SELECT posts.id,timeline_definition.id FROM posts,timeline_definition
|
||||
INNER JOIN blogs ON posts.blog_id = blogs.id
|
||||
INNER JOIN instances ON blogs.instance_id = instances.id
|
||||
WHERE timeline_definition.query = 'local' and instances.local = 1;
|
||||
|
||||
INSERT INTO timeline (post_id, timeline_id)
|
||||
SELECT posts.id,timeline_definition.id FROM posts
|
||||
INNER JOIN blog_authors ON posts.blog_id = blog_authors.blog_id
|
||||
LEFT JOIN follows ON blog_authors.author_id = follows.following_id
|
||||
INNER JOIN timeline_definition ON follows.follower_id = timeline_definition.user_id
|
||||
or blog_authors.author_id = timeline_definition.user_id
|
||||
WHERE timeline_definition.query LIKE 'followed or [%]';
|
||||
@@ -1,3 +0,0 @@
|
||||
-- This file should undo anything in `up.sql`
|
||||
|
||||
drop table email_blocklist;
|
||||
@@ -1,6 +0,0 @@
|
||||
-- Your SQL goes here
|
||||
CREATE TABLE email_blocklist(id INTEGER PRIMARY KEY,
|
||||
email_address TEXT UNIQUE,
|
||||
note TEXT,
|
||||
notify_user BOOLEAN DEFAULT FALSE,
|
||||
notification_text TEXT);
|
||||
@@ -1,8 +1,7 @@
|
||||
[package]
|
||||
name = "plume-api"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
authors = ["Plume contributors"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
serde = "1.0"
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
|
||||
|
||||
@@ -1,8 +1,7 @@
|
||||
[package]
|
||||
name = "plume-cli"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
authors = ["Plume contributors"]
|
||||
edition = "2018"
|
||||
|
||||
[[bin]]
|
||||
name = "plm"
|
||||
@@ -11,7 +10,7 @@ path = "src/main.rs"
|
||||
[dependencies]
|
||||
clap = "2.33"
|
||||
dotenv = "0.14"
|
||||
rpassword = "4.0"
|
||||
rpassword = "3.0"
|
||||
|
||||
[dependencies.diesel]
|
||||
features = ["r2d2", "chrono"]
|
||||
|
||||
@@ -1,4 +1,8 @@
|
||||
use dotenv;
|
||||
extern crate clap;
|
||||
extern crate diesel;
|
||||
extern crate dotenv;
|
||||
extern crate plume_models;
|
||||
extern crate rpassword;
|
||||
|
||||
use clap::App;
|
||||
use diesel::Connection;
|
||||
@@ -21,11 +25,7 @@ fn main() {
|
||||
.subcommand(users::command());
|
||||
let matches = app.clone().get_matches();
|
||||
|
||||
match dotenv::dotenv() {
|
||||
Ok(path) => println!("Configuration read from {}", path.display()),
|
||||
Err(ref e) if e.not_found() => eprintln!("no .env was found"),
|
||||
e => e.map(|_| ()).unwrap(),
|
||||
}
|
||||
dotenv::dotenv().ok();
|
||||
let conn = Conn::establish(CONFIG.database_url.as_str());
|
||||
let _ = conn.as_ref().map(|conn| Instance::cache_local(conn));
|
||||
|
||||
|
||||
+5
-11
@@ -93,11 +93,8 @@ fn init<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
}
|
||||
|
||||
fn refill<'a>(args: &ArgMatches<'a>, conn: &Connection, searcher: Option<Searcher>) {
|
||||
let path = args.value_of("path");
|
||||
let path = match path {
|
||||
Some(path) => Path::new(path).join("search_index"),
|
||||
None => Path::new(&CONFIG.search_index).to_path_buf(),
|
||||
};
|
||||
let path = args.value_of("path").unwrap_or(".");
|
||||
let path = Path::new(path).join("search_index");
|
||||
let searcher = searcher.unwrap_or_else(|| Searcher::open(&path).unwrap());
|
||||
|
||||
searcher.fill(conn).expect("Couldn't import post");
|
||||
@@ -106,12 +103,9 @@ fn refill<'a>(args: &ArgMatches<'a>, conn: &Connection, searcher: Option<Searche
|
||||
}
|
||||
|
||||
fn unlock<'a>(args: &ArgMatches<'a>) {
|
||||
let path = match args.value_of("path") {
|
||||
None => Path::new(&CONFIG.search_index),
|
||||
Some(x) => Path::new(x),
|
||||
};
|
||||
let meta = Path::new(path).join(".tantivy-meta.lock");
|
||||
let path = args.value_of("path").unwrap_or(".");
|
||||
let meta = Path::new(path).join("search_index/.tantivy-meta.lock");
|
||||
remove_file(meta).unwrap();
|
||||
let writer = Path::new(path).join(".tantivy-writer.lock");
|
||||
let writer = Path::new(path).join("search_index/.tantivy-writer.lock");
|
||||
remove_file(writer).unwrap();
|
||||
}
|
||||
|
||||
+1
-17
@@ -52,12 +52,6 @@ pub fn command<'a, 'b>() -> App<'a, 'b> {
|
||||
.long("admin")
|
||||
.help("Makes the user an administrator of the instance"),
|
||||
)
|
||||
.arg(
|
||||
Arg::with_name("moderator")
|
||||
.short("m")
|
||||
.long("moderator")
|
||||
.help("Makes the user a moderator of the instance"),
|
||||
)
|
||||
.about("Create a new user on this instance"),
|
||||
)
|
||||
.subcommand(
|
||||
@@ -100,17 +94,7 @@ fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
.value_of("display-name")
|
||||
.map(String::from)
|
||||
.unwrap_or_else(|| super::ask_for("Display name"));
|
||||
|
||||
let admin = args.is_present("admin");
|
||||
let moderator = args.is_present("moderator");
|
||||
let role = if admin {
|
||||
Role::Admin
|
||||
} else if moderator {
|
||||
Role::Moderator
|
||||
} else {
|
||||
Role::Normal
|
||||
};
|
||||
|
||||
let bio = args.value_of("biography").unwrap_or("").to_string();
|
||||
let email = args
|
||||
.value_of("email")
|
||||
@@ -129,7 +113,7 @@ fn new<'a>(args: &ArgMatches<'a>, conn: &Connection) {
|
||||
conn,
|
||||
username,
|
||||
display_name,
|
||||
role,
|
||||
admin,
|
||||
&bio,
|
||||
email,
|
||||
User::hash_pass(&password).expect("Couldn't hash password"),
|
||||
|
||||
+5
-13
@@ -1,28 +1,24 @@
|
||||
[package]
|
||||
name = "plume-common"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
authors = ["Plume contributors"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
activitypub = "0.1.1"
|
||||
activitystreams-derive = "0.2"
|
||||
activitystreams-derive = "0.1.0"
|
||||
activitystreams-traits = "0.1.0"
|
||||
array_tool = "1.0"
|
||||
base64 = "0.10"
|
||||
futures-util = "*"
|
||||
heck = "0.3.0"
|
||||
hex = "0.3"
|
||||
hyper = "0.13"
|
||||
hyper = "0.12.28"
|
||||
openssl = "0.10.22"
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" }
|
||||
rocket = "0.4.0"
|
||||
reqwest = "0.9"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
shrinkwraprs = "0.2.1"
|
||||
syntect = "3.3"
|
||||
tokio = "0.2"
|
||||
regex-syntax = { version = "0.6.17", default-features = false, features = ["unicode-perl"] }
|
||||
|
||||
[dependencies.chrono]
|
||||
features = ["serde"]
|
||||
@@ -31,7 +27,3 @@ version = "0.4"
|
||||
[dependencies.pulldown-cmark]
|
||||
default-features = false
|
||||
version = "0.2.0"
|
||||
|
||||
[dependencies.reqwest]
|
||||
features = ["json", "blocking"]
|
||||
version = "0.10"
|
||||
|
||||
@@ -86,7 +86,7 @@ where
|
||||
/// - the context to be passed to each handler.
|
||||
/// - the activity
|
||||
/// - the reason it has not been handled yet
|
||||
NotHandled(&'a mut C, serde_json::Value, InboxError<E>),
|
||||
NotHandled(&'a C, serde_json::Value, InboxError<E>),
|
||||
|
||||
/// A matching handler have been found but failed
|
||||
///
|
||||
@@ -139,16 +139,16 @@ where
|
||||
///
|
||||
/// - `ctx`: the context to pass to each handler
|
||||
/// - `json`: the JSON representation of the incoming activity
|
||||
pub fn handle(ctx: &'a mut C, json: serde_json::Value) -> Inbox<'a, C, E, R> {
|
||||
pub fn handle(ctx: &'a C, json: serde_json::Value) -> Inbox<'a, C, E, R> {
|
||||
Inbox::NotHandled(ctx, json, InboxError::NoMatch)
|
||||
}
|
||||
|
||||
/// Registers an handler on this Inbox.
|
||||
pub fn with<A, V, M>(self) -> Inbox<'a, C, E, R>
|
||||
where
|
||||
A: AsActor<&'a mut C> + FromId<C, Error = E>,
|
||||
A: AsActor<&'a C> + FromId<C, Error = E>,
|
||||
V: activitypub::Activity,
|
||||
M: AsObject<A, V, &'a mut C, Error = E> + FromId<C, Error = E>,
|
||||
M: AsObject<A, V, &'a C, Error = E> + FromId<C, Error = E>,
|
||||
M::Output: Into<R>,
|
||||
{
|
||||
if let Inbox::NotHandled(ctx, mut act, e) = self {
|
||||
@@ -264,7 +264,7 @@ pub trait FromId<C>: Sized {
|
||||
/// - `object`: optional object that will be used if the object was not found in the database
|
||||
/// If absent, the ID will be dereferenced.
|
||||
fn from_id(
|
||||
ctx: &mut C,
|
||||
ctx: &C,
|
||||
id: &str,
|
||||
object: Option<Self::Object>,
|
||||
) -> Result<Self, (Option<serde_json::Value>, Self::Error)> {
|
||||
@@ -279,9 +279,8 @@ pub trait FromId<C>: Sized {
|
||||
|
||||
/// Dereferences an ID
|
||||
fn deref(id: &str) -> Result<Self::Object, (Option<serde_json::Value>, Self::Error)> {
|
||||
// Use blocking reqwest API here, since defer cannot be async (yet)
|
||||
reqwest::blocking::Client::builder()
|
||||
.connect_timeout(std::time::Duration::from_secs(5))
|
||||
reqwest::ClientBuilder::new()
|
||||
.connect_timeout(Some(std::time::Duration::from_secs(5)))
|
||||
.build()
|
||||
.map_err(|_| (None, InboxError::DerefError.into()))?
|
||||
.get(id)
|
||||
@@ -297,7 +296,7 @@ pub trait FromId<C>: Sized {
|
||||
)
|
||||
.send()
|
||||
.map_err(|_| (None, InboxError::DerefError))
|
||||
.and_then(|r| {
|
||||
.and_then(|mut r| {
|
||||
let json: serde_json::Value = r
|
||||
.json()
|
||||
.map_err(|_| (None, InboxError::InvalidObject(None)))?;
|
||||
@@ -308,10 +307,10 @@ pub trait FromId<C>: Sized {
|
||||
}
|
||||
|
||||
/// Builds a `Self` from its ActivityPub representation
|
||||
fn from_activity(ctx: &mut C, activity: Self::Object) -> Result<Self, Self::Error>;
|
||||
fn from_activity(ctx: &C, activity: Self::Object) -> Result<Self, Self::Error>;
|
||||
|
||||
/// Tries to find a `Self` with a given ID (`id`), using `ctx` (a database)
|
||||
fn from_db(ctx: &mut C, id: &str) -> Result<Self, Self::Error>;
|
||||
fn from_db(ctx: &C, id: &str) -> Result<Self, Self::Error>;
|
||||
}
|
||||
|
||||
/// Should be implemented by anything representing an ActivityPub actor.
|
||||
|
||||
@@ -4,9 +4,10 @@ use reqwest::ClientBuilder;
|
||||
use rocket::{
|
||||
http::Status,
|
||||
request::{FromRequest, Request},
|
||||
response::{Responder, Response, Result},
|
||||
response::{Responder, Response},
|
||||
Outcome,
|
||||
};
|
||||
use serde_json;
|
||||
|
||||
use self::sign::Signable;
|
||||
|
||||
@@ -60,45 +61,39 @@ impl<T> ActivityStream<T> {
|
||||
ActivityStream(t)
|
||||
}
|
||||
}
|
||||
#[rocket::async_trait]
|
||||
impl<'r, O: Object + Send + 'r> Responder<'r> for ActivityStream<O> {
|
||||
async fn respond_to(self, request: &'r Request<'_>) -> Result<'r> {
|
||||
|
||||
impl<'r, O: Object> Responder<'r> for ActivityStream<O> {
|
||||
fn respond_to(self, request: &Request) -> Result<Response<'r>, Status> {
|
||||
let mut json = serde_json::to_value(&self.0).map_err(|_| Status::InternalServerError)?;
|
||||
json["@context"] = context();
|
||||
let result = serde_json::to_string(&json).map_err(rocket::response::Debug);
|
||||
match result.respond_to(request).await {
|
||||
Ok(r) => Response::build_from(r)
|
||||
serde_json::to_string(&json).respond_to(request).map(|r| {
|
||||
Response::build_from(r)
|
||||
.raw_header("Content-Type", "application/activity+json")
|
||||
.ok(),
|
||||
Err(e) => Err(e),
|
||||
}
|
||||
.finalize()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone)]
|
||||
pub struct ApRequest;
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for ApRequest {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> {
|
||||
fn from_request(request: &'a Request<'r>) -> Outcome<Self, (Status, Self::Error), ()> {
|
||||
request
|
||||
.headers()
|
||||
.get_one("Accept")
|
||||
.map(|header| {
|
||||
header
|
||||
.split(',')
|
||||
.map(|ct| {
|
||||
match ct.trim() {
|
||||
// bool for Forward: true if found a valid Content-Type for Plume first (HTML),
|
||||
// false otherwise
|
||||
"application/ld+json; profile=\"https://w3.org/ns/activitystreams\""
|
||||
| "application/ld+json;profile=\"https://w3.org/ns/activitystreams\""
|
||||
| "application/activity+json"
|
||||
| "application/ld+json" => Outcome::Success(ApRequest),
|
||||
"text/html" => Outcome::Forward(true),
|
||||
_ => Outcome::Forward(false),
|
||||
}
|
||||
.map(|ct| match ct.trim() {
|
||||
// bool for Forward: true if found a valid Content-Type for Plume first (HTML), false otherwise
|
||||
"application/ld+json; profile=\"https://w3.org/ns/activitystreams\""
|
||||
| "application/ld+json;profile=\"https://w3.org/ns/activitystreams\""
|
||||
| "application/activity+json"
|
||||
| "application/ld+json" => Outcome::Success(ApRequest),
|
||||
"text/html" => Outcome::Forward(true),
|
||||
_ => Outcome::Forward(false),
|
||||
})
|
||||
.fold(Outcome::Forward(false), |out, ct| {
|
||||
if out.clone().forwarded().unwrap_or_else(|| out.is_success()) {
|
||||
@@ -134,37 +129,37 @@ where
|
||||
.sign(sender)
|
||||
.expect("activity_pub::broadcast: signature error");
|
||||
|
||||
let rt = tokio::runtime::Builder::new()
|
||||
.threaded_scheduler()
|
||||
.build()
|
||||
.expect("Error while initializing tokio runtime for federation");
|
||||
for inbox in boxes {
|
||||
// TODO: run it in Sidekiq or something like that
|
||||
let body = signed.to_string();
|
||||
let mut headers = request::headers();
|
||||
headers.insert("Digest", request::Digest::digest(&body));
|
||||
let sig = request::signature(sender, &headers)
|
||||
.expect("activity_pub::broadcast: request signature error");
|
||||
let client = ClientBuilder::new()
|
||||
.connect_timeout(std::time::Duration::from_secs(5))
|
||||
let res = ClientBuilder::new()
|
||||
.connect_timeout(Some(std::time::Duration::from_secs(5)))
|
||||
.build()
|
||||
.expect("Can't build client");
|
||||
rt.spawn(async move {
|
||||
client
|
||||
.post(&inbox)
|
||||
.headers(headers.clone())
|
||||
.header("Signature", sig)
|
||||
.body(body)
|
||||
.send()
|
||||
.await
|
||||
.unwrap()
|
||||
.text()
|
||||
.await
|
||||
.map(move |response| {
|
||||
println!("Successfully sent activity to inbox ({})", inbox);
|
||||
println!("Response: \"{:?}\"\n", response)
|
||||
})
|
||||
.map_err(|e| println!("Error while sending to inbox ({:?})", e))
|
||||
});
|
||||
.and_then(|client| {
|
||||
client
|
||||
.post(&inbox)
|
||||
.headers(headers.clone())
|
||||
.header(
|
||||
"Signature",
|
||||
request::signature(sender, &headers)
|
||||
.expect("activity_pub::broadcast: request signature error"),
|
||||
)
|
||||
.body(body)
|
||||
.send()
|
||||
});
|
||||
match res {
|
||||
Ok(mut r) => {
|
||||
println!("Successfully sent activity to inbox ({})", inbox);
|
||||
if let Ok(response) = r.text() {
|
||||
println!("Response: \"{:?}\"\n\n", response)
|
||||
} else {
|
||||
println!("Error while reading response")
|
||||
}
|
||||
}
|
||||
Err(e) => println!("Error while sending to inbox ({:?})", e),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -209,7 +204,8 @@ pub struct PublicKey {
|
||||
pub public_key_pem: Option<serde_json::Value>,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||
#[derive(Clone, Debug, Default, UnitString)]
|
||||
#[activitystreams(Hashtag)]
|
||||
pub struct HashtagType;
|
||||
|
||||
#[derive(Clone, Debug, Default, Deserialize, Serialize, Properties)]
|
||||
|
||||
@@ -1,11 +1,12 @@
|
||||
use base64;
|
||||
use chrono::{offset::Utc, DateTime};
|
||||
use openssl::hash::{Hasher, MessageDigest};
|
||||
use reqwest::header::{HeaderMap, HeaderValue, ACCEPT, CONTENT_TYPE, DATE, USER_AGENT};
|
||||
use std::ops::Deref;
|
||||
use std::time::SystemTime;
|
||||
|
||||
use crate::activity_pub::sign::Signer;
|
||||
use crate::activity_pub::{ap_accept_header, AP_CONTENT_TYPE};
|
||||
use activity_pub::sign::Signer;
|
||||
use activity_pub::{ap_accept_header, AP_CONTENT_TYPE};
|
||||
|
||||
const PLUME_USER_AGENT: &str = concat!("Plume/", env!("CARGO_PKG_VERSION"));
|
||||
|
||||
|
||||
@@ -1,7 +1,10 @@
|
||||
use super::request;
|
||||
use base64;
|
||||
use chrono::{naive::NaiveDateTime, DateTime, Duration, Utc};
|
||||
use hex;
|
||||
use openssl::{pkey::PKey, rsa::Rsa, sha::sha256};
|
||||
use rocket::http::HeaderMap;
|
||||
use serde_json;
|
||||
|
||||
/// Returns (public key, private key)
|
||||
pub fn gen_keypair() -> (Vec<u8>, Vec<u8>) {
|
||||
@@ -128,7 +131,7 @@ impl SignatureValidity {
|
||||
|
||||
pub fn verify_http_headers<S: Signer + ::std::fmt::Debug>(
|
||||
sender: &S,
|
||||
all_headers: &HeaderMap<'_>,
|
||||
all_headers: &HeaderMap,
|
||||
data: &request::Digest,
|
||||
) -> SignatureValidity {
|
||||
let sig_header = all_headers.get_one("Signature");
|
||||
|
||||
+13
-2
@@ -1,8 +1,19 @@
|
||||
#![feature(associated_type_defaults)]
|
||||
#![feature(custom_attribute, associated_type_defaults)]
|
||||
|
||||
extern crate activitypub;
|
||||
#[macro_use]
|
||||
extern crate activitystreams_derive;
|
||||
|
||||
extern crate activitystreams_traits;
|
||||
extern crate array_tool;
|
||||
extern crate base64;
|
||||
extern crate chrono;
|
||||
extern crate heck;
|
||||
extern crate hex;
|
||||
extern crate openssl;
|
||||
extern crate pulldown_cmark;
|
||||
extern crate reqwest;
|
||||
extern crate rocket;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate shrinkwraprs;
|
||||
#[macro_use]
|
||||
|
||||
+18
-97
@@ -1,15 +1,12 @@
|
||||
use heck::CamelCase;
|
||||
use openssl::rand::rand_bytes;
|
||||
use pulldown_cmark::{html, Event, Options, Parser, Tag};
|
||||
use regex_syntax::is_word_character;
|
||||
use rocket::{
|
||||
http::uri::Uri,
|
||||
response::{Flash, Redirect},
|
||||
};
|
||||
use std::borrow::Cow;
|
||||
use std::collections::HashSet;
|
||||
use syntect::html::ClassedHTMLGenerator;
|
||||
use syntect::parsing::SyntaxSet;
|
||||
|
||||
/// Generates an hexadecimal representation of 32 bytes of random data
|
||||
pub fn random_hex() -> String {
|
||||
@@ -49,7 +46,7 @@ enum State {
|
||||
Ready,
|
||||
}
|
||||
|
||||
fn to_inline(tag: Tag<'_>) -> Tag<'_> {
|
||||
fn to_inline(tag: Tag) -> Tag {
|
||||
match tag {
|
||||
Tag::Header(_) | Tag::Table(_) | Tag::TableHead | Tag::TableRow | Tag::TableCell => {
|
||||
Tag::Paragraph
|
||||
@@ -58,54 +55,7 @@ fn to_inline(tag: Tag<'_>) -> Tag<'_> {
|
||||
t => t,
|
||||
}
|
||||
}
|
||||
struct HighlighterContext {
|
||||
content: Vec<String>,
|
||||
}
|
||||
fn highlight_code<'a>(
|
||||
context: &mut Option<HighlighterContext>,
|
||||
evt: Event<'a>,
|
||||
) -> Option<Vec<Event<'a>>> {
|
||||
match evt {
|
||||
Event::Start(Tag::CodeBlock(lang)) => {
|
||||
if lang.is_empty() {
|
||||
Some(vec![Event::Start(Tag::CodeBlock(lang))])
|
||||
} else {
|
||||
*context = Some(HighlighterContext { content: vec![] });
|
||||
Some(vec![Event::Start(Tag::CodeBlock(lang))])
|
||||
}
|
||||
}
|
||||
Event::End(Tag::CodeBlock(x)) => {
|
||||
let mut result = vec![];
|
||||
if let Some(ctx) = context.take() {
|
||||
let syntax_set = SyntaxSet::load_defaults_newlines();
|
||||
let syntax = syntax_set.find_syntax_by_token(&x).unwrap_or_else(|| {
|
||||
syntax_set
|
||||
.find_syntax_by_name(&x)
|
||||
.unwrap_or_else(|| syntax_set.find_syntax_plain_text())
|
||||
});
|
||||
let mut html = ClassedHTMLGenerator::new(&syntax, &syntax_set);
|
||||
for line in ctx.content {
|
||||
html.parse_html_for_line(&line);
|
||||
}
|
||||
let q = html.finalize();
|
||||
result.push(Event::Html(q.into()));
|
||||
}
|
||||
result.push(Event::End(Tag::CodeBlock(x)));
|
||||
*context = None;
|
||||
Some(result)
|
||||
}
|
||||
Event::Text(t) => {
|
||||
if let Some(mut c) = context.take() {
|
||||
c.content.push(t.to_string());
|
||||
*context = Some(c);
|
||||
Some(vec![])
|
||||
} else {
|
||||
Some(vec![Event::Text(t)])
|
||||
}
|
||||
}
|
||||
_ => Some(vec![evt]),
|
||||
}
|
||||
}
|
||||
|
||||
fn flatten_text<'a>(state: &mut Option<String>, evt: Event<'a>) -> Option<Vec<Event<'a>>> {
|
||||
let (s, res) = match evt {
|
||||
Event::Text(txt) => match state.take() {
|
||||
@@ -147,7 +97,7 @@ fn inline_tags<'a>(
|
||||
}
|
||||
}
|
||||
|
||||
pub type MediaProcessor<'a> = Box<dyn 'a + Fn(i32) -> Option<(String, Option<String>)>>;
|
||||
pub type MediaProcessor<'a> = Box<'a + Fn(i32) -> Option<(String, Option<String>)>>;
|
||||
|
||||
fn process_image<'a, 'b>(
|
||||
evt: Event<'a>,
|
||||
@@ -158,7 +108,9 @@ fn process_image<'a, 'b>(
|
||||
match evt {
|
||||
Event::Start(Tag::Image(id, title)) => {
|
||||
if let Some((url, cw)) = id.parse::<i32>().ok().and_then(processor.as_ref()) {
|
||||
if let (Some(cw), false) = (cw, inline) {
|
||||
if inline || cw.is_none() {
|
||||
Event::Start(Tag::Image(Cow::Owned(url), title))
|
||||
} else {
|
||||
// there is a cw, and where are not inline
|
||||
Event::Html(Cow::Owned(format!(
|
||||
r#"<label for="postcontent-cw-{id}">
|
||||
@@ -169,11 +121,9 @@ fn process_image<'a, 'b>(
|
||||
</span>
|
||||
<img src="{url}" alt=""#,
|
||||
id = random_hex(),
|
||||
cw = cw,
|
||||
cw = cw.unwrap(),
|
||||
url = url
|
||||
)))
|
||||
} else {
|
||||
Event::Start(Tag::Image(Cow::Owned(url), title))
|
||||
}
|
||||
} else {
|
||||
Event::Start(Tag::Image(id, title))
|
||||
@@ -201,12 +151,6 @@ fn process_image<'a, 'b>(
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
struct DocumentContext {
|
||||
in_code: bool,
|
||||
in_link: bool,
|
||||
}
|
||||
|
||||
/// Returns (HTML, mentions, hashtags)
|
||||
pub fn md_to_html<'a>(
|
||||
md: &str,
|
||||
@@ -221,32 +165,14 @@ pub fn md_to_html<'a>(
|
||||
};
|
||||
let parser = Parser::new_ext(md, Options::all());
|
||||
|
||||
let (parser, mentions, hashtags): (Vec<Event<'_>>, Vec<String>, Vec<String>) = parser
|
||||
let (parser, mentions, hashtags): (Vec<Event>, Vec<String>, Vec<String>) = parser
|
||||
// Flatten text because pulldown_cmark break #hashtag in two individual text elements
|
||||
.scan(None, flatten_text)
|
||||
.flatten()
|
||||
.scan(None, highlight_code)
|
||||
.flatten()
|
||||
.flat_map(IntoIterator::into_iter)
|
||||
.map(|evt| process_image(evt, inline, &media_processor))
|
||||
// Ignore headings, images, and tables if inline = true
|
||||
.scan((vec![], inline), inline_tags)
|
||||
.scan(&mut DocumentContext::default(), |ctx, evt| match evt {
|
||||
Event::Start(Tag::CodeBlock(_)) | Event::Start(Tag::Code) => {
|
||||
ctx.in_code = true;
|
||||
Some((vec![evt], vec![], vec![]))
|
||||
}
|
||||
Event::End(Tag::CodeBlock(_)) | Event::End(Tag::Code) => {
|
||||
ctx.in_code = false;
|
||||
Some((vec![evt], vec![], vec![]))
|
||||
}
|
||||
Event::Start(Tag::Link(_, _)) => {
|
||||
ctx.in_link = true;
|
||||
Some((vec![evt], vec![], vec![]))
|
||||
}
|
||||
Event::End(Tag::Link(_, _)) => {
|
||||
ctx.in_link = false;
|
||||
Some((vec![evt], vec![], vec![]))
|
||||
}
|
||||
.map(|evt| match evt {
|
||||
Event::Text(txt) => {
|
||||
let (evts, _, _, _, new_mentions, new_hashtags) = txt.chars().fold(
|
||||
(vec![], State::Ready, String::new(), 0, vec![], vec![]),
|
||||
@@ -262,7 +188,7 @@ pub fn md_to_html<'a>(
|
||||
text_acc.push(c)
|
||||
}
|
||||
let mention = text_acc;
|
||||
let short_mention = mention.splitn(1, '@').next().unwrap_or("");
|
||||
let short_mention = mention.splitn(1, '@').nth(0).unwrap_or("");
|
||||
let link = Tag::Link(
|
||||
format!("{}@/{}/", base_url, &mention).into(),
|
||||
short_mention.to_owned().into(),
|
||||
@@ -284,7 +210,7 @@ pub fn md_to_html<'a>(
|
||||
}
|
||||
}
|
||||
State::Hashtag => {
|
||||
let char_matches = c == '-' || is_word_character(c);
|
||||
let char_matches = c.is_alphanumeric() || "-_".contains(c);
|
||||
if char_matches && (n < (txt.chars().count() - 1)) {
|
||||
text_acc.push(c);
|
||||
(events, State::Hashtag, text_acc, n + 1, mentions, hashtags)
|
||||
@@ -315,7 +241,7 @@ pub fn md_to_html<'a>(
|
||||
}
|
||||
}
|
||||
State::Ready => {
|
||||
if !ctx.in_code && !ctx.in_link && c == '@' {
|
||||
if c == '@' {
|
||||
events.push(Event::Text(text_acc.into()));
|
||||
(
|
||||
events,
|
||||
@@ -325,7 +251,7 @@ pub fn md_to_html<'a>(
|
||||
mentions,
|
||||
hashtags,
|
||||
)
|
||||
} else if !ctx.in_code && !ctx.in_link && c == '#' {
|
||||
} else if c == '#' {
|
||||
events.push(Event::Text(text_acc.into()));
|
||||
(
|
||||
events,
|
||||
@@ -370,9 +296,9 @@ pub fn md_to_html<'a>(
|
||||
}
|
||||
},
|
||||
);
|
||||
Some((evts, new_mentions, new_hashtags))
|
||||
(evts, new_mentions, new_hashtags)
|
||||
}
|
||||
_ => Some((vec![evt], vec![], vec![])),
|
||||
_ => (vec![evt], vec![], vec![]),
|
||||
})
|
||||
.fold(
|
||||
(vec![], vec![], vec![]),
|
||||
@@ -409,12 +335,9 @@ mod tests {
|
||||
("mention at @end", vec!["end"]),
|
||||
("between parenthesis (@test)", vec!["test"]),
|
||||
("with some punctuation @test!", vec!["test"]),
|
||||
(" @spaces ", vec!["spaces"]),
|
||||
(" @spaces ", vec!["spaces"]),
|
||||
("@is_a@mention", vec!["is_a@mention"]),
|
||||
("not_a@mention", vec![]),
|
||||
("`@helo`", vec![]),
|
||||
("```\n@hello\n```", vec![]),
|
||||
("[@atmark in link](https://example.org/)", vec![]),
|
||||
];
|
||||
|
||||
for (md, mentions) in tests {
|
||||
@@ -438,10 +361,8 @@ mod tests {
|
||||
("hashtag at #end", vec!["end"]),
|
||||
("between parenthesis (#test)", vec!["test"]),
|
||||
("with some punctuation #test!", vec!["test"]),
|
||||
(" #spaces ", vec!["spaces"]),
|
||||
(" #spaces ", vec!["spaces"]),
|
||||
("not_a#hashtag", vec![]),
|
||||
("#نرمافزار_آزاد", vec!["نرمافزار_آزاد"]),
|
||||
("[#hash in link](https://example.org/)", vec![]),
|
||||
];
|
||||
|
||||
for (md, mentions) in tests {
|
||||
|
||||
@@ -1,15 +1,12 @@
|
||||
[package]
|
||||
name = "plume-front"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
authors = ["Plume contributors"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
stdweb = "=0.4.18"
|
||||
stdweb-internal-runtime = "=0.1.4"
|
||||
stdweb = "=0.4.14"
|
||||
stdweb-internal-runtime = "=0.1.3"
|
||||
gettext = { git = "https://github.com/Plume-org/gettext/", rev = "294c54d74c699fbc66502b480a37cc66c1daa7f3" }
|
||||
gettext-macros = { git = "https://github.com/Plume-org/gettext-macros/", rev = "a7c605f7edd6bfbfbfe7778026bfefd88d82db10" }
|
||||
gettext-utils = { git = "https://github.com/Plume-org/gettext-macros/", rev = "a7c605f7edd6bfbfbfe7778026bfefd88d82db10" }
|
||||
lazy_static = "1.3"
|
||||
serde = "1.0"
|
||||
serde_json = "1.0"
|
||||
|
||||
+26
-192
@@ -1,11 +1,8 @@
|
||||
use crate::CATALOG;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use serde_json;
|
||||
use std::sync::Mutex;
|
||||
use stdweb::{
|
||||
unstable::{TryFrom, TryInto},
|
||||
web::{event::*, html_element::*, *},
|
||||
};
|
||||
use CATALOG;
|
||||
|
||||
macro_rules! mv {
|
||||
( $( $var:ident ),* => $exp:expr ) => {
|
||||
@@ -19,26 +16,17 @@ macro_rules! mv {
|
||||
fn get_elt_value(id: &'static str) -> String {
|
||||
let elt = document().get_element_by_id(id).unwrap();
|
||||
let inp: Result<InputElement, _> = elt.clone().try_into();
|
||||
let textarea: Result<TextAreaElement, _> = elt.clone().try_into();
|
||||
let select: Result<SelectElement, _> = elt.try_into();
|
||||
inp.map(|i| i.raw_value()).unwrap_or_else(|_| {
|
||||
textarea
|
||||
.map(|t| t.value())
|
||||
.unwrap_or_else(|_| select.unwrap().raw_value())
|
||||
})
|
||||
let textarea: Result<TextAreaElement, _> = elt.try_into();
|
||||
inp.map(|i| i.raw_value())
|
||||
.unwrap_or_else(|_| textarea.unwrap().value())
|
||||
}
|
||||
|
||||
fn set_value<S: AsRef<str>>(id: &'static str, val: S) {
|
||||
let elt = document().get_element_by_id(id).unwrap();
|
||||
let inp: Result<InputElement, _> = elt.clone().try_into();
|
||||
let textarea: Result<TextAreaElement, _> = elt.clone().try_into();
|
||||
let select: Result<SelectElement, _> = elt.try_into();
|
||||
let textarea: Result<TextAreaElement, _> = elt.try_into();
|
||||
inp.map(|i| i.set_raw_value(val.as_ref()))
|
||||
.unwrap_or_else(|_| {
|
||||
textarea
|
||||
.map(|t| t.set_value(val.as_ref()))
|
||||
.unwrap_or_else(|_| select.unwrap().set_raw_value(val.as_ref()))
|
||||
})
|
||||
.unwrap_or_else(|_| textarea.unwrap().set_value(val.as_ref()))
|
||||
}
|
||||
|
||||
fn no_return(evt: KeyDownEvent) {
|
||||
@@ -74,148 +62,7 @@ impl From<stdweb::private::ConversionError> for EditorError {
|
||||
EditorError::TypeError
|
||||
}
|
||||
}
|
||||
const AUTOSAVE_DEBOUNCE_TIME: u32 = 5000;
|
||||
#[derive(Serialize, Deserialize)]
|
||||
struct AutosaveInformation {
|
||||
contents: String,
|
||||
cover: String,
|
||||
last_saved: f64,
|
||||
license: String,
|
||||
subtitle: String,
|
||||
tags: String,
|
||||
title: String,
|
||||
}
|
||||
js_serializable!(AutosaveInformation);
|
||||
fn is_basic_editor() -> bool {
|
||||
if let Some(basic_editor) = window().local_storage().get("basic-editor") {
|
||||
basic_editor == "true"
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
fn get_title() -> String {
|
||||
if is_basic_editor() {
|
||||
get_elt_value("title")
|
||||
} else {
|
||||
let title_field = HtmlElement::try_from(
|
||||
document()
|
||||
.query_selector("#plume-editor > h1")
|
||||
.ok()
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
)
|
||||
.ok()
|
||||
.unwrap();
|
||||
title_field.inner_text()
|
||||
}
|
||||
}
|
||||
fn get_autosave_id() -> String {
|
||||
format!(
|
||||
"editor_contents={}",
|
||||
window().location().unwrap().pathname().unwrap()
|
||||
)
|
||||
}
|
||||
fn get_editor_contents() -> String {
|
||||
if is_basic_editor() {
|
||||
get_elt_value("editor-content")
|
||||
} else {
|
||||
let editor =
|
||||
HtmlElement::try_from(document().query_selector("article").ok().unwrap().unwrap())
|
||||
.ok()
|
||||
.unwrap();
|
||||
editor.child_nodes().iter().fold(String::new(), |md, ch| {
|
||||
let to_append = match ch.node_type() {
|
||||
NodeType::Element => {
|
||||
if js! { return @{&ch}.tagName; } == "DIV" {
|
||||
(js! { return @{&ch}.innerHTML; })
|
||||
.try_into()
|
||||
.unwrap_or_default()
|
||||
} else {
|
||||
(js! { return @{&ch}.outerHTML; })
|
||||
.try_into()
|
||||
.unwrap_or_default()
|
||||
}
|
||||
}
|
||||
NodeType::Text => ch.node_value().unwrap_or_default(),
|
||||
_ => unreachable!(),
|
||||
};
|
||||
format!("{}\n\n{}", md, to_append)
|
||||
})
|
||||
}
|
||||
}
|
||||
fn get_subtitle() -> String {
|
||||
if is_basic_editor() {
|
||||
get_elt_value("subtitle")
|
||||
} else {
|
||||
let subtitle_element = HtmlElement::try_from(
|
||||
document()
|
||||
.query_selector("#plume-editor > h2")
|
||||
.unwrap()
|
||||
.unwrap(),
|
||||
)
|
||||
.ok()
|
||||
.unwrap();
|
||||
subtitle_element.inner_text()
|
||||
}
|
||||
}
|
||||
fn autosave() {
|
||||
let info = AutosaveInformation {
|
||||
contents: get_editor_contents(),
|
||||
title: get_title(),
|
||||
subtitle: get_subtitle(),
|
||||
tags: get_elt_value("tags"),
|
||||
license: get_elt_value("license"),
|
||||
last_saved: Date::now(),
|
||||
cover: get_elt_value("cover"),
|
||||
};
|
||||
let id = get_autosave_id();
|
||||
match window()
|
||||
.local_storage()
|
||||
.insert(&id, &serde_json::to_string(&info).unwrap())
|
||||
{
|
||||
Ok(_) => {}
|
||||
_ => console!(log, "Autosave failed D:"),
|
||||
}
|
||||
}
|
||||
//This is only necessary until we go to stdweb 4.20 at least
|
||||
fn confirm(message: &str) -> bool {
|
||||
let result: bool = js! {return confirm(@{message});} == true;
|
||||
result
|
||||
}
|
||||
fn load_autosave() {
|
||||
if let Some(autosave_str) = window().local_storage().get(&get_autosave_id()) {
|
||||
let autosave_info: AutosaveInformation = serde_json::from_str(&autosave_str).ok().unwrap();
|
||||
let message = i18n!(
|
||||
CATALOG,
|
||||
"Do you want to load the local autosave last edited at {}?";
|
||||
Date::from_time(autosave_info.last_saved).to_date_string()
|
||||
);
|
||||
if confirm(&message) {
|
||||
set_value("editor-content", &autosave_info.contents);
|
||||
set_value("title", &autosave_info.title);
|
||||
set_value("subtitle", &autosave_info.subtitle);
|
||||
set_value("tags", &autosave_info.tags);
|
||||
set_value("license", &autosave_info.license);
|
||||
set_value("cover", &autosave_info.cover);
|
||||
} else {
|
||||
clear_autosave();
|
||||
}
|
||||
}
|
||||
}
|
||||
fn clear_autosave() {
|
||||
window().local_storage().remove(&get_autosave_id());
|
||||
console!(log, &format!("Saved to {}", &get_autosave_id()));
|
||||
}
|
||||
lazy_static! {
|
||||
static ref AUTOSAVE_TIMEOUT: Mutex<Option<TimeoutHandle>> = Mutex::new(None);
|
||||
}
|
||||
fn autosave_debounce() {
|
||||
let timeout = &mut AUTOSAVE_TIMEOUT.lock().unwrap();
|
||||
if let Some(timeout) = timeout.take() {
|
||||
timeout.clear();
|
||||
}
|
||||
**timeout = Some(window().set_clearable_timeout(autosave, AUTOSAVE_DEBOUNCE_TIME));
|
||||
}
|
||||
|
||||
fn init_widget(
|
||||
parent: &Element,
|
||||
tag: &'static str,
|
||||
@@ -253,40 +100,29 @@ fn filter_paste(elt: &HtmlElement) {
|
||||
}
|
||||
|
||||
pub fn init() -> Result<(), EditorError> {
|
||||
if let Some(ed) = document().get_element_by_id("plume-fallback-editor") {
|
||||
load_autosave();
|
||||
ed.add_event_listener(|_: SubmitEvent| clear_autosave());
|
||||
}
|
||||
// Check if the user wants to use the basic editor
|
||||
if window()
|
||||
.local_storage()
|
||||
.get("basic-editor")
|
||||
.map(|x| x == "true")
|
||||
.unwrap_or(true)
|
||||
{
|
||||
if let Some(editor) = document().get_element_by_id("plume-fallback-editor") {
|
||||
if let Ok(Some(title_label)) = document().query_selector("label[for=title]") {
|
||||
let editor_button = document().create_element("a")?;
|
||||
js! { @{&editor_button}.href = "#"; }
|
||||
editor_button.add_event_listener(|_: ClickEvent| {
|
||||
window().local_storage().remove("basic-editor");
|
||||
window().history().go(0).ok(); // refresh
|
||||
});
|
||||
editor_button.append_child(
|
||||
&document().create_text_node(&i18n!(CATALOG, "Open the rich text editor")),
|
||||
);
|
||||
editor.insert_before(&editor_button, &title_label).ok();
|
||||
document()
|
||||
.get_element_by_id("editor-content")
|
||||
.unwrap()
|
||||
.add_event_listener(|_: KeyDownEvent| autosave_debounce());
|
||||
if let Some(basic_editor) = window().local_storage().get("basic-editor") {
|
||||
if basic_editor == "true" {
|
||||
if let Some(editor) = document().get_element_by_id("plume-fallback-editor") {
|
||||
if let Ok(Some(title_label)) = document().query_selector("label[for=title]") {
|
||||
let editor_button = document().create_element("a")?;
|
||||
js! { @{&editor_button}.href = "#"; }
|
||||
editor_button.add_event_listener(|_: ClickEvent| {
|
||||
window().local_storage().remove("basic-editor");
|
||||
window().history().go(0).ok(); // refresh
|
||||
});
|
||||
editor_button.append_child(
|
||||
&document().create_text_node(&i18n!(CATALOG, "Open the rich text editor")),
|
||||
);
|
||||
editor.insert_before(&editor_button, &title_label).ok();
|
||||
return Ok(());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Ok(())
|
||||
} else {
|
||||
init_editor()
|
||||
}
|
||||
|
||||
// If we didn't returned above
|
||||
init_editor()
|
||||
}
|
||||
|
||||
fn init_editor() -> Result<(), EditorError> {
|
||||
@@ -334,7 +170,6 @@ fn init_editor() -> Result<(), EditorError> {
|
||||
}).ok();
|
||||
};
|
||||
}), 0);
|
||||
autosave_debounce();
|
||||
}));
|
||||
|
||||
document().get_element_by_id("publish")?.add_event_listener(
|
||||
@@ -470,7 +305,6 @@ fn init_popup(
|
||||
cover.parent_element().unwrap().remove_child(&cover).ok();
|
||||
old_ed.append_child(&cover);
|
||||
set_value("license", get_elt_value("popup-license"));
|
||||
clear_autosave();
|
||||
js! {
|
||||
@{&old_ed}.submit();
|
||||
};
|
||||
|
||||
@@ -1,12 +1,14 @@
|
||||
#![recursion_limit = "128"]
|
||||
#![feature(decl_macro, proc_macro_hygiene, try_trait)]
|
||||
|
||||
extern crate gettext;
|
||||
#[macro_use]
|
||||
extern crate gettext_macros;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
#[macro_use]
|
||||
extern crate stdweb;
|
||||
|
||||
use stdweb::web::{event::*, *};
|
||||
|
||||
init_i18n!(
|
||||
@@ -44,15 +46,10 @@ lazy_static! {
|
||||
let catalogs = include_i18n!();
|
||||
let lang = js! { return navigator.language }.into_string().unwrap();
|
||||
let lang = lang.splitn(2, '-').next().unwrap_or("en");
|
||||
|
||||
let english_position = catalogs
|
||||
.iter()
|
||||
.position(|(language_code, _)| *language_code == "en")
|
||||
.unwrap();
|
||||
catalogs
|
||||
.iter()
|
||||
.find(|(l, _)| l == &lang)
|
||||
.unwrap_or(&catalogs[english_position])
|
||||
.unwrap_or(&catalogs[0])
|
||||
.clone()
|
||||
.1
|
||||
};
|
||||
@@ -66,19 +63,19 @@ fn main() {
|
||||
.ok();
|
||||
}
|
||||
|
||||
/// Toggle menu on mobile devices
|
||||
/// Toggle menu on mobile device
|
||||
///
|
||||
/// It should normally be working fine even without this code
|
||||
/// But :focus-within is not yet supported by Webkit/Blink
|
||||
fn menu() {
|
||||
if let Some(button) = document().get_element_by_id("menu") {
|
||||
if let Some(menu) = document().get_element_by_id("content") {
|
||||
button.add_event_listener(|_: TouchEnd| {
|
||||
button.add_event_listener(|_: ClickEvent| {
|
||||
document()
|
||||
.get_element_by_id("menu")
|
||||
.map(|menu| menu.class_list().add("show"));
|
||||
});
|
||||
menu.add_event_listener(|_: TouchEnd| {
|
||||
menu.add_event_listener(|_: ClickEvent| {
|
||||
document()
|
||||
.get_element_by_id("menu")
|
||||
.map(|menu| menu.class_list().remove("show"));
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
[package]
|
||||
name = "plume-macro"
|
||||
version = "0.4.0"
|
||||
version = "0.1.0"
|
||||
authors = ["Trinity Pointard <trinity.pointard@insa-rennes.fr>"]
|
||||
edition = "2018"
|
||||
description = "Plume procedural macros"
|
||||
|
||||
+14
-11
@@ -1,7 +1,8 @@
|
||||
#![recursion_limit = "128"]
|
||||
|
||||
extern crate proc_macro;
|
||||
#[macro_use]
|
||||
extern crate quote;
|
||||
extern crate syn;
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::TokenStream as TokenStream2;
|
||||
@@ -102,17 +103,19 @@ fn file_to_migration(file: &str) -> TokenStream2 {
|
||||
acc.push_str(line);
|
||||
acc.push('\n');
|
||||
}
|
||||
} else if line.starts_with("--#!") {
|
||||
acc.push_str(&line[4..]);
|
||||
acc.push('\n');
|
||||
} else if line.starts_with("--") {
|
||||
continue;
|
||||
} else {
|
||||
let func: TokenStream2 = trampoline(TokenStream::from_str(&acc).unwrap().into());
|
||||
actions.push(quote!(Action::Function(&#func)));
|
||||
sql = true;
|
||||
acc = line.to_string();
|
||||
acc.push('\n');
|
||||
if line.starts_with("--#!") {
|
||||
acc.push_str(&line[4..]);
|
||||
acc.push('\n');
|
||||
} else if line.starts_with("--") {
|
||||
continue;
|
||||
} else {
|
||||
let func: TokenStream2 = trampoline(TokenStream::from_str(&acc).unwrap().into());
|
||||
actions.push(quote!(Action::Function(&#func)));
|
||||
sql = true;
|
||||
acc = line.to_string();
|
||||
acc.push('\n');
|
||||
}
|
||||
}
|
||||
}
|
||||
if !acc.trim().is_empty() {
|
||||
|
||||
+14
-28
@@ -1,35 +1,32 @@
|
||||
[package]
|
||||
name = "plume-models"
|
||||
version = "0.4.0"
|
||||
version = "0.3.0"
|
||||
authors = ["Plume contributors"]
|
||||
edition = "2018"
|
||||
|
||||
[dependencies]
|
||||
activitypub = "0.1.1"
|
||||
ammonia = "2.1.1"
|
||||
askama_escape = "0.1"
|
||||
bcrypt = "0.5"
|
||||
bcrypt = "0.4"
|
||||
guid-create = "0.1"
|
||||
futures = "0.3"
|
||||
heck = "0.3.0"
|
||||
itertools = "0.8.0"
|
||||
lazy_static = "1.0"
|
||||
lazy_static = "*"
|
||||
migrations_internals= "1.4.0"
|
||||
openssl = "0.10.22"
|
||||
rocket = { git = "https://github.com/SergioBenitez/Rocket", rev = "async" }
|
||||
scheduled-thread-pool = "0.2.2"
|
||||
rocket = "0.4.0"
|
||||
rocket_i18n = { git = "https://github.com/Plume-org/rocket_i18n", rev = "e922afa7c366038b3433278c03b1456b346074f2" }
|
||||
reqwest = "0.9"
|
||||
scheduled-thread-pool = "0.2.0"
|
||||
serde = "1.0"
|
||||
serde_derive = "1.0"
|
||||
serde_json = "1.0"
|
||||
tantivy = "0.10.1"
|
||||
tokio = "0.2"
|
||||
url = "2.1"
|
||||
walkdir = "2.2"
|
||||
webfinger = { git = "https://github.com/Plume-org/webfinger", rev = "4e8f12810c4a7ba7a07bbcb722cd265fdff512b6", features = ["async"] }
|
||||
tantivy = "0.9.1"
|
||||
url = "1.7"
|
||||
webfinger = "0.3.1"
|
||||
whatlang = "0.7.1"
|
||||
shrinkwraprs = "0.3"
|
||||
shrinkwraprs = "0.2.1"
|
||||
diesel-derive-newtype = "0.1.2"
|
||||
glob = "0.3.0"
|
||||
|
||||
[dependencies.chrono]
|
||||
features = ["serde"]
|
||||
@@ -48,20 +45,9 @@ path = "../plume-common"
|
||||
[dependencies.plume-macro]
|
||||
path = "../plume-macro"
|
||||
|
||||
[dependencies.reqwest]
|
||||
features = ["json", "blocking"]
|
||||
version = "0.10"
|
||||
|
||||
|
||||
[dependencies.rocket_i18n]
|
||||
git = "https://github.com/Plume-org/rocket_i18n"
|
||||
branch = "go-async"
|
||||
default-features = false
|
||||
features = ["rocket"]
|
||||
|
||||
[dev-dependencies]
|
||||
diesel_migrations = "1.4.0"
|
||||
diesel_migrations = "1.3.0"
|
||||
|
||||
[features]
|
||||
postgres = ["diesel/postgres", "plume-macro/postgres" ]
|
||||
sqlite = ["diesel/sqlite", "plume-macro/sqlite" ]
|
||||
postgres = ["diesel/postgres", "plume-macro/postgres"]
|
||||
sqlite = ["diesel/sqlite", "plume-macro/sqlite"]
|
||||
|
||||
@@ -1,40 +1,23 @@
|
||||
use crate::users::User;
|
||||
use rocket::{
|
||||
http::Status,
|
||||
request::{self, FromRequest, Request},
|
||||
Outcome,
|
||||
};
|
||||
|
||||
use users::User;
|
||||
|
||||
/// Wrapper around User to use as a request guard on pages reserved to admins.
|
||||
pub struct Admin(pub User);
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Admin {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let user = try_outcome!(User::from_request(request).await);
|
||||
if user.is_admin() {
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Admin, ()> {
|
||||
let user = request.guard::<User>()?;
|
||||
if user.is_admin {
|
||||
Outcome::Success(Admin(user))
|
||||
} else {
|
||||
Outcome::Failure((Status::Unauthorized, ()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as `Admin` but for moderators.
|
||||
pub struct Moderator(pub User);
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Moderator {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let user = try_outcome!(User::from_request(request).await);
|
||||
if user.is_moderator() {
|
||||
Outcome::Success(Moderator(user))
|
||||
} else {
|
||||
Outcome::Failure((Status::Unauthorized, ()))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use crate::{db_conn::DbConn, schema::api_tokens, Error, Result};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use rocket::{
|
||||
@@ -7,6 +6,10 @@ use rocket::{
|
||||
Outcome,
|
||||
};
|
||||
|
||||
use db_conn::DbConn;
|
||||
use schema::api_tokens;
|
||||
use {Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable)]
|
||||
pub struct ApiToken {
|
||||
pub id: i32,
|
||||
@@ -76,36 +79,32 @@ pub enum TokenError {
|
||||
DbError,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for ApiToken {
|
||||
type Error = TokenError;
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<ApiToken, TokenError> {
|
||||
let headers: Vec<_> = request.headers().get("Authorization").collect();
|
||||
if headers.len() != 1 {
|
||||
return Outcome::Failure((Status::BadRequest, TokenError::NoHeader));
|
||||
}
|
||||
|
||||
let mut parsed_header = headers[0].split(' ');
|
||||
if let Some(auth_type) = parsed_header.next() {
|
||||
if let Some(val) = parsed_header.next() {
|
||||
if auth_type == "Bearer" {
|
||||
if let Outcome::Success(conn) = DbConn::from_request(request).await {
|
||||
if let Ok(token) = ApiToken::find_by_value(&*conn, val) {
|
||||
return Outcome::Success(token);
|
||||
}
|
||||
} else {
|
||||
return Outcome::Failure((
|
||||
Status::InternalServerError,
|
||||
TokenError::DbError,
|
||||
));
|
||||
}
|
||||
}
|
||||
} else {
|
||||
return Outcome::Failure((Status::BadRequest, TokenError::NoValue));
|
||||
let auth_type = parsed_header.next().map_or_else(
|
||||
|| Outcome::Failure((Status::BadRequest, TokenError::NoType)),
|
||||
Outcome::Success,
|
||||
)?;
|
||||
let val = parsed_header.next().map_or_else(
|
||||
|| Outcome::Failure((Status::BadRequest, TokenError::NoValue)),
|
||||
Outcome::Success,
|
||||
)?;
|
||||
|
||||
if auth_type == "Bearer" {
|
||||
let conn = request
|
||||
.guard::<DbConn>()
|
||||
.map_failure(|_| (Status::InternalServerError, TokenError::DbError))?;
|
||||
if let Ok(token) = ApiToken::find_by_value(&*conn, val) {
|
||||
return Outcome::Success(token);
|
||||
}
|
||||
} else {
|
||||
return Outcome::Failure((Status::BadRequest, TokenError::NoType));
|
||||
}
|
||||
|
||||
Outcome::Forward(())
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
use crate::{schema::apps, Error, Result};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use schema::apps;
|
||||
use {Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Serialize)]
|
||||
pub struct App {
|
||||
pub id: i32,
|
||||
|
||||
@@ -1,139 +0,0 @@
|
||||
use crate::{schema::email_blocklist, Connection, Error, Result};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, TextExpressionMethods};
|
||||
use glob::Pattern;
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
#[table_name = "email_blocklist"]
|
||||
pub struct BlocklistedEmail {
|
||||
pub id: i32,
|
||||
pub email_address: String,
|
||||
pub note: String,
|
||||
pub notify_user: bool,
|
||||
pub notification_text: String,
|
||||
}
|
||||
|
||||
#[derive(Insertable, FromForm)]
|
||||
#[table_name = "email_blocklist"]
|
||||
pub struct NewBlocklistedEmail {
|
||||
pub email_address: String,
|
||||
pub note: String,
|
||||
pub notify_user: bool,
|
||||
pub notification_text: String,
|
||||
}
|
||||
|
||||
impl BlocklistedEmail {
|
||||
insert!(email_blocklist, NewBlocklistedEmail);
|
||||
get!(email_blocklist);
|
||||
find_by!(email_blocklist, find_by_id, id as i32);
|
||||
pub fn delete_entries(conn: &Connection, ids: Vec<i32>) -> Result<bool> {
|
||||
use diesel::delete;
|
||||
for i in ids {
|
||||
let be: BlocklistedEmail = BlocklistedEmail::find_by_id(&conn, i)?;
|
||||
delete(&be).execute(conn)?;
|
||||
}
|
||||
Ok(true)
|
||||
}
|
||||
pub fn find_for_domain(conn: &Connection, domain: &str) -> Result<Vec<BlocklistedEmail>> {
|
||||
let effective = format!("%@{}", domain);
|
||||
email_blocklist::table
|
||||
.filter(email_blocklist::email_address.like(effective))
|
||||
.load::<BlocklistedEmail>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub fn matches_blocklist(conn: &Connection, email: &str) -> Result<Option<BlocklistedEmail>> {
|
||||
let mut result = email_blocklist::table.load::<BlocklistedEmail>(conn)?;
|
||||
for i in result.drain(..) {
|
||||
if let Ok(x) = Pattern::new(&i.email_address) {
|
||||
if x.matches(email) {
|
||||
return Ok(Some(i));
|
||||
}
|
||||
}
|
||||
}
|
||||
Ok(None)
|
||||
}
|
||||
pub fn page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<BlocklistedEmail>> {
|
||||
email_blocklist::table
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.load::<BlocklistedEmail>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub fn count(conn: &Connection) -> Result<i64> {
|
||||
email_blocklist::table
|
||||
.count()
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
pub fn pattern_errors(pat: &str) -> Option<glob::PatternError> {
|
||||
let c = Pattern::new(pat);
|
||||
c.err()
|
||||
}
|
||||
pub fn new(
|
||||
conn: &Connection,
|
||||
pattern: &str,
|
||||
note: &str,
|
||||
show_notification: bool,
|
||||
notification_text: &str,
|
||||
) -> Result<BlocklistedEmail> {
|
||||
let c = NewBlocklistedEmail {
|
||||
email_address: pattern.to_owned(),
|
||||
note: note.to_owned(),
|
||||
notify_user: show_notification,
|
||||
notification_text: notification_text.to_owned(),
|
||||
};
|
||||
BlocklistedEmail::insert(conn, c)
|
||||
}
|
||||
}
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::{instance::tests as instance_tests, tests::rockets, Connection as Conn};
|
||||
use diesel::Connection;
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> Vec<BlocklistedEmail> {
|
||||
instance_tests::fill_database(conn);
|
||||
let domainblock =
|
||||
BlocklistedEmail::new(conn, "*@bad-actor.com", "Mean spammers", false, "").unwrap();
|
||||
let userblock = BlocklistedEmail::new(
|
||||
conn,
|
||||
"spammer@lax-administration.com",
|
||||
"Decent enough domain, but this user is a problem.",
|
||||
true,
|
||||
"Stop it please",
|
||||
)
|
||||
.unwrap();
|
||||
vec![domainblock, userblock]
|
||||
}
|
||||
#[test]
|
||||
fn test_match() {
|
||||
let r = rockets();
|
||||
let conn = &*r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let various = fill_database(conn);
|
||||
let match1 = "user1@bad-actor.com";
|
||||
let match2 = "spammer@lax-administration.com";
|
||||
let no_match = "happy-user@lax-administration.com";
|
||||
assert_eq!(
|
||||
BlocklistedEmail::matches_blocklist(conn, match1)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.id,
|
||||
various[0].id
|
||||
);
|
||||
assert_eq!(
|
||||
BlocklistedEmail::matches_blocklist(conn, match2)
|
||||
.unwrap()
|
||||
.unwrap()
|
||||
.id,
|
||||
various[1].id
|
||||
);
|
||||
assert_eq!(
|
||||
BlocklistedEmail::matches_blocklist(conn, no_match)
|
||||
.unwrap()
|
||||
.is_none(),
|
||||
true
|
||||
);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,8 @@
|
||||
use crate::{schema::blog_authors, Error, Result};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use schema::blog_authors;
|
||||
use {Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
pub struct BlogAuthor {
|
||||
pub id: i32,
|
||||
|
||||
+52
-87
@@ -1,27 +1,28 @@
|
||||
use crate::{
|
||||
ap_url, instance::*, medias::Media, posts::Post, safe_string::SafeString, schema::blogs,
|
||||
search::Searcher, users::User, Connection, Error, PlumeRocket, Result, ITEMS_PER_PAGE,
|
||||
};
|
||||
use activitypub::{
|
||||
actor::Group,
|
||||
collection::{OrderedCollection, OrderedCollectionPage},
|
||||
object::Image,
|
||||
CustomObject,
|
||||
};
|
||||
use activitypub::{actor::Group, collection::OrderedCollection, object::Image, CustomObject};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, OptionalExtension, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
||||
use openssl::{
|
||||
hash::MessageDigest,
|
||||
pkey::{PKey, Private},
|
||||
rsa::Rsa,
|
||||
sign::{Signer, Verifier},
|
||||
};
|
||||
use serde_json;
|
||||
use url::Url;
|
||||
use webfinger::*;
|
||||
|
||||
use instance::*;
|
||||
use medias::Media;
|
||||
use plume_common::activity_pub::{
|
||||
inbox::{AsActor, FromId},
|
||||
sign, ActivityStream, ApSignature, Id, IntoId, PublicKey, Source,
|
||||
};
|
||||
use url::Url;
|
||||
use webfinger::*;
|
||||
use posts::Post;
|
||||
use safe_string::SafeString;
|
||||
use schema::blogs;
|
||||
use search::Searcher;
|
||||
use users::User;
|
||||
use {Connection, Error, PlumeRocket, Result};
|
||||
|
||||
pub type CustomGroup = CustomObject<ApSignature, Group>;
|
||||
|
||||
@@ -43,7 +44,6 @@ pub struct Blog {
|
||||
pub summary_html: SafeString,
|
||||
pub icon_id: Option<i32>,
|
||||
pub banner_id: Option<i32>,
|
||||
pub theme: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
@@ -61,7 +61,6 @@ pub struct NewBlog {
|
||||
pub summary_html: SafeString,
|
||||
pub icon_id: Option<i32>,
|
||||
pub banner_id: Option<i32>,
|
||||
pub theme: Option<String>,
|
||||
}
|
||||
|
||||
const BLOG_PREFIX: &str = "~";
|
||||
@@ -70,8 +69,7 @@ impl Blog {
|
||||
insert!(blogs, NewBlog, |inserted, conn| {
|
||||
let instance = inserted.get_instance(conn)?;
|
||||
if inserted.outbox_url.is_empty() {
|
||||
inserted.outbox_url =
|
||||
instance.compute_box(BLOG_PREFIX, &inserted.actor_id, r#"outbox"#);
|
||||
inserted.outbox_url = instance.compute_box(BLOG_PREFIX, &inserted.actor_id, "outbox");
|
||||
}
|
||||
|
||||
if inserted.inbox_url.is_empty() {
|
||||
@@ -101,8 +99,8 @@ impl Blog {
|
||||
}
|
||||
|
||||
pub fn list_authors(&self, conn: &Connection) -> Result<Vec<User>> {
|
||||
use crate::schema::blog_authors;
|
||||
use crate::schema::users;
|
||||
use schema::blog_authors;
|
||||
use schema::users;
|
||||
let authors_ids = blog_authors::table
|
||||
.filter(blog_authors::blog_id.eq(self.id))
|
||||
.select(blog_authors::author_id);
|
||||
@@ -113,7 +111,7 @@ impl Blog {
|
||||
}
|
||||
|
||||
pub fn count_authors(&self, conn: &Connection) -> Result<i64> {
|
||||
use crate::schema::blog_authors;
|
||||
use schema::blog_authors;
|
||||
blog_authors::table
|
||||
.filter(blog_authors::blog_id.eq(self.id))
|
||||
.count()
|
||||
@@ -122,7 +120,7 @@ impl Blog {
|
||||
}
|
||||
|
||||
pub fn find_for_author(conn: &Connection, author: &User) -> Result<Vec<Blog>> {
|
||||
use crate::schema::blog_authors;
|
||||
use schema::blog_authors;
|
||||
let author_ids = blog_authors::table
|
||||
.filter(blog_authors::author_id.eq(author.id))
|
||||
.select(blog_authors::blog_id);
|
||||
@@ -132,21 +130,22 @@ impl Blog {
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub async fn find_by_fqn(c: &mut PlumeRocket, fqn: &str) -> Result<Blog> {
|
||||
pub fn find_by_fqn(c: &PlumeRocket, fqn: &str) -> Result<Blog> {
|
||||
let from_db = blogs::table
|
||||
.filter(blogs::fqn.eq(fqn))
|
||||
.first(&*c.conn)
|
||||
.optional()?;
|
||||
.limit(1)
|
||||
.load::<Blog>(&*c.conn)?
|
||||
.into_iter()
|
||||
.next();
|
||||
if let Some(from_db) = from_db {
|
||||
Ok(from_db)
|
||||
} else {
|
||||
Blog::fetch_from_webfinger(c, fqn).await
|
||||
Blog::fetch_from_webfinger(c, fqn)
|
||||
}
|
||||
}
|
||||
|
||||
async fn fetch_from_webfinger(c: &mut PlumeRocket, acct: &str) -> Result<Blog> {
|
||||
resolve_with_prefix(Prefix::Group, acct.to_owned(), true)
|
||||
.await?
|
||||
fn fetch_from_webfinger(c: &PlumeRocket, acct: &str) -> Result<Blog> {
|
||||
resolve(acct.to_owned(), true)?
|
||||
.links
|
||||
.into_iter()
|
||||
.find(|l| l.mime_type == Some(String::from("application/activity+json")))
|
||||
@@ -221,49 +220,12 @@ impl Blog {
|
||||
coll.collection_props.items = serde_json::to_value(self.get_activities(conn)?)?;
|
||||
coll.collection_props
|
||||
.set_total_items_u64(self.get_activities(conn)?.len() as u64)?;
|
||||
coll.collection_props
|
||||
.set_first_link(Id::new(ap_url(&format!("{}?page=1", &self.outbox_url))))?;
|
||||
coll.collection_props
|
||||
.set_last_link(Id::new(ap_url(&format!(
|
||||
"{}?page={}",
|
||||
&self.outbox_url,
|
||||
(self.get_activities(conn)?.len() as u64 + ITEMS_PER_PAGE as u64 - 1) as u64
|
||||
/ ITEMS_PER_PAGE as u64
|
||||
))))?;
|
||||
Ok(ActivityStream::new(coll))
|
||||
}
|
||||
pub fn outbox_page(
|
||||
&self,
|
||||
conn: &Connection,
|
||||
(min, max): (i32, i32),
|
||||
) -> Result<ActivityStream<OrderedCollectionPage>> {
|
||||
let mut coll = OrderedCollectionPage::default();
|
||||
let acts = self.get_activity_page(&conn, (min, max))?;
|
||||
//This still doesn't do anything because the outbox
|
||||
//doesn't do anything yet
|
||||
coll.collection_page_props.set_next_link(Id::new(&format!(
|
||||
"{}?page={}",
|
||||
&self.outbox_url,
|
||||
min / ITEMS_PER_PAGE + 1
|
||||
)))?;
|
||||
coll.collection_page_props.set_prev_link(Id::new(&format!(
|
||||
"{}?page={}",
|
||||
&self.outbox_url,
|
||||
min / ITEMS_PER_PAGE - 1
|
||||
)))?;
|
||||
coll.collection_props.items = serde_json::to_value(acts)?;
|
||||
Ok(ActivityStream::new(coll))
|
||||
}
|
||||
|
||||
fn get_activities(&self, _conn: &Connection) -> Result<Vec<serde_json::Value>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
fn get_activity_page(
|
||||
&self,
|
||||
_conn: &Connection,
|
||||
(_min, _max): (i32, i32),
|
||||
) -> Result<Vec<serde_json::Value>> {
|
||||
Ok(vec![])
|
||||
}
|
||||
|
||||
pub fn get_keypair(&self) -> Result<PKey<Private>> {
|
||||
PKey::from_rsa(Rsa::private_key_from_pem(
|
||||
@@ -310,7 +272,7 @@ impl Blog {
|
||||
pub fn icon_url(&self, conn: &Connection) -> String {
|
||||
self.icon_id
|
||||
.and_then(|id| Media::get(conn, id).and_then(|m| m.url()).ok())
|
||||
.unwrap_or_else(|| "/static/images/default-avatar.png".to_string())
|
||||
.unwrap_or_else(|| "/static/default-avatar.png".to_string())
|
||||
}
|
||||
|
||||
pub fn banner_url(&self, conn: &Connection) -> Option<String> {
|
||||
@@ -340,11 +302,11 @@ impl FromId<PlumeRocket> for Blog {
|
||||
type Error = Error;
|
||||
type Object = CustomGroup;
|
||||
|
||||
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Self::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, acct: CustomGroup) -> Result<Self> {
|
||||
fn from_activity(c: &PlumeRocket, acct: CustomGroup) -> Result<Self> {
|
||||
let url = Url::parse(&acct.object.object_props.id_string()?)?;
|
||||
let inst = url.host_str()?;
|
||||
let instance = Instance::find_by_domain(&c.conn, inst).or_else(|_| {
|
||||
@@ -430,13 +392,12 @@ impl FromId<PlumeRocket> for Blog {
|
||||
.summary_string()
|
||||
.unwrap_or_default(),
|
||||
),
|
||||
theme: None,
|
||||
},
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsActor<&mut PlumeRocket> for Blog {
|
||||
impl AsActor<&PlumeRocket> for Blog {
|
||||
fn get_inbox_url(&self) -> String {
|
||||
self.inbox_url.clone()
|
||||
}
|
||||
@@ -497,16 +458,14 @@ impl NewBlog {
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
blog_authors::*,
|
||||
instance::tests as instance_tests,
|
||||
medias::NewMedia,
|
||||
search::tests::get_searcher,
|
||||
tests::{db, rockets},
|
||||
users::tests as usersTests,
|
||||
Connection as Conn,
|
||||
};
|
||||
use blog_authors::*;
|
||||
use diesel::Connection;
|
||||
use instance::tests as instance_tests;
|
||||
use medias::NewMedia;
|
||||
use search::tests::get_searcher;
|
||||
use tests::{db, rockets};
|
||||
use users::tests as usersTests;
|
||||
use Connection as Conn;
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Blog>) {
|
||||
instance_tests::fill_database(conn);
|
||||
@@ -610,8 +569,9 @@ pub(crate) mod tests {
|
||||
Instance::get_local().unwrap().id
|
||||
);
|
||||
// TODO add tests for remote instance
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -711,8 +671,9 @@ pub(crate) mod tests {
|
||||
.unwrap()
|
||||
.iter()
|
||||
.any(|b| b.id == blog[1].id));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -735,8 +696,9 @@ pub(crate) mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(Blog::find_by_fqn(&r, "SomeName").unwrap().id, blog.id);
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -758,8 +720,9 @@ pub(crate) mod tests {
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(blog.fqn, "SomeName");
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -770,8 +733,9 @@ pub(crate) mod tests {
|
||||
|
||||
blogs[0].delete(conn, &get_searcher()).unwrap();
|
||||
assert!(Blog::get(conn, blogs[0].id).is_err());
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -840,8 +804,9 @@ pub(crate) mod tests {
|
||||
assert!(Blog::get(conn, blog[1].id).is_err());
|
||||
user[1].delete(conn, &searcher).unwrap();
|
||||
assert!(Blog::get(conn, blog[0].id).is_err());
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -902,6 +867,6 @@ pub(crate) mod tests {
|
||||
assert_eq!(blog.banner_url(conn), blogs[0].banner_url(conn));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
use crate::{comments::Comment, schema::comment_seers, users::User, Connection, Error, Result};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use comments::Comment;
|
||||
use schema::comment_seers;
|
||||
use users::User;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
#[derive(Queryable, Clone)]
|
||||
pub struct CommentSeers {
|
||||
pub id: i32,
|
||||
|
||||
@@ -1,15 +1,3 @@
|
||||
use crate::{
|
||||
comment_seers::{CommentSeers, NewCommentSeers},
|
||||
instance::Instance,
|
||||
medias::Media,
|
||||
mentions::Mention,
|
||||
notifications::*,
|
||||
posts::Post,
|
||||
safe_string::SafeString,
|
||||
schema::comments,
|
||||
users::User,
|
||||
Connection, Error, PlumeRocket, Result,
|
||||
};
|
||||
use activitypub::{
|
||||
activity::{Create, Delete},
|
||||
link,
|
||||
@@ -17,15 +5,26 @@ use activitypub::{
|
||||
};
|
||||
use chrono::{self, NaiveDateTime};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
||||
use plume_common::{
|
||||
activity_pub::{
|
||||
inbox::{AsActor, AsObject, FromId},
|
||||
Id, IntoId, PUBLIC_VISIBILITY,
|
||||
},
|
||||
utils,
|
||||
};
|
||||
use serde_json;
|
||||
|
||||
use std::collections::HashSet;
|
||||
|
||||
use comment_seers::{CommentSeers, NewCommentSeers};
|
||||
use instance::Instance;
|
||||
use medias::Media;
|
||||
use mentions::Mention;
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::{
|
||||
inbox::{AsActor, AsObject, FromId},
|
||||
Id, IntoId, PUBLIC_VISIBILITY,
|
||||
};
|
||||
use plume_common::utils;
|
||||
use posts::Post;
|
||||
use safe_string::SafeString;
|
||||
use schema::comments;
|
||||
use users::User;
|
||||
use {Connection, Error, PlumeRocket, Result};
|
||||
|
||||
#[derive(Queryable, Identifiable, Clone, AsChangeset)]
|
||||
pub struct Comment {
|
||||
pub id: i32,
|
||||
@@ -78,7 +77,7 @@ impl Comment {
|
||||
}
|
||||
|
||||
pub fn count_local(conn: &Connection) -> Result<i64> {
|
||||
use crate::schema::users;
|
||||
use schema::users;
|
||||
let local_authors = users::table
|
||||
.filter(users::instance_id.eq(Instance::get_local()?.id))
|
||||
.select(users::id);
|
||||
@@ -104,7 +103,7 @@ impl Comment {
|
||||
.unwrap_or(false)
|
||||
}
|
||||
|
||||
pub async fn to_activity(&self, c: &mut PlumeRocket) -> Result<Note> {
|
||||
pub fn to_activity(&self, c: &PlumeRocket) -> Result<Note> {
|
||||
let author = User::get(&c.conn, self.author_id)?;
|
||||
let (html, mentions, _hashtags) = utils::md_to_html(
|
||||
self.content.get().as_ref(),
|
||||
@@ -128,25 +127,22 @@ impl Comment {
|
||||
)?))?;
|
||||
note.object_props
|
||||
.set_published_string(chrono::Utc::now().to_rfc3339())?;
|
||||
note.object_props.set_attributed_to_link(author.into_id())?;
|
||||
note.object_props.set_to_link_vec(to)?;
|
||||
|
||||
let mut tag_link_vec = vec![];
|
||||
let mut iter = mentions.into_iter();
|
||||
while let Some(m) = iter.next() {
|
||||
if let Ok(a) = Mention::build_activity(c, &m).await {
|
||||
tag_link_vec.push(a);
|
||||
}
|
||||
}
|
||||
note.object_props.set_tag_link_vec(tag_link_vec)?;
|
||||
|
||||
note.object_props
|
||||
.set_attributed_to_link(author.clone().into_id())?;
|
||||
note.object_props.set_to_link_vec(to.clone())?;
|
||||
note.object_props.set_tag_link_vec(
|
||||
mentions
|
||||
.into_iter()
|
||||
.filter_map(|m| Mention::build_activity(c, &m).ok())
|
||||
.collect::<Vec<link::Mention>>(),
|
||||
)?;
|
||||
Ok(note)
|
||||
}
|
||||
|
||||
pub async fn create_activity(&self, c: &mut PlumeRocket) -> Result<Create> {
|
||||
pub fn create_activity(&self, c: &PlumeRocket) -> Result<Create> {
|
||||
let author = User::get(&c.conn, self.author_id)?;
|
||||
|
||||
let note = self.to_activity(c).await?;
|
||||
let note = self.to_activity(c)?;
|
||||
let mut act = Create::default();
|
||||
act.create_props.set_actor_link(author.into_id())?;
|
||||
act.create_props.set_object_object(note.clone())?;
|
||||
@@ -201,11 +197,11 @@ impl FromId<PlumeRocket> for Comment {
|
||||
type Error = Error;
|
||||
type Object = Note;
|
||||
|
||||
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Self::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, note: Note) -> Result<Self> {
|
||||
fn from_activity(c: &PlumeRocket, note: Note) -> Result<Self> {
|
||||
let conn = &*c.conn;
|
||||
let comm = {
|
||||
let previous_url = note.object_props.in_reply_to.as_ref()?.as_str()?;
|
||||
@@ -325,21 +321,21 @@ impl FromId<PlumeRocket> for Comment {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Create, &mut PlumeRocket> for Comment {
|
||||
impl AsObject<User, Create, &PlumeRocket> for Comment {
|
||||
type Error = Error;
|
||||
type Output = Self;
|
||||
|
||||
fn activity(self, _c: &mut PlumeRocket, _actor: User, _id: &str) -> Result<Self> {
|
||||
fn activity(self, _c: &PlumeRocket, _actor: User, _id: &str) -> Result<Self> {
|
||||
// The actual creation takes place in the FromId impl
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Delete, &mut PlumeRocket> for Comment {
|
||||
impl AsObject<User, Delete, &PlumeRocket> for Comment {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
if self.author_id != actor.id {
|
||||
return Err(Error::Unauthorized);
|
||||
}
|
||||
@@ -440,7 +436,8 @@ mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -11,13 +11,9 @@ pub struct Config {
|
||||
pub base_url: String,
|
||||
pub database_url: String,
|
||||
pub db_name: &'static str,
|
||||
pub db_max_size: Option<u32>,
|
||||
pub db_min_idle: Option<u32>,
|
||||
pub search_index: String,
|
||||
pub rocket: Result<RocketConfig, RocketError>,
|
||||
pub logo: LogoConfig,
|
||||
pub default_theme: String,
|
||||
pub media_directory: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
@@ -195,14 +191,6 @@ lazy_static! {
|
||||
var("ROCKET_PORT").unwrap_or_else(|_| "7878".to_owned())
|
||||
)),
|
||||
db_name: DB_NAME,
|
||||
db_max_size: var("DB_MAX_SIZE").map_or(None, |s| Some(
|
||||
s.parse::<u32>()
|
||||
.expect("Couldn't parse DB_MAX_SIZE into u32")
|
||||
)),
|
||||
db_min_idle: var("DB_MIN_IDLE").map_or(None, |s| Some(
|
||||
s.parse::<u32>()
|
||||
.expect("Couldn't parse DB_MIN_IDLE into u32")
|
||||
)),
|
||||
#[cfg(feature = "postgres")]
|
||||
database_url: var("DATABASE_URL")
|
||||
.unwrap_or_else(|_| format!("postgres://plume:plume@localhost/{}", DB_NAME)),
|
||||
@@ -211,8 +199,5 @@ lazy_static! {
|
||||
search_index: var("SEARCH_INDEX").unwrap_or_else(|_| "search_index".to_owned()),
|
||||
rocket: get_rocket_config(),
|
||||
logo: LogoConfig::default(),
|
||||
default_theme: var("DEFAULT_THEME").unwrap_or_else(|_| "default-light".to_owned()),
|
||||
media_directory: var("MEDIA_UPLOAD_DIRECTORY")
|
||||
.unwrap_or_else(|_| "static/media".to_owned()),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -1,4 +1,3 @@
|
||||
use crate::Connection;
|
||||
use diesel::r2d2::{
|
||||
ConnectionManager, CustomizeConnection, Error as ConnError, Pool, PooledConnection,
|
||||
};
|
||||
@@ -7,10 +6,12 @@ use diesel::{dsl::sql_query, ConnectionError, RunQueryDsl};
|
||||
use rocket::{
|
||||
http::Status,
|
||||
request::{self, FromRequest},
|
||||
Outcome, Request,
|
||||
Outcome, Request, State,
|
||||
};
|
||||
use std::ops::Deref;
|
||||
|
||||
use Connection;
|
||||
|
||||
pub type DbPool = Pool<ConnectionManager<Connection>>;
|
||||
|
||||
// From rocket documentation
|
||||
@@ -21,14 +22,14 @@ pub struct DbConn(pub PooledConnection<ConnectionManager<Connection>>);
|
||||
/// Attempts to retrieve a single connection from the managed database pool. If
|
||||
/// no pool is currently managed, fails with an `InternalServerError` status. If
|
||||
/// no connections are available, fails with a `ServiceUnavailable` status.
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for DbConn {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
match DbConn::from_request(request).await {
|
||||
Outcome::Success(a) => Outcome::Success(a),
|
||||
_ => Outcome::Failure((Status::ServiceUnavailable, ())),
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let pool = request.guard::<State<DbPool>>()?;
|
||||
match pool.get() {
|
||||
Ok(conn) => Outcome::Success(DbConn(conn)),
|
||||
Err(_) => Outcome::Failure((Status::ServiceUnavailable, ())),
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -58,18 +59,3 @@ impl CustomizeConnection<Connection, ConnError> for PragmaForeignKey {
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use diesel::Connection as _;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub struct TestConnectionCustomizer;
|
||||
impl CustomizeConnection<Connection, ConnError> for TestConnectionCustomizer {
|
||||
fn on_acquire(&self, conn: &mut Connection) -> Result<(), ConnError> {
|
||||
PragmaForeignKey.on_acquire(conn)?;
|
||||
Ok(conn.begin_test_transaction().unwrap())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
+16
-14
@@ -1,15 +1,16 @@
|
||||
use crate::{
|
||||
ap_url, notifications::*, schema::follows, users::User, Connection, Error, PlumeRocket, Result,
|
||||
CONFIG,
|
||||
};
|
||||
use activitypub::activity::{Accept, Follow as FollowAct, Undo};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
||||
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::{
|
||||
broadcast,
|
||||
inbox::{AsActor, AsObject, FromId},
|
||||
sign::Signer,
|
||||
Id, IntoId, PUBLIC_VISIBILITY,
|
||||
};
|
||||
use schema::follows;
|
||||
use users::User;
|
||||
use {ap_url, Connection, Error, PlumeRocket, Result, CONFIG};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable, Associations, AsChangeset)]
|
||||
#[belongs_to(User, foreign_key = "following_id")]
|
||||
@@ -55,7 +56,8 @@ impl Follow {
|
||||
let target = User::get(conn, self.following_id)?;
|
||||
|
||||
let mut act = FollowAct::default();
|
||||
act.follow_props.set_actor_link::<Id>(user.into_id())?;
|
||||
act.follow_props
|
||||
.set_actor_link::<Id>(user.clone().into_id())?;
|
||||
act.follow_props
|
||||
.set_object_link::<Id>(target.clone().into_id())?;
|
||||
act.object_props.set_id_string(self.ap_url.clone())?;
|
||||
@@ -136,11 +138,11 @@ impl Follow {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, FollowAct, &mut PlumeRocket> for User {
|
||||
impl AsObject<User, FollowAct, &PlumeRocket> for User {
|
||||
type Error = Error;
|
||||
type Output = Follow;
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, id: &str) -> Result<Follow> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Follow> {
|
||||
// Mastodon (at least) requires the full Follow object when accepting it,
|
||||
// so we rebuilt it here
|
||||
let mut follow = FollowAct::default();
|
||||
@@ -156,11 +158,11 @@ impl FromId<PlumeRocket> for Follow {
|
||||
type Error = Error;
|
||||
type Object = FollowAct;
|
||||
|
||||
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Follow::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, follow: FollowAct) -> Result<Self> {
|
||||
fn from_activity(c: &PlumeRocket, follow: FollowAct) -> Result<Self> {
|
||||
let actor =
|
||||
User::from_id(c, &follow.follow_props.actor_link::<Id>()?, None).map_err(|(_, e)| e)?;
|
||||
|
||||
@@ -170,11 +172,11 @@ impl FromId<PlumeRocket> for Follow {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Undo, &mut PlumeRocket> for Follow {
|
||||
impl AsObject<User, Undo, &PlumeRocket> for Follow {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
let conn = &*c.conn;
|
||||
if self.follower_id == actor.id {
|
||||
diesel::delete(&self).execute(conn)?;
|
||||
@@ -200,8 +202,9 @@ impl IntoId for Follow {
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{tests::db, users::tests as user_tests};
|
||||
use diesel::Connection;
|
||||
use tests::db;
|
||||
use users::tests as user_tests;
|
||||
|
||||
#[test]
|
||||
fn test_id() {
|
||||
@@ -232,8 +235,7 @@ mod tests {
|
||||
)
|
||||
.expect("Couldn't insert new follow");
|
||||
assert_eq!(follow.ap_url, String::from("https://some.url/"));
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
@@ -6,11 +6,10 @@ use rocket::{
|
||||
|
||||
pub struct Headers<'r>(pub HeaderMap<'r>);
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for Headers<'r> {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> {
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, ()> {
|
||||
let mut headers = HeaderMap::new();
|
||||
for header in request.headers().clone().into_iter() {
|
||||
headers.add(header);
|
||||
|
||||
@@ -1,4 +1,5 @@
|
||||
use activitypub::activity::*;
|
||||
use serde_json;
|
||||
|
||||
use crate::{
|
||||
comments::Comment,
|
||||
@@ -45,7 +46,7 @@ impl_into_inbox_result! {
|
||||
Reshare => Reshared
|
||||
}
|
||||
|
||||
pub fn inbox(ctx: &mut PlumeRocket, act: serde_json::Value) -> Result<InboxResult, Error> {
|
||||
pub fn inbox(ctx: &PlumeRocket, act: serde_json::Value) -> Result<InboxResult, Error> {
|
||||
Inbox::handle(ctx, act)
|
||||
.with::<User, Announce, Post>()
|
||||
.with::<User, Create, Comment>()
|
||||
@@ -72,7 +73,7 @@ pub(crate) mod tests {
|
||||
use diesel::Connection;
|
||||
|
||||
pub fn fill_database(
|
||||
rockets: &mut PlumeRocket,
|
||||
rockets: &PlumeRocket,
|
||||
) -> (
|
||||
Vec<crate::posts::Post>,
|
||||
Vec<crate::users::User>,
|
||||
@@ -134,6 +135,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -168,6 +170,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -209,6 +212,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -251,8 +255,9 @@ pub(crate) mod tests {
|
||||
"type": "Delete",
|
||||
});
|
||||
assert!(super::inbox(&r, ok_act).is_ok());
|
||||
|
||||
Ok(())
|
||||
})
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
@@ -277,6 +282,7 @@ pub(crate) mod tests {
|
||||
"type": "Delete",
|
||||
});
|
||||
assert!(super::inbox(&r, ok_act).is_ok());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -330,6 +336,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -355,6 +362,7 @@ pub(crate) mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -393,6 +401,7 @@ pub(crate) mod tests {
|
||||
"type": "Undo",
|
||||
});
|
||||
assert!(super::inbox(&r, ok_act).is_ok());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -431,6 +440,7 @@ pub(crate) mod tests {
|
||||
"type": "Undo",
|
||||
});
|
||||
assert!(super::inbox(&r, ok_act).is_ok());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -469,6 +479,7 @@ pub(crate) mod tests {
|
||||
"type": "Undo",
|
||||
});
|
||||
assert!(super::inbox(&r, ok_act).is_ok());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -498,6 +509,7 @@ pub(crate) mod tests {
|
||||
});
|
||||
|
||||
super::inbox(&r, act).unwrap();
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,16 +1,16 @@
|
||||
use crate::{
|
||||
ap_url,
|
||||
medias::Media,
|
||||
safe_string::SafeString,
|
||||
schema::{instances, users},
|
||||
users::{Role, User},
|
||||
Connection, Error, Result,
|
||||
};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use plume_common::utils::md_to_html;
|
||||
use std::iter::Iterator;
|
||||
use std::sync::RwLock;
|
||||
|
||||
use ap_url;
|
||||
use medias::Media;
|
||||
use plume_common::utils::md_to_html;
|
||||
use safe_string::SafeString;
|
||||
use schema::{instances, users};
|
||||
use users::User;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
#[derive(Clone, Identifiable, Queryable)]
|
||||
pub struct Instance {
|
||||
pub id: i32,
|
||||
@@ -61,8 +61,11 @@ impl Instance {
|
||||
pub fn get_local_uncached(conn: &Connection) -> Result<Instance> {
|
||||
instances::table
|
||||
.filter(instances::local.eq(true))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
.limit(1)
|
||||
.load::<Instance>(conn)?
|
||||
.into_iter()
|
||||
.nth(0)
|
||||
.ok_or(Error::NotFound)
|
||||
}
|
||||
|
||||
pub fn cache_local(conn: &Connection) {
|
||||
@@ -114,7 +117,7 @@ impl Instance {
|
||||
pub fn has_admin(&self, conn: &Connection) -> Result<bool> {
|
||||
users::table
|
||||
.filter(users::instance_id.eq(self.id))
|
||||
.filter(users::role.eq(Role::Admin as i32))
|
||||
.filter(users::is_admin.eq(true))
|
||||
.load::<User>(conn)
|
||||
.map_err(Error::from)
|
||||
.map(|r| !r.is_empty())
|
||||
@@ -123,8 +126,9 @@ impl Instance {
|
||||
pub fn main_admin(&self, conn: &Connection) -> Result<User> {
|
||||
users::table
|
||||
.filter(users::instance_id.eq(self.id))
|
||||
.filter(users::role.eq(Role::Admin as i32))
|
||||
.first(conn)
|
||||
.filter(users::is_admin.eq(true))
|
||||
.limit(1)
|
||||
.get_result::<User>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
@@ -145,7 +149,6 @@ impl Instance {
|
||||
open_registrations: bool,
|
||||
short_description: SafeString,
|
||||
long_description: SafeString,
|
||||
default_license: String,
|
||||
) -> Result<()> {
|
||||
let (sd, _, _) = md_to_html(
|
||||
short_description.as_ref(),
|
||||
@@ -167,7 +170,6 @@ impl Instance {
|
||||
instances::long_description.eq(long_description),
|
||||
instances::short_description_html.eq(sd),
|
||||
instances::long_description_html.eq(ld),
|
||||
instances::default_license.eq(default_license),
|
||||
))
|
||||
.execute(conn)
|
||||
.map(|_| ())
|
||||
@@ -184,67 +186,14 @@ impl Instance {
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Returns a list of the local instance themes (all files matching `static/css/NAME/theme.css`)
|
||||
///
|
||||
/// The list only contains the name of the themes, without their extension or full path.
|
||||
pub fn list_themes() -> Result<Vec<String>> {
|
||||
// List all the files in static/css
|
||||
std::path::Path::new("static")
|
||||
.join("css")
|
||||
.read_dir()
|
||||
.map(|files| {
|
||||
files
|
||||
.filter_map(std::result::Result::ok)
|
||||
// Only keep actual directories (each theme has its own dir)
|
||||
.filter(|f| f.file_type().map(|t| t.is_dir()).unwrap_or(false))
|
||||
// Only keep the directory name (= theme name)
|
||||
.filter_map(|f| {
|
||||
f.path()
|
||||
.file_name()
|
||||
.and_then(std::ffi::OsStr::to_str)
|
||||
.map(std::borrow::ToOwned::to_owned)
|
||||
})
|
||||
// Ignore the one starting with "blog-": these are the blog themes
|
||||
.filter(|f| !f.starts_with("blog-"))
|
||||
.collect()
|
||||
})
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Returns a list of the local blog themes (all files matching `static/css/blog-NAME/theme.css`)
|
||||
///
|
||||
/// The list only contains the name of the themes, without their extension or full path.
|
||||
pub fn list_blog_themes() -> Result<Vec<String>> {
|
||||
// List all the files in static/css
|
||||
std::path::Path::new("static")
|
||||
.join("css")
|
||||
.read_dir()
|
||||
.map(|files| {
|
||||
files
|
||||
.filter_map(std::result::Result::ok)
|
||||
// Only keep actual directories (each theme has its own dir)
|
||||
.filter(|f| f.file_type().map(|t| t.is_dir()).unwrap_or(false))
|
||||
// Only keep the directory name (= theme name)
|
||||
.filter_map(|f| {
|
||||
f.path()
|
||||
.file_name()
|
||||
.and_then(std::ffi::OsStr::to_str)
|
||||
.map(std::borrow::ToOwned::to_owned)
|
||||
})
|
||||
// Only keep the one starting with "blog-": these are the blog themes
|
||||
.filter(|f| f.starts_with("blog-"))
|
||||
.collect()
|
||||
})
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::{tests::db, Connection as Conn};
|
||||
use diesel::Connection;
|
||||
use tests::db;
|
||||
use Connection as Conn;
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> Vec<(NewInstance, Instance)> {
|
||||
let res = vec![
|
||||
@@ -338,6 +287,7 @@ pub(crate) mod tests {
|
||||
res.short_description_html.get(),
|
||||
&inserted.short_description_html
|
||||
);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -398,6 +348,7 @@ pub(crate) mod tests {
|
||||
assert!(last_domaine <= page[0].public_domain);
|
||||
last_domaine = page[0].public_domain.clone();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -460,6 +411,7 @@ pub(crate) mod tests {
|
||||
.count(),
|
||||
0
|
||||
);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
@@ -476,7 +428,6 @@ pub(crate) mod tests {
|
||||
false,
|
||||
SafeString::new("[short](#link)"),
|
||||
SafeString::new("[long_description](/with_link)"),
|
||||
"CC-BY-SAO".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
let inst = Instance::get(conn, inst.id).unwrap();
|
||||
@@ -495,7 +446,7 @@ pub(crate) mod tests {
|
||||
inst.short_description_html,
|
||||
SafeString::new("<p><a href=\"#link\">short</a></p>\n")
|
||||
);
|
||||
assert_eq!(inst.default_license, "CC-BY-SAO".to_owned());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
+46
-35
@@ -1,22 +1,40 @@
|
||||
#![feature(try_trait)]
|
||||
#![feature(never_type)]
|
||||
#![feature(custom_attribute)]
|
||||
#![feature(proc_macro_hygiene)]
|
||||
|
||||
extern crate activitypub;
|
||||
extern crate ammonia;
|
||||
extern crate askama_escape;
|
||||
extern crate bcrypt;
|
||||
extern crate chrono;
|
||||
#[macro_use]
|
||||
extern crate diesel;
|
||||
extern crate futures;
|
||||
extern crate guid_create;
|
||||
extern crate heck;
|
||||
extern crate itertools;
|
||||
#[macro_use]
|
||||
extern crate lazy_static;
|
||||
extern crate migrations_internals;
|
||||
extern crate openssl;
|
||||
extern crate plume_api;
|
||||
extern crate plume_common;
|
||||
#[macro_use]
|
||||
extern crate plume_macro;
|
||||
#[macro_use]
|
||||
extern crate reqwest;
|
||||
extern crate rocket;
|
||||
extern crate rocket_i18n;
|
||||
extern crate scheduled_thread_pool;
|
||||
extern crate serde;
|
||||
#[macro_use]
|
||||
extern crate serde_derive;
|
||||
#[macro_use]
|
||||
extern crate serde_json;
|
||||
#[macro_use]
|
||||
extern crate tantivy;
|
||||
extern crate url;
|
||||
extern crate webfinger;
|
||||
extern crate whatlang;
|
||||
|
||||
use plume_common::activity_pub::inbox::InboxError;
|
||||
|
||||
@@ -34,7 +52,6 @@ pub type Connection = diesel::PgConnection;
|
||||
/// All the possible errors that can be encoutered in this crate
|
||||
#[derive(Debug)]
|
||||
pub enum Error {
|
||||
Blocklisted(bool, String),
|
||||
Db(diesel::result::Error),
|
||||
Inbox(Box<InboxError<Error>>),
|
||||
InvalidValue,
|
||||
@@ -45,11 +62,9 @@ pub enum Error {
|
||||
SerDe,
|
||||
Search(search::SearcherError),
|
||||
Signature,
|
||||
TimelineQuery(timeline::query::QueryError),
|
||||
Unauthorized,
|
||||
Url,
|
||||
Webfinger,
|
||||
Expired,
|
||||
}
|
||||
|
||||
impl From<bcrypt::BcryptError> for Error {
|
||||
@@ -57,7 +72,7 @@ impl From<bcrypt::BcryptError> for Error {
|
||||
Error::Signature
|
||||
}
|
||||
}
|
||||
pub const ITEMS_PER_PAGE: i32 = 12;
|
||||
|
||||
impl From<openssl::error::ErrorStack> for Error {
|
||||
fn from(_: openssl::error::ErrorStack) -> Self {
|
||||
Error::Signature
|
||||
@@ -76,12 +91,6 @@ impl From<std::option::NoneError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Error> for std::option::NoneError {
|
||||
fn from(_: Error) -> Self {
|
||||
std::option::NoneError
|
||||
}
|
||||
}
|
||||
|
||||
impl From<url::ParseError> for Error {
|
||||
fn from(_: url::ParseError) -> Self {
|
||||
Error::Url
|
||||
@@ -127,12 +136,6 @@ impl From<search::SearcherError> for Error {
|
||||
}
|
||||
}
|
||||
|
||||
impl From<timeline::query::QueryError> for Error {
|
||||
fn from(err: timeline::query::QueryError) -> Self {
|
||||
Error::TimelineQuery(err)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<std::io::Error> for Error {
|
||||
fn from(err: std::io::Error) -> Self {
|
||||
Error::Io(err)
|
||||
@@ -169,8 +172,11 @@ macro_rules! find_by {
|
||||
pub fn $fn(conn: &crate::Connection, $($col: $type),+) -> Result<Self> {
|
||||
$table::table
|
||||
$(.filter($table::$col.eq($col)))+
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
.limit(1)
|
||||
.load::<Self>(conn)?
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or(Error::NotFound)
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -216,8 +222,11 @@ macro_rules! get {
|
||||
pub fn get(conn: &crate::Connection, id: i32) -> Result<Self> {
|
||||
$table::table
|
||||
.filter($table::id.eq(id))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
.limit(1)
|
||||
.load::<Self>(conn)?
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or(Error::NotFound)
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -235,13 +244,12 @@ macro_rules! get {
|
||||
/// Model::insert(connection, NewModelType::new());
|
||||
/// ```
|
||||
macro_rules! insert {
|
||||
($table:ident, $from:ty) => {
|
||||
($table:ident, $from:ident) => {
|
||||
insert!($table, $from, |x, _conn| Ok(x));
|
||||
};
|
||||
($table:ident, $from:ty, |$val:ident, $conn:ident | $( $after:tt )+) => {
|
||||
($table:ident, $from:ident, |$val:ident, $conn:ident | $( $after:tt )+) => {
|
||||
last!($table);
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn insert(conn: &crate::Connection, new: $from) -> Result<Self> {
|
||||
diesel::insert_into($table::table)
|
||||
.values(new)
|
||||
@@ -268,12 +276,14 @@ macro_rules! insert {
|
||||
/// ```
|
||||
macro_rules! last {
|
||||
($table:ident) => {
|
||||
#[allow(dead_code)]
|
||||
pub fn last(conn: &crate::Connection) -> Result<Self> {
|
||||
$table::table
|
||||
.order_by($table::id.desc())
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
.limit(1)
|
||||
.load::<Self>(conn)?
|
||||
.into_iter()
|
||||
.next()
|
||||
.ok_or(Error::NotFound)
|
||||
}
|
||||
};
|
||||
}
|
||||
@@ -288,12 +298,18 @@ pub fn ap_url(url: &str) -> String {
|
||||
#[cfg(test)]
|
||||
#[macro_use]
|
||||
mod tests {
|
||||
use crate::{db_conn, migrations::IMPORTED_MIGRATIONS, search, Connection as Conn, CONFIG};
|
||||
use db_conn;
|
||||
use diesel::r2d2::ConnectionManager;
|
||||
#[cfg(feature = "sqlite")]
|
||||
use diesel::{dsl::sql_query, RunQueryDsl};
|
||||
use migrations::IMPORTED_MIGRATIONS;
|
||||
use plume_common::utils::random_hex;
|
||||
use scheduled_thread_pool::ScheduledThreadPool;
|
||||
use search;
|
||||
use std::env::temp_dir;
|
||||
use std::sync::Arc;
|
||||
use Connection as Conn;
|
||||
use CONFIG;
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! part_eq {
|
||||
@@ -313,7 +329,7 @@ mod tests {
|
||||
lazy_static! {
|
||||
static ref DB_POOL: db_conn::DbPool = {
|
||||
let pool = db_conn::DbPool::builder()
|
||||
.connection_customizer(Box::new(db_conn::tests::TestConnectionCustomizer))
|
||||
.connection_customizer(Box::new(db_conn::PragmaForeignKey))
|
||||
.build(ConnectionManager::<Conn>::new(CONFIG.database_url.as_str()))
|
||||
.unwrap();
|
||||
let dir = temp_dir().join(format!("plume-test-{}", random_hex()));
|
||||
@@ -337,7 +353,6 @@ mod tests {
|
||||
pub mod admin;
|
||||
pub mod api_tokens;
|
||||
pub mod apps;
|
||||
pub mod blocklisted_emails;
|
||||
pub mod blog_authors;
|
||||
pub mod blogs;
|
||||
pub mod comment_seers;
|
||||
@@ -348,21 +363,17 @@ pub mod headers;
|
||||
pub mod inbox;
|
||||
pub mod instance;
|
||||
pub mod likes;
|
||||
pub mod lists;
|
||||
pub mod medias;
|
||||
pub mod mentions;
|
||||
pub mod migrations;
|
||||
pub mod notifications;
|
||||
pub mod password_reset_requests;
|
||||
pub mod plume_rocket;
|
||||
pub mod post_authors;
|
||||
pub mod posts;
|
||||
pub mod reshares;
|
||||
pub mod safe_string;
|
||||
#[allow(unused_imports)]
|
||||
pub mod schema;
|
||||
pub mod search;
|
||||
pub mod tags;
|
||||
pub mod timeline;
|
||||
pub mod users;
|
||||
pub use plume_rocket::PlumeRocket;
|
||||
|
||||
+12
-12
@@ -1,14 +1,16 @@
|
||||
use crate::{
|
||||
notifications::*, posts::Post, schema::likes, timeline::*, users::User, Connection, Error,
|
||||
PlumeRocket, Result,
|
||||
};
|
||||
use activitypub::activity;
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::{
|
||||
inbox::{AsActor, AsObject, FromId},
|
||||
Id, IntoId, PUBLIC_VISIBILITY,
|
||||
};
|
||||
use posts::Post;
|
||||
use schema::likes;
|
||||
use users::User;
|
||||
use {Connection, Error, PlumeRocket, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
pub struct Like {
|
||||
@@ -83,11 +85,11 @@ impl Like {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, activity::Like, &mut PlumeRocket> for Post {
|
||||
impl AsObject<User, activity::Like, &PlumeRocket> for Post {
|
||||
type Error = Error;
|
||||
type Output = Like;
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, id: &str) -> Result<Like> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Like> {
|
||||
let res = Like::insert(
|
||||
&c.conn,
|
||||
NewLike {
|
||||
@@ -97,8 +99,6 @@ impl AsObject<User, activity::Like, &mut PlumeRocket> for Post {
|
||||
},
|
||||
)?;
|
||||
res.notify(&c.conn)?;
|
||||
|
||||
Timeline::add_to_all_timelines(c, &self, Kind::Like(&actor))?;
|
||||
Ok(res)
|
||||
}
|
||||
}
|
||||
@@ -107,11 +107,11 @@ impl FromId<PlumeRocket> for Like {
|
||||
type Error = Error;
|
||||
type Object = activity::Like;
|
||||
|
||||
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Like::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, act: activity::Like) -> Result<Self> {
|
||||
fn from_activity(c: &PlumeRocket, act: activity::Like) -> Result<Self> {
|
||||
let res = Like::insert(
|
||||
&c.conn,
|
||||
NewLike {
|
||||
@@ -129,11 +129,11 @@ impl FromId<PlumeRocket> for Like {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, activity::Undo, &mut PlumeRocket> for Like {
|
||||
impl AsObject<User, activity::Undo, &PlumeRocket> for Like {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
let conn = &*c.conn;
|
||||
if actor.id == self.user_id {
|
||||
diesel::delete(&self).execute(conn)?;
|
||||
|
||||
@@ -1,594 +0,0 @@
|
||||
use crate::{
|
||||
blogs::Blog,
|
||||
schema::{blogs, list_elems, lists, users},
|
||||
users::User,
|
||||
Connection, Error, Result,
|
||||
};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use std::convert::{TryFrom, TryInto};
|
||||
|
||||
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
|
||||
/// Represent what a list is supposed to store. Represented in database as an integer
|
||||
pub enum ListType {
|
||||
User,
|
||||
Blog,
|
||||
Word,
|
||||
Prefix,
|
||||
}
|
||||
|
||||
impl TryFrom<i32> for ListType {
|
||||
type Error = ();
|
||||
|
||||
fn try_from(i: i32) -> std::result::Result<Self, ()> {
|
||||
match i {
|
||||
0 => Ok(ListType::User),
|
||||
1 => Ok(ListType::Blog),
|
||||
2 => Ok(ListType::Word),
|
||||
3 => Ok(ListType::Prefix),
|
||||
_ => Err(()),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Into<i32> for ListType {
|
||||
fn into(self) -> i32 {
|
||||
match self {
|
||||
ListType::User => 0,
|
||||
ListType::Blog => 1,
|
||||
ListType::Word => 2,
|
||||
ListType::Prefix => 3,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
pub struct List {
|
||||
pub id: i32,
|
||||
pub name: String,
|
||||
pub user_id: Option<i32>,
|
||||
type_: i32,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
#[table_name = "lists"]
|
||||
struct NewList<'a> {
|
||||
pub name: &'a str,
|
||||
pub user_id: Option<i32>,
|
||||
type_: i32,
|
||||
}
|
||||
|
||||
macro_rules! func {
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
@elem User $id:expr, $value:expr
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
NewListElem {
|
||||
list_id: $id,
|
||||
user_id: Some(*$value),
|
||||
blog_id: None,
|
||||
word: None,
|
||||
}
|
||||
};
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
@elem Blog $id:expr, $value:expr
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
NewListElem {
|
||||
list_id: $id,
|
||||
user_id: None,
|
||||
blog_id: Some(*$value),
|
||||
word: None,
|
||||
}
|
||||
};
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
@elem Word $id:expr, $value:expr
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
NewListElem {
|
||||
list_id: $id,
|
||||
user_id: None,
|
||||
blog_id: None,
|
||||
word: Some($value),
|
||||
}
|
||||
};
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
@elem Prefix $id:expr, $value:expr
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
NewListElem {
|
||||
list_id: $id,
|
||||
user_id: None,
|
||||
blog_id: None,
|
||||
word: Some($value),
|
||||
}
|
||||
};
|
||||
(@in_type User) => { i32 };
|
||||
(@in_type Blog) => { i32 };
|
||||
(@in_type Word) => { &str };
|
||||
(@in_type Prefix) => { &str };
|
||||
(@out_type User) => { User };
|
||||
(@out_type Blog) => { Blog };
|
||||
(@out_type Word) => { String };
|
||||
(@out_type Prefix) => { String };
|
||||
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
add: $fn:ident, $kind:ident
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
pub fn $fn(&self, conn: &Connection, vals: &[func!(@in_type $kind)]) -> Result<()> {
|
||||
if self.kind() != ListType::$kind {
|
||||
return Err(Error::InvalidValue);
|
||||
}
|
||||
diesel::insert_into(list_elems::table)
|
||||
.values(
|
||||
vals
|
||||
.iter()
|
||||
.map(|u| func!(@elem $kind self.id, u))
|
||||
.collect::<Vec<_>>(),
|
||||
)
|
||||
.execute(conn)?;
|
||||
Ok(())
|
||||
}
|
||||
};
|
||||
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
list: $fn:ident, $kind:ident, $table:ident
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
pub fn $fn(&self, conn: &Connection) -> Result<Vec<func!(@out_type $kind)>> {
|
||||
if self.kind() != ListType::$kind {
|
||||
return Err(Error::InvalidValue);
|
||||
}
|
||||
list_elems::table
|
||||
.filter(list_elems::list_id.eq(self.id))
|
||||
.inner_join($table::table)
|
||||
.select($table::all_columns)
|
||||
.load(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
|
||||
(
|
||||
$(#[$outer:meta])*
|
||||
set: $fn:ident, $kind:ident, $add:ident
|
||||
) => {
|
||||
$(#[$outer])*
|
||||
pub fn $fn(&self, conn: &Connection, val: &[func!(@in_type $kind)]) -> Result<()> {
|
||||
if self.kind() != ListType::$kind {
|
||||
return Err(Error::InvalidValue);
|
||||
}
|
||||
self.clear(conn)?;
|
||||
self.$add(conn, val)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
struct ListElem {
|
||||
pub id: i32,
|
||||
pub list_id: i32,
|
||||
pub user_id: Option<i32>,
|
||||
pub blog_id: Option<i32>,
|
||||
pub word: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
#[table_name = "list_elems"]
|
||||
struct NewListElem<'a> {
|
||||
pub list_id: i32,
|
||||
pub user_id: Option<i32>,
|
||||
pub blog_id: Option<i32>,
|
||||
pub word: Option<&'a str>,
|
||||
}
|
||||
|
||||
impl List {
|
||||
last!(lists);
|
||||
get!(lists);
|
||||
|
||||
fn insert(conn: &Connection, val: NewList<'_>) -> Result<Self> {
|
||||
diesel::insert_into(lists::table)
|
||||
.values(val)
|
||||
.execute(conn)?;
|
||||
List::last(conn)
|
||||
}
|
||||
|
||||
pub fn list_for_user(conn: &Connection, user_id: Option<i32>) -> Result<Vec<Self>> {
|
||||
if let Some(user_id) = user_id {
|
||||
lists::table
|
||||
.filter(lists::user_id.eq(user_id))
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
} else {
|
||||
lists::table
|
||||
.filter(lists::user_id.is_null())
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn find_for_user_by_name(
|
||||
conn: &Connection,
|
||||
user_id: Option<i32>,
|
||||
name: &str,
|
||||
) -> Result<Self> {
|
||||
if let Some(user_id) = user_id {
|
||||
lists::table
|
||||
.filter(lists::user_id.eq(user_id))
|
||||
.filter(lists::name.eq(name))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
} else {
|
||||
lists::table
|
||||
.filter(lists::user_id.is_null())
|
||||
.filter(lists::name.eq(name))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new(conn: &Connection, name: &str, user: Option<&User>, kind: ListType) -> Result<Self> {
|
||||
Self::insert(
|
||||
conn,
|
||||
NewList {
|
||||
name,
|
||||
user_id: user.map(|u| u.id),
|
||||
type_: kind.into(),
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
/// Returns the kind of a list
|
||||
pub fn kind(&self) -> ListType {
|
||||
self.type_.try_into().expect("invalid list was constructed")
|
||||
}
|
||||
|
||||
/// Return Ok(true) if the list contain the given user, Ok(false) otherwiser,
|
||||
/// and Err(_) on error
|
||||
pub fn contains_user(&self, conn: &Connection, user: i32) -> Result<bool> {
|
||||
private::ListElem::user_in_list(conn, self, user)
|
||||
}
|
||||
|
||||
/// Return Ok(true) if the list contain the given blog, Ok(false) otherwiser,
|
||||
/// and Err(_) on error
|
||||
pub fn contains_blog(&self, conn: &Connection, blog: i32) -> Result<bool> {
|
||||
private::ListElem::blog_in_list(conn, self, blog)
|
||||
}
|
||||
|
||||
/// Return Ok(true) if the list contain the given word, Ok(false) otherwiser,
|
||||
/// and Err(_) on error
|
||||
pub fn contains_word(&self, conn: &Connection, word: &str) -> Result<bool> {
|
||||
private::ListElem::word_in_list(conn, self, word)
|
||||
}
|
||||
|
||||
/// Return Ok(true) if the list match the given prefix, Ok(false) otherwiser,
|
||||
/// and Err(_) on error
|
||||
pub fn contains_prefix(&self, conn: &Connection, word: &str) -> Result<bool> {
|
||||
private::ListElem::prefix_in_list(conn, self, word)
|
||||
}
|
||||
|
||||
func! {
|
||||
/// Insert new users in a list
|
||||
add: add_users, User
|
||||
}
|
||||
|
||||
func! {
|
||||
/// Insert new blogs in a list
|
||||
add: add_blogs, Blog
|
||||
}
|
||||
|
||||
func! {
|
||||
/// Insert new words in a list
|
||||
add: add_words, Word
|
||||
}
|
||||
|
||||
func! {
|
||||
/// Insert new prefixes in a list
|
||||
add: add_prefixes, Prefix
|
||||
}
|
||||
|
||||
func! {
|
||||
/// Get all users in the list
|
||||
list: list_users, User, users
|
||||
}
|
||||
|
||||
func! {
|
||||
/// Get all blogs in the list
|
||||
list: list_blogs, Blog, blogs
|
||||
}
|
||||
|
||||
/// Get all words in the list
|
||||
pub fn list_words(&self, conn: &Connection) -> Result<Vec<String>> {
|
||||
self.list_stringlike(conn, ListType::Word)
|
||||
}
|
||||
|
||||
/// Get all prefixes in the list
|
||||
pub fn list_prefixes(&self, conn: &Connection) -> Result<Vec<String>> {
|
||||
self.list_stringlike(conn, ListType::Prefix)
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn list_stringlike(&self, conn: &Connection, t: ListType) -> Result<Vec<String>> {
|
||||
if self.kind() != t {
|
||||
return Err(Error::InvalidValue);
|
||||
}
|
||||
list_elems::table
|
||||
.filter(list_elems::list_id.eq(self.id))
|
||||
.filter(list_elems::word.is_not_null())
|
||||
.select(list_elems::word)
|
||||
.load::<Option<String>>(conn)
|
||||
.map_err(Error::from)
|
||||
.map(|r| r.into_iter().filter_map(|o| o).collect::<Vec<String>>())
|
||||
}
|
||||
|
||||
pub fn clear(&self, conn: &Connection) -> Result<()> {
|
||||
diesel::delete(list_elems::table.filter(list_elems::list_id.eq(self.id)))
|
||||
.execute(conn)
|
||||
.map(|_| ())
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
func! {set: set_users, User, add_users}
|
||||
func! {set: set_blogs, Blog, add_blogs}
|
||||
func! {set: set_words, Word, add_words}
|
||||
func! {set: set_prefixes, Prefix, add_prefixes}
|
||||
}
|
||||
|
||||
mod private {
|
||||
pub use super::*;
|
||||
use diesel::{
|
||||
dsl,
|
||||
sql_types::{Nullable, Text},
|
||||
IntoSql, TextExpressionMethods,
|
||||
};
|
||||
|
||||
impl ListElem {
|
||||
insert!(list_elems, NewListElem<'_>);
|
||||
|
||||
pub fn user_in_list(conn: &Connection, list: &List, user: i32) -> Result<bool> {
|
||||
dsl::select(dsl::exists(
|
||||
list_elems::table
|
||||
.filter(list_elems::list_id.eq(list.id))
|
||||
.filter(list_elems::user_id.eq(Some(user))),
|
||||
))
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn blog_in_list(conn: &Connection, list: &List, blog: i32) -> Result<bool> {
|
||||
dsl::select(dsl::exists(
|
||||
list_elems::table
|
||||
.filter(list_elems::list_id.eq(list.id))
|
||||
.filter(list_elems::blog_id.eq(Some(blog))),
|
||||
))
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn word_in_list(conn: &Connection, list: &List, word: &str) -> Result<bool> {
|
||||
dsl::select(dsl::exists(
|
||||
list_elems::table
|
||||
.filter(list_elems::list_id.eq(list.id))
|
||||
.filter(list_elems::word.eq(word)),
|
||||
))
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn prefix_in_list(conn: &Connection, list: &List, word: &str) -> Result<bool> {
|
||||
dsl::select(dsl::exists(
|
||||
list_elems::table
|
||||
.filter(
|
||||
word.into_sql::<Nullable<Text>>()
|
||||
.like(list_elems::word.concat("%")),
|
||||
)
|
||||
.filter(list_elems::list_id.eq(list.id)),
|
||||
))
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{blogs::tests as blog_tests, tests::db};
|
||||
use diesel::Connection;
|
||||
|
||||
#[test]
|
||||
fn list_type() {
|
||||
for i in 0..4 {
|
||||
assert_eq!(i, Into::<i32>::into(ListType::try_from(i).unwrap()));
|
||||
}
|
||||
ListType::try_from(4).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn list_lists() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, _) = blog_tests::fill_database(conn);
|
||||
|
||||
let l1 = List::new(conn, "list1", None, ListType::User).unwrap();
|
||||
let l2 = List::new(conn, "list2", None, ListType::Blog).unwrap();
|
||||
let l1u = List::new(conn, "list1", Some(&users[0]), ListType::Word).unwrap();
|
||||
|
||||
let l_eq = |l1: &List, l2: &List| {
|
||||
assert_eq!(l1.id, l2.id);
|
||||
assert_eq!(l1.user_id, l2.user_id);
|
||||
assert_eq!(l1.name, l2.name);
|
||||
assert_eq!(l1.type_, l2.type_);
|
||||
};
|
||||
|
||||
let l1bis = List::get(conn, l1.id).unwrap();
|
||||
l_eq(&l1, &l1bis);
|
||||
|
||||
let l_inst = List::list_for_user(conn, None).unwrap();
|
||||
let l_user = List::list_for_user(conn, Some(users[0].id)).unwrap();
|
||||
assert_eq!(2, l_inst.len());
|
||||
assert_eq!(1, l_user.len());
|
||||
assert!(l_inst.iter().all(|l| l.id != l1u.id));
|
||||
|
||||
l_eq(&l1u, &l_user[0]);
|
||||
if l_inst[0].id == l1.id {
|
||||
l_eq(&l1, &l_inst[0]);
|
||||
l_eq(&l2, &l_inst[1]);
|
||||
} else {
|
||||
l_eq(&l1, &l_inst[1]);
|
||||
l_eq(&l2, &l_inst[0]);
|
||||
}
|
||||
|
||||
l_eq(
|
||||
&l1,
|
||||
&List::find_for_user_by_name(conn, l1.user_id, &l1.name).unwrap(),
|
||||
);
|
||||
l_eq(
|
||||
&&l1u,
|
||||
&List::find_for_user_by_name(conn, l1u.user_id, &l1u.name).unwrap(),
|
||||
);
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_user_list() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blog_tests::fill_database(conn);
|
||||
|
||||
let l = List::new(conn, "list", None, ListType::User).unwrap();
|
||||
|
||||
assert_eq!(l.kind(), ListType::User);
|
||||
assert!(l.list_users(conn).unwrap().is_empty());
|
||||
|
||||
assert!(!l.contains_user(conn, users[0].id).unwrap());
|
||||
assert!(l.add_users(conn, &[users[0].id]).is_ok());
|
||||
assert!(l.contains_user(conn, users[0].id).unwrap());
|
||||
|
||||
assert!(l.add_users(conn, &[users[1].id]).is_ok());
|
||||
assert!(l.contains_user(conn, users[0].id).unwrap());
|
||||
assert!(l.contains_user(conn, users[1].id).unwrap());
|
||||
assert_eq!(2, l.list_users(conn).unwrap().len());
|
||||
|
||||
assert!(l.set_users(conn, &[users[0].id]).is_ok());
|
||||
assert!(l.contains_user(conn, users[0].id).unwrap());
|
||||
assert!(!l.contains_user(conn, users[1].id).unwrap());
|
||||
assert_eq!(1, l.list_users(conn).unwrap().len());
|
||||
assert!(users[0] == l.list_users(conn).unwrap()[0]);
|
||||
|
||||
l.clear(conn).unwrap();
|
||||
assert!(l.list_users(conn).unwrap().is_empty());
|
||||
|
||||
assert!(l.add_blogs(conn, &[blogs[0].id]).is_err());
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_blog_list() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blog_tests::fill_database(conn);
|
||||
|
||||
let l = List::new(conn, "list", None, ListType::Blog).unwrap();
|
||||
|
||||
assert_eq!(l.kind(), ListType::Blog);
|
||||
assert!(l.list_blogs(conn).unwrap().is_empty());
|
||||
|
||||
assert!(!l.contains_blog(conn, blogs[0].id).unwrap());
|
||||
assert!(l.add_blogs(conn, &[blogs[0].id]).is_ok());
|
||||
assert!(l.contains_blog(conn, blogs[0].id).unwrap());
|
||||
|
||||
assert!(l.add_blogs(conn, &[blogs[1].id]).is_ok());
|
||||
assert!(l.contains_blog(conn, blogs[0].id).unwrap());
|
||||
assert!(l.contains_blog(conn, blogs[1].id).unwrap());
|
||||
assert_eq!(2, l.list_blogs(conn).unwrap().len());
|
||||
|
||||
assert!(l.set_blogs(conn, &[blogs[0].id]).is_ok());
|
||||
assert!(l.contains_blog(conn, blogs[0].id).unwrap());
|
||||
assert!(!l.contains_blog(conn, blogs[1].id).unwrap());
|
||||
assert_eq!(1, l.list_blogs(conn).unwrap().len());
|
||||
assert_eq!(blogs[0].id, l.list_blogs(conn).unwrap()[0].id);
|
||||
|
||||
l.clear(conn).unwrap();
|
||||
assert!(l.list_blogs(conn).unwrap().is_empty());
|
||||
|
||||
assert!(l.add_users(conn, &[users[0].id]).is_err());
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_word_list() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let l = List::new(conn, "list", None, ListType::Word).unwrap();
|
||||
|
||||
assert_eq!(l.kind(), ListType::Word);
|
||||
assert!(l.list_words(conn).unwrap().is_empty());
|
||||
|
||||
assert!(!l.contains_word(conn, "plume").unwrap());
|
||||
assert!(l.add_words(conn, &["plume"]).is_ok());
|
||||
assert!(l.contains_word(conn, "plume").unwrap());
|
||||
assert!(!l.contains_word(conn, "plumelin").unwrap());
|
||||
|
||||
assert!(l.add_words(conn, &["amsterdam"]).is_ok());
|
||||
assert!(l.contains_word(conn, "plume").unwrap());
|
||||
assert!(l.contains_word(conn, "amsterdam").unwrap());
|
||||
assert_eq!(2, l.list_words(conn).unwrap().len());
|
||||
|
||||
assert!(l.set_words(conn, &["plume"]).is_ok());
|
||||
assert!(l.contains_word(conn, "plume").unwrap());
|
||||
assert!(!l.contains_word(conn, "amsterdam").unwrap());
|
||||
assert_eq!(1, l.list_words(conn).unwrap().len());
|
||||
assert_eq!("plume", l.list_words(conn).unwrap()[0]);
|
||||
|
||||
l.clear(conn).unwrap();
|
||||
assert!(l.list_words(conn).unwrap().is_empty());
|
||||
|
||||
assert!(l.add_prefixes(conn, &["something"]).is_err());
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_prefix_list() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let l = List::new(conn, "list", None, ListType::Prefix).unwrap();
|
||||
|
||||
assert_eq!(l.kind(), ListType::Prefix);
|
||||
assert!(l.list_prefixes(conn).unwrap().is_empty());
|
||||
|
||||
assert!(!l.contains_prefix(conn, "plume").unwrap());
|
||||
assert!(l.add_prefixes(conn, &["plume"]).is_ok());
|
||||
assert!(l.contains_prefix(conn, "plume").unwrap());
|
||||
assert!(l.contains_prefix(conn, "plumelin").unwrap());
|
||||
|
||||
assert!(l.add_prefixes(conn, &["amsterdam"]).is_ok());
|
||||
assert!(l.contains_prefix(conn, "plume").unwrap());
|
||||
assert!(l.contains_prefix(conn, "amsterdam").unwrap());
|
||||
assert_eq!(2, l.list_prefixes(conn).unwrap().len());
|
||||
|
||||
assert!(l.set_prefixes(conn, &["plume"]).is_ok());
|
||||
assert!(l.contains_prefix(conn, "plume").unwrap());
|
||||
assert!(!l.contains_prefix(conn, "amsterdam").unwrap());
|
||||
assert_eq!(1, l.list_prefixes(conn).unwrap().len());
|
||||
assert_eq!("plume", l.list_prefixes(conn).unwrap()[0]);
|
||||
|
||||
l.clear(conn).unwrap();
|
||||
assert!(l.list_prefixes(conn).unwrap().is_empty());
|
||||
|
||||
assert!(l.add_words(conn, &["something"]).is_err());
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
+48
-52
@@ -1,17 +1,20 @@
|
||||
use crate::{
|
||||
ap_url, instance::Instance, safe_string::SafeString, schema::medias, users::User, Connection,
|
||||
Error, PlumeRocket, Result,
|
||||
};
|
||||
use activitypub::object::Image;
|
||||
use askama_escape::escape;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use guid_create::GUID;
|
||||
use reqwest;
|
||||
use std::{fs, path::Path};
|
||||
|
||||
use plume_common::{
|
||||
activity_pub::{inbox::FromId, Id},
|
||||
utils::MediaProcessor,
|
||||
};
|
||||
use std::{fs, path::Path};
|
||||
use tokio::prelude::*;
|
||||
|
||||
use instance::Instance;
|
||||
use safe_string::SafeString;
|
||||
use schema::medias;
|
||||
use users::User;
|
||||
use {ap_url, Connection, Error, PlumeRocket, Result};
|
||||
|
||||
#[derive(Clone, Identifiable, Queryable)]
|
||||
pub struct Media {
|
||||
@@ -59,14 +62,7 @@ impl MediaCategory {
|
||||
impl Media {
|
||||
insert!(medias, NewMedia);
|
||||
get!(medias);
|
||||
|
||||
pub fn for_user(conn: &Connection, owner: i32) -> Result<Vec<Media>> {
|
||||
medias::table
|
||||
.filter(medias::owner_id.eq(owner))
|
||||
.order(medias::id.desc())
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
list_by!(medias, for_user, owner_id as i32);
|
||||
|
||||
pub fn list_all_medias(conn: &Connection) -> Result<Vec<Media>> {
|
||||
medias::table.load::<Media>(conn).map_err(Error::from)
|
||||
@@ -79,7 +75,6 @@ impl Media {
|
||||
) -> Result<Vec<Media>> {
|
||||
medias::table
|
||||
.filter(medias::owner_id.eq(user.id))
|
||||
.order(medias::id.desc())
|
||||
.offset(i64::from(min))
|
||||
.limit(i64::from(max - min))
|
||||
.load::<Media>(conn)
|
||||
@@ -114,19 +109,15 @@ impl Media {
|
||||
Ok(match self.category() {
|
||||
MediaCategory::Image => SafeString::trusted(&format!(
|
||||
r#"<img src="{}" alt="{}" title="{}">"#,
|
||||
url,
|
||||
escape(&self.alt_text),
|
||||
escape(&self.alt_text)
|
||||
url, escape(&self.alt_text), escape(&self.alt_text)
|
||||
)),
|
||||
MediaCategory::Audio => SafeString::trusted(&format!(
|
||||
r#"<div class="media-preview audio"></div><audio src="{}" title="{}" controls></audio>"#,
|
||||
url,
|
||||
escape(&self.alt_text)
|
||||
url, escape(&self.alt_text)
|
||||
)),
|
||||
MediaCategory::Video => SafeString::trusted(&format!(
|
||||
r#"<video src="{}" title="{}" controls></video>"#,
|
||||
url,
|
||||
escape(&self.alt_text)
|
||||
url, escape(&self.alt_text)
|
||||
)),
|
||||
MediaCategory::Unknown => SafeString::trusted(&format!(
|
||||
r#"<a href="{}" class="media-preview unknown"></a>"#,
|
||||
@@ -149,12 +140,10 @@ impl Media {
|
||||
if self.is_remote {
|
||||
Ok(self.remote_url.clone().unwrap_or_default())
|
||||
} else {
|
||||
let p = Path::new(&self.file_path);
|
||||
let filename: String = p.file_name().unwrap().to_str().unwrap().to_owned();
|
||||
Ok(ap_url(&format!(
|
||||
"{}/static/media/{}",
|
||||
"{}/{}",
|
||||
Instance::get_local()?.public_domain,
|
||||
&filename
|
||||
self.file_path
|
||||
)))
|
||||
}
|
||||
}
|
||||
@@ -197,39 +186,27 @@ impl Media {
|
||||
}
|
||||
|
||||
// TODO: merge with save_remote?
|
||||
pub async fn from_activity(c: &mut PlumeRocket, image: &Image) -> Result<Media> {
|
||||
pub fn from_activity(c: &PlumeRocket, image: &Image) -> Result<Media> {
|
||||
let conn = &*c.conn;
|
||||
let remote_url = image.object_props.url_string().ok()?;
|
||||
let ext = remote_url
|
||||
.rsplit('.')
|
||||
.next()
|
||||
.map(ToOwned::to_owned)
|
||||
.unwrap_or_else(|| String::from("png"));
|
||||
let path = Path::new(&super::CONFIG.media_directory).join(format!(
|
||||
"{}.{}",
|
||||
GUID::rand().to_string(),
|
||||
ext
|
||||
));
|
||||
let path =
|
||||
Path::new("static")
|
||||
.join("media")
|
||||
.join(format!("{}.{}", GUID::rand().to_string(), ext));
|
||||
|
||||
let mut dest = tokio::fs::File::create(path.clone()).await?;
|
||||
let contents = reqwest::get(remote_url.as_str()).await?.bytes().await?;
|
||||
dest.write_all(&contents).await?;
|
||||
|
||||
let owner_id = User::from_id(
|
||||
c,
|
||||
image
|
||||
.object_props
|
||||
.attributed_to_link_vec::<Id>()
|
||||
.ok()?
|
||||
.into_iter()
|
||||
.next()?
|
||||
.as_ref(),
|
||||
None,
|
||||
)
|
||||
.map_err(|(_, e)| e)?
|
||||
.id;
|
||||
let mut dest = fs::File::create(path.clone()).ok()?;
|
||||
reqwest::get(remote_url.as_str())
|
||||
.ok()?
|
||||
.copy_to(&mut dest)
|
||||
.ok()?;
|
||||
|
||||
Media::insert(
|
||||
&mut c.conn,
|
||||
conn,
|
||||
NewMedia {
|
||||
file_path: path.to_str()?.to_string(),
|
||||
alt_text: image.object_props.content_string().ok()?,
|
||||
@@ -237,7 +214,19 @@ impl Media {
|
||||
remote_url: None,
|
||||
sensitive: image.object_props.summary_string().is_ok(),
|
||||
content_warning: image.object_props.summary_string().ok(),
|
||||
owner_id
|
||||
owner_id: User::from_id(
|
||||
c,
|
||||
image
|
||||
.object_props
|
||||
.attributed_to_link_vec::<Id>()
|
||||
.ok()?
|
||||
.into_iter()
|
||||
.next()?
|
||||
.as_ref(),
|
||||
None,
|
||||
)
|
||||
.map_err(|(_, e)| e)?
|
||||
.id,
|
||||
},
|
||||
)
|
||||
}
|
||||
@@ -259,11 +248,13 @@ impl Media {
|
||||
#[cfg(test)]
|
||||
pub(crate) mod tests {
|
||||
use super::*;
|
||||
use crate::{tests::db, users::tests as usersTests, Connection as Conn};
|
||||
use diesel::Connection;
|
||||
use std::env::{current_dir, set_current_dir};
|
||||
use std::fs;
|
||||
use std::path::Path;
|
||||
use tests::db;
|
||||
use users::tests as usersTests;
|
||||
use Connection as Conn;
|
||||
|
||||
pub(crate) fn fill_database(conn: &Conn) -> (Vec<User>, Vec<Media>) {
|
||||
let mut wd = current_dir().unwrap().to_path_buf();
|
||||
@@ -325,6 +316,8 @@ pub(crate) mod tests {
|
||||
}
|
||||
}
|
||||
|
||||
//set_owner
|
||||
|
||||
#[test]
|
||||
fn delete() {
|
||||
let conn = &db();
|
||||
@@ -353,11 +346,13 @@ pub(crate) mod tests {
|
||||
assert!(!Path::new(&path).exists());
|
||||
|
||||
clean(conn);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
||||
fn set_owner() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
@@ -401,6 +396,7 @@ pub(crate) mod tests {
|
||||
.any(|m| m.id == media.id));
|
||||
|
||||
clean(conn);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,10 +1,14 @@
|
||||
use crate::{
|
||||
comments::Comment, notifications::*, posts::Post, schema::mentions, users::User, Connection,
|
||||
Error, PlumeRocket, Result,
|
||||
};
|
||||
use activitypub::link;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use comments::Comment;
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::inbox::AsActor;
|
||||
use posts::Post;
|
||||
use schema::mentions;
|
||||
use users::User;
|
||||
use PlumeRocket;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
pub struct Mention {
|
||||
@@ -52,8 +56,8 @@ impl Mention {
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn build_activity(c: &mut PlumeRocket, ment: &str) -> Result<link::Mention> {
|
||||
let user = User::find_by_fqn(c, ment).await?;
|
||||
pub fn build_activity(c: &PlumeRocket, ment: &str) -> Result<link::Mention> {
|
||||
let user = User::find_by_fqn(c, ment)?;
|
||||
let mut mention = link::Mention::default();
|
||||
mention.link_props.set_href_string(user.ap_url)?;
|
||||
mention.link_props.set_name_string(format!("@{}", ment))?;
|
||||
|
||||
@@ -1,12 +1,16 @@
|
||||
use crate::{Connection, Error, Result};
|
||||
use Connection;
|
||||
use Error;
|
||||
use Result;
|
||||
|
||||
use diesel::connection::{Connection as Conn, SimpleConnection};
|
||||
use migrations_internals::{setup_database, MigrationConnection};
|
||||
|
||||
use std::path::Path;
|
||||
|
||||
#[allow(dead_code)] //variants might not be constructed if not required by current migrations
|
||||
enum Action {
|
||||
Sql(&'static str),
|
||||
Function(&'static dyn Fn(&Connection, &Path) -> Result<()>),
|
||||
Function(&'static Fn(&Connection, &Path) -> Result<()>),
|
||||
}
|
||||
|
||||
impl Action {
|
||||
|
||||
@@ -1,16 +1,15 @@
|
||||
use crate::{
|
||||
comments::Comment,
|
||||
follows::Follow,
|
||||
likes::Like,
|
||||
mentions::Mention,
|
||||
posts::Post,
|
||||
reshares::Reshare,
|
||||
schema::{follows, notifications},
|
||||
users::User,
|
||||
Connection, Error, Result,
|
||||
};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, JoinOnDsl, QueryDsl, RunQueryDsl};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use comments::Comment;
|
||||
use follows::Follow;
|
||||
use likes::Like;
|
||||
use mentions::Mention;
|
||||
use posts::Post;
|
||||
use reshares::Reshare;
|
||||
use schema::notifications;
|
||||
use users::User;
|
||||
use {Connection, Error, Result};
|
||||
|
||||
pub mod notification_kind {
|
||||
pub const COMMENT: &str = "COMMENT";
|
||||
@@ -65,16 +64,6 @@ impl Notification {
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn find_followed_by(conn: &Connection, user: &User) -> Result<Vec<Notification>> {
|
||||
notifications::table
|
||||
.inner_join(follows::table.on(notifications::object_id.eq(follows::id)))
|
||||
.filter(notifications::kind.eq(notification_kind::FOLLOW))
|
||||
.filter(follows::follower_id.eq(user.id))
|
||||
.select(notifications::all_columns)
|
||||
.load::<Notification>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn count_for_user(conn: &Connection, user: &User) -> Result<i64> {
|
||||
notifications::table
|
||||
.filter(notifications::user_id.eq(user.id))
|
||||
|
||||
@@ -1,162 +0,0 @@
|
||||
use crate::{schema::password_reset_requests, Connection, Error, Result};
|
||||
use chrono::{offset::Utc, Duration, NaiveDateTime};
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
#[derive(Clone, Identifiable, Queryable)]
|
||||
pub struct PasswordResetRequest {
|
||||
pub id: i32,
|
||||
pub email: String,
|
||||
pub token: String,
|
||||
pub expiration_date: NaiveDateTime,
|
||||
}
|
||||
|
||||
#[derive(Insertable)]
|
||||
#[table_name = "password_reset_requests"]
|
||||
pub struct NewPasswordResetRequest {
|
||||
pub email: String,
|
||||
pub token: String,
|
||||
pub expiration_date: NaiveDateTime,
|
||||
}
|
||||
|
||||
const TOKEN_VALIDITY_HOURS: i64 = 2;
|
||||
|
||||
impl PasswordResetRequest {
|
||||
pub fn insert(conn: &Connection, email: &str) -> Result<String> {
|
||||
// first, delete other password reset tokens associated with this email:
|
||||
let existing_requests =
|
||||
password_reset_requests::table.filter(password_reset_requests::email.eq(email));
|
||||
diesel::delete(existing_requests).execute(conn)?;
|
||||
|
||||
// now, generate a random token, set the expiry date,
|
||||
// and insert it into the DB:
|
||||
let token = plume_common::utils::random_hex();
|
||||
let expiration_date = Utc::now()
|
||||
.naive_utc()
|
||||
.checked_add_signed(Duration::hours(TOKEN_VALIDITY_HOURS))
|
||||
.expect("could not calculate expiration date");
|
||||
let new_request = NewPasswordResetRequest {
|
||||
email: email.to_owned(),
|
||||
token: token.clone(),
|
||||
expiration_date,
|
||||
};
|
||||
diesel::insert_into(password_reset_requests::table)
|
||||
.values(new_request)
|
||||
.execute(conn)
|
||||
.map_err(Error::from)?;
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
pub fn find_by_token(conn: &Connection, token: &str) -> Result<Self> {
|
||||
let token = password_reset_requests::table
|
||||
.filter(password_reset_requests::token.eq(token))
|
||||
.first::<Self>(conn)
|
||||
.map_err(Error::from)?;
|
||||
|
||||
if token.expiration_date < Utc::now().naive_utc() {
|
||||
return Err(Error::Expired);
|
||||
}
|
||||
|
||||
Ok(token)
|
||||
}
|
||||
|
||||
pub fn find_and_delete_by_token(conn: &Connection, token: &str) -> Result<Self> {
|
||||
let request = Self::find_by_token(&conn, &token)?;
|
||||
|
||||
let filter =
|
||||
password_reset_requests::table.filter(password_reset_requests::id.eq(request.id));
|
||||
diesel::delete(filter).execute(conn)?;
|
||||
|
||||
Ok(request)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{tests::db, users::tests as user_tests};
|
||||
use diesel::Connection;
|
||||
|
||||
#[test]
|
||||
fn test_insert_and_find_password_reset_request() {
|
||||
let conn = db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
user_tests::fill_database(&conn);
|
||||
let admin_email = "admin@example.com";
|
||||
|
||||
let token = PasswordResetRequest::insert(&conn, admin_email)
|
||||
.expect("couldn't insert new request");
|
||||
let request = PasswordResetRequest::find_by_token(&conn, &token)
|
||||
.expect("couldn't retrieve request");
|
||||
|
||||
assert!(&token.len() > &32);
|
||||
assert_eq!(&request.email, &admin_email);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_insert_delete_previous_password_reset_request() {
|
||||
let conn = db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
user_tests::fill_database(&conn);
|
||||
let admin_email = "admin@example.com";
|
||||
|
||||
PasswordResetRequest::insert(&conn, &admin_email).expect("couldn't insert new request");
|
||||
PasswordResetRequest::insert(&conn, &admin_email)
|
||||
.expect("couldn't insert second request");
|
||||
|
||||
let count = password_reset_requests::table.count().get_result(&*conn);
|
||||
assert_eq!(Ok(1), count);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_password_reset_request_by_token_time() {
|
||||
let conn = db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
user_tests::fill_database(&conn);
|
||||
let admin_email = "admin@example.com";
|
||||
let token = "abcdef";
|
||||
let now = Utc::now().naive_utc();
|
||||
|
||||
diesel::insert_into(password_reset_requests::table)
|
||||
.values((
|
||||
password_reset_requests::email.eq(&admin_email),
|
||||
password_reset_requests::token.eq(&token),
|
||||
password_reset_requests::expiration_date.eq(now),
|
||||
))
|
||||
.execute(&*conn)
|
||||
.expect("could not insert request");
|
||||
|
||||
match PasswordResetRequest::find_by_token(&conn, &token) {
|
||||
Err(Error::Expired) => (),
|
||||
_ => panic!("Received unexpected result finding expired token"),
|
||||
}
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_find_and_delete_password_reset_request() {
|
||||
let conn = db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
user_tests::fill_database(&conn);
|
||||
let admin_email = "admin@example.com";
|
||||
|
||||
let token = PasswordResetRequest::insert(&conn, &admin_email)
|
||||
.expect("couldn't insert new request");
|
||||
PasswordResetRequest::find_and_delete_by_token(&conn, &token)
|
||||
.expect("couldn't find and delete request");
|
||||
|
||||
let count = password_reset_requests::table.count().get_result(&*conn);
|
||||
assert_eq!(Ok(0), count);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
@@ -2,7 +2,9 @@ pub use self::module::PlumeRocket;
|
||||
|
||||
#[cfg(not(test))]
|
||||
mod module {
|
||||
use crate::{db_conn::DbConn, search, users};
|
||||
use crate::db_conn::DbConn;
|
||||
use crate::search;
|
||||
use crate::users;
|
||||
use rocket::{
|
||||
request::{self, FlashMessage, FromRequest, Request},
|
||||
Outcome, State,
|
||||
@@ -20,35 +22,20 @@ mod module {
|
||||
pub flash_msg: Option<(String, String)>,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let conn = DbConn::from_request(request).await.succeeded().unwrap();
|
||||
let intl = rocket_i18n::I18n::from_request(request)
|
||||
.await
|
||||
.succeeded()
|
||||
.unwrap();
|
||||
let user = users::User::from_request(request)
|
||||
.await
|
||||
.succeeded()
|
||||
.unwrap();
|
||||
let worker = request
|
||||
.guard::<State<'_, Arc<ScheduledThreadPool>>>()
|
||||
.await
|
||||
.succeeded()
|
||||
.unwrap();
|
||||
let searcher = request
|
||||
.guard::<State<'_, Arc<search::Searcher>>>()
|
||||
.await
|
||||
.succeeded()
|
||||
.unwrap();
|
||||
let flash_msg = request.guard::<FlashMessage<'_, '_>>().await.succeeded();
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
|
||||
let conn = request.guard::<DbConn>()?;
|
||||
let intl = request.guard::<rocket_i18n::I18n>()?;
|
||||
let user = request.guard::<users::User>().succeeded();
|
||||
let worker = request.guard::<State<Arc<ScheduledThreadPool>>>()?;
|
||||
let searcher = request.guard::<State<Arc<search::Searcher>>>()?;
|
||||
let flash_msg = request.guard::<FlashMessage>().succeeded();
|
||||
Outcome::Success(PlumeRocket {
|
||||
conn,
|
||||
intl,
|
||||
user: Some(user),
|
||||
user,
|
||||
flash_msg: flash_msg.map(|f| (f.name().into(), f.msg().into())),
|
||||
worker: worker.clone(),
|
||||
searcher: searcher.clone(),
|
||||
@@ -59,7 +46,9 @@ mod module {
|
||||
|
||||
#[cfg(test)]
|
||||
mod module {
|
||||
use crate::{db_conn::DbConn, search, users};
|
||||
use crate::db_conn::DbConn;
|
||||
use crate::search;
|
||||
use crate::users;
|
||||
use rocket::{
|
||||
request::{self, FromRequest, Request},
|
||||
Outcome, State,
|
||||
@@ -75,18 +64,17 @@ mod module {
|
||||
pub worker: Arc<ScheduledThreadPool>,
|
||||
}
|
||||
|
||||
#[rocket::async_trait]
|
||||
impl<'a, 'r> FromRequest<'a, 'r> for PlumeRocket {
|
||||
type Error = ();
|
||||
|
||||
async fn from_request(request: &'a Request<'r>) -> request::Outcome<Self, Self::Error> {
|
||||
let conn = try_outcome!(DbConn::from_request(request).await);
|
||||
let user = try_outcome!(users::User::from_request(request).await);
|
||||
let worker = try_outcome!(request.guard::<'_, State<'_, Arc<ScheduledThreadPool>>>());
|
||||
let searcher = try_outcome!(request.guard::<'_, State<'_, Arc<search::Searcher>>>());
|
||||
fn from_request(request: &'a Request<'r>) -> request::Outcome<PlumeRocket, ()> {
|
||||
let conn = request.guard::<DbConn>()?;
|
||||
let user = request.guard::<users::User>().succeeded();
|
||||
let worker = request.guard::<State<Arc<ScheduledThreadPool>>>()?;
|
||||
let searcher = request.guard::<State<Arc<search::Searcher>>>()?;
|
||||
Outcome::Success(PlumeRocket {
|
||||
conn,
|
||||
user: Some(user),
|
||||
user,
|
||||
worker: worker.clone(),
|
||||
searcher: searcher.clone(),
|
||||
})
|
||||
|
||||
@@ -1,6 +1,10 @@
|
||||
use crate::{posts::Post, schema::post_authors, users::User, Error, Result};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use posts::Post;
|
||||
use schema::post_authors;
|
||||
use users::User;
|
||||
use {Error, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable, Associations)]
|
||||
#[belongs_to(Post)]
|
||||
#[belongs_to(User, foreign_key = "author_id")]
|
||||
|
||||
+126
-59
@@ -1,8 +1,3 @@
|
||||
use crate::{
|
||||
ap_url, blogs::Blog, instance::Instance, medias::Media, mentions::Mention, post_authors::*,
|
||||
safe_string::SafeString, schema::posts, search::Searcher, tags::*, timeline::*, users::User,
|
||||
Connection, Error, PlumeRocket, Result, CONFIG,
|
||||
};
|
||||
use activitypub::{
|
||||
activity::{Create, Delete, Update},
|
||||
link,
|
||||
@@ -12,6 +7,13 @@ use activitypub::{
|
||||
use chrono::{NaiveDateTime, TimeZone, Utc};
|
||||
use diesel::{self, BelongingToDsl, ExpressionMethods, QueryDsl, RunQueryDsl, SaveChangesDsl};
|
||||
use heck::{CamelCase, KebabCase};
|
||||
use serde_json;
|
||||
use std::collections::HashSet;
|
||||
|
||||
use blogs::Blog;
|
||||
use instance::Instance;
|
||||
use medias::Media;
|
||||
use mentions::Mention;
|
||||
use plume_common::{
|
||||
activity_pub::{
|
||||
inbox::{AsObject, FromId},
|
||||
@@ -19,8 +21,13 @@ use plume_common::{
|
||||
},
|
||||
utils::md_to_html,
|
||||
};
|
||||
use std::collections::HashSet;
|
||||
use tokio::runtime::Runtime;
|
||||
use post_authors::*;
|
||||
use safe_string::SafeString;
|
||||
use schema::posts;
|
||||
use search::Searcher;
|
||||
use tags::*;
|
||||
use users::User;
|
||||
use {ap_url, Connection, Error, PlumeRocket, Result, CONFIG};
|
||||
|
||||
pub type LicensedArticle = CustomObject<Licensed, Article>;
|
||||
|
||||
@@ -103,7 +110,7 @@ impl Post {
|
||||
tag: String,
|
||||
(min, max): (i32, i32),
|
||||
) -> Result<Vec<Post>> {
|
||||
use crate::schema::tags;
|
||||
use schema::tags;
|
||||
|
||||
let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id);
|
||||
posts::table
|
||||
@@ -117,7 +124,7 @@ impl Post {
|
||||
}
|
||||
|
||||
pub fn count_for_tag(conn: &Connection, tag: String) -> Result<i64> {
|
||||
use crate::schema::tags;
|
||||
use schema::tags;
|
||||
let ids = tags::table.filter(tags::tag.eq(tag)).select(tags::post_id);
|
||||
posts::table
|
||||
.filter(posts::id.eq_any(ids))
|
||||
@@ -131,8 +138,8 @@ impl Post {
|
||||
}
|
||||
|
||||
pub fn count_local(conn: &Connection) -> Result<i64> {
|
||||
use crate::schema::post_authors;
|
||||
use crate::schema::users;
|
||||
use schema::post_authors;
|
||||
use schema::users;
|
||||
let local_authors = users::table
|
||||
.filter(users::instance_id.eq(Instance::get_local()?.id))
|
||||
.select(users::id);
|
||||
@@ -175,12 +182,21 @@ impl Post {
|
||||
query.get_results::<Post>(conn).map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn get_recents(conn: &Connection, limit: i64) -> Result<Vec<Post>> {
|
||||
posts::table
|
||||
.order(posts::creation_date.desc())
|
||||
.filter(posts::published.eq(true))
|
||||
.limit(limit)
|
||||
.load::<Post>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn get_recents_for_author(
|
||||
conn: &Connection,
|
||||
author: &User,
|
||||
limit: i64,
|
||||
) -> Result<Vec<Post>> {
|
||||
use crate::schema::post_authors;
|
||||
use schema::post_authors;
|
||||
|
||||
let posts = PostAuthor::belonging_to(author).select(post_authors::post_id);
|
||||
posts::table
|
||||
@@ -230,8 +246,62 @@ impl Post {
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Give a page of all the recent posts known to this instance (= federated timeline)
|
||||
pub fn get_recents_page(conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Post>> {
|
||||
posts::table
|
||||
.order(posts::creation_date.desc())
|
||||
.filter(posts::published.eq(true))
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.load::<Post>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Give a page of posts from a specific instance
|
||||
pub fn get_instance_page(
|
||||
conn: &Connection,
|
||||
instance_id: i32,
|
||||
(min, max): (i32, i32),
|
||||
) -> Result<Vec<Post>> {
|
||||
use schema::blogs;
|
||||
|
||||
let blog_ids = blogs::table
|
||||
.filter(blogs::instance_id.eq(instance_id))
|
||||
.select(blogs::id);
|
||||
|
||||
posts::table
|
||||
.order(posts::creation_date.desc())
|
||||
.filter(posts::published.eq(true))
|
||||
.filter(posts::blog_id.eq_any(blog_ids))
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.load::<Post>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
/// Give a page of customized user feed, based on a list of followed users
|
||||
pub fn user_feed_page(
|
||||
conn: &Connection,
|
||||
followed: Vec<i32>,
|
||||
(min, max): (i32, i32),
|
||||
) -> Result<Vec<Post>> {
|
||||
use schema::post_authors;
|
||||
let post_ids = post_authors::table
|
||||
.filter(post_authors::author_id.eq_any(followed))
|
||||
.select(post_authors::post_id);
|
||||
|
||||
posts::table
|
||||
.order(posts::creation_date.desc())
|
||||
.filter(posts::published.eq(true))
|
||||
.filter(posts::id.eq_any(post_ids))
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.load::<Post>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn drafts_by_author(conn: &Connection, author: &User) -> Result<Vec<Post>> {
|
||||
use crate::schema::post_authors;
|
||||
use schema::post_authors;
|
||||
|
||||
let posts = PostAuthor::belonging_to(author).select(post_authors::post_id);
|
||||
posts::table
|
||||
@@ -243,8 +313,8 @@ impl Post {
|
||||
}
|
||||
|
||||
pub fn get_authors(&self, conn: &Connection) -> Result<Vec<User>> {
|
||||
use crate::schema::post_authors;
|
||||
use crate::schema::users;
|
||||
use schema::post_authors;
|
||||
use schema::users;
|
||||
let author_list = PostAuthor::belonging_to(self).select(post_authors::author_id);
|
||||
users::table
|
||||
.filter(users::id.eq_any(author_list))
|
||||
@@ -253,7 +323,7 @@ impl Post {
|
||||
}
|
||||
|
||||
pub fn is_author(&self, conn: &Connection, author_id: i32) -> Result<bool> {
|
||||
use crate::schema::post_authors;
|
||||
use schema::post_authors;
|
||||
Ok(PostAuthor::belonging_to(self)
|
||||
.filter(post_authors::author_id.eq(author_id))
|
||||
.count()
|
||||
@@ -262,15 +332,18 @@ impl Post {
|
||||
}
|
||||
|
||||
pub fn get_blog(&self, conn: &Connection) -> Result<Blog> {
|
||||
use crate::schema::blogs;
|
||||
use schema::blogs;
|
||||
blogs::table
|
||||
.filter(blogs::id.eq(self.blog_id))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
.limit(1)
|
||||
.load::<Blog>(conn)?
|
||||
.into_iter()
|
||||
.nth(0)
|
||||
.ok_or(Error::NotFound)
|
||||
}
|
||||
|
||||
pub fn count_likes(&self, conn: &Connection) -> Result<i64> {
|
||||
use crate::schema::likes;
|
||||
use schema::likes;
|
||||
likes::table
|
||||
.filter(likes::post_id.eq(self.id))
|
||||
.count()
|
||||
@@ -279,7 +352,7 @@ impl Post {
|
||||
}
|
||||
|
||||
pub fn count_reshares(&self, conn: &Connection) -> Result<i64> {
|
||||
use crate::schema::reshares;
|
||||
use schema::reshares;
|
||||
reshares::table
|
||||
.filter(reshares::post_id.eq(self.id))
|
||||
.count()
|
||||
@@ -554,11 +627,12 @@ impl FromId<PlumeRocket> for Post {
|
||||
type Error = Error;
|
||||
type Object = LicensedArticle;
|
||||
|
||||
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Self::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, article: LicensedArticle) -> Result<Self> {
|
||||
fn from_activity(c: &PlumeRocket, article: LicensedArticle) -> Result<Self> {
|
||||
let conn = &*c.conn;
|
||||
let searcher = &c.searcher;
|
||||
let license = article.custom_props.license_string().unwrap_or_default();
|
||||
let article = article.object;
|
||||
@@ -569,24 +643,24 @@ impl FromId<PlumeRocket> for Post {
|
||||
.into_iter()
|
||||
.fold((None, vec![]), |(blog, mut authors), link| {
|
||||
let url = link;
|
||||
match User::from_id(&mut c, &url, None) {
|
||||
match User::from_id(&c, &url, None) {
|
||||
Ok(u) => {
|
||||
authors.push(u);
|
||||
(blog, authors)
|
||||
}
|
||||
Err(_) => (blog.or_else(|| Blog::from_id(&mut c, &url, None).ok()), authors),
|
||||
Err(_) => (blog.or_else(|| Blog::from_id(&c, &url, None).ok()), authors),
|
||||
}
|
||||
});
|
||||
|
||||
let image = article.object_props.icon_object::<Image>().ok().unwrap();
|
||||
|
||||
let mut r = Runtime::new().unwrap();
|
||||
let cover =
|
||||
Some(r.block_on(async { Media::from_activity(&mut c, &image).await.ok().unwrap().id }));
|
||||
let cover = article
|
||||
.object_props
|
||||
.icon_object::<Image>()
|
||||
.ok()
|
||||
.and_then(|img| Media::from_activity(&c, &img).ok().map(|m| m.id));
|
||||
|
||||
let title = article.object_props.name_string()?;
|
||||
let post = Post::insert(
|
||||
&mut c.conn,
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blog?.id,
|
||||
slug: title.to_kebab_case(),
|
||||
@@ -609,7 +683,7 @@ impl FromId<PlumeRocket> for Post {
|
||||
|
||||
for author in authors {
|
||||
PostAuthor::insert(
|
||||
&mut c.conn,
|
||||
conn,
|
||||
NewPostAuthor {
|
||||
post_id: post.id,
|
||||
author_id: author.id,
|
||||
@@ -623,10 +697,10 @@ impl FromId<PlumeRocket> for Post {
|
||||
.into_iter()
|
||||
.map(|s| s.to_camel_case())
|
||||
.collect::<HashSet<_>>();
|
||||
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag {
|
||||
if let Some(serde_json::Value::Array(tags)) = article.object_props.tag.clone() {
|
||||
for tag in tags {
|
||||
serde_json::from_value::<link::Mention>(tag.clone())
|
||||
.map(|m| Mention::from_activity(&mut c.conn, &m, post.id, true, true))
|
||||
.map(|m| Mention::from_activity(conn, &m, post.id, true, true))
|
||||
.ok();
|
||||
|
||||
serde_json::from_value::<Hashtag>(tag.clone())
|
||||
@@ -634,7 +708,7 @@ impl FromId<PlumeRocket> for Post {
|
||||
.and_then(|t| {
|
||||
let tag_name = t.name_string()?;
|
||||
Ok(Tag::from_activity(
|
||||
&mut c.conn,
|
||||
conn,
|
||||
&t,
|
||||
post.id,
|
||||
hashtags.remove(&tag_name),
|
||||
@@ -643,28 +717,25 @@ impl FromId<PlumeRocket> for Post {
|
||||
.ok();
|
||||
}
|
||||
}
|
||||
|
||||
Timeline::add_to_all_timelines(c, &post, Kind::Original)?;
|
||||
|
||||
Ok(post)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Create, &mut PlumeRocket> for Post {
|
||||
impl AsObject<User, Create, &PlumeRocket> for Post {
|
||||
type Error = Error;
|
||||
type Output = Post;
|
||||
|
||||
fn activity(self, _c: &mut PlumeRocket, _actor: User, _id: &str) -> Result<Post> {
|
||||
fn activity(self, _c: &PlumeRocket, _actor: User, _id: &str) -> Result<Post> {
|
||||
// TODO: check that _actor is actually one of the author?
|
||||
Ok(self)
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Delete, &mut PlumeRocket> for Post {
|
||||
impl AsObject<User, Delete, &PlumeRocket> for Post {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
let can_delete = self
|
||||
.get_authors(&c.conn)?
|
||||
.into_iter()
|
||||
@@ -692,28 +763,23 @@ impl FromId<PlumeRocket> for PostUpdate {
|
||||
type Error = Error;
|
||||
type Object = LicensedArticle;
|
||||
|
||||
fn from_db(_: &mut PlumeRocket, _: &str) -> Result<Self> {
|
||||
fn from_db(_: &PlumeRocket, _: &str) -> Result<Self> {
|
||||
// Always fail because we always want to deserialize the AP object
|
||||
Err(Error::NotFound)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, updated: LicensedArticle) -> Result<Self> {
|
||||
let image = updated
|
||||
.object
|
||||
.object_props
|
||||
.icon_object::<Image>()
|
||||
.ok()
|
||||
.unwrap();
|
||||
let mut r = Runtime::new().unwrap();
|
||||
let cover =
|
||||
Some(r.block_on(async { Media::from_activity(&mut c, &image).await.ok().unwrap().id }));
|
||||
|
||||
fn from_activity(c: &PlumeRocket, updated: LicensedArticle) -> Result<Self> {
|
||||
Ok(PostUpdate {
|
||||
ap_url: updated.object.object_props.id_string()?,
|
||||
title: updated.object.object_props.name_string().ok(),
|
||||
subtitle: updated.object.object_props.summary_string().ok(),
|
||||
content: updated.object.object_props.content_string().ok(),
|
||||
cover,
|
||||
cover: updated
|
||||
.object
|
||||
.object_props
|
||||
.icon_object::<Image>()
|
||||
.ok()
|
||||
.and_then(|img| Media::from_activity(&c, &img).ok().map(|m| m.id)),
|
||||
source: updated
|
||||
.object
|
||||
.ap_object_props
|
||||
@@ -721,16 +787,16 @@ impl FromId<PlumeRocket> for PostUpdate {
|
||||
.ok()
|
||||
.map(|x| x.content),
|
||||
license: updated.custom_props.license_string().ok(),
|
||||
tags: updated.object.object_props.tag,
|
||||
tags: updated.object.object_props.tag.clone(),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Update, &mut PlumeRocket> for PostUpdate {
|
||||
impl AsObject<User, Update, &PlumeRocket> for PostUpdate {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
let conn = &*c.conn;
|
||||
let searcher = &c.searcher;
|
||||
let mut post = Post::from_id(c, &self.ap_url, None).map_err(|(_, e)| e)?;
|
||||
@@ -802,7 +868,7 @@ impl AsObject<User, Update, &mut PlumeRocket> for PostUpdate {
|
||||
|
||||
impl IntoId for Post {
|
||||
fn into_id(self) -> Id {
|
||||
Id::new(self.ap_url)
|
||||
Id::new(self.ap_url.clone())
|
||||
}
|
||||
}
|
||||
|
||||
@@ -862,6 +928,7 @@ mod tests {
|
||||
}
|
||||
_ => panic!("Unexpected result"),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,14 +1,16 @@
|
||||
use crate::{
|
||||
notifications::*, posts::Post, schema::reshares, timeline::*, users::User, Connection, Error,
|
||||
PlumeRocket, Result,
|
||||
};
|
||||
use activitypub::activity::{Announce, Undo};
|
||||
use chrono::NaiveDateTime;
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use notifications::*;
|
||||
use plume_common::activity_pub::{
|
||||
inbox::{AsActor, AsObject, FromId},
|
||||
Id, IntoId, PUBLIC_VISIBILITY,
|
||||
};
|
||||
use posts::Post;
|
||||
use schema::reshares;
|
||||
use users::User;
|
||||
use {Connection, Error, PlumeRocket, Result};
|
||||
|
||||
#[derive(Clone, Queryable, Identifiable)]
|
||||
pub struct Reshare {
|
||||
@@ -107,11 +109,11 @@ impl Reshare {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Announce, &mut PlumeRocket> for Post {
|
||||
impl AsObject<User, Announce, &PlumeRocket> for Post {
|
||||
type Error = Error;
|
||||
type Output = Reshare;
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, id: &str) -> Result<Reshare> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, id: &str) -> Result<Reshare> {
|
||||
let conn = &*c.conn;
|
||||
let reshare = Reshare::insert(
|
||||
conn,
|
||||
@@ -122,8 +124,6 @@ impl AsObject<User, Announce, &mut PlumeRocket> for Post {
|
||||
},
|
||||
)?;
|
||||
reshare.notify(conn)?;
|
||||
|
||||
Timeline::add_to_all_timelines(c, &self, Kind::Reshare(&actor))?;
|
||||
Ok(reshare)
|
||||
}
|
||||
}
|
||||
@@ -132,11 +132,11 @@ impl FromId<PlumeRocket> for Reshare {
|
||||
type Error = Error;
|
||||
type Object = Announce;
|
||||
|
||||
fn from_db(c: &mut PlumeRocket, id: &str) -> Result<Self> {
|
||||
fn from_db(c: &PlumeRocket, id: &str) -> Result<Self> {
|
||||
Reshare::find_by_ap_url(&c.conn, id)
|
||||
}
|
||||
|
||||
fn from_activity(c: &mut PlumeRocket, act: Announce) -> Result<Self> {
|
||||
fn from_activity(c: &PlumeRocket, act: Announce) -> Result<Self> {
|
||||
let res = Reshare::insert(
|
||||
&c.conn,
|
||||
NewReshare {
|
||||
@@ -154,11 +154,11 @@ impl FromId<PlumeRocket> for Reshare {
|
||||
}
|
||||
}
|
||||
|
||||
impl AsObject<User, Undo, &mut PlumeRocket> for Reshare {
|
||||
impl AsObject<User, Undo, &PlumeRocket> for Reshare {
|
||||
type Error = Error;
|
||||
type Output = ();
|
||||
|
||||
fn activity(self, c: &mut PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
fn activity(self, c: &PlumeRocket, actor: User, _id: &str) -> Result<()> {
|
||||
let conn = &*c.conn;
|
||||
if actor.id == self.user_id {
|
||||
diesel::delete(&self).execute(conn)?;
|
||||
|
||||
@@ -31,41 +31,7 @@ lazy_static! {
|
||||
.add_tag_attributes("label", ["for"].iter())
|
||||
.add_tag_attributes("input", ["type", "checked"].iter())
|
||||
.add_allowed_classes("input", ["cw-checkbox"].iter())
|
||||
.add_allowed_classes(
|
||||
"span",
|
||||
[
|
||||
"cw-container",
|
||||
"cw-text",
|
||||
//Scope classes for the syntax highlighting.
|
||||
"attribute-name",
|
||||
"comment",
|
||||
"constant",
|
||||
"control",
|
||||
"declaration",
|
||||
"entity",
|
||||
"function",
|
||||
"invalid",
|
||||
"keyword",
|
||||
"language",
|
||||
"modifier",
|
||||
"name",
|
||||
"numeric",
|
||||
"operator",
|
||||
"parameter",
|
||||
"punctuation",
|
||||
"source",
|
||||
"storage",
|
||||
"string",
|
||||
"support",
|
||||
"tag",
|
||||
"type",
|
||||
"variable",
|
||||
]
|
||||
.iter(),
|
||||
)
|
||||
// Related to https://github.com/Plume-org/Plume/issues/637
|
||||
.add_allowed_classes("sup", ["footnote-reference", "footnote-definition-label"].iter())
|
||||
.add_allowed_classes("div", ["footnote-definition"].iter())
|
||||
.add_allowed_classes("span", ["cw-container", "cw-text"].iter())
|
||||
.attribute_filter(|elem, att, val| match (elem, att) {
|
||||
("input", "type") => Some("checkbox".into()),
|
||||
("input", "checked") => Some("checked".into()),
|
||||
@@ -82,7 +48,7 @@ lazy_static! {
|
||||
};
|
||||
}
|
||||
|
||||
fn url_add_prefix(url: &str) -> Option<Cow<'_, str>> {
|
||||
fn url_add_prefix(url: &str) -> Option<Cow<str>> {
|
||||
if url.starts_with('#') && !url.starts_with("#postcontent-") {
|
||||
//if start with an #
|
||||
let mut new_url = "#postcontent-".to_owned(); //change to valid id
|
||||
@@ -139,7 +105,7 @@ struct SafeStringVisitor;
|
||||
impl<'de> Visitor<'de> for SafeStringVisitor {
|
||||
type Value = SafeString;
|
||||
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fn expecting(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
|
||||
formatter.write_str("a string")
|
||||
}
|
||||
|
||||
@@ -181,7 +147,7 @@ where
|
||||
DB: diesel::backend::Backend,
|
||||
str: ToSql<diesel::sql_types::Text, DB>,
|
||||
{
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<'_, W, DB>) -> serialize::Result {
|
||||
fn to_sql<W: Write>(&self, out: &mut Output<W, DB>) -> serialize::Result {
|
||||
str::to_sql(&self.value, out)
|
||||
}
|
||||
}
|
||||
@@ -193,7 +159,7 @@ impl Borrow<str> for SafeString {
|
||||
}
|
||||
|
||||
impl Display for SafeString {
|
||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
write!(f, "{}", self.value)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -47,7 +47,6 @@ table! {
|
||||
summary_html -> Text,
|
||||
icon_id -> Nullable<Int4>,
|
||||
banner_id -> Nullable<Int4>,
|
||||
theme -> Nullable<Varchar>,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -73,15 +72,6 @@ table! {
|
||||
user_id -> Int4,
|
||||
}
|
||||
}
|
||||
table! {
|
||||
email_blocklist(id){
|
||||
id -> Int4,
|
||||
email_address -> VarChar,
|
||||
note -> Text,
|
||||
notify_user -> Bool,
|
||||
notification_text -> Text,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
follows (id) {
|
||||
@@ -119,26 +109,6 @@ table! {
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
list_elems (id) {
|
||||
id -> Int4,
|
||||
list_id -> Int4,
|
||||
user_id -> Nullable<Int4>,
|
||||
blog_id -> Nullable<Int4>,
|
||||
word -> Nullable<Varchar>,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
lists (id) {
|
||||
id -> Int4,
|
||||
name -> Varchar,
|
||||
user_id -> Nullable<Int4>,
|
||||
#[sql_name = "type"]
|
||||
type_ -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
medias (id) {
|
||||
id -> Int4,
|
||||
@@ -171,15 +141,6 @@ table! {
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
password_reset_requests (id) {
|
||||
id -> Int4,
|
||||
email -> Varchar,
|
||||
token -> Varchar,
|
||||
expiration_date -> Timestamp,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
post_authors (id) {
|
||||
id -> Int4,
|
||||
@@ -224,23 +185,6 @@ table! {
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
timeline (id) {
|
||||
id -> Int4,
|
||||
post_id -> Int4,
|
||||
timeline_id -> Int4,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
timeline_definition (id) {
|
||||
id -> Int4,
|
||||
user_id -> Nullable<Int4>,
|
||||
name -> Varchar,
|
||||
query -> Varchar,
|
||||
}
|
||||
}
|
||||
|
||||
table! {
|
||||
users (id) {
|
||||
id -> Int4,
|
||||
@@ -248,6 +192,7 @@ table! {
|
||||
display_name -> Varchar,
|
||||
outbox_url -> Varchar,
|
||||
inbox_url -> Varchar,
|
||||
is_admin -> Bool,
|
||||
summary -> Text,
|
||||
email -> Nullable<Text>,
|
||||
hashed_password -> Nullable<Text>,
|
||||
@@ -262,9 +207,6 @@ table! {
|
||||
last_fetched_date -> Timestamp,
|
||||
fqn -> Text,
|
||||
summary_html -> Text,
|
||||
role -> Int4,
|
||||
preferred_theme -> Nullable<Varchar>,
|
||||
hide_custom_css -> Bool,
|
||||
}
|
||||
}
|
||||
|
||||
@@ -279,10 +221,6 @@ joinable!(comments -> posts (post_id));
|
||||
joinable!(comments -> users (author_id));
|
||||
joinable!(likes -> posts (post_id));
|
||||
joinable!(likes -> users (user_id));
|
||||
joinable!(list_elems -> blogs (blog_id));
|
||||
joinable!(list_elems -> lists (list_id));
|
||||
joinable!(list_elems -> users (user_id));
|
||||
joinable!(lists -> users (user_id));
|
||||
joinable!(mentions -> comments (comment_id));
|
||||
joinable!(mentions -> posts (post_id));
|
||||
joinable!(mentions -> users (mentioned_id));
|
||||
@@ -294,9 +232,6 @@ joinable!(posts -> medias (cover_id));
|
||||
joinable!(reshares -> posts (post_id));
|
||||
joinable!(reshares -> users (user_id));
|
||||
joinable!(tags -> posts (post_id));
|
||||
joinable!(timeline -> posts (post_id));
|
||||
joinable!(timeline -> timeline_definition (timeline_id));
|
||||
joinable!(timeline_definition -> users (user_id));
|
||||
joinable!(users -> instances (instance_id));
|
||||
|
||||
allow_tables_to_appear_in_same_query!(
|
||||
@@ -309,17 +244,12 @@ allow_tables_to_appear_in_same_query!(
|
||||
follows,
|
||||
instances,
|
||||
likes,
|
||||
list_elems,
|
||||
lists,
|
||||
medias,
|
||||
mentions,
|
||||
notifications,
|
||||
password_reset_requests,
|
||||
post_authors,
|
||||
posts,
|
||||
reshares,
|
||||
tags,
|
||||
timeline,
|
||||
timeline_definition,
|
||||
users,
|
||||
);
|
||||
|
||||
@@ -8,20 +8,18 @@ pub use self::searcher::*;
|
||||
pub(crate) mod tests {
|
||||
use super::{Query, Searcher};
|
||||
use diesel::Connection;
|
||||
use plume_common::utils::random_hex;
|
||||
use std::env::temp_dir;
|
||||
use std::str::FromStr;
|
||||
|
||||
use crate::{
|
||||
blogs::tests::fill_database,
|
||||
post_authors::*,
|
||||
posts::{NewPost, Post},
|
||||
safe_string::SafeString,
|
||||
tests::db,
|
||||
};
|
||||
use blogs::tests::fill_database;
|
||||
use plume_common::utils::random_hex;
|
||||
use post_authors::*;
|
||||
use posts::{NewPost, Post};
|
||||
use safe_string::SafeString;
|
||||
use tests::db;
|
||||
|
||||
pub(crate) fn get_searcher() -> Searcher {
|
||||
let dir = temp_dir().join(&format!("plume-test-{}", random_hex()));
|
||||
let dir = temp_dir().join("plume-test");
|
||||
if dir.exists() {
|
||||
Searcher::open(&dir)
|
||||
} else {
|
||||
@@ -98,10 +96,11 @@ pub(crate) mod tests {
|
||||
|
||||
#[test]
|
||||
fn open() {
|
||||
let dir = temp_dir().join(format!("plume-test-{}", random_hex()));
|
||||
{
|
||||
Searcher::create(&dir).unwrap();
|
||||
}
|
||||
get_searcher()
|
||||
}; //make sure $tmp/plume-test-tantivy exist
|
||||
|
||||
let dir = temp_dir().join("plume-test");
|
||||
Searcher::open(&dir).unwrap();
|
||||
}
|
||||
|
||||
@@ -176,6 +175,7 @@ pub(crate) mod tests {
|
||||
assert!(searcher
|
||||
.search_document(conn, Query::from_str(&newtitle).unwrap(), (0, 1))
|
||||
.is_empty());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
use crate::search::searcher::Searcher;
|
||||
use chrono::{naive::NaiveDate, offset::Utc, Datelike};
|
||||
use search::searcher::Searcher;
|
||||
use std::{cmp, ops::Bound};
|
||||
use tantivy::{query::*, schema::*, Term};
|
||||
|
||||
@@ -153,7 +153,7 @@ impl PlumeQuery {
|
||||
|
||||
/// Convert this Query to a Tantivy Query
|
||||
pub fn into_query(self) -> BooleanQuery {
|
||||
let mut result: Vec<(Occur, Box<dyn Query>)> = Vec::new();
|
||||
let mut result: Vec<(Occur, Box<Query>)> = Vec::new();
|
||||
gen_to_query!(self, result; normal: title, subtitle, content, tag;
|
||||
oneoff: instance, author, blog, lang, license);
|
||||
|
||||
@@ -279,7 +279,7 @@ impl PlumeQuery {
|
||||
}
|
||||
|
||||
// map a token and it's field to a query
|
||||
fn token_to_query(token: &str, field_name: &str) -> Box<dyn Query> {
|
||||
fn token_to_query(token: &str, field_name: &str) -> Box<Query> {
|
||||
let token = token.to_lowercase();
|
||||
let token = token.as_str();
|
||||
let field = Searcher::schema().get_field(field_name).unwrap();
|
||||
|
||||
@@ -1,11 +1,9 @@
|
||||
use crate::{
|
||||
instance::Instance,
|
||||
posts::Post,
|
||||
schema::posts,
|
||||
search::{query::PlumeQuery, tokenizer},
|
||||
tags::Tag,
|
||||
Connection, Result,
|
||||
};
|
||||
use instance::Instance;
|
||||
use posts::Post;
|
||||
use schema::posts;
|
||||
use tags::Tag;
|
||||
use Connection;
|
||||
|
||||
use chrono::Datelike;
|
||||
use diesel::{ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use itertools::Itertools;
|
||||
@@ -16,6 +14,10 @@ use tantivy::{
|
||||
};
|
||||
use whatlang::{detect as detect_lang, Lang};
|
||||
|
||||
use super::tokenizer;
|
||||
use search::query::PlumeQuery;
|
||||
use Result;
|
||||
|
||||
#[derive(Debug)]
|
||||
pub enum SearcherError {
|
||||
IndexCreationError,
|
||||
@@ -70,7 +72,7 @@ impl Searcher {
|
||||
schema_builder.build()
|
||||
}
|
||||
|
||||
pub fn create(path: &dyn AsRef<Path>) -> Result<Self> {
|
||||
pub fn create(path: &AsRef<Path>) -> Result<Self> {
|
||||
let whitespace_tokenizer = tokenizer::WhitespaceTokenizer.filter(LowerCaser);
|
||||
|
||||
let content_tokenizer = SimpleTokenizer
|
||||
@@ -109,7 +111,7 @@ impl Searcher {
|
||||
})
|
||||
}
|
||||
|
||||
pub fn open(path: &dyn AsRef<Path>) -> Result<Self> {
|
||||
pub fn open(path: &AsRef<Path>) -> Result<Self> {
|
||||
let whitespace_tokenizer = tokenizer::WhitespaceTokenizer.filter(LowerCaser);
|
||||
|
||||
let content_tokenizer = SimpleTokenizer
|
||||
@@ -146,10 +148,6 @@ impl Searcher {
|
||||
}
|
||||
|
||||
pub fn add_document(&self, conn: &Connection, post: &Post) -> Result<()> {
|
||||
if !post.published {
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
let schema = self.index.schema();
|
||||
|
||||
let post_id = schema.get_field("post_id").unwrap();
|
||||
@@ -173,7 +171,7 @@ impl Searcher {
|
||||
post_id => i64::from(post.id),
|
||||
author => post.get_authors(conn)?.into_iter().map(|u| u.fqn).join(" "),
|
||||
creation_date => i64::from(post.creation_date.num_days_from_ce()),
|
||||
instance => Instance::get(conn, post.get_blog(conn)?.instance_id)?.public_domain,
|
||||
instance => Instance::get(conn, post.get_blog(conn)?.instance_id)?.public_domain.clone(),
|
||||
tag => Tag::for_post(conn, post.id)?.into_iter().map(|t| t.tag).join(" "),
|
||||
blog_name => post.get_blog(conn)?.title,
|
||||
content => post.content.get().clone(),
|
||||
|
||||
@@ -1,6 +1,9 @@
|
||||
use crate::{ap_url, instance::Instance, schema::tags, Connection, Error, Result};
|
||||
use diesel::{self, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
|
||||
use instance::Instance;
|
||||
use plume_common::activity_pub::Hashtag;
|
||||
use schema::tags;
|
||||
use {ap_url, Connection, Error, Result};
|
||||
|
||||
#[derive(Clone, Identifiable, Queryable)]
|
||||
pub struct Tag {
|
||||
|
||||
@@ -1,846 +0,0 @@
|
||||
use crate::{
|
||||
lists::List,
|
||||
posts::Post,
|
||||
schema::{posts, timeline, timeline_definition},
|
||||
Connection, Error, PlumeRocket, Result,
|
||||
};
|
||||
use diesel::{self, BoolExpressionMethods, ExpressionMethods, QueryDsl, RunQueryDsl};
|
||||
use std::ops::Deref;
|
||||
|
||||
pub(crate) mod query;
|
||||
|
||||
pub use self::query::Kind;
|
||||
use self::query::{QueryError, TimelineQuery};
|
||||
|
||||
#[derive(Clone, Debug, PartialEq, Queryable, Identifiable, AsChangeset)]
|
||||
#[table_name = "timeline_definition"]
|
||||
pub struct Timeline {
|
||||
pub id: i32,
|
||||
pub user_id: Option<i32>,
|
||||
pub name: String,
|
||||
pub query: String,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
#[table_name = "timeline_definition"]
|
||||
pub struct NewTimeline {
|
||||
user_id: Option<i32>,
|
||||
name: String,
|
||||
query: String,
|
||||
}
|
||||
|
||||
#[derive(Default, Insertable)]
|
||||
#[table_name = "timeline"]
|
||||
struct TimelineEntry {
|
||||
pub post_id: i32,
|
||||
pub timeline_id: i32,
|
||||
}
|
||||
|
||||
impl Timeline {
|
||||
insert!(timeline_definition, NewTimeline);
|
||||
get!(timeline_definition);
|
||||
|
||||
pub fn find_for_user_by_name(
|
||||
conn: &Connection,
|
||||
user_id: Option<i32>,
|
||||
name: &str,
|
||||
) -> Result<Self> {
|
||||
if let Some(user_id) = user_id {
|
||||
timeline_definition::table
|
||||
.filter(timeline_definition::user_id.eq(user_id))
|
||||
.filter(timeline_definition::name.eq(name))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
} else {
|
||||
timeline_definition::table
|
||||
.filter(timeline_definition::user_id.is_null())
|
||||
.filter(timeline_definition::name.eq(name))
|
||||
.first(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn list_for_user(conn: &Connection, user_id: Option<i32>) -> Result<Vec<Self>> {
|
||||
if let Some(user_id) = user_id {
|
||||
timeline_definition::table
|
||||
.filter(timeline_definition::user_id.eq(user_id))
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
} else {
|
||||
timeline_definition::table
|
||||
.filter(timeline_definition::user_id.is_null())
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
/// Same as `list_for_user`, but also includes instance timelines if `user_id` is `Some`.
|
||||
pub fn list_all_for_user(conn: &Connection, user_id: Option<i32>) -> Result<Vec<Self>> {
|
||||
if let Some(user_id) = user_id {
|
||||
timeline_definition::table
|
||||
.filter(
|
||||
timeline_definition::user_id
|
||||
.eq(user_id)
|
||||
.or(timeline_definition::user_id.is_null()),
|
||||
)
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
} else {
|
||||
timeline_definition::table
|
||||
.filter(timeline_definition::user_id.is_null())
|
||||
.load::<Self>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
}
|
||||
|
||||
pub fn new_for_user(
|
||||
conn: &Connection,
|
||||
user_id: i32,
|
||||
name: String,
|
||||
query_string: String,
|
||||
) -> Result<Timeline> {
|
||||
{
|
||||
let query = TimelineQuery::parse(&query_string)?; // verify the query is valid
|
||||
if let Some(err) =
|
||||
query
|
||||
.list_used_lists()
|
||||
.into_iter()
|
||||
.find_map(|(name, kind)| {
|
||||
let list = List::find_for_user_by_name(conn, Some(user_id), &name)
|
||||
.map(|l| l.kind() == kind);
|
||||
match list {
|
||||
Ok(true) => None,
|
||||
Ok(false) => Some(Error::TimelineQuery(QueryError::RuntimeError(
|
||||
format!("list '{}' has the wrong type for this usage", name),
|
||||
))),
|
||||
Err(_) => Some(Error::TimelineQuery(QueryError::RuntimeError(
|
||||
format!("list '{}' was not found", name),
|
||||
))),
|
||||
}
|
||||
})
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
Self::insert(
|
||||
conn,
|
||||
NewTimeline {
|
||||
user_id: Some(user_id),
|
||||
name,
|
||||
query: query_string,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn new_for_instance(
|
||||
conn: &Connection,
|
||||
name: String,
|
||||
query_string: String,
|
||||
) -> Result<Timeline> {
|
||||
{
|
||||
let query = TimelineQuery::parse(&query_string)?; // verify the query is valid
|
||||
if let Some(err) =
|
||||
query
|
||||
.list_used_lists()
|
||||
.into_iter()
|
||||
.find_map(|(name, kind)| {
|
||||
let list = List::find_for_user_by_name(conn, None, &name)
|
||||
.map(|l| l.kind() == kind);
|
||||
match list {
|
||||
Ok(true) => None,
|
||||
Ok(false) => Some(Error::TimelineQuery(QueryError::RuntimeError(
|
||||
format!("list '{}' has the wrong type for this usage", name),
|
||||
))),
|
||||
Err(_) => Some(Error::TimelineQuery(QueryError::RuntimeError(
|
||||
format!("list '{}' was not found", name),
|
||||
))),
|
||||
}
|
||||
})
|
||||
{
|
||||
return Err(err);
|
||||
}
|
||||
}
|
||||
Self::insert(
|
||||
conn,
|
||||
NewTimeline {
|
||||
user_id: None,
|
||||
name,
|
||||
query: query_string,
|
||||
},
|
||||
)
|
||||
}
|
||||
|
||||
pub fn update(&self, conn: &Connection) -> Result<Self> {
|
||||
diesel::update(self).set(self).execute(conn)?;
|
||||
let timeline = Self::get(conn, self.id)?;
|
||||
Ok(timeline)
|
||||
}
|
||||
|
||||
pub fn delete(&self, conn: &Connection) -> Result<()> {
|
||||
diesel::delete(self)
|
||||
.execute(conn)
|
||||
.map(|_| ())
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn get_latest(&self, conn: &Connection, count: i32) -> Result<Vec<Post>> {
|
||||
self.get_page(conn, (0, count))
|
||||
}
|
||||
|
||||
pub fn get_page(&self, conn: &Connection, (min, max): (i32, i32)) -> Result<Vec<Post>> {
|
||||
timeline::table
|
||||
.filter(timeline::timeline_id.eq(self.id))
|
||||
.inner_join(posts::table)
|
||||
.order(posts::creation_date.desc())
|
||||
.offset(min.into())
|
||||
.limit((max - min).into())
|
||||
.select(posts::all_columns)
|
||||
.load::<Post>(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn count_posts(&self, conn: &Connection) -> Result<i64> {
|
||||
timeline::table
|
||||
.filter(timeline::timeline_id.eq(self.id))
|
||||
.inner_join(posts::table)
|
||||
.count()
|
||||
.get_result(conn)
|
||||
.map_err(Error::from)
|
||||
}
|
||||
|
||||
pub fn add_to_all_timelines(rocket: &mut PlumeRocket, post: &Post, kind: Kind<'_>) -> Result<()> {
|
||||
let timelines = timeline_definition::table
|
||||
.load::<Self>(rocket.conn.deref())
|
||||
.map_err(Error::from)?;
|
||||
|
||||
for t in timelines {
|
||||
if t.matches(rocket, post, kind)? {
|
||||
t.add_post(&rocket.conn, post)?;
|
||||
}
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn add_post(&self, conn: &Connection, post: &Post) -> Result<()> {
|
||||
diesel::insert_into(timeline::table)
|
||||
.values(TimelineEntry {
|
||||
post_id: post.id,
|
||||
timeline_id: self.id,
|
||||
})
|
||||
.execute(conn)?;
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn matches(&self, rocket: &mut PlumeRocket, post: &Post, kind: Kind<'_>) -> Result<bool> {
|
||||
let query = TimelineQuery::parse(&self.query)?;
|
||||
query.matches(rocket, self, post, kind)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use crate::{
|
||||
blogs::tests as blogTests,
|
||||
follows::*,
|
||||
lists::ListType,
|
||||
post_authors::{NewPostAuthor, PostAuthor},
|
||||
posts::NewPost,
|
||||
safe_string::SafeString,
|
||||
tags::Tag,
|
||||
tests::{db, rockets},
|
||||
users::tests as userTests,
|
||||
};
|
||||
use diesel::Connection;
|
||||
|
||||
#[test]
|
||||
fn test_timeline() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let users = userTests::fill_database(conn);
|
||||
|
||||
let mut tl1_u1 = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"my timeline".to_owned(),
|
||||
"all".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
List::new(conn, "languages I speak", Some(&users[1]), ListType::Prefix).unwrap();
|
||||
let tl2_u1 = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"another timeline".to_owned(),
|
||||
"followed".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
let tl1_u2 = Timeline::new_for_user(
|
||||
conn,
|
||||
users[1].id,
|
||||
"english posts".to_owned(),
|
||||
"lang in \"languages I speak\"".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
let tl1_instance = Timeline::new_for_instance(
|
||||
conn,
|
||||
"english posts".to_owned(),
|
||||
"license in [cc]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
assert_eq!(tl1_u1, Timeline::get(conn, tl1_u1.id).unwrap());
|
||||
assert_eq!(
|
||||
tl2_u1,
|
||||
Timeline::find_for_user_by_name(conn, Some(users[0].id), "another timeline")
|
||||
.unwrap()
|
||||
);
|
||||
assert_eq!(
|
||||
tl1_instance,
|
||||
Timeline::find_for_user_by_name(conn, None, "english posts").unwrap()
|
||||
);
|
||||
|
||||
let tl_u1 = Timeline::list_for_user(conn, Some(users[0].id)).unwrap();
|
||||
assert_eq!(3, tl_u1.len()); // it is not 2 because there is a "Your feed" tl created for each user automatically
|
||||
assert!(tl_u1.iter().fold(false, |res, tl| { res || *tl == tl1_u1 }));
|
||||
assert!(tl_u1.iter().fold(false, |res, tl| { res || *tl == tl2_u1 }));
|
||||
|
||||
let tl_instance = Timeline::list_for_user(conn, None).unwrap();
|
||||
assert_eq!(3, tl_instance.len()); // there are also the local and federated feed by default
|
||||
assert!(tl_instance
|
||||
.iter()
|
||||
.fold(false, |res, tl| { res || *tl == tl1_instance }));
|
||||
|
||||
tl1_u1.name = "My Super TL".to_owned();
|
||||
let new_tl1_u2 = tl1_u2.update(conn).unwrap();
|
||||
|
||||
let tl_u2 = Timeline::list_for_user(conn, Some(users[1].id)).unwrap();
|
||||
assert_eq!(2, tl_u2.len()); // same here
|
||||
assert!(tl_u2
|
||||
.iter()
|
||||
.fold(false, |res, tl| { res || *tl == new_tl1_u2 }));
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_timeline_creation_error() {
|
||||
let conn = &db();
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let users = userTests::fill_database(conn);
|
||||
|
||||
assert!(Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"my timeline".to_owned(),
|
||||
"invalid keyword".to_owned(),
|
||||
)
|
||||
.is_err());
|
||||
assert!(Timeline::new_for_instance(
|
||||
conn,
|
||||
"my timeline".to_owned(),
|
||||
"invalid keyword".to_owned(),
|
||||
)
|
||||
.is_err());
|
||||
|
||||
assert!(Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"my timeline".to_owned(),
|
||||
"author in non_existant_list".to_owned(),
|
||||
)
|
||||
.is_err());
|
||||
assert!(Timeline::new_for_instance(
|
||||
conn,
|
||||
"my timeline".to_owned(),
|
||||
"lang in dont-exist".to_owned(),
|
||||
)
|
||||
.is_err());
|
||||
|
||||
List::new(conn, "friends", Some(&users[0]), ListType::User).unwrap();
|
||||
List::new(conn, "idk", None, ListType::Blog).unwrap();
|
||||
|
||||
assert!(Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"my timeline".to_owned(),
|
||||
"blog in friends".to_owned(),
|
||||
)
|
||||
.is_err());
|
||||
assert!(Timeline::new_for_instance(
|
||||
conn,
|
||||
"my timeline".to_owned(),
|
||||
"not author in idk".to_owned(),
|
||||
)
|
||||
.is_err());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_simple_match() {
|
||||
let r = &rockets();
|
||||
let conn = &r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blogTests::fill_database(conn);
|
||||
|
||||
let gnu_tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"GNU timeline".to_owned(),
|
||||
"license in [AGPL, LGPL, GPL]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let gnu_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug".to_string(),
|
||||
title: "About Linux".to_string(),
|
||||
content: SafeString::new("you must say GNU/Linux, not Linux!!!"),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
source: "you must say GNU/Linux, not Linux!!!".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(gnu_tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
|
||||
let non_free_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug2".to_string(),
|
||||
title: "Private is bad".to_string(),
|
||||
content: SafeString::new("so is Microsoft"),
|
||||
published: true,
|
||||
license: "all right reserved".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
source: "so is Microsoft".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!gnu_tl.matches(r, &non_free_post, Kind::Original).unwrap());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_complex_match() {
|
||||
let r = &rockets();
|
||||
let conn = &r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blogTests::fill_database(conn);
|
||||
Follow::insert(
|
||||
conn,
|
||||
NewFollow {
|
||||
follower_id: users[0].id,
|
||||
following_id: users[1].id,
|
||||
ap_url: String::new(),
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let fav_blogs_list =
|
||||
List::new(conn, "fav_blogs", Some(&users[0]), ListType::Blog).unwrap();
|
||||
fav_blogs_list.add_blogs(conn, &[blogs[0].id]).unwrap();
|
||||
|
||||
let my_tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"My timeline".to_owned(),
|
||||
"blog in fav_blogs and not has_cover or local and followed exclude likes"
|
||||
.to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "about-linux".to_string(),
|
||||
title: "About Linux".to_string(),
|
||||
content: SafeString::new("you must say GNU/Linux, not Linux!!!"),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
source: "you must say GNU/Linux, not Linux!!!".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(my_tl.matches(r, &post, Kind::Original).unwrap()); // matches because of "blog in fav_blogs" (and there is no cover)
|
||||
|
||||
let post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[1].id,
|
||||
slug: "about-linux-2".to_string(),
|
||||
title: "About Linux (2)".to_string(),
|
||||
content: SafeString::new(
|
||||
"Actually, GNU+Linux, GNU×Linux, or GNU¿Linux are better.",
|
||||
),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
source: "Actually, GNU+Linux, GNU×Linux, or GNU¿Linux are better.".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!my_tl.matches(r, &post, Kind::Like(&users[1])).unwrap());
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_add_to_all_timelines() {
|
||||
let r = &rockets();
|
||||
let conn = &r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blogTests::fill_database(conn);
|
||||
|
||||
let gnu_tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"GNU timeline".to_owned(),
|
||||
"license in [AGPL, LGPL, GPL]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
let non_gnu_tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Stallman disapproved timeline".to_owned(),
|
||||
"not license in [AGPL, LGPL, GPL]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let gnu_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug".to_string(),
|
||||
title: "About Linux".to_string(),
|
||||
content: SafeString::new("you must say GNU/Linux, not Linux!!!"),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
source: "you must say GNU/Linux, not Linux!!!".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let non_free_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug2".to_string(),
|
||||
title: "Private is bad".to_string(),
|
||||
content: SafeString::new("so is Microsoft"),
|
||||
published: true,
|
||||
license: "all right reserved".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
source: "so is Microsoft".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
Timeline::add_to_all_timelines(r, &gnu_post, Kind::Original).unwrap();
|
||||
Timeline::add_to_all_timelines(r, &non_free_post, Kind::Original).unwrap();
|
||||
|
||||
let res = gnu_tl.get_latest(conn, 2).unwrap();
|
||||
assert_eq!(res.len(), 1);
|
||||
assert_eq!(res[0].id, gnu_post.id);
|
||||
let res = non_gnu_tl.get_latest(conn, 2).unwrap();
|
||||
assert_eq!(res.len(), 1);
|
||||
assert_eq!(res[0].id, non_free_post.id);
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn test_matches_lists_direct() {
|
||||
let r = &rockets();
|
||||
let conn = &r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blogTests::fill_database(conn);
|
||||
|
||||
let gnu_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug".to_string(),
|
||||
title: "About Linux".to_string(),
|
||||
content: SafeString::new("you must say GNU/Linux, not Linux!!!"),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
source: "you must say GNU/Linux, not Linux!!!".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
gnu_post
|
||||
.update_tags(conn, vec![Tag::build_activity("free".to_owned()).unwrap()])
|
||||
.unwrap();
|
||||
PostAuthor::insert(
|
||||
conn,
|
||||
NewPostAuthor {
|
||||
post_id: gnu_post.id,
|
||||
author_id: blogs[0].list_authors(conn).unwrap()[0].id,
|
||||
},
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"blog timeline".to_owned(),
|
||||
format!("blog in [{}]", blogs[0].fqn),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"blog timeline".to_owned(),
|
||||
"blog in [no_one@nowhere]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"author timeline".to_owned(),
|
||||
format!(
|
||||
"author in [{}]",
|
||||
blogs[0].list_authors(conn).unwrap()[0].fqn
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"author timeline".to_owned(),
|
||||
format!("author in [{}]", users[2].fqn),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Reshare(&users[2])).unwrap());
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Like(&users[2])).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"author timeline".to_owned(),
|
||||
format!(
|
||||
"author in [{}] include likes exclude reshares",
|
||||
users[2].fqn
|
||||
),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Reshare(&users[2])).unwrap());
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Like(&users[2])).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"tag timeline".to_owned(),
|
||||
"tags in [free]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"tag timeline".to_owned(),
|
||||
"tags in [private]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"english timeline".to_owned(),
|
||||
"lang in [en]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"franco-italian timeline".to_owned(),
|
||||
"lang in [fr, it]".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
|
||||
/*
|
||||
#[test]
|
||||
fn test_matches_lists_saved() {
|
||||
let r = &rockets();
|
||||
let conn = &r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blogTests::fill_database(conn);
|
||||
|
||||
let gnu_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug".to_string(),
|
||||
title: "About Linux".to_string(),
|
||||
content: SafeString::new("you must say GNU/Linux, not Linux!!!"),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "".to_string(),
|
||||
source: "you must say GNU/Linux, not Linux!!!".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
gnu_post.update_tags(conn, vec![Tag::build_activity("free".to_owned()).unwrap()]).unwrap();
|
||||
PostAuthor::insert(conn, NewPostAuthor {post_id: gnu_post.id, author_id: blogs[0].list_authors(conn).unwrap()[0].id}).unwrap();
|
||||
|
||||
unimplemented!();
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}*/
|
||||
|
||||
#[test]
|
||||
fn test_matches_keyword() {
|
||||
let r = &rockets();
|
||||
let conn = &r.conn;
|
||||
conn.test_transaction::<_, (), _>(|| {
|
||||
let (users, blogs) = blogTests::fill_database(conn);
|
||||
|
||||
let gnu_post = Post::insert(
|
||||
conn,
|
||||
NewPost {
|
||||
blog_id: blogs[0].id,
|
||||
slug: "slug".to_string(),
|
||||
title: "About Linux".to_string(),
|
||||
content: SafeString::new("you must say GNU/Linux, not Linux!!!"),
|
||||
published: true,
|
||||
license: "GPL".to_string(),
|
||||
ap_url: "".to_string(),
|
||||
creation_date: None,
|
||||
subtitle: "Stallman is our god".to_string(),
|
||||
source: "you must say GNU/Linux, not Linux!!!".to_string(),
|
||||
cover_id: None,
|
||||
},
|
||||
&r.searcher,
|
||||
)
|
||||
.unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Linux title".to_owned(),
|
||||
"title contains Linux".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Microsoft title".to_owned(),
|
||||
"title contains Microsoft".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Linux subtitle".to_owned(),
|
||||
"subtitle contains Stallman".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Microsoft subtitle".to_owned(),
|
||||
"subtitle contains Nadella".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Linux content".to_owned(),
|
||||
"content contains Linux".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
let tl = Timeline::new_for_user(
|
||||
conn,
|
||||
users[0].id,
|
||||
"Microsoft content".to_owned(),
|
||||
"subtitle contains Windows".to_owned(),
|
||||
)
|
||||
.unwrap();
|
||||
assert!(!tl.matches(r, &gnu_post, Kind::Original).unwrap());
|
||||
tl.delete(conn).unwrap();
|
||||
|
||||
Ok(())
|
||||
});
|
||||
}
|
||||
}
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user