Compare commits

..

No commits in common. "main" and "comments-posts-public" have entirely different histories.

214 changed files with 5497 additions and 7042 deletions

View file

@ -9,7 +9,7 @@ platform:
steps:
- name: chown repo
image: ekidd/rust-musl-builder:1.50.0
image: ekidd/rust-musl-builder:1.47.0
user: root
commands:
- chown 1000:1000 . -R
@ -20,33 +20,23 @@ steps:
- /root/.cargo/bin/cargo fmt -- --check
- name: cargo clippy
image: ekidd/rust-musl-builder:1.50.0
environment:
CARGO_HOME: /drone/src/.cargo
image: ekidd/rust-musl-builder:1.47.0
commands:
- whoami
- ls -la ~/.cargo
- mv ~/.cargo .
- ls -la .cargo
- cargo clippy --workspace --tests --all-targets --all-features -- -D warnings -D deprecated -D clippy::perf -D clippy::complexity -D clippy::dbg_macro
- cargo clippy --workspace -- -D clippy::unwrap_used
- name: cargo test
image: ekidd/rust-musl-builder:1.50.0
image: ekidd/rust-musl-builder:1.47.0
environment:
LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
RUST_BACKTRACE: 1
RUST_TEST_THREADS: 1
CARGO_HOME: /drone/src/.cargo
commands:
- sudo apt-get update
- sudo apt-get -y install --no-install-recommends espeak postgresql-client
- cargo test --workspace --no-fail-fast
- name: cargo build
image: ekidd/rust-musl-builder:1.50.0
environment:
CARGO_HOME: /drone/src/.cargo
image: ekidd/rust-musl-builder:1.47.0
commands:
- cargo build
- mv target/x86_64-unknown-linux-musl/debug/lemmy_server target/lemmy_server
@ -113,12 +103,11 @@ platform:
steps:
- name: cargo test
image: rust:1.50-slim-buster
image: rust:1.47-slim-buster
environment:
LEMMY_DATABASE_URL: postgres://lemmy:password@database:5432/lemmy
RUST_BACKTRACE: 1
RUST_TEST_THREADS: 1
CARGO_HOME: /drone/src/.cargo
commands:
- apt-get update
- apt-get -y install --no-install-recommends espeak postgresql-client libssl-dev pkg-config libpq-dev
@ -127,9 +116,7 @@ steps:
# Using Debian here because there seems to be no official Alpine-based Rust docker image for ARM.
- name: cargo build
image: rust:1.50-slim-buster
environment:
CARGO_HOME: /drone/src/.cargo
image: rust:1.47-slim-buster
commands:
- apt-get update
- apt-get -y install --no-install-recommends libssl-dev pkg-config libpq-dev

View file

@ -1 +1 @@
*.sqldump
*.sql

View file

@ -1,5 +1,5 @@
tab_spaces = 2
edition="2018"
imports_layout="HorizontalVertical"
imports_granularity="Crate"
merge_imports=true
reorder_imports=true

654
Cargo.lock generated

File diff suppressed because it is too large Load diff

View file

@ -3,9 +3,6 @@ name = "lemmy_server"
version = "0.0.1"
edition = "2018"
[lib]
doctest = false
[profile.dev]
debug = 0
@ -19,9 +16,8 @@ members = [
"crates/db_views",
"crates/db_views_actor",
"crates/db_views_actor",
"crates/api_structs",
"crates/structs",
"crates/websocket",
"crates/routes"
]
[dependencies]
@ -33,22 +29,27 @@ lemmy_db_queries = { path = "./crates/db_queries" }
lemmy_db_views = { path = "./crates/db_views" }
lemmy_db_views_moderator = { path = "./crates/db_views_moderator" }
lemmy_db_views_actor = { path = "./crates/db_views_actor" }
lemmy_api_structs = { path = "crates/api_structs" }
lemmy_structs = { path = "./crates/structs" }
lemmy_websocket = { path = "./crates/websocket" }
lemmy_routes = { path = "./crates/routes" }
diesel = "1.4.5"
diesel_migrations = "1.4.0"
chrono = { version = "0.4.19", features = ["serde"] }
serde = { version = "1.0.123", features = ["derive"] }
actix = "0.10.0"
actix-web = { version = "3.3.2", default-features = false, features = ["rustls"] }
actix-files = { version = "0.5.0", default-features = false }
actix-web-actors = { version = "3.0.0", default-features = false }
awc = { version = "2.0.3", default-features = false }
log = "0.4.14"
env_logger = "0.8.2"
strum = "0.20.0"
url = { version = "2.2.1", features = ["serde"] }
lazy_static = "1.4.0"
rss = "1.10.0"
url = { version = "2.2.0", features = ["serde"] }
openssl = "0.10.32"
http-signature-normalization-actix = { version = "0.4.1", default-features = false, features = ["sha-2"] }
tokio = "0.3.6"
sha2 = "0.9.3"
anyhow = "1.0.38"
reqwest = { version = "0.10.10", features = ["json"] }
activitystreams = "0.7.0-alpha.10"

View file

@ -47,7 +47,7 @@ The overall goal is to create an easily self-hostable, decentralized alternative
Each Lemmy server can set its own moderation policy; appointing site-wide admins, and community moderators to keep out the trolls, and foster a healthy, non-toxic environment where all can feel comfortable contributing.
*Note: The WebSocket and HTTP APIs are currently unstable*
*Note: Federation is still in active development and the WebSocket, as well as, HTTP API are currently unstable*
### Why's it called Lemmy?

View file

@ -1,23 +1,3 @@
# Lemmy v0.9.9 Release (2021-02-19)
## Changes
### Lemmy backend
- Added an federated activity query sorting order.
- Explicitly marking posts and comments as public.
- Added a `NewComment` / forum sort for posts.
- Fixed an issue with not setting correct published time for fetched posts.
- Fixed an issue with an open docker port on lemmy-ui.
- Using lemmy post link for RSS link.
- Fixed reason and display name lengths to use char counts instead.
### Lemmy-ui
- Updated translations.
- Made websocket host configurable.
- Added some accessibility features.
- Always showing password reset link.
# Lemmy v0.9.7 Release (2021-02-08)
## Changes

View file

@ -1 +1 @@
0.10.0-rc.7
0.9.7

View file

@ -64,14 +64,6 @@
- src: '../docker/iframely.config.local.js'
dest: '{{lemmy_base_dir}}/iframely.config.local.js'
mode: '0600'
vars:
lemmy_docker_image: "dessalines/lemmy:dev"
lemmy_docker_ui_image: "dessalines/lemmy-ui:{{ lookup('file', 'VERSION') }}"
lemmy_port: "8536"
lemmy_ui_port: "1235"
pictshare_port: "8537"
iframely_port: "8538"
postgres_password: "{{ lookup('password', 'passwords/{{ inventory_hostname }}/postgres chars=ascii_letters,digits') }}"
- name: add config file (only during initial setup)
template:

View file

@ -29,9 +29,8 @@
# https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist
#
# comma separated list of instances with which federation is allowed
# Only one of these blocks should be uncommented
# allowed_instances: ["instance1.tld","instance2.tld"]
# allowed_instances: ""
# comma separated list of instances which are blocked from federating
# blocked_instances: []
# blocked_instances: ""
}
}

View file

@ -1,4 +1,4 @@
version: '2.2'
version: '3.3'
services:
lemmy:
@ -38,14 +38,13 @@ services:
restart: always
pictrs:
image: asonix/pictrs:v0.2.6-r1
image: asonix/pictrs:v0.2.5-r0
user: 991:991
ports:
- "127.0.0.1:8537:8080"
volumes:
- ./volumes/pictrs:/mnt
restart: always
mem_limit: 200m
iframely:
image: dogbin/iframely:latest
@ -54,7 +53,6 @@ services:
volumes:
- ./iframely.config.local.js:/iframely/config.local.js:ro
restart: always
mem_limit: 200m
postfix:
image: mwader/postfix-relay

View file

@ -16,7 +16,7 @@
"eslint": "^7.18.0",
"eslint-plugin-jane": "^9.0.3",
"jest": "^26.6.3",
"lemmy-js-client": "0.10.0-rc.4",
"lemmy-js-client": "0.9.1-rc.1",
"node-fetch": "^2.6.1",
"prettier": "^2.1.2",
"ts-jest": "^26.4.4",

View file

@ -1,6 +1,13 @@
#!/bin/bash
set -e
export LEMMY_JWT_SECRET=changeme
export LEMMY_FEDERATION__ENABLED=true
export LEMMY_TLS_ENABLED=false
export LEMMY_SETUP__ADMIN_PASSWORD=lemmy
export LEMMY_RATE_LIMIT__POST=99999
export LEMMY_RATE_LIMIT__REGISTER=99999
export LEMMY_CAPTCHA__ENABLED=false
export LEMMY_TEST_SEND_SYNC=1
export RUST_BACKTRACE=1
@ -28,40 +35,52 @@ fi
killall lemmy_server || true
echo "$PWD"
echo "start alpha"
LEMMY_HOSTNAME=lemmy-alpha:8541 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_alpha.hjson \
LEMMY_PORT=8541 \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_alpha" \
LEMMY_HOSTNAME="lemmy-alpha:8541" \
target/lemmy_server >/tmp/lemmy_alpha.out 2>&1 &
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta,lemmy-gamma,lemmy-delta,lemmy-epsilon \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_alpha \
LEMMY_SETUP__SITE_NAME=lemmy-alpha \
target/lemmy_server >/dev/null 2>&1 &
echo "start beta"
LEMMY_HOSTNAME=lemmy-beta:8551 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_beta.hjson \
LEMMY_PORT=8551 \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_beta" \
target/lemmy_server >/tmp/lemmy_beta.out 2>&1 &
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-gamma,lemmy-delta,lemmy-epsilon \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_beta \
LEMMY_SETUP__SITE_NAME=lemmy-beta \
target/lemmy_server >/dev/null 2>&1 &
echo "start gamma"
LEMMY_HOSTNAME=lemmy-gamma:8561 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_gamma.hjson \
LEMMY_PORT=8561 \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_gamma" \
target/lemmy_server >/tmp/lemmy_gamma.out 2>&1 &
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-alpha,lemmy-beta,lemmy-delta,lemmy-epsilon \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_gamma \
LEMMY_SETUP__SITE_NAME=lemmy-gamma \
target/lemmy_server >/dev/null 2>&1 &
echo "start delta"
# An instance with only an allowlist for beta
LEMMY_HOSTNAME=lemmy-delta:8571 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_delta.hjson \
LEMMY_PORT=8571 \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_delta" \
target/lemmy_server >/tmp/lemmy_delta.out 2>&1 &
LEMMY_FEDERATION__ALLOWED_INSTANCES=lemmy-beta \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_delta \
LEMMY_SETUP__SITE_NAME=lemmy-delta \
target/lemmy_server >/dev/null 2>&1 &
echo "start epsilon"
# An instance who has a blocklist, with lemmy-alpha blocked
LEMMY_HOSTNAME=lemmy-epsilon:8581 \
LEMMY_CONFIG_LOCATION=./docker/federation/lemmy_epsilon.hjson \
LEMMY_PORT=8581 \
LEMMY_DATABASE_URL="${LEMMY_DATABASE_URL}/lemmy_epsilon" \
target/lemmy_server >/tmp/lemmy_epsilon.out 2>&1 &
LEMMY_FEDERATION__BLOCKED_INSTANCES=lemmy-alpha \
LEMMY_SETUP__ADMIN_USERNAME=lemmy_epsilon \
LEMMY_SETUP__SITE_NAME=lemmy-epsilon \
target/lemmy_server >/dev/null 2>&1 &
echo "wait for all instances to start"
while [[ "$(curl -s -o /dev/null -w '%{http_code}' 'localhost:8541/api/v2/site')" != "200" ]]; do sleep 1; done

View file

@ -4,7 +4,7 @@ set -e
export LEMMY_DATABASE_URL=postgres://lemmy:password@localhost:5432
pushd ..
cargo build
cargo +1.47.0 build
rm target/lemmy_server || true
cp target/debug/lemmy_server target/lemmy_server
./api_tests/prepare-drone-federation-test.sh

View file

@ -33,6 +33,9 @@ function assertCommunityFederation(
);
expect(communityOne.creator.actor_id).toBe(communityTwo.creator.actor_id);
expect(communityOne.community.nsfw).toBe(communityTwo.community.nsfw);
expect(communityOne.community.category_id).toBe(
communityTwo.community.category_id
);
expect(communityOne.community.removed).toBe(communityTwo.community.removed);
expect(communityOne.community.deleted).toBe(communityTwo.community.deleted);
}

View file

@ -20,9 +20,9 @@ import {
getPost,
unfollowRemotes,
searchForUser,
banPersonFromSite,
banUserFromSite,
searchPostLocal,
banPersonFromCommunity,
banUserFromCommunity,
} from './shared';
import { PostView, CommunityView } from 'lemmy-js-client';
@ -305,7 +305,7 @@ test('Enforce site ban for federated user', async () => {
expect(alphaUser).toBeDefined();
// ban alpha from beta site
let banAlpha = await banPersonFromSite(beta, alphaUser.person.id, true);
let banAlpha = await banUserFromSite(beta, alphaUser.user.id, true);
expect(banAlpha.banned).toBe(true);
// Alpha makes post on beta
@ -321,7 +321,7 @@ test('Enforce site ban for federated user', async () => {
expect(betaPost).toBeUndefined();
// Unban alpha
let unBanAlpha = await banPersonFromSite(beta, alphaUser.person.id, false);
let unBanAlpha = await banUserFromSite(beta, alphaUser.user.id, false);
expect(unBanAlpha.banned).toBe(false);
});
@ -332,8 +332,8 @@ test('Enforce community ban for federated user', async () => {
expect(alphaUser).toBeDefined();
// ban alpha from beta site
await banPersonFromCommunity(beta, alphaUser.person.id, 2, false);
let banAlpha = await banPersonFromCommunity(beta, alphaUser.person.id, 2, true);
await banUserFromCommunity(beta, alphaUser.user.id, 2, false);
let banAlpha = await banUserFromCommunity(beta, alphaUser.user.id, 2, true);
expect(banAlpha.banned).toBe(true);
// Alpha makes post on beta
@ -349,9 +349,9 @@ test('Enforce community ban for federated user', async () => {
expect(betaPost).toBeUndefined();
// Unban alpha
let unBanAlpha = await banPersonFromCommunity(
let unBanAlpha = await banUserFromCommunity(
beta,
alphaUser.person.id,
alphaUser.user.id,
2,
false
);

View file

@ -25,7 +25,7 @@ import {
CreateCommunity,
DeleteCommunity,
RemoveCommunity,
GetPersonMentions,
GetUserMentions,
CreateCommentLike,
CreatePostLike,
EditPrivateMessage,
@ -36,15 +36,15 @@ import {
GetPost,
PrivateMessageResponse,
PrivateMessagesResponse,
GetPersonMentionsResponse,
GetUserMentionsResponse,
SaveUserSettings,
SortType,
ListingType,
GetSiteResponse,
SearchType,
LemmyHttp,
BanPersonResponse,
BanPerson,
BanUserResponse,
BanUser,
BanFromCommunity,
BanFromCommunityResponse,
Post,
@ -289,32 +289,32 @@ export async function searchForUser(
return api.client.search(form);
}
export async function banPersonFromSite(
export async function banUserFromSite(
api: API,
person_id: number,
user_id: number,
ban: boolean
): Promise<BanPersonResponse> {
): Promise<BanUserResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
let form: BanPerson = {
person_id,
let form: BanUser = {
user_id,
ban,
remove_data: false,
auth: api.auth,
};
return api.client.banPerson(form);
return api.client.banUser(form);
}
export async function banPersonFromCommunity(
export async function banUserFromCommunity(
api: API,
person_id: number,
user_id: number,
community_id: number,
ban: boolean
): Promise<BanFromCommunityResponse> {
// Make sure lemmy-beta/c/main is cached on lemmy_alpha
// Use short-hand search url
let form: BanFromCommunity = {
person_id,
user_id,
community_id,
remove_data: false,
ban,
@ -413,13 +413,13 @@ export async function removeComment(
return api.client.removeComment(form);
}
export async function getMentions(api: API): Promise<GetPersonMentionsResponse> {
let form: GetPersonMentions = {
export async function getMentions(api: API): Promise<GetUserMentionsResponse> {
let form: GetUserMentions = {
sort: SortType.New,
unread_only: false,
auth: api.auth,
};
return api.client.getPersonMentions(form);
return api.client.getUserMentions(form);
}
export async function likeComment(
@ -448,6 +448,7 @@ export async function createCommunity(
description,
icon,
banner,
category_id: 1,
nsfw: false,
auth: api.auth,
};

View file

@ -8,7 +8,7 @@ import {
getSite,
} from './shared';
import {
PersonViewSafe,
UserViewSafe,
SaveUserSettings,
SortType,
ListingType,
@ -17,14 +17,14 @@ import {
let auth: string;
let apShortname: string;
function assertUserFederation(userOne: PersonViewSafe, userTwo: PersonViewSafe) {
expect(userOne.person.name).toBe(userTwo.person.name);
expect(userOne.person.preferred_username).toBe(userTwo.person.preferred_username);
expect(userOne.person.bio).toBe(userTwo.person.bio);
expect(userOne.person.actor_id).toBe(userTwo.person.actor_id);
expect(userOne.person.avatar).toBe(userTwo.person.avatar);
expect(userOne.person.banner).toBe(userTwo.person.banner);
expect(userOne.person.published).toBe(userTwo.person.published);
function assertUserFederation(userOne: UserViewSafe, userTwo: UserViewSafe) {
expect(userOne.user.name).toBe(userTwo.user.name);
expect(userOne.user.preferred_username).toBe(userTwo.user.preferred_username);
expect(userOne.user.bio).toBe(userTwo.user.bio);
expect(userOne.user.actor_id).toBe(userTwo.user.actor_id);
expect(userOne.user.avatar).toBe(userTwo.user.avatar);
expect(userOne.user.banner).toBe(userTwo.user.banner);
expect(userOne.user.published).toBe(userTwo.user.published);
}
test('Create user', async () => {
@ -34,7 +34,7 @@ test('Create user', async () => {
let site = await getSite(alpha, auth);
expect(site.my_user).toBeDefined();
apShortname = `@${site.my_user.person.name}@lemmy-alpha:8541`;
apShortname = `@${site.my_user.name}@lemmy-alpha:8541`;
});
test('Set some user settings, check that they are federated', async () => {

View file

@ -3233,10 +3233,10 @@ language-tags@^1.0.5:
dependencies:
language-subtag-registry "~0.3.2"
lemmy-js-client@0.10.0-rc.4:
version "0.10.0-rc.4"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.10.0-rc.4.tgz#ac6fe6940fc5f73260ddb166ce0ef3c0520901fc"
integrity sha512-yJPnvGaWneOOwjKEqb4qXtQk+4DbRgO+hEzSin2GgUgnxluY43gemwiCPt6EnV+j4ueKoi0+QORVg2RuRC2PaQ==
lemmy-js-client@0.9.1-rc.1:
version "0.9.1-rc.1"
resolved "https://registry.yarnpkg.com/lemmy-js-client/-/lemmy-js-client-0.9.1-rc.1.tgz#afe3cb0d4852f849dd087a4756a3771bc920a907"
integrity sha512-aVvo4IeJvIPUvypipk4GnyLB6nVQVLfB0arYrMkVV4L7zrZ/0pGtpkMDLaOAj/KpA6O0u9eLmaou5RberZQolA==
leven@^3.1.0:
version "3.1.0"

View file

@ -66,10 +66,9 @@
# https://join.lemmy.ml/docs/en/federation/administration.html#instance-allowlist-and-blocklist
#
# comma separated list of instances with which federation is allowed
# Only one of these blocks should be uncommented
# allowed_instances: ["instance1.tld","instance2.tld"]
allowed_instances: ""
# comma separated list of instances which are blocked from federating
# blocked_instances: []
blocked_instances: ""
}
captcha: {
enabled: true

View file

@ -1,12 +1,12 @@
[package]
name = "lemmy_api"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
[lib]
name = "lemmy_api"
path = "src/lib.rs"
doctest = false
[dependencies]
lemmy_apub = { path = "../apub" }
@ -16,7 +16,7 @@ lemmy_db_schema = { path = "../db_schema" }
lemmy_db_views = { path = "../db_views" }
lemmy_db_views_moderator = { path = "../db_views_moderator" }
lemmy_db_views_actor = { path = "../db_views_actor" }
lemmy_api_structs = { path = "../api_structs" }
lemmy_structs = { path = "../structs" }
lemmy_websocket = { path = "../websocket" }
diesel = "1.4.5"
bcrypt = "0.9.0"
@ -31,8 +31,9 @@ log = "0.4.14"
rand = "0.8.3"
strum = "0.20.0"
strum_macros = "0.20.1"
jsonwebtoken = "7.2.0"
lazy_static = "1.4.0"
url = { version = "2.2.1", features = ["serde"] }
url = { version = "2.2.0", features = ["serde"] }
openssl = "0.10.32"
http = "0.2.3"
http-signature-normalization-actix = { version = "0.4.1", default-features = false, features = ["sha-2"] }

View file

@ -1,17 +1,14 @@
use crate::settings::structs::Settings;
use chrono::Utc;
use jsonwebtoken::{decode, encode, DecodingKey, EncodingKey, Header, TokenData, Validation};
use lemmy_db_schema::source::user::User_;
use lemmy_utils::settings::Settings;
use serde::{Deserialize, Serialize};
type Jwt = String;
#[derive(Debug, Serialize, Deserialize)]
pub struct Claims {
/// local_user_id, standard claim by RFC 7519.
pub sub: i32,
pub id: i32,
pub iss: String,
/// Time when this token was issued as UNIX-timestamp in seconds
pub iat: i64,
}
impl Claims {
@ -22,21 +19,20 @@ impl Claims {
};
decode::<Claims>(
&jwt,
&DecodingKey::from_secret(Settings::get().jwt_secret().as_ref()),
&DecodingKey::from_secret(Settings::get().jwt_secret.as_ref()),
&v,
)
}
pub fn jwt(local_user_id: i32) -> Result<Jwt, jsonwebtoken::errors::Error> {
pub fn jwt(user: User_, hostname: String) -> Result<Jwt, jsonwebtoken::errors::Error> {
let my_claims = Claims {
sub: local_user_id,
iss: Settings::get().hostname(),
iat: Utc::now().timestamp(),
id: user.id,
iss: hostname,
};
encode(
&Header::default(),
&my_claims,
&EncodingKey::from_secret(Settings::get().jwt_secret().as_ref()),
&EncodingKey::from_secret(Settings::get().jwt_secret.as_ref()),
)
}
}

View file

@ -2,14 +2,13 @@ use crate::{
check_community_ban,
check_downvotes_enabled,
collect_moderated_communities,
get_local_user_view_from_jwt,
get_local_user_view_from_jwt_opt,
get_post,
get_user_from_jwt,
get_user_from_jwt_opt,
is_mod_or_admin,
Perform,
};
use actix_web::web::Data;
use lemmy_api_structs::{blocking, comment::*, send_local_notifs};
use lemmy_apub::{generate_apub_endpoint, ApubLikeableType, ApubObjectType, EndpointType};
use lemmy_db_queries::{
source::comment::Comment_,
@ -20,18 +19,15 @@ use lemmy_db_queries::{
Saveable,
SortType,
};
use lemmy_db_schema::{
source::{comment::*, comment_report::*, moderator::*},
LocalUserId,
};
use lemmy_db_schema::source::{comment::*, comment_report::*, moderator::*};
use lemmy_db_views::{
comment_report_view::{CommentReportQueryBuilder, CommentReportView},
comment_view::{CommentQueryBuilder, CommentView},
local_user_view::LocalUserView,
};
use lemmy_structs::{blocking, comment::*, send_local_notifs};
use lemmy_utils::{
utils::{remove_slurs, scrape_text_for_mentions},
ApiError,
APIError,
ConnectionId,
LemmyError,
};
@ -52,7 +48,7 @@ impl Perform for CreateComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &CreateComment = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let content_slurs_removed = remove_slurs(&data.content.to_owned());
@ -60,11 +56,11 @@ impl Perform for CreateComment {
let post_id = data.post_id;
let post = get_post(post_id, context.pool()).await?;
check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
check_community_ban(user.id, post.community_id, context.pool()).await?;
// Check if post is locked, no new comments
if post.locked {
return Err(ApiError::err("locked").into());
return Err(APIError::err("locked").into());
}
// If there's a parent_id, check to make sure that comment is in that post
@ -73,10 +69,10 @@ impl Perform for CreateComment {
let parent =
match blocking(context.pool(), move |conn| Comment::read(&conn, parent_id)).await? {
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
};
if parent.post_id != post_id {
return Err(ApiError::err("couldnt_create_comment").into());
return Err(APIError::err("couldnt_create_comment").into());
}
}
@ -84,7 +80,7 @@ impl Perform for CreateComment {
content: content_slurs_removed,
parent_id: data.parent_id.to_owned(),
post_id: data.post_id,
creator_id: local_user_view.person.id,
creator_id: user.id,
removed: None,
deleted: None,
read: None,
@ -102,7 +98,7 @@ impl Perform for CreateComment {
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
};
// Necessary to update the ap_id
@ -116,12 +112,10 @@ impl Perform for CreateComment {
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_create_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_create_comment").into()),
};
updated_comment
.send_create(&local_user_view.person, context)
.await?;
updated_comment.send_create(&user, context).await?;
// Scan the comment for user mentions, add those rows
let post_id = post.id;
@ -129,7 +123,7 @@ impl Perform for CreateComment {
let recipient_ids = send_local_notifs(
mentions,
updated_comment.clone(),
local_user_view.person.clone(),
&user,
post,
context.pool(),
true,
@ -140,35 +134,33 @@ impl Perform for CreateComment {
let like_form = CommentLikeForm {
comment_id: inserted_comment.id,
post_id,
person_id: local_user_view.person.id,
user_id: user.id,
score: 1,
};
let like = move |conn: &'_ _| CommentLike::like(&conn, &like_form);
if blocking(context.pool(), like).await?.is_err() {
return Err(ApiError::err("couldnt_like_comment").into());
return Err(APIError::err("couldnt_like_comment").into());
}
updated_comment
.send_like(&local_user_view.person, context)
.await?;
updated_comment.send_like(&user, context).await?;
let person_id = local_user_view.person.id;
let user_id = user.id;
let mut comment_view = blocking(context.pool(), move |conn| {
CommentView::read(&conn, inserted_comment.id, Some(person_id))
CommentView::read(&conn, inserted_comment.id, Some(user_id))
})
.await??;
// If its a comment to yourself, mark it as read
let comment_id = comment_view.comment.id;
if local_user_view.person.id == comment_view.get_recipient_id() {
if user.id == comment_view.get_recipient_id() {
match blocking(context.pool(), move |conn| {
Comment::update_read(conn, comment_id, true)
})
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
};
comment_view.comment.read = true;
}
@ -201,7 +193,7 @@ impl Perform for EditComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &EditComment = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {
@ -209,16 +201,11 @@ impl Perform for EditComment {
})
.await??;
check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_comment.community.id, context.pool()).await?;
// Verify that only the creator can edit
if local_user_view.person.id != orig_comment.creator.id {
return Err(ApiError::err("no_comment_edit_allowed").into());
if user.id != orig_comment.creator.id {
return Err(APIError::err("no_comment_edit_allowed").into());
}
// Do the update
@ -230,13 +217,11 @@ impl Perform for EditComment {
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
};
// Send the apub update
updated_comment
.send_update(&local_user_view.person, context)
.await?;
updated_comment.send_update(&user, context).await?;
// Do the mentions / recipients
let updated_comment_content = updated_comment.content.to_owned();
@ -244,7 +229,7 @@ impl Perform for EditComment {
let recipient_ids = send_local_notifs(
mentions,
updated_comment,
local_user_view.person.clone(),
&user,
orig_comment.post,
context.pool(),
false,
@ -252,9 +237,9 @@ impl Perform for EditComment {
.await?;
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(person_id))
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
@ -284,7 +269,7 @@ impl Perform for DeleteComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &DeleteComment = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {
@ -292,16 +277,11 @@ impl Perform for DeleteComment {
})
.await??;
check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_comment.community.id, context.pool()).await?;
// Verify that only the creator can delete
if local_user_view.person.id != orig_comment.creator.id {
return Err(ApiError::err("no_comment_edit_allowed").into());
if user.id != orig_comment.creator.id {
return Err(APIError::err("no_comment_edit_allowed").into());
}
// Do the delete
@ -312,25 +292,21 @@ impl Perform for DeleteComment {
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
};
// Send the apub message
if deleted {
updated_comment
.send_delete(&local_user_view.person, context)
.await?;
updated_comment.send_delete(&user, context).await?;
} else {
updated_comment
.send_undo_delete(&local_user_view.person, context)
.await?;
updated_comment.send_undo_delete(&user, context).await?;
}
// Refetch it
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(person_id))
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
@ -340,7 +316,7 @@ impl Perform for DeleteComment {
let recipient_ids = send_local_notifs(
mentions,
updated_comment,
local_user_view.person.clone(),
&user,
comment_view_2.post,
context.pool(),
false,
@ -373,7 +349,7 @@ impl Perform for RemoveComment {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &RemoveComment = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {
@ -381,20 +357,10 @@ impl Perform for RemoveComment {
})
.await??;
check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_comment.community.id, context.pool()).await?;
// Verify that only a mod or admin can remove
is_mod_or_admin(
context.pool(),
local_user_view.person.id,
orig_comment.community.id,
)
.await?;
is_mod_or_admin(context.pool(), user.id, orig_comment.community.id).await?;
// Do the remove
let removed = data.removed;
@ -404,12 +370,12 @@ impl Perform for RemoveComment {
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
};
// Mod tables
let form = ModRemoveCommentForm {
mod_person_id: local_user_view.person.id,
mod_user_id: user.id,
comment_id: data.comment_id,
removed: Some(removed),
reason: data.reason.to_owned(),
@ -421,20 +387,16 @@ impl Perform for RemoveComment {
// Send the apub message
if removed {
updated_comment
.send_remove(&local_user_view.person, context)
.await?;
updated_comment.send_remove(&user, context).await?;
} else {
updated_comment
.send_undo_remove(&local_user_view.person, context)
.await?;
updated_comment.send_undo_remove(&user, context).await?;
}
// Refetch it
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(person_id))
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
@ -445,7 +407,7 @@ impl Perform for RemoveComment {
let recipient_ids = send_local_notifs(
mentions,
updated_comment,
local_user_view.person.clone(),
&user,
comment_view_2.post,
context.pool(),
false,
@ -478,7 +440,7 @@ impl Perform for MarkCommentAsRead {
_websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &MarkCommentAsRead = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let comment_id = data.comment_id;
let orig_comment = blocking(context.pool(), move |conn| {
@ -486,16 +448,11 @@ impl Perform for MarkCommentAsRead {
})
.await??;
check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_comment.community.id, context.pool()).await?;
// Verify that only the recipient can mark as read
if local_user_view.person.id != orig_comment.get_recipient_id() {
return Err(ApiError::err("no_comment_edit_allowed").into());
if user.id != orig_comment.get_recipient_id() {
return Err(APIError::err("no_comment_edit_allowed").into());
}
// Do the mark as read
@ -506,14 +463,14 @@ impl Perform for MarkCommentAsRead {
.await?
{
Ok(comment) => comment,
Err(_e) => return Err(ApiError::err("couldnt_update_comment").into()),
Err(_e) => return Err(APIError::err("couldnt_update_comment").into()),
};
// Refetch it
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(person_id))
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
@ -537,29 +494,29 @@ impl Perform for SaveComment {
_websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &SaveComment = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let comment_saved_form = CommentSavedForm {
comment_id: data.comment_id,
person_id: local_user_view.person.id,
user_id: user.id,
};
if data.save {
let save_comment = move |conn: &'_ _| CommentSaved::save(conn, &comment_saved_form);
if blocking(context.pool(), save_comment).await?.is_err() {
return Err(ApiError::err("couldnt_save_comment").into());
return Err(APIError::err("couldnt_save_comment").into());
}
} else {
let unsave_comment = move |conn: &'_ _| CommentSaved::unsave(conn, &comment_saved_form);
if blocking(context.pool(), unsave_comment).await?.is_err() {
return Err(ApiError::err("couldnt_save_comment").into());
return Err(APIError::err("couldnt_save_comment").into());
}
}
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(person_id))
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
@ -581,9 +538,9 @@ impl Perform for CreateCommentLike {
websocket_id: Option<ConnectionId>,
) -> Result<CommentResponse, LemmyError> {
let data: &CreateCommentLike = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let mut recipient_ids = Vec::<LocalUserId>::new();
let mut recipient_ids = Vec::new();
// Don't do a downvote if site has downvotes disabled
check_downvotes_enabled(data.score, context.pool()).await?;
@ -594,34 +551,22 @@ impl Perform for CreateCommentLike {
})
.await??;
check_community_ban(
local_user_view.person.id,
orig_comment.community.id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_comment.community.id, context.pool()).await?;
// Add parent user to recipients
let recipient_id = orig_comment.get_recipient_id();
if let Ok(local_recipient) = blocking(context.pool(), move |conn| {
LocalUserView::read_person(conn, recipient_id)
})
.await?
{
recipient_ids.push(local_recipient.local_user.id);
}
recipient_ids.push(orig_comment.get_recipient_id());
let like_form = CommentLikeForm {
comment_id: data.comment_id,
post_id: orig_comment.post.id,
person_id: local_user_view.person.id,
user_id: user.id,
score: data.score,
};
// Remove any likes first
let person_id = local_user_view.person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
CommentLike::remove(conn, person_id, comment_id)
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
@ -632,27 +577,23 @@ impl Perform for CreateCommentLike {
let like_form2 = like_form.clone();
let like = move |conn: &'_ _| CommentLike::like(conn, &like_form2);
if blocking(context.pool(), like).await?.is_err() {
return Err(ApiError::err("couldnt_like_comment").into());
return Err(APIError::err("couldnt_like_comment").into());
}
if like_form.score == 1 {
comment.send_like(&local_user_view.person, context).await?;
comment.send_like(&user, context).await?;
} else if like_form.score == -1 {
comment
.send_dislike(&local_user_view.person, context)
.await?;
comment.send_dislike(&user, context).await?;
}
} else {
comment
.send_undo_like(&local_user_view.person, context)
.await?;
comment.send_undo_like(&user, context).await?;
}
// Have to refetch the comment to get the current state
let comment_id = data.comment_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let liked_comment = blocking(context.pool(), move |conn| {
CommentView::read(conn, comment_id, Some(person_id))
CommentView::read(conn, comment_id, Some(user_id))
})
.await??;
@ -682,8 +623,8 @@ impl Perform for GetComments {
_websocket_id: Option<ConnectionId>,
) -> Result<GetCommentsResponse, LemmyError> {
let data: &GetComments = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = local_user_view.map(|u| u.person.id);
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let type_ = ListingType::from_str(&data.type_)?;
let sort = SortType::from_str(&data.sort)?;
@ -698,7 +639,7 @@ impl Perform for GetComments {
.sort(&sort)
.community_id(community_id)
.community_name(community_name)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -706,7 +647,7 @@ impl Perform for GetComments {
.await?;
let comments = match comments {
Ok(comments) => comments,
Err(_) => return Err(ApiError::err("couldnt_get_comments").into()),
Err(_) => return Err(APIError::err("couldnt_get_comments").into()),
};
Ok(GetCommentsResponse { comments })
@ -724,28 +665,28 @@ impl Perform for CreateCommentReport {
websocket_id: Option<ConnectionId>,
) -> Result<CreateCommentReportResponse, LemmyError> {
let data: &CreateCommentReport = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// check size of report and check for whitespace
let reason = data.reason.trim();
if reason.is_empty() {
return Err(ApiError::err("report_reason_required").into());
return Err(APIError::err("report_reason_required").into());
}
if reason.chars().count() > 1000 {
return Err(ApiError::err("report_too_long").into());
if reason.len() > 1000 {
return Err(APIError::err("report_too_long").into());
}
let person_id = local_user_view.person.id;
let user_id = user.id;
let comment_id = data.comment_id;
let comment_view = blocking(context.pool(), move |conn| {
CommentView::read(&conn, comment_id, None)
})
.await??;
check_community_ban(person_id, comment_view.community.id, context.pool()).await?;
check_community_ban(user_id, comment_view.community.id, context.pool()).await?;
let report_form = CommentReportForm {
creator_id: person_id,
creator_id: user_id,
comment_id,
original_comment_text: comment_view.comment.content,
reason: data.reason.to_owned(),
@ -757,7 +698,7 @@ impl Perform for CreateCommentReport {
.await?
{
Ok(report) => report,
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
Err(_e) => return Err(APIError::err("couldnt_create_report").into()),
};
let res = CreateCommentReportResponse { success: true };
@ -765,7 +706,7 @@ impl Perform for CreateCommentReport {
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreateCommentReport,
response: res.clone(),
local_recipient_id: local_user_view.local_user.id,
recipient_id: user.id,
websocket_id,
});
@ -791,7 +732,7 @@ impl Perform for ResolveCommentReport {
websocket_id: Option<ConnectionId>,
) -> Result<ResolveCommentReportResponse, LemmyError> {
let data: &ResolveCommentReport = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let report_id = data.report_id;
let report = blocking(context.pool(), move |conn| {
@ -799,20 +740,20 @@ impl Perform for ResolveCommentReport {
})
.await??;
let person_id = local_user_view.person.id;
is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
let user_id = user.id;
is_mod_or_admin(context.pool(), user_id, report.community.id).await?;
let resolved = data.resolved;
let resolve_fun = move |conn: &'_ _| {
if resolved {
CommentReport::resolve(conn, report_id, person_id)
CommentReport::resolve(conn, report_id, user_id)
} else {
CommentReport::unresolve(conn, report_id, person_id)
CommentReport::unresolve(conn, report_id, user_id)
}
};
if blocking(context.pool(), resolve_fun).await?.is_err() {
return Err(ApiError::err("couldnt_resolve_report").into());
return Err(APIError::err("couldnt_resolve_report").into());
};
let report_id = data.report_id;
@ -844,12 +785,12 @@ impl Perform for ListCommentReports {
websocket_id: Option<ConnectionId>,
) -> Result<ListCommentReportsResponse, LemmyError> {
let data: &ListCommentReports = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_id = data.community;
let community_ids =
collect_moderated_communities(person_id, community_id, context.pool()).await?;
collect_moderated_communities(user_id, community_id, context.pool()).await?;
let page = data.page;
let limit = data.limit;
@ -867,7 +808,7 @@ impl Perform for ListCommentReports {
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::ListCommentReports,
response: res.clone(),
local_recipient_id: local_user_view.local_user.id,
recipient_id: user.id,
websocket_id,
});

View file

@ -1,14 +1,14 @@
use crate::{
check_community_ban,
get_local_user_view_from_jwt,
get_local_user_view_from_jwt_opt,
check_optional_url,
get_user_from_jwt,
get_user_from_jwt_opt,
is_admin,
is_mod_or_admin,
Perform,
};
use actix_web::web::Data;
use anyhow::Context;
use lemmy_api_structs::{blocking, community::*};
use lemmy_apub::{
generate_apub_endpoint,
generate_followers_url,
@ -18,7 +18,7 @@ use lemmy_apub::{
EndpointType,
};
use lemmy_db_queries::{
diesel_option_overwrite_to_url,
diesel_option_overwrite,
source::{
comment::Comment_,
community::{CommunityModerator_, Community_},
@ -35,20 +35,20 @@ use lemmy_db_queries::{
use lemmy_db_schema::{
naive_now,
source::{comment::Comment, community::*, moderator::*, post::Post, site::*},
PersonId,
};
use lemmy_db_views::comment_view::CommentQueryBuilder;
use lemmy_db_views_actor::{
community_follower_view::CommunityFollowerView,
community_moderator_view::CommunityModeratorView,
community_view::{CommunityQueryBuilder, CommunityView},
person_view::PersonViewSafe,
user_view::UserViewSafe,
};
use lemmy_structs::{blocking, community::*};
use lemmy_utils::{
apub::generate_actor_keypair,
location_info,
utils::{check_slurs, check_slurs_opt, is_valid_community_name, naive_from_unix},
ApiError,
APIError,
ConnectionId,
LemmyError,
};
@ -69,8 +69,8 @@ impl Perform for GetCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &GetCommunity = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = local_user_view.map(|u| u.person.id);
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let community_id = match data.id {
Some(id) => id,
@ -82,19 +82,19 @@ impl Perform for GetCommunity {
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
}
.id
}
};
let community_view = match blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, person_id)
CommunityView::read(conn, community_id, user_id)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
let moderators: Vec<CommunityModeratorView> = match blocking(context.pool(), move |conn| {
@ -103,7 +103,7 @@ impl Perform for GetCommunity {
.await?
{
Ok(moderators) => moderators,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
let online = context
@ -133,14 +133,14 @@ impl Perform for CreateCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &CreateCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs(&data.title)?;
check_slurs_opt(&data.description)?;
if !is_valid_community_name(&data.name) {
return Err(ApiError::err("invalid_community_name").into());
return Err(APIError::err("invalid_community_name").into());
}
// Double check for duplicate community actor_ids
@ -151,12 +151,15 @@ impl Perform for CreateCommunity {
})
.await?;
if community_dupe.is_ok() {
return Err(ApiError::err("community_already_exists").into());
return Err(APIError::err("community_already_exists").into());
}
// Check to make sure the icon and banners are urls
let icon = diesel_option_overwrite_to_url(&data.icon)?;
let banner = diesel_option_overwrite_to_url(&data.banner)?;
let icon = diesel_option_overwrite(&data.icon);
let banner = diesel_option_overwrite(&data.banner);
check_optional_url(&icon)?;
check_optional_url(&banner)?;
// When you create a community, make sure the user becomes a moderator and a follower
let keypair = generate_actor_keypair()?;
@ -167,7 +170,8 @@ impl Perform for CreateCommunity {
description: data.description.to_owned(),
icon,
banner,
creator_id: local_user_view.person.id,
category_id: data.category_id,
creator_id: user.id,
removed: None,
deleted: None,
nsfw: data.nsfw,
@ -189,35 +193,35 @@ impl Perform for CreateCommunity {
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("community_already_exists").into()),
Err(_e) => return Err(APIError::err("community_already_exists").into()),
};
// The community creator becomes a moderator
let community_moderator_form = CommunityModeratorForm {
community_id: inserted_community.id,
person_id: local_user_view.person.id,
user_id: user.id,
};
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
return Err(APIError::err("community_moderator_already_exists").into());
}
// Follow your own community
let community_follower_form = CommunityFollowerForm {
community_id: inserted_community.id,
person_id: local_user_view.person.id,
user_id: user.id,
pending: false,
};
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
return Err(APIError::err("community_follower_already_exists").into());
}
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, inserted_community.id, Some(person_id))
CommunityView::read(conn, inserted_community.id, Some(user_id))
})
.await??;
@ -235,20 +239,20 @@ impl Perform for EditCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &EditCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.title)?;
check_slurs_opt(&data.description)?;
// Verify its a mod (only mods can edit it)
let community_id = data.community_id;
let mods: Vec<PersonId> = blocking(context.pool(), move |conn| {
let mods: Vec<i32> = blocking(context.pool(), move |conn| {
CommunityModeratorView::for_community(conn, community_id)
.map(|v| v.into_iter().map(|m| m.moderator.id).collect())
})
.await??;
if !mods.contains(&local_user_view.person.id) {
return Err(ApiError::err("not_a_moderator").into());
if !mods.contains(&user.id) {
return Err(APIError::err("not_a_moderator").into());
}
let community_id = data.community_id;
@ -257,8 +261,11 @@ impl Perform for EditCommunity {
})
.await??;
let icon = diesel_option_overwrite_to_url(&data.icon)?;
let banner = diesel_option_overwrite_to_url(&data.banner)?;
let icon = diesel_option_overwrite(&data.icon);
let banner = diesel_option_overwrite(&data.banner);
check_optional_url(&icon)?;
check_optional_url(&banner)?;
let community_form = CommunityForm {
name: read_community.name,
@ -266,6 +273,7 @@ impl Perform for EditCommunity {
description: data.description.to_owned(),
icon,
banner,
category_id: data.category_id.to_owned(),
creator_id: read_community.creator_id,
removed: Some(read_community.removed),
deleted: Some(read_community.deleted),
@ -289,16 +297,16 @@ impl Perform for EditCommunity {
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
};
// TODO there needs to be some kind of an apub update
// process for communities and users
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id))
CommunityView::read(conn, community_id, Some(user_id))
})
.await??;
@ -320,7 +328,7 @@ impl Perform for DeleteCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &DeleteCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Verify its the creator (only a creator can delete the community)
let community_id = data.community_id;
@ -328,8 +336,8 @@ impl Perform for DeleteCommunity {
Community::read(conn, community_id)
})
.await??;
if read_community.creator_id != local_user_view.person.id {
return Err(ApiError::err("no_community_edit_allowed").into());
if read_community.creator_id != user.id {
return Err(APIError::err("no_community_edit_allowed").into());
}
// Do the delete
@ -341,7 +349,7 @@ impl Perform for DeleteCommunity {
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
};
// Send apub messages
@ -352,9 +360,9 @@ impl Perform for DeleteCommunity {
}
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id))
CommunityView::read(conn, community_id, Some(user_id))
})
.await??;
@ -376,10 +384,10 @@ impl Perform for RemoveCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &RemoveCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Verify its an admin (only an admin can remove a community)
is_admin(&local_user_view)?;
is_admin(context.pool(), user.id).await?;
// Do the remove
let community_id = data.community_id;
@ -390,7 +398,7 @@ impl Perform for RemoveCommunity {
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_update_community").into()),
Err(_e) => return Err(APIError::err("couldnt_update_community").into()),
};
// Mod tables
@ -399,7 +407,7 @@ impl Perform for RemoveCommunity {
None => None,
};
let form = ModRemoveCommunityForm {
mod_person_id: local_user_view.person.id,
mod_user_id: user.id,
community_id: data.community_id,
removed: Some(removed),
reason: data.reason.to_owned(),
@ -418,9 +426,9 @@ impl Perform for RemoveCommunity {
}
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id))
CommunityView::read(conn, community_id, Some(user_id))
})
.await??;
@ -442,16 +450,15 @@ impl Perform for ListCommunities {
_websocket_id: Option<ConnectionId>,
) -> Result<ListCommunitiesResponse, LemmyError> {
let data: &ListCommunities = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = match &local_user_view {
Some(uv) => Some(uv.person.id),
let user_id = match &user {
Some(user) => Some(user.id),
None => None,
};
// Don't show NSFW by default
let show_nsfw = match &local_user_view {
Some(uv) => uv.local_user.show_nsfw,
let show_nsfw = match &user {
Some(user) => user.show_nsfw,
None => false,
};
@ -465,7 +472,7 @@ impl Perform for ListCommunities {
.listing_type(&type_)
.sort(&sort)
.show_nsfw(show_nsfw)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -487,7 +494,7 @@ impl Perform for FollowCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<CommunityResponse, LemmyError> {
let data: &FollowCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_id = data.community_id;
let community = blocking(context.pool(), move |conn| {
@ -496,47 +503,41 @@ impl Perform for FollowCommunity {
.await??;
let community_follower_form = CommunityFollowerForm {
community_id: data.community_id,
person_id: local_user_view.person.id,
user_id: user.id,
pending: false,
};
if community.local {
if data.follow {
check_community_ban(local_user_view.person.id, community_id, context.pool()).await?;
check_community_ban(user.id, community_id, context.pool()).await?;
let follow = move |conn: &'_ _| CommunityFollower::follow(conn, &community_follower_form);
if blocking(context.pool(), follow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
return Err(APIError::err("community_follower_already_exists").into());
}
} else {
let unfollow =
move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
return Err(APIError::err("community_follower_already_exists").into());
}
}
} else if data.follow {
// Dont actually add to the community followers here, because you need
// to wait for the accept
local_user_view
.person
.send_follow(&community.actor_id(), context)
.await?;
user.send_follow(&community.actor_id(), context).await?;
} else {
local_user_view
.person
.send_unfollow(&community.actor_id(), context)
.await?;
user.send_unfollow(&community.actor_id(), context).await?;
let unfollow = move |conn: &'_ _| CommunityFollower::unfollow(conn, &community_follower_form);
if blocking(context.pool(), unfollow).await?.is_err() {
return Err(ApiError::err("community_follower_already_exists").into());
return Err(APIError::err("community_follower_already_exists").into());
}
}
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let mut community_view = blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id))
CommunityView::read(conn, community_id, Some(user_id))
})
.await??;
@ -561,16 +562,16 @@ impl Perform for GetFollowedCommunities {
_websocket_id: Option<ConnectionId>,
) -> Result<GetFollowedCommunitiesResponse, LemmyError> {
let data: &GetFollowedCommunities = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let person_id = local_user_view.person.id;
let user_id = user.id;
let communities = match blocking(context.pool(), move |conn| {
CommunityFollowerView::for_person(conn, person_id)
CommunityFollowerView::for_user(conn, user_id)
})
.await?
{
Ok(communities) => communities,
_ => return Err(ApiError::err("system_err_login").into()),
_ => return Err(APIError::err("system_err_login").into()),
};
// Return the jwt
@ -588,29 +589,29 @@ impl Perform for BanFromCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<BanFromCommunityResponse, LemmyError> {
let data: &BanFromCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_id = data.community_id;
let banned_person_id = data.person_id;
let banned_user_id = data.user_id;
// Verify that only mods or admins can ban
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
is_mod_or_admin(context.pool(), user.id, community_id).await?;
let community_user_ban_form = CommunityPersonBanForm {
let community_user_ban_form = CommunityUserBanForm {
community_id: data.community_id,
person_id: data.person_id,
user_id: data.user_id,
};
if data.ban {
let ban = move |conn: &'_ _| CommunityPersonBan::ban(conn, &community_user_ban_form);
let ban = move |conn: &'_ _| CommunityUserBan::ban(conn, &community_user_ban_form);
if blocking(context.pool(), ban).await?.is_err() {
return Err(ApiError::err("community_user_already_banned").into());
return Err(APIError::err("community_user_already_banned").into());
}
// Also unsubscribe them from the community, if they are subscribed
let community_follower_form = CommunityFollowerForm {
community_id: data.community_id,
person_id: banned_person_id,
user_id: banned_user_id,
pending: false,
};
blocking(context.pool(), move |conn: &'_ _| {
@ -619,9 +620,9 @@ impl Perform for BanFromCommunity {
.await?
.ok();
} else {
let unban = move |conn: &'_ _| CommunityPersonBan::unban(conn, &community_user_ban_form);
let unban = move |conn: &'_ _| CommunityUserBan::unban(conn, &community_user_ban_form);
if blocking(context.pool(), unban).await?.is_err() {
return Err(ApiError::err("community_user_already_banned").into());
return Err(APIError::err("community_user_already_banned").into());
}
}
@ -629,7 +630,7 @@ impl Perform for BanFromCommunity {
if data.remove_data {
// Posts
blocking(context.pool(), move |conn: &'_ _| {
Post::update_removed_for_creator(conn, banned_person_id, Some(community_id), true)
Post::update_removed_for_creator(conn, banned_user_id, Some(community_id), true)
})
.await??;
@ -637,7 +638,7 @@ impl Perform for BanFromCommunity {
// TODO Diesel doesn't allow updates with joins, so this has to be a loop
let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.creator_id(banned_person_id)
.creator_id(banned_user_id)
.community_id(community_id)
.limit(std::i64::MAX)
.list()
@ -661,8 +662,8 @@ impl Perform for BanFromCommunity {
};
let form = ModBanFromCommunityForm {
mod_person_id: local_user_view.person.id,
other_person_id: data.person_id,
mod_user_id: user.id,
other_user_id: data.user_id,
community_id: data.community_id,
reason: data.reason.to_owned(),
banned: Some(data.ban),
@ -673,14 +674,14 @@ impl Perform for BanFromCommunity {
})
.await??;
let person_id = data.person_id;
let person_view = blocking(context.pool(), move |conn| {
PersonViewSafe::read(conn, person_id)
let user_id = data.user_id;
let user_view = blocking(context.pool(), move |conn| {
UserViewSafe::read(conn, user_id)
})
.await??;
let res = BanFromCommunityResponse {
person_view,
user_view,
banned: data.ban,
};
@ -705,34 +706,34 @@ impl Perform for AddModToCommunity {
websocket_id: Option<ConnectionId>,
) -> Result<AddModToCommunityResponse, LemmyError> {
let data: &AddModToCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_moderator_form = CommunityModeratorForm {
community_id: data.community_id,
person_id: data.person_id,
user_id: data.user_id,
};
let community_id = data.community_id;
// Verify that only mods or admins can add mod
is_mod_or_admin(context.pool(), local_user_view.person.id, community_id).await?;
is_mod_or_admin(context.pool(), user.id, community_id).await?;
if data.added {
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
return Err(APIError::err("community_moderator_already_exists").into());
}
} else {
let leave = move |conn: &'_ _| CommunityModerator::leave(conn, &community_moderator_form);
if blocking(context.pool(), leave).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
return Err(APIError::err("community_moderator_already_exists").into());
}
}
// Mod tables
let form = ModAddCommunityForm {
mod_person_id: local_user_view.person.id,
other_person_id: data.person_id,
mod_user_id: user.id,
other_user_id: data.user_id,
community_id: data.community_id,
removed: Some(!data.added),
};
@ -770,7 +771,7 @@ impl Perform for TransferCommunity {
_websocket_id: Option<ConnectionId>,
) -> Result<GetCommunityResponse, LemmyError> {
let data: &TransferCommunity = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let community_id = data.community_id;
let read_community = blocking(context.pool(), move |conn| {
@ -783,31 +784,28 @@ impl Perform for TransferCommunity {
})
.await??;
let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
// Making sure the creator, if an admin, is at the top
let creator_index = admins
.iter()
.position(|r| r.person.id == site_creator_id)
.position(|r| r.user.id == site_creator_id)
.context(location_info!())?;
let creator_person = admins.remove(creator_index);
admins.insert(0, creator_person);
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
// Make sure user is the creator, or an admin
if local_user_view.person.id != read_community.creator_id
&& !admins
.iter()
.map(|a| a.person.id)
.any(|x| x == local_user_view.person.id)
if user.id != read_community.creator_id
&& !admins.iter().map(|a| a.user.id).any(|x| x == user.id)
{
return Err(ApiError::err("not_an_admin").into());
return Err(APIError::err("not_an_admin").into());
}
let community_id = data.community_id;
let new_creator = data.person_id;
let new_creator = data.user_id;
let update = move |conn: &'_ _| Community::update_creator(conn, community_id, new_creator);
if blocking(context.pool(), update).await?.is_err() {
return Err(ApiError::err("couldnt_update_community").into());
return Err(APIError::err("couldnt_update_community").into());
};
// You also have to re-do the community_moderator table, reordering it.
@ -818,10 +816,10 @@ impl Perform for TransferCommunity {
.await??;
let creator_index = community_mods
.iter()
.position(|r| r.moderator.id == data.person_id)
.position(|r| r.moderator.id == data.user_id)
.context(location_info!())?;
let creator_person = community_mods.remove(creator_index);
community_mods.insert(0, creator_person);
let creator_user = community_mods.remove(creator_index);
community_mods.insert(0, creator_user);
let community_id = data.community_id;
blocking(context.pool(), move |conn| {
@ -833,19 +831,19 @@ impl Perform for TransferCommunity {
for cmod in &community_mods {
let community_moderator_form = CommunityModeratorForm {
community_id: cmod.community.id,
person_id: cmod.moderator.id,
user_id: cmod.moderator.id,
};
let join = move |conn: &'_ _| CommunityModerator::join(conn, &community_moderator_form);
if blocking(context.pool(), join).await?.is_err() {
return Err(ApiError::err("community_moderator_already_exists").into());
return Err(APIError::err("community_moderator_already_exists").into());
}
}
// Mod tables
let form = ModAddCommunityForm {
mod_person_id: local_user_view.person.id,
other_person_id: data.person_id,
mod_user_id: user.id,
other_user_id: data.user_id,
community_id: data.community_id,
removed: Some(false),
};
@ -855,14 +853,14 @@ impl Perform for TransferCommunity {
.await??;
let community_id = data.community_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_view = match blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, Some(person_id))
CommunityView::read(conn, community_id, Some(user_id))
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
let community_id = data.community_id;
@ -872,7 +870,7 @@ impl Perform for TransferCommunity {
.await?
{
Ok(moderators) => moderators,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
// Return the jwt
@ -890,7 +888,7 @@ fn send_community_websocket(
websocket_id: Option<ConnectionId>,
op: UserOperation,
) {
// Strip out the person id and subscribed when sending to others
// Strip out the user id and subscribed when sending to others
let mut res_sent = res.clone();
res_sent.community_view.subscribed = false;

View file

@ -1,55 +1,38 @@
use crate::claims::Claims;
use actix_web::{web, web::Data};
use lemmy_api_structs::{
blocking,
comment::*,
community::*,
person::*,
post::*,
site::*,
websocket::*,
};
use lemmy_db_queries::{
source::{
community::{CommunityModerator_, Community_},
site::Site_,
user::UserSafeSettings_,
},
Crud,
DbPool,
};
use lemmy_db_schema::{
source::{
community::{Community, CommunityModerator},
post::Post,
site::Site,
},
CommunityId,
LocalUserId,
PersonId,
PostId,
use lemmy_db_schema::source::{
community::{Community, CommunityModerator},
post::Post,
site::Site,
user::{UserSafeSettings, User_},
};
use lemmy_db_views::local_user_view::{LocalUserSettingsView, LocalUserView};
use lemmy_db_views_actor::{
community_person_ban_view::CommunityPersonBanView,
community_user_ban_view::CommunityUserBanView,
community_view::CommunityView,
};
use lemmy_utils::{
claims::Claims,
settings::structs::Settings,
ApiError,
ConnectionId,
LemmyError,
};
use lemmy_structs::{blocking, comment::*, community::*, post::*, site::*, user::*, websocket::*};
use lemmy_utils::{settings::Settings, APIError, ConnectionId, LemmyError};
use lemmy_websocket::{serialize_websocket_message, LemmyContext, UserOperation};
use serde::Deserialize;
use std::{env, process::Command};
use std::process::Command;
use url::Url;
pub mod claims;
pub mod comment;
pub mod community;
pub mod local_user;
pub mod post;
pub mod routes;
pub mod site;
pub mod user;
pub mod version;
pub mod websocket;
#[async_trait::async_trait(?Send)]
@ -65,121 +48,92 @@ pub trait Perform {
pub(crate) async fn is_mod_or_admin(
pool: &DbPool,
person_id: PersonId,
community_id: CommunityId,
user_id: i32,
community_id: i32,
) -> Result<(), LemmyError> {
let is_mod_or_admin = blocking(pool, move |conn| {
CommunityView::is_mod_or_admin(conn, person_id, community_id)
CommunityView::is_mod_or_admin(conn, user_id, community_id)
})
.await?;
if !is_mod_or_admin {
return Err(ApiError::err("not_a_mod_or_admin").into());
return Err(APIError::err("not_a_mod_or_admin").into());
}
Ok(())
}
pub async fn is_admin(pool: &DbPool, user_id: i32) -> Result<(), LemmyError> {
let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
if !user.admin {
return Err(APIError::err("not_an_admin").into());
}
Ok(())
}
pub fn is_admin(local_user_view: &LocalUserView) -> Result<(), LemmyError> {
if !local_user_view.local_user.admin {
return Err(ApiError::err("not_an_admin").into());
}
Ok(())
}
pub(crate) async fn get_post(post_id: PostId, pool: &DbPool) -> Result<Post, LemmyError> {
pub(crate) async fn get_post(post_id: i32, pool: &DbPool) -> Result<Post, LemmyError> {
match blocking(pool, move |conn| Post::read(conn, post_id)).await? {
Ok(post) => Ok(post),
Err(_e) => Err(ApiError::err("couldnt_find_post").into()),
Err(_e) => Err(APIError::err("couldnt_find_post").into()),
}
}
pub(crate) async fn get_local_user_view_from_jwt(
jwt: &str,
pool: &DbPool,
) -> Result<LocalUserView, LemmyError> {
pub(crate) async fn get_user_from_jwt(jwt: &str, pool: &DbPool) -> Result<User_, LemmyError> {
let claims = match Claims::decode(&jwt) {
Ok(claims) => claims.claims,
Err(_e) => return Err(ApiError::err("not_logged_in").into()),
Err(_e) => return Err(APIError::err("not_logged_in").into()),
};
let local_user_id = LocalUserId(claims.sub);
let local_user_view =
blocking(pool, move |conn| LocalUserView::read(conn, local_user_id)).await??;
let user_id = claims.id;
let user = blocking(pool, move |conn| User_::read(conn, user_id)).await??;
// Check for a site ban
if local_user_view.person.banned {
return Err(ApiError::err("site_ban").into());
if user.banned {
return Err(APIError::err("site_ban").into());
}
check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
Ok(local_user_view)
Ok(user)
}
/// Checks if user's token was issued before user's password reset.
pub(crate) fn check_validator_time(
validator_time: &chrono::NaiveDateTime,
claims: &Claims,
) -> Result<(), LemmyError> {
let user_validation_time = validator_time.timestamp();
if user_validation_time > claims.iat {
Err(ApiError::err("not_logged_in").into())
} else {
Ok(())
}
}
pub(crate) async fn get_local_user_view_from_jwt_opt(
pub(crate) async fn get_user_from_jwt_opt(
jwt: &Option<String>,
pool: &DbPool,
) -> Result<Option<LocalUserView>, LemmyError> {
) -> Result<Option<User_>, LemmyError> {
match jwt {
Some(jwt) => Ok(Some(get_local_user_view_from_jwt(jwt, pool).await?)),
Some(jwt) => Ok(Some(get_user_from_jwt(jwt, pool).await?)),
None => Ok(None),
}
}
pub(crate) async fn get_local_user_settings_view_from_jwt(
pub(crate) async fn get_user_safe_settings_from_jwt(
jwt: &str,
pool: &DbPool,
) -> Result<LocalUserSettingsView, LemmyError> {
) -> Result<UserSafeSettings, LemmyError> {
let claims = match Claims::decode(&jwt) {
Ok(claims) => claims.claims,
Err(_e) => return Err(ApiError::err("not_logged_in").into()),
Err(_e) => return Err(APIError::err("not_logged_in").into()),
};
let local_user_id = LocalUserId(claims.sub);
let local_user_view = blocking(pool, move |conn| {
LocalUserSettingsView::read(conn, local_user_id)
})
.await??;
let user_id = claims.id;
let user = blocking(pool, move |conn| UserSafeSettings::read(conn, user_id)).await??;
// Check for a site ban
if local_user_view.person.banned {
return Err(ApiError::err("site_ban").into());
if user.banned {
return Err(APIError::err("site_ban").into());
}
check_validator_time(&local_user_view.local_user.validator_time, &claims)?;
Ok(local_user_view)
Ok(user)
}
pub(crate) async fn get_local_user_settings_view_from_jwt_opt(
pub(crate) async fn get_user_safe_settings_from_jwt_opt(
jwt: &Option<String>,
pool: &DbPool,
) -> Result<Option<LocalUserSettingsView>, LemmyError> {
) -> Result<Option<UserSafeSettings>, LemmyError> {
match jwt {
Some(jwt) => Ok(Some(
get_local_user_settings_view_from_jwt(jwt, pool).await?,
)),
Some(jwt) => Ok(Some(get_user_safe_settings_from_jwt(jwt, pool).await?)),
None => Ok(None),
}
}
pub(crate) async fn check_community_ban(
person_id: PersonId,
community_id: CommunityId,
user_id: i32,
community_id: i32,
pool: &DbPool,
) -> Result<(), LemmyError> {
let is_banned =
move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
let is_banned = move |conn: &'_ _| CommunityUserBanView::get(conn, user_id, community_id).is_ok();
if blocking(pool, is_banned).await? {
Err(ApiError::err("community_ban").into())
Err(APIError::err("community_ban").into())
} else {
Ok(())
}
@ -189,7 +143,7 @@ pub(crate) async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result
if score == -1 {
let site = blocking(pool, move |conn| Site::read_simple(conn)).await??;
if !site.enable_downvotes {
return Err(ApiError::err("downvotes_disabled").into());
return Err(APIError::err("downvotes_disabled").into());
}
}
Ok(())
@ -199,31 +153,40 @@ pub(crate) async fn check_downvotes_enabled(score: i16, pool: &DbPool) -> Result
/// or if a community_id is supplied validates the user is a moderator
/// of that community and returns the community id in a vec
///
/// * `person_id` - the person id of the moderator
/// * `user_id` - the user id of the moderator
/// * `community_id` - optional community id to check for moderator privileges
/// * `pool` - the diesel db pool
pub(crate) async fn collect_moderated_communities(
person_id: PersonId,
community_id: Option<CommunityId>,
user_id: i32,
community_id: Option<i32>,
pool: &DbPool,
) -> Result<Vec<CommunityId>, LemmyError> {
) -> Result<Vec<i32>, LemmyError> {
if let Some(community_id) = community_id {
// if the user provides a community_id, just check for mod/admin privileges
is_mod_or_admin(pool, person_id, community_id).await?;
is_mod_or_admin(pool, user_id, community_id).await?;
Ok(vec![community_id])
} else {
let ids = blocking(pool, move |conn: &'_ _| {
CommunityModerator::get_person_moderated_communities(conn, person_id)
CommunityModerator::get_user_moderated_communities(conn, user_id)
})
.await??;
Ok(ids)
}
}
pub(crate) fn check_optional_url(item: &Option<Option<String>>) -> Result<(), LemmyError> {
if let Some(Some(item)) = &item {
if Url::parse(item).is_err() {
return Err(APIError::err("invalid_url").into());
}
}
Ok(())
}
pub(crate) async fn build_federated_instances(
pool: &DbPool,
) -> Result<Option<FederatedInstances>, LemmyError> {
if Settings::get().federation().enabled {
if Settings::get().federation.enabled {
let distinct_communities = blocking(pool, move |conn| {
Community::distinct_federated_communities(conn)
})
@ -237,13 +200,8 @@ pub(crate) async fn build_federated_instances(
.map(|actor_id| Ok(Url::parse(actor_id)?.host_str().unwrap_or("").to_string()))
.collect::<Result<Vec<String>, LemmyError>>()?;
if let Some(allowed) = allowed.as_ref() {
linked.extend_from_slice(allowed);
}
if let Some(blocked) = blocked.as_ref() {
linked.retain(|a| !blocked.contains(a) && !a.eq(&Settings::get().hostname()));
}
linked.extend_from_slice(&allowed);
linked.retain(|a| !blocked.contains(a) && !a.eq("") && !a.eq(&Settings::get().hostname));
// Sort and remove dupes
linked.sort_unstable();
@ -270,17 +228,17 @@ pub async fn match_websocket_operation(
UserOperation::Login => do_websocket_operation::<Login>(context, id, op, data).await,
UserOperation::Register => do_websocket_operation::<Register>(context, id, op, data).await,
UserOperation::GetCaptcha => do_websocket_operation::<GetCaptcha>(context, id, op, data).await,
UserOperation::GetPersonDetails => {
do_websocket_operation::<GetPersonDetails>(context, id, op, data).await
UserOperation::GetUserDetails => {
do_websocket_operation::<GetUserDetails>(context, id, op, data).await
}
UserOperation::GetReplies => do_websocket_operation::<GetReplies>(context, id, op, data).await,
UserOperation::AddAdmin => do_websocket_operation::<AddAdmin>(context, id, op, data).await,
UserOperation::BanPerson => do_websocket_operation::<BanPerson>(context, id, op, data).await,
UserOperation::GetPersonMentions => {
do_websocket_operation::<GetPersonMentions>(context, id, op, data).await
UserOperation::BanUser => do_websocket_operation::<BanUser>(context, id, op, data).await,
UserOperation::GetUserMentions => {
do_websocket_operation::<GetUserMentions>(context, id, op, data).await
}
UserOperation::MarkPersonMentionAsRead => {
do_websocket_operation::<MarkPersonMentionAsRead>(context, id, op, data).await
UserOperation::MarkUserMentionAsRead => {
do_websocket_operation::<MarkUserMentionAsRead>(context, id, op, data).await
}
UserOperation::MarkAllAsRead => {
do_websocket_operation::<MarkAllAsRead>(context, id, op, data).await
@ -342,6 +300,9 @@ pub async fn match_websocket_operation(
UserOperation::TransferSite => {
do_websocket_operation::<TransferSite>(context, id, op, data).await
}
UserOperation::ListCategories => {
do_websocket_operation::<ListCategories>(context, id, op, data).await
}
// Community ops
UserOperation::GetCommunity => {
@ -478,11 +439,7 @@ pub(crate) fn captcha_espeak_wav_base64(captcha: &str) -> Result<String, LemmyEr
pub(crate) fn espeak_wav_base64(text: &str) -> Result<String, LemmyError> {
// Make a temp file path
let uuid = uuid::Uuid::new_v4().to_string();
let file_path = format!(
"{}/lemmy_espeak_{}.wav",
env::temp_dir().to_string_lossy(),
&uuid
);
let file_path = format!("/tmp/lemmy_espeak_{}.wav", &uuid);
// Write the wav file
Command::new("espeak")
@ -503,81 +460,9 @@ pub(crate) fn espeak_wav_base64(text: &str) -> Result<String, LemmyError> {
Ok(base64)
}
/// Checks the password length
pub(crate) fn password_length_check(pass: &str) -> Result<(), LemmyError> {
if pass.len() > 60 {
Err(ApiError::err("invalid_password").into())
} else {
Ok(())
}
}
#[cfg(test)]
mod tests {
use crate::{captcha_espeak_wav_base64, check_validator_time};
use lemmy_db_queries::{establish_unpooled_connection, source::local_user::LocalUser_, Crud};
use lemmy_db_schema::source::{
local_user::{LocalUser, LocalUserForm},
person::{Person, PersonForm},
};
use lemmy_utils::claims::Claims;
#[test]
fn test_should_not_validate_user_token_after_password_change() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
name: "Gerry9812".into(),
preferred_username: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
published: None,
updated: None,
actor_id: None,
bio: None,
local: None,
private_key: None,
public_key: None,
last_refreshed_at: None,
inbox_url: None,
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let local_user_form = LocalUserForm {
person_id: inserted_person.id,
email: None,
matrix_user_id: None,
password_encrypted: "123456".to_string(),
admin: None,
show_nsfw: None,
theme: None,
default_sort_type: None,
default_listing_type: None,
lang: None,
show_avatars: None,
send_notifications_to_email: None,
};
let inserted_local_user = LocalUser::create(&conn, &local_user_form).unwrap();
let jwt = Claims::jwt(inserted_local_user.id.0).unwrap();
let claims = Claims::decode(&jwt).unwrap().claims;
let check = check_validator_time(&inserted_local_user.validator_time, &claims);
assert!(check.is_ok());
// The check should fail, since the validator time is now newer than the jwt issue time
let updated_local_user =
LocalUser::update_password(&conn, inserted_local_user.id, &"password111").unwrap();
let check_after = check_validator_time(&updated_local_user.validator_time, &claims);
assert!(check_after.is_err());
let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(1, num_deleted);
}
use crate::captcha_espeak_wav_base64;
#[test]
fn test_espeak() {

View file

@ -1,14 +1,14 @@
use crate::{
check_community_ban,
check_downvotes_enabled,
check_optional_url,
collect_moderated_communities,
get_local_user_view_from_jwt,
get_local_user_view_from_jwt_opt,
get_user_from_jwt,
get_user_from_jwt_opt,
is_mod_or_admin,
Perform,
};
use actix_web::web::Data;
use lemmy_api_structs::{blocking, post::*};
use lemmy_apub::{generate_apub_endpoint, ApubLikeableType, ApubObjectType, EndpointType};
use lemmy_db_queries::{
source::post::Post_,
@ -36,10 +36,11 @@ use lemmy_db_views_actor::{
community_moderator_view::CommunityModeratorView,
community_view::CommunityView,
};
use lemmy_structs::{blocking, post::*};
use lemmy_utils::{
request::fetch_iframely_and_pictrs_data,
utils::{check_slurs, check_slurs_opt, is_valid_post_title},
ApiError,
APIError,
ConnectionId,
LemmyError,
};
@ -60,28 +61,29 @@ impl Perform for CreatePost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &CreatePost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.body)?;
if !is_valid_post_title(&data.name) {
return Err(ApiError::err("invalid_post_title").into());
return Err(APIError::err("invalid_post_title").into());
}
check_community_ban(local_user_view.person.id, data.community_id, context.pool()).await?;
check_community_ban(user.id, data.community_id, context.pool()).await?;
check_optional_url(&Some(data.url.to_owned()))?;
// Fetch Iframely and pictrs cached image
let data_url = data.url.as_ref();
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
fetch_iframely_and_pictrs_data(context.client(), data_url).await;
fetch_iframely_and_pictrs_data(context.client(), data.url.to_owned()).await;
let post_form = PostForm {
name: data.name.trim().to_owned(),
url: data_url.map(|u| u.to_owned().into()),
url: data.url.to_owned(),
body: data.body.to_owned(),
community_id: data.community_id,
creator_id: local_user_view.person.id,
creator_id: user.id,
removed: None,
deleted: None,
nsfw: data.nsfw,
@ -91,7 +93,7 @@ impl Perform for CreatePost {
embed_title: iframely_title,
embed_description: iframely_description,
embed_html: iframely_html,
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
thumbnail_url: pictrs_thumbnail,
ap_id: None,
local: true,
published: None,
@ -107,7 +109,7 @@ impl Perform for CreatePost {
"couldnt_create_post"
};
return Err(ApiError::err(err_type).into());
return Err(APIError::err(err_type).into());
}
};
@ -119,38 +121,34 @@ impl Perform for CreatePost {
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_create_post").into()),
Err(_e) => return Err(APIError::err("couldnt_create_post").into()),
};
updated_post
.send_create(&local_user_view.person, context)
.await?;
updated_post.send_create(&user, context).await?;
// They like their own post by default
let like_form = PostLikeForm {
post_id: inserted_post.id,
person_id: local_user_view.person.id,
user_id: user.id,
score: 1,
};
let like = move |conn: &'_ _| PostLike::like(conn, &like_form);
if blocking(context.pool(), like).await?.is_err() {
return Err(ApiError::err("couldnt_like_post").into());
return Err(APIError::err("couldnt_like_post").into());
}
updated_post
.send_like(&local_user_view.person, context)
.await?;
updated_post.send_like(&user, context).await?;
// Refetch the view
let inserted_post_id = inserted_post.id;
let post_view = match blocking(context.pool(), move |conn| {
PostView::read(conn, inserted_post_id, Some(local_user_view.person.id))
PostView::read(conn, inserted_post_id, Some(user.id))
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
};
let res = PostResponse { post_view };
@ -175,23 +173,23 @@ impl Perform for GetPost {
_websocket_id: Option<ConnectionId>,
) -> Result<GetPostResponse, LemmyError> {
let data: &GetPost = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = local_user_view.map(|u| u.person.id);
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let id = data.id;
let post_view = match blocking(context.pool(), move |conn| {
PostView::read(conn, id, person_id)
PostView::read(conn, id, user_id)
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
};
let id = data.id;
let comments = blocking(context.pool(), move |conn| {
CommentQueryBuilder::create(conn)
.my_person_id(person_id)
.my_user_id(user_id)
.post_id(id)
.limit(9999)
.list()
@ -206,12 +204,12 @@ impl Perform for GetPost {
// Necessary for the sidebar
let community_view = match blocking(context.pool(), move |conn| {
CommunityView::read(conn, community_id, person_id)
CommunityView::read(conn, community_id, user_id)
})
.await?
{
Ok(community) => community,
Err(_e) => return Err(ApiError::err("couldnt_find_community").into()),
Err(_e) => return Err(APIError::err("couldnt_find_community").into()),
};
let online = context
@ -241,15 +239,15 @@ impl Perform for GetPosts {
_websocket_id: Option<ConnectionId>,
) -> Result<GetPostsResponse, LemmyError> {
let data: &GetPosts = &self;
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = match &local_user_view {
Some(uv) => Some(uv.person.id),
let user_id = match &user {
Some(user) => Some(user.id),
None => None,
};
let show_nsfw = match &local_user_view {
Some(uv) => uv.local_user.show_nsfw,
let show_nsfw = match &user {
Some(user) => user.show_nsfw,
None => false,
};
@ -267,7 +265,7 @@ impl Perform for GetPosts {
.show_nsfw(show_nsfw)
.community_id(community_id)
.community_name(community_name)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -275,7 +273,7 @@ impl Perform for GetPosts {
.await?
{
Ok(posts) => posts,
Err(_e) => return Err(ApiError::err("couldnt_get_posts").into()),
Err(_e) => return Err(APIError::err("couldnt_get_posts").into()),
};
Ok(GetPostsResponse { posts })
@ -292,7 +290,7 @@ impl Perform for CreatePostLike {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &CreatePostLike = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Don't do a downvote if site has downvotes disabled
check_downvotes_enabled(data.score, context.pool()).await?;
@ -301,18 +299,18 @@ impl Perform for CreatePostLike {
let post_id = data.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_community_ban(local_user_view.person.id, post.community_id, context.pool()).await?;
check_community_ban(user.id, post.community_id, context.pool()).await?;
let like_form = PostLikeForm {
post_id: data.post_id,
person_id: local_user_view.person.id,
user_id: user.id,
score: data.score,
};
// Remove any likes first
let person_id = local_user_view.person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
PostLike::remove(conn, person_id, post_id)
PostLike::remove(conn, user_id, post_id)
})
.await??;
@ -322,29 +320,27 @@ impl Perform for CreatePostLike {
let like_form2 = like_form.clone();
let like = move |conn: &'_ _| PostLike::like(conn, &like_form2);
if blocking(context.pool(), like).await?.is_err() {
return Err(ApiError::err("couldnt_like_post").into());
return Err(APIError::err("couldnt_like_post").into());
}
if like_form.score == 1 {
post.send_like(&local_user_view.person, context).await?;
post.send_like(&user, context).await?;
} else if like_form.score == -1 {
post.send_dislike(&local_user_view.person, context).await?;
post.send_dislike(&user, context).await?;
}
} else {
post
.send_undo_like(&local_user_view.person, context)
.await?;
post.send_undo_like(&user, context).await?;
}
let post_id = data.post_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let post_view = match blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(person_id))
PostView::read(conn, post_id, Some(user_id))
})
.await?
{
Ok(post) => post,
Err(_e) => return Err(ApiError::err("couldnt_find_post").into()),
Err(_e) => return Err(APIError::err("couldnt_find_post").into()),
};
let res = PostResponse { post_view };
@ -369,38 +365,32 @@ impl Perform for EditPost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &EditPost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.body)?;
if !is_valid_post_title(&data.name) {
return Err(ApiError::err("invalid_post_title").into());
return Err(APIError::err("invalid_post_title").into());
}
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_community_ban(
local_user_view.person.id,
orig_post.community_id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the creator can edit
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
return Err(ApiError::err("no_post_edit_allowed").into());
if !Post::is_post_creator(user.id, orig_post.creator_id) {
return Err(APIError::err("no_post_edit_allowed").into());
}
// Fetch Iframely and Pictrs cached image
let data_url = data.url.as_ref();
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
fetch_iframely_and_pictrs_data(context.client(), data_url).await;
fetch_iframely_and_pictrs_data(context.client(), data.url.to_owned()).await;
let post_form = PostForm {
name: data.name.trim().to_owned(),
url: data_url.map(|u| u.to_owned().into()),
url: data.url.to_owned(),
body: data.body.to_owned(),
nsfw: data.nsfw,
creator_id: orig_post.creator_id.to_owned(),
@ -413,7 +403,7 @@ impl Perform for EditPost {
embed_title: iframely_title,
embed_description: iframely_description,
embed_html: iframely_html,
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
thumbnail_url: pictrs_thumbnail,
ap_id: Some(orig_post.ap_id),
local: orig_post.local,
published: None,
@ -433,18 +423,16 @@ impl Perform for EditPost {
"couldnt_update_post"
};
return Err(ApiError::err(err_type).into());
return Err(APIError::err(err_type).into());
}
};
// Send apub update
updated_post
.send_update(&local_user_view.person, context)
.await?;
updated_post.send_update(&user, context).await?;
let post_id = data.post_id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(local_user_view.person.id))
PostView::read(conn, post_id, Some(user.id))
})
.await??;
@ -470,21 +458,16 @@ impl Perform for DeletePost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &DeletePost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_community_ban(
local_user_view.person.id,
orig_post.community_id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the creator can delete
if !Post::is_post_creator(local_user_view.person.id, orig_post.creator_id) {
return Err(ApiError::err("no_post_edit_allowed").into());
if !Post::is_post_creator(user.id, orig_post.creator_id) {
return Err(APIError::err("no_post_edit_allowed").into());
}
// Update the post
@ -497,19 +480,15 @@ impl Perform for DeletePost {
// apub updates
if deleted {
updated_post
.send_delete(&local_user_view.person, context)
.await?;
updated_post.send_delete(&user, context).await?;
} else {
updated_post
.send_undo_delete(&local_user_view.person, context)
.await?;
updated_post.send_undo_delete(&user, context).await?;
}
// Refetch the post
let post_id = data.post_id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(local_user_view.person.id))
PostView::read(conn, post_id, Some(user.id))
})
.await??;
@ -535,25 +514,15 @@ impl Perform for RemovePost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &RemovePost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_community_ban(
local_user_view.person.id,
orig_post.community_id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the mods can remove
is_mod_or_admin(
context.pool(),
local_user_view.person.id,
orig_post.community_id,
)
.await?;
is_mod_or_admin(context.pool(), user.id, orig_post.community_id).await?;
// Update the post
let post_id = data.post_id;
@ -565,7 +534,7 @@ impl Perform for RemovePost {
// Mod tables
let form = ModRemovePostForm {
mod_person_id: local_user_view.person.id,
mod_user_id: user.id,
post_id: data.post_id,
removed: Some(removed),
reason: data.reason.to_owned(),
@ -577,20 +546,16 @@ impl Perform for RemovePost {
// apub updates
if removed {
updated_post
.send_remove(&local_user_view.person, context)
.await?;
updated_post.send_remove(&user, context).await?;
} else {
updated_post
.send_undo_remove(&local_user_view.person, context)
.await?;
updated_post.send_undo_remove(&user, context).await?;
}
// Refetch the post
let post_id = data.post_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(person_id))
PostView::read(conn, post_id, Some(user_id))
})
.await??;
@ -616,25 +581,15 @@ impl Perform for LockPost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &LockPost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_community_ban(
local_user_view.person.id,
orig_post.community_id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the mods can lock
is_mod_or_admin(
context.pool(),
local_user_view.person.id,
orig_post.community_id,
)
.await?;
is_mod_or_admin(context.pool(), user.id, orig_post.community_id).await?;
// Update the post
let post_id = data.post_id;
@ -646,21 +601,19 @@ impl Perform for LockPost {
// Mod tables
let form = ModLockPostForm {
mod_person_id: local_user_view.person.id,
mod_user_id: user.id,
post_id: data.post_id,
locked: Some(locked),
};
blocking(context.pool(), move |conn| ModLockPost::create(conn, &form)).await??;
// apub updates
updated_post
.send_update(&local_user_view.person, context)
.await?;
updated_post.send_update(&user, context).await?;
// Refetch the post
let post_id = data.post_id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(local_user_view.person.id))
PostView::read(conn, post_id, Some(user.id))
})
.await??;
@ -686,25 +639,15 @@ impl Perform for StickyPost {
websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &StickyPost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let post_id = data.post_id;
let orig_post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_community_ban(
local_user_view.person.id,
orig_post.community_id,
context.pool(),
)
.await?;
check_community_ban(user.id, orig_post.community_id, context.pool()).await?;
// Verify that only the mods can sticky
is_mod_or_admin(
context.pool(),
local_user_view.person.id,
orig_post.community_id,
)
.await?;
is_mod_or_admin(context.pool(), user.id, orig_post.community_id).await?;
// Update the post
let post_id = data.post_id;
@ -716,7 +659,7 @@ impl Perform for StickyPost {
// Mod tables
let form = ModStickyPostForm {
mod_person_id: local_user_view.person.id,
mod_user_id: user.id,
post_id: data.post_id,
stickied: Some(stickied),
};
@ -727,14 +670,12 @@ impl Perform for StickyPost {
// Apub updates
// TODO stickied should pry work like locked for ease of use
updated_post
.send_update(&local_user_view.person, context)
.await?;
updated_post.send_update(&user, context).await?;
// Refetch the post
let post_id = data.post_id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(local_user_view.person.id))
PostView::read(conn, post_id, Some(user.id))
})
.await??;
@ -760,29 +701,29 @@ impl Perform for SavePost {
_websocket_id: Option<ConnectionId>,
) -> Result<PostResponse, LemmyError> {
let data: &SavePost = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let post_saved_form = PostSavedForm {
post_id: data.post_id,
person_id: local_user_view.person.id,
user_id: user.id,
};
if data.save {
let save = move |conn: &'_ _| PostSaved::save(conn, &post_saved_form);
if blocking(context.pool(), save).await?.is_err() {
return Err(ApiError::err("couldnt_save_post").into());
return Err(APIError::err("couldnt_save_post").into());
}
} else {
let unsave = move |conn: &'_ _| PostSaved::unsave(conn, &post_saved_form);
if blocking(context.pool(), unsave).await?.is_err() {
return Err(ApiError::err("couldnt_save_post").into());
return Err(APIError::err("couldnt_save_post").into());
}
}
let post_id = data.post_id;
let person_id = local_user_view.person.id;
let user_id = user.id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(conn, post_id, Some(person_id))
PostView::read(conn, post_id, Some(user_id))
})
.await??;
@ -801,28 +742,28 @@ impl Perform for CreatePostReport {
websocket_id: Option<ConnectionId>,
) -> Result<CreatePostReportResponse, LemmyError> {
let data: &CreatePostReport = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// check size of report and check for whitespace
let reason = data.reason.trim();
if reason.is_empty() {
return Err(ApiError::err("report_reason_required").into());
return Err(APIError::err("report_reason_required").into());
}
if reason.chars().count() > 1000 {
return Err(ApiError::err("report_too_long").into());
if reason.len() > 1000 {
return Err(APIError::err("report_too_long").into());
}
let person_id = local_user_view.person.id;
let user_id = user.id;
let post_id = data.post_id;
let post_view = blocking(context.pool(), move |conn| {
PostView::read(&conn, post_id, None)
})
.await??;
check_community_ban(person_id, post_view.community.id, context.pool()).await?;
check_community_ban(user_id, post_view.community.id, context.pool()).await?;
let report_form = PostReportForm {
creator_id: person_id,
creator_id: user_id,
post_id,
original_post_name: post_view.post.name,
original_post_url: post_view.post.url,
@ -836,7 +777,7 @@ impl Perform for CreatePostReport {
.await?
{
Ok(report) => report,
Err(_e) => return Err(ApiError::err("couldnt_create_report").into()),
Err(_e) => return Err(APIError::err("couldnt_create_report").into()),
};
let res = CreatePostReportResponse { success: true };
@ -844,7 +785,7 @@ impl Perform for CreatePostReport {
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePostReport,
response: res.clone(),
local_recipient_id: local_user_view.local_user.id,
recipient_id: user.id,
websocket_id,
});
@ -870,7 +811,7 @@ impl Perform for ResolvePostReport {
websocket_id: Option<ConnectionId>,
) -> Result<ResolvePostReportResponse, LemmyError> {
let data: &ResolvePostReport = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let report_id = data.report_id;
let report = blocking(context.pool(), move |conn| {
@ -878,15 +819,15 @@ impl Perform for ResolvePostReport {
})
.await??;
let person_id = local_user_view.person.id;
is_mod_or_admin(context.pool(), person_id, report.community.id).await?;
let user_id = user.id;
is_mod_or_admin(context.pool(), user_id, report.community.id).await?;
let resolved = data.resolved;
let resolve_fun = move |conn: &'_ _| {
if resolved {
PostReport::resolve(conn, report_id, person_id)
PostReport::resolve(conn, report_id, user_id)
} else {
PostReport::unresolve(conn, report_id, person_id)
PostReport::unresolve(conn, report_id, user_id)
}
};
@ -896,7 +837,7 @@ impl Perform for ResolvePostReport {
};
if blocking(context.pool(), resolve_fun).await?.is_err() {
return Err(ApiError::err("couldnt_resolve_report").into());
return Err(APIError::err("couldnt_resolve_report").into());
};
context.chat_server().do_send(SendModRoomMessage {
@ -922,12 +863,12 @@ impl Perform for ListPostReports {
websocket_id: Option<ConnectionId>,
) -> Result<ListPostReportsResponse, LemmyError> {
let data: &ListPostReports = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
let person_id = local_user_view.person.id;
let user_id = user.id;
let community_id = data.community;
let community_ids =
collect_moderated_communities(person_id, community_id, context.pool()).await?;
collect_moderated_communities(user_id, community_id, context.pool()).await?;
let page = data.page;
let limit = data.limit;
@ -945,7 +886,7 @@ impl Perform for ListPostReports {
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::ListPostReports,
response: res.clone(),
local_recipient_id: local_user_view.local_user.id,
recipient_id: user.id,
websocket_id,
});

View file

@ -1,19 +1,19 @@
use crate::{
build_federated_instances,
get_local_user_settings_view_from_jwt,
get_local_user_settings_view_from_jwt_opt,
get_local_user_view_from_jwt,
get_local_user_view_from_jwt_opt,
get_user_from_jwt,
get_user_from_jwt_opt,
get_user_safe_settings_from_jwt,
get_user_safe_settings_from_jwt_opt,
is_admin,
version,
Perform,
};
use actix_web::web::Data;
use anyhow::Context;
use lemmy_api_structs::{blocking, person::Register, site::*};
use lemmy_apub::fetcher::search::search_by_apub_id;
use lemmy_db_queries::{
diesel_option_overwrite_to_url,
source::site::Site_,
diesel_option_overwrite,
source::{category::Category_, site::Site_},
Crud,
SearchType,
SortType,
@ -21,6 +21,7 @@ use lemmy_db_queries::{
use lemmy_db_schema::{
naive_now,
source::{
category::Category,
moderator::*,
site::{Site, *},
},
@ -32,7 +33,7 @@ use lemmy_db_views::{
};
use lemmy_db_views_actor::{
community_view::CommunityQueryBuilder,
person_view::{PersonQueryBuilder, PersonViewSafe},
user_view::{UserQueryBuilder, UserViewSafe},
};
use lemmy_db_views_moderator::{
mod_add_community_view::ModAddCommunityView,
@ -45,12 +46,12 @@ use lemmy_db_views_moderator::{
mod_remove_post_view::ModRemovePostView,
mod_sticky_post_view::ModStickyPostView,
};
use lemmy_structs::{blocking, site::*, user::Register};
use lemmy_utils::{
location_info,
settings::structs::Settings,
settings::Settings,
utils::{check_slurs, check_slurs_opt},
version,
ApiError,
APIError,
ConnectionId,
LemmyError,
};
@ -62,6 +63,24 @@ use lemmy_websocket::{
use log::{debug, info};
use std::str::FromStr;
#[async_trait::async_trait(?Send)]
impl Perform for ListCategories {
type Response = ListCategoriesResponse;
async fn perform(
&self,
context: &Data<LemmyContext>,
_websocket_id: Option<ConnectionId>,
) -> Result<ListCategoriesResponse, LemmyError> {
let _data: &ListCategories = &self;
let categories = blocking(context.pool(), move |conn| Category::list_all(conn)).await??;
// Return the jwt
Ok(ListCategoriesResponse { categories })
}
}
#[async_trait::async_trait(?Send)]
impl Perform for GetModlog {
type Response = GetModlogResponse;
@ -74,36 +93,36 @@ impl Perform for GetModlog {
let data: &GetModlog = &self;
let community_id = data.community_id;
let mod_person_id = data.mod_person_id;
let mod_user_id = data.mod_user_id;
let page = data.page;
let limit = data.limit;
let removed_posts = blocking(context.pool(), move |conn| {
ModRemovePostView::list(conn, community_id, mod_person_id, page, limit)
ModRemovePostView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
let locked_posts = blocking(context.pool(), move |conn| {
ModLockPostView::list(conn, community_id, mod_person_id, page, limit)
ModLockPostView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
let stickied_posts = blocking(context.pool(), move |conn| {
ModStickyPostView::list(conn, community_id, mod_person_id, page, limit)
ModStickyPostView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
let removed_comments = blocking(context.pool(), move |conn| {
ModRemoveCommentView::list(conn, community_id, mod_person_id, page, limit)
ModRemoveCommentView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
let banned_from_community = blocking(context.pool(), move |conn| {
ModBanFromCommunityView::list(conn, community_id, mod_person_id, page, limit)
ModBanFromCommunityView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
let added_to_community = blocking(context.pool(), move |conn| {
ModAddCommunityView::list(conn, community_id, mod_person_id, page, limit)
ModAddCommunityView::list(conn, community_id, mod_user_id, page, limit)
})
.await??;
@ -111,9 +130,9 @@ impl Perform for GetModlog {
let (removed_communities, banned, added) = if data.community_id.is_none() {
blocking(context.pool(), move |conn| {
Ok((
ModRemoveCommunityView::list(conn, mod_person_id, page, limit)?,
ModBanView::list(conn, mod_person_id, page, limit)?,
ModAddView::list(conn, mod_person_id, page, limit)?,
ModRemoveCommunityView::list(conn, mod_user_id, page, limit)?,
ModBanView::list(conn, mod_user_id, page, limit)?,
ModAddView::list(conn, mod_user_id, page, limit)?,
)) as Result<_, LemmyError>
})
.await??
@ -149,23 +168,23 @@ impl Perform for CreateSite {
let read_site = move |conn: &'_ _| Site::read_simple(conn);
if blocking(context.pool(), read_site).await?.is_ok() {
return Err(ApiError::err("site_already_exists").into());
return Err(APIError::err("site_already_exists").into());
};
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.description)?;
// Make sure user is an admin
is_admin(&local_user_view)?;
is_admin(context.pool(), user.id).await?;
let site_form = SiteForm {
name: data.name.to_owned(),
description: data.description.to_owned(),
icon: Some(data.icon.to_owned().map(|url| url.into())),
banner: Some(data.banner.to_owned().map(|url| url.into())),
creator_id: local_user_view.person.id,
icon: Some(data.icon.to_owned()),
banner: Some(data.banner.to_owned()),
creator_id: user.id,
enable_downvotes: data.enable_downvotes,
open_registration: data.open_registration,
enable_nsfw: data.enable_nsfw,
@ -174,7 +193,7 @@ impl Perform for CreateSite {
let create_site = move |conn: &'_ _| Site::create(conn, &site_form);
if blocking(context.pool(), create_site).await?.is_err() {
return Err(ApiError::err("site_already_exists").into());
return Err(APIError::err("site_already_exists").into());
}
let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
@ -192,18 +211,18 @@ impl Perform for EditSite {
websocket_id: Option<ConnectionId>,
) -> Result<SiteResponse, LemmyError> {
let data: &EditSite = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
check_slurs(&data.name)?;
check_slurs_opt(&data.description)?;
// Make sure user is an admin
is_admin(&local_user_view)?;
is_admin(context.pool(), user.id).await?;
let found_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
let icon = diesel_option_overwrite_to_url(&data.icon)?;
let banner = diesel_option_overwrite_to_url(&data.banner)?;
let icon = diesel_option_overwrite(&data.icon);
let banner = diesel_option_overwrite(&data.banner);
let site_form = SiteForm {
name: data.name.to_owned(),
@ -219,7 +238,7 @@ impl Perform for EditSite {
let update_site = move |conn: &'_ _| Site::update(conn, 1, &site_form);
if blocking(context.pool(), update_site).await?.is_err() {
return Err(ApiError::err("couldnt_update_site").into());
return Err(APIError::err("couldnt_update_site").into());
}
let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
@ -251,7 +270,7 @@ impl Perform for GetSite {
Ok(site_view) => Some(site_view),
// If the site isn't created yet, check the setup
Err(_) => {
if let Some(setup) = Settings::get().setup().as_ref() {
if let Some(setup) = Settings::get().setup.as_ref() {
let register = Register {
username: setup.admin_username.to_owned(),
email: setup.admin_email.to_owned(),
@ -283,20 +302,20 @@ impl Perform for GetSite {
}
};
let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
// Make sure the site creator is the top admin
if let Some(site_view) = site_view.to_owned() {
let site_creator_id = site_view.creator.id;
// TODO investigate why this is sometimes coming back null
// Maybe user_.admin isn't being set to true?
if let Some(creator_index) = admins.iter().position(|r| r.person.id == site_creator_id) {
let creator_person = admins.remove(creator_index);
admins.insert(0, creator_person);
if let Some(creator_index) = admins.iter().position(|r| r.user.id == site_creator_id) {
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
}
}
let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
let online = context
.chat_server()
@ -304,7 +323,7 @@ impl Perform for GetSite {
.await
.unwrap_or(1);
let my_user = get_local_user_settings_view_from_jwt_opt(&data.auth, context.pool()).await?;
let my_user = get_user_safe_settings_from_jwt_opt(&data.auth, context.pool()).await?;
let federated_instances = build_federated_instances(context.pool()).await?;
Ok(GetSiteResponse {
@ -335,8 +354,8 @@ impl Perform for Search {
Err(e) => debug!("Failed to resolve search query as activitypub ID: {}", e),
}
let local_user_view = get_local_user_view_from_jwt_opt(&data.auth, context.pool()).await?;
let person_id = local_user_view.map(|u| u.person.id);
let user = get_user_from_jwt_opt(&data.auth, context.pool()).await?;
let user_id = user.map(|u| u.id);
let type_ = SearchType::from_str(&data.type_)?;
@ -361,7 +380,7 @@ impl Perform for Search {
.show_nsfw(true)
.community_id(community_id)
.community_name(community_name)
.my_person_id(person_id)
.my_user_id(user_id)
.search_term(q)
.page(page)
.limit(limit)
@ -374,7 +393,7 @@ impl Perform for Search {
CommentQueryBuilder::create(&conn)
.sort(&sort)
.search_term(q)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -386,7 +405,7 @@ impl Perform for Search {
CommunityQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -395,7 +414,7 @@ impl Perform for Search {
}
SearchType::Users => {
users = blocking(context.pool(), move |conn| {
PersonQueryBuilder::create(conn)
UserQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.page(page)
@ -411,7 +430,7 @@ impl Perform for Search {
.show_nsfw(true)
.community_id(community_id)
.community_name(community_name)
.my_person_id(person_id)
.my_user_id(user_id)
.search_term(q)
.page(page)
.limit(limit)
@ -426,7 +445,7 @@ impl Perform for Search {
CommentQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -440,7 +459,7 @@ impl Perform for Search {
CommunityQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.my_person_id(person_id)
.my_user_id(user_id)
.page(page)
.limit(limit)
.list()
@ -451,7 +470,7 @@ impl Perform for Search {
let sort = SortType::from_str(&data.sort)?;
users = blocking(context.pool(), move |conn| {
PersonQueryBuilder::create(conn)
UserQueryBuilder::create(conn)
.sort(&sort)
.search_term(q)
.page(page)
@ -465,7 +484,7 @@ impl Perform for Search {
PostQueryBuilder::create(conn)
.sort(&sort)
.show_nsfw(true)
.my_person_id(person_id)
.my_user_id(user_id)
.community_id(community_id)
.community_name(community_name)
.url_search(q)
@ -498,27 +517,27 @@ impl Perform for TransferSite {
_websocket_id: Option<ConnectionId>,
) -> Result<GetSiteResponse, LemmyError> {
let data: &TransferSite = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_safe_settings_from_jwt(&data.auth, context.pool()).await?;
is_admin(&local_user_view)?;
is_admin(context.pool(), user.id).await?;
let read_site = blocking(context.pool(), move |conn| Site::read_simple(conn)).await??;
// Make sure user is the creator
if read_site.creator_id != local_user_view.person.id {
return Err(ApiError::err("not_an_admin").into());
if read_site.creator_id != user.id {
return Err(APIError::err("not_an_admin").into());
}
let new_creator_id = data.person_id;
let new_creator_id = data.user_id;
let transfer_site = move |conn: &'_ _| Site::transfer(conn, new_creator_id);
if blocking(context.pool(), transfer_site).await?.is_err() {
return Err(ApiError::err("couldnt_update_site").into());
return Err(APIError::err("couldnt_update_site").into());
};
// Mod tables
let form = ModAddForm {
mod_person_id: local_user_view.person.id,
other_person_id: data.person_id,
mod_user_id: user.id,
other_user_id: data.user_id,
removed: Some(false),
};
@ -526,26 +545,24 @@ impl Perform for TransferSite {
let site_view = blocking(context.pool(), move |conn| SiteView::read(conn)).await??;
let mut admins = blocking(context.pool(), move |conn| PersonViewSafe::admins(conn)).await??;
let mut admins = blocking(context.pool(), move |conn| UserViewSafe::admins(conn)).await??;
let creator_index = admins
.iter()
.position(|r| r.person.id == site_view.creator.id)
.position(|r| r.user.id == site_view.creator.id)
.context(location_info!())?;
let creator_person = admins.remove(creator_index);
admins.insert(0, creator_person);
let creator_user = admins.remove(creator_index);
admins.insert(0, creator_user);
let banned = blocking(context.pool(), move |conn| PersonViewSafe::banned(conn)).await??;
let banned = blocking(context.pool(), move |conn| UserViewSafe::banned(conn)).await??;
let federated_instances = build_federated_instances(context.pool()).await?;
let my_user = Some(get_local_user_settings_view_from_jwt(&data.auth, context.pool()).await?);
Ok(GetSiteResponse {
site_view: Some(site_view),
admins,
banned,
online: 0,
version: version::VERSION.to_string(),
my_user,
my_user: Some(user),
federated_instances,
})
}
@ -561,10 +578,10 @@ impl Perform for GetSiteConfig {
_websocket_id: Option<ConnectionId>,
) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &GetSiteConfig = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Only let admins read this
is_admin(&local_user_view)?;
is_admin(context.pool(), user.id).await?;
let config_hjson = Settings::read_config_file()?;
@ -582,15 +599,16 @@ impl Perform for SaveSiteConfig {
_websocket_id: Option<ConnectionId>,
) -> Result<GetSiteConfigResponse, LemmyError> {
let data: &SaveSiteConfig = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
// Only let admins read this
is_admin(&local_user_view)?;
let user_id = user.id;
is_admin(context.pool(), user_id).await?;
// Make sure docker doesn't have :ro at the end of the volume, so its not a read-only filesystem
let config_hjson = match Settings::save_config_file(&data.config_hjson) {
Ok(config_hjson) => config_hjson,
Err(_e) => return Err(ApiError::err("couldnt_update_site").into()),
Err(_e) => return Err(APIError::err("couldnt_update_site").into()),
};
Ok(GetSiteConfigResponse { config_hjson })

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1 @@
pub const VERSION: &str = "0.9.7";

View file

@ -1,6 +1,6 @@
use crate::{get_local_user_view_from_jwt, Perform};
use crate::{get_user_from_jwt, Perform};
use actix_web::web::Data;
use lemmy_api_structs::websocket::*;
use lemmy_structs::websocket::*;
use lemmy_utils::{ConnectionId, LemmyError};
use lemmy_websocket::{
messages::{JoinCommunityRoom, JoinModRoom, JoinPostRoom, JoinUserRoom},
@ -17,11 +17,11 @@ impl Perform for UserJoin {
websocket_id: Option<ConnectionId>,
) -> Result<UserJoinResponse, LemmyError> {
let data: &UserJoin = &self;
let local_user_view = get_local_user_view_from_jwt(&data.auth, context.pool()).await?;
let user = get_user_from_jwt(&data.auth, context.pool()).await?;
if let Some(ws_id) = websocket_id {
context.chat_server().do_send(JoinUserRoom {
local_user_id: local_user_view.local_user.id,
user_id: user.id,
id: ws_id,
});
}

View file

@ -1,12 +1,12 @@
[package]
name = "lemmy_apub"
version = "0.1.0"
authors = ["Felix Ableitner <me@nutomic.com>"]
edition = "2018"
[lib]
name = "lemmy_apub"
path = "src/lib.rs"
doctest = false
[dependencies]
lemmy_utils = { path = "../utils" }
@ -14,7 +14,7 @@ lemmy_db_queries = { path = "../db_queries" }
lemmy_db_schema = { path = "../db_schema" }
lemmy_db_views = { path = "../db_views" }
lemmy_db_views_actor = { path = "../db_views_actor" }
lemmy_api_structs = { path = "../api_structs" }
lemmy_structs = { path = "../structs" }
lemmy_websocket = { path = "../websocket" }
diesel = "1.4.5"
activitystreams = "0.7.0-alpha.10"
@ -32,7 +32,7 @@ rand = "0.8.3"
strum = "0.20.0"
strum_macros = "0.20.1"
lazy_static = "1.4.0"
url = { version = "2.2.1", features = ["serde"] }
url = { version = "2.2.0", features = ["serde"] }
percent-encoding = "2.1.0"
openssl = "0.10.32"
http = "0.2.3"

View file

@ -1,16 +1,16 @@
use crate::{activities::receive::get_actor_as_person, objects::FromApub, ActorType, NoteExt};
use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, NoteExt};
use activitystreams::{
activity::{ActorAndObjectRefExt, Create, Dislike, Like, Remove, Update},
base::ExtendsExt,
};
use anyhow::Context;
use lemmy_api_structs::{blocking, comment::CommentResponse, send_local_notifs};
use lemmy_db_queries::{source::comment::Comment_, Crud, Likeable};
use lemmy_db_schema::source::{
comment::{Comment, CommentLike, CommentLikeForm},
post::Post,
};
use lemmy_db_views::comment_view::CommentView;
use lemmy_structs::{blocking, comment::CommentResponse, send_local_notifs};
use lemmy_utils::{location_info, utils::scrape_text_for_mentions, LemmyError};
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
@ -19,11 +19,11 @@ pub(crate) async fn receive_create_comment(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&create, context, request_counter).await?;
let user = get_actor_as_user(&create, context, request_counter).await?;
let note = NoteExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
let comment = Comment::from_apub(&note, context, person.actor_id(), request_counter).await?;
let comment = Comment::from_apub(&note, context, user.actor_id(), request_counter).await?;
let post_id = comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -33,15 +33,8 @@ pub(crate) async fn receive_create_comment(
// Its much easier to scrape them from the comment body, since the API has to do that
// anyway.
let mentions = scrape_text_for_mentions(&comment.content);
let recipient_ids = send_local_notifs(
mentions,
comment.clone(),
person,
post,
context.pool(),
true,
)
.await?;
let recipient_ids =
send_local_notifs(mentions, comment.clone(), &user, post, context.pool(), true).await?;
// Refetch the view
let comment_view = blocking(context.pool(), move |conn| {
@ -71,9 +64,9 @@ pub(crate) async fn receive_update_comment(
) -> Result<(), LemmyError> {
let note = NoteExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
let person = get_actor_as_person(&update, context, request_counter).await?;
let user = get_actor_as_user(&update, context, request_counter).await?;
let comment = Comment::from_apub(&note, context, person.actor_id(), request_counter).await?;
let comment = Comment::from_apub(&note, context, user.actor_id(), request_counter).await?;
let comment_id = comment.id;
let post_id = comment.post_id;
@ -81,7 +74,7 @@ pub(crate) async fn receive_update_comment(
let mentions = scrape_text_for_mentions(&comment.content);
let recipient_ids =
send_local_notifs(mentions, comment, person, post, context.pool(), false).await?;
send_local_notifs(mentions, comment, &user, post, context.pool(), false).await?;
// Refetch the view
let comment_view = blocking(context.pool(), move |conn| {
@ -110,18 +103,18 @@ pub(crate) async fn receive_like_comment(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&like, context, request_counter).await?;
let user = get_actor_as_user(&like, context, request_counter).await?;
let comment_id = comment.id;
let like_form = CommentLikeForm {
comment_id,
post_id: comment.post_id,
person_id: person.id,
user_id: user.id,
score: 1,
};
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
CommentLike::remove(conn, person_id, comment_id)?;
CommentLike::remove(conn, user_id, comment_id)?;
CommentLike::like(conn, &like_form)
})
.await??;
@ -155,18 +148,18 @@ pub(crate) async fn receive_dislike_comment(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&dislike, context, request_counter).await?;
let user = get_actor_as_user(&dislike, context, request_counter).await?;
let comment_id = comment.id;
let like_form = CommentLikeForm {
comment_id,
post_id: comment.post_id,
person_id: person.id,
user_id: user.id,
score: -1,
};
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
CommentLike::remove(conn, person_id, comment_id)?;
CommentLike::remove(conn, user_id, comment_id)?;
CommentLike::like(conn, &like_form)
})
.await??;

View file

@ -1,9 +1,9 @@
use crate::activities::receive::get_actor_as_person;
use crate::activities::receive::get_actor_as_user;
use activitystreams::activity::{Dislike, Like};
use lemmy_api_structs::{blocking, comment::CommentResponse};
use lemmy_db_queries::{source::comment::Comment_, Likeable};
use lemmy_db_schema::source::comment::{Comment, CommentLike};
use lemmy_db_views::comment_view::CommentView;
use lemmy_structs::{blocking, comment::CommentResponse};
use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendComment, LemmyContext, UserOperation};
@ -13,12 +13,12 @@ pub(crate) async fn receive_undo_like_comment(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(like, context, request_counter).await?;
let user = get_actor_as_user(like, context, request_counter).await?;
let comment_id = comment.id;
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
CommentLike::remove(conn, person_id, comment_id)
CommentLike::remove(conn, user_id, comment_id)
})
.await??;
@ -51,12 +51,12 @@ pub(crate) async fn receive_undo_dislike_comment(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(dislike, context, request_counter).await?;
let user = get_actor_as_user(dislike, context, request_counter).await?;
let comment_id = comment.id;
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
CommentLike::remove(conn, person_id, comment_id)
CommentLike::remove(conn, user_id, comment_id)
})
.await??;

View file

@ -4,10 +4,10 @@ use activitystreams::{
base::{AnyBase, ExtendsExt},
};
use anyhow::Context;
use lemmy_api_structs::{blocking, community::CommunityResponse};
use lemmy_db_queries::{source::community::Community_, ApubObject};
use lemmy_db_schema::source::community::Community;
use lemmy_db_views_actor::community_view::CommunityView;
use lemmy_structs::{blocking, community::CommunityResponse};
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendCommunityRoomMessage, LemmyContext, UserOperation};
use url::Url;

View file

@ -1,11 +1,11 @@
use crate::fetcher::person::get_or_fetch_and_upsert_person;
use crate::fetcher::user::get_or_fetch_and_upsert_user;
use activitystreams::{
activity::{ActorAndObjectRef, ActorAndObjectRefExt},
base::{AsBase, BaseExt},
error::DomainError,
};
use anyhow::{anyhow, Context};
use lemmy_db_schema::source::person::Person;
use lemmy_db_schema::source::user::User_;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug;
@ -28,18 +28,18 @@ where
Err(anyhow!("Activity not supported").into())
}
/// Reads the actor field of an activity and returns the corresponding `Person`.
pub(crate) async fn get_actor_as_person<T, A>(
/// Reads the actor field of an activity and returns the corresponding `User_`.
pub(crate) async fn get_actor_as_user<T, A>(
activity: &T,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<Person, LemmyError>
) -> Result<User_, LemmyError>
where
T: AsBase<A> + ActorAndObjectRef,
{
let actor = activity.actor()?;
let person_uri = actor.as_single_xsd_any_uri().context(location_info!())?;
get_or_fetch_and_upsert_person(&person_uri, context, request_counter).await
let user_uri = actor.as_single_xsd_any_uri().context(location_info!())?;
get_or_fetch_and_upsert_user(&user_uri, context, request_counter).await
}
/// Ensure that the ID of an incoming activity comes from the same domain as the actor. Optionally

View file

@ -1,13 +1,13 @@
use crate::{activities::receive::get_actor_as_person, objects::FromApub, ActorType, PageExt};
use crate::{activities::receive::get_actor_as_user, objects::FromApub, ActorType, PageExt};
use activitystreams::{
activity::{Create, Dislike, Like, Remove, Update},
prelude::*,
};
use anyhow::Context;
use lemmy_api_structs::{blocking, post::PostResponse};
use lemmy_db_queries::{source::post::Post_, Likeable};
use lemmy_db_schema::source::post::{Post, PostLike, PostLikeForm};
use lemmy_db_views::post_view::PostView;
use lemmy_structs::{blocking, post::PostResponse};
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
@ -16,11 +16,11 @@ pub(crate) async fn receive_create_post(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&create, context, request_counter).await?;
let user = get_actor_as_user(&create, context, request_counter).await?;
let page = PageExt::from_any_base(create.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
let post = Post::from_apub(&page, context, person.actor_id(), request_counter).await?;
let post = Post::from_apub(&page, context, user.actor_id(), request_counter).await?;
// Refetch the view
let post_id = post.id;
@ -45,11 +45,11 @@ pub(crate) async fn receive_update_post(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&update, context, request_counter).await?;
let user = get_actor_as_user(&update, context, request_counter).await?;
let page = PageExt::from_any_base(update.object().to_owned().one().context(location_info!())?)?
.context(location_info!())?;
let post = Post::from_apub(&page, context, person.actor_id(), request_counter).await?;
let post = Post::from_apub(&page, context, user.actor_id(), request_counter).await?;
let post_id = post.id;
// Refetch the view
@ -75,17 +75,17 @@ pub(crate) async fn receive_like_post(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&like, context, request_counter).await?;
let user = get_actor_as_user(&like, context, request_counter).await?;
let post_id = post.id;
let like_form = PostLikeForm {
post_id,
person_id: person.id,
user_id: user.id,
score: 1,
};
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
PostLike::remove(conn, person_id, post_id)?;
PostLike::remove(conn, user_id, post_id)?;
PostLike::like(conn, &like_form)
})
.await??;
@ -113,17 +113,17 @@ pub(crate) async fn receive_dislike_post(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(&dislike, context, request_counter).await?;
let user = get_actor_as_user(&dislike, context, request_counter).await?;
let post_id = post.id;
let like_form = PostLikeForm {
post_id,
person_id: person.id,
user_id: user.id,
score: -1,
};
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
PostLike::remove(conn, person_id, post_id)?;
PostLike::remove(conn, user_id, post_id)?;
PostLike::like(conn, &like_form)
})
.await??;

View file

@ -1,9 +1,9 @@
use crate::activities::receive::get_actor_as_person;
use crate::activities::receive::get_actor_as_user;
use activitystreams::activity::{Dislike, Like};
use lemmy_api_structs::{blocking, post::PostResponse};
use lemmy_db_queries::{source::post::Post_, Likeable};
use lemmy_db_schema::source::post::{Post, PostLike};
use lemmy_db_views::post_view::PostView;
use lemmy_structs::{blocking, post::PostResponse};
use lemmy_utils::LemmyError;
use lemmy_websocket::{messages::SendPost, LemmyContext, UserOperation};
@ -13,12 +13,12 @@ pub(crate) async fn receive_undo_like_post(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(like, context, request_counter).await?;
let user = get_actor_as_user(like, context, request_counter).await?;
let post_id = post.id;
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
PostLike::remove(conn, person_id, post_id)
PostLike::remove(conn, user_id, post_id)
})
.await??;
@ -45,12 +45,12 @@ pub(crate) async fn receive_undo_dislike_post(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let person = get_actor_as_person(dislike, context, request_counter).await?;
let user = get_actor_as_user(dislike, context, request_counter).await?;
let post_id = post.id;
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
PostLike::remove(conn, person_id, post_id)
PostLike::remove(conn, user_id, post_id)
})
.await??;

View file

@ -1,7 +1,7 @@
use crate::{
activities::receive::verify_activity_domains_valid,
check_is_apub_id_valid,
fetcher::person::get_or_fetch_and_upsert_person,
fetcher::user::get_or_fetch_and_upsert_user,
inbox::get_activity_to_and_cc,
objects::FromApub,
NoteExt,
@ -13,10 +13,10 @@ use activitystreams::{
public,
};
use anyhow::{anyhow, Context};
use lemmy_api_structs::{blocking, person::PrivateMessageResponse};
use lemmy_db_queries::source::private_message::PrivateMessage_;
use lemmy_db_schema::source::private_message::PrivateMessage;
use lemmy_db_views::{local_user_view::LocalUserView, private_message_view::PrivateMessageView};
use lemmy_db_views::private_message_view::PrivateMessageView;
use lemmy_structs::{blocking, user::PrivateMessageResponse};
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::{messages::SendUserRoomMessage, LemmyContext, UserOperation};
use url::Url;
@ -50,19 +50,12 @@ pub(crate) async fn receive_create_private_message(
private_message_view: message,
};
// Send notifications to the local recipient, if one exists
let recipient_id = res.private_message_view.recipient.id;
let local_recipient_id = blocking(context.pool(), move |conn| {
LocalUserView::read_person(conn, recipient_id)
})
.await??
.local_user
.id;
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::CreatePrivateMessage,
response: res,
local_recipient_id,
recipient_id,
websocket_id: None,
});
@ -98,17 +91,11 @@ pub(crate) async fn receive_update_private_message(
};
let recipient_id = res.private_message_view.recipient.id;
let local_recipient_id = blocking(context.pool(), move |conn| {
LocalUserView::read_person(conn, recipient_id)
})
.await??
.local_user
.id;
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage,
response: res,
local_recipient_id,
recipient_id,
websocket_id: None,
});
@ -136,19 +123,11 @@ pub(crate) async fn receive_delete_private_message(
let res = PrivateMessageResponse {
private_message_view: message,
};
let recipient_id = res.private_message_view.recipient.id;
let local_recipient_id = blocking(context.pool(), move |conn| {
LocalUserView::read_person(conn, recipient_id)
})
.await??
.local_user
.id;
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage,
response: res,
local_recipient_id,
recipient_id,
websocket_id: None,
});
@ -181,19 +160,11 @@ pub(crate) async fn receive_undo_delete_private_message(
let res = PrivateMessageResponse {
private_message_view: message,
};
let recipient_id = res.private_message_view.recipient.id;
let local_recipient_id = blocking(context.pool(), move |conn| {
LocalUserView::read_person(conn, recipient_id)
})
.await??
.local_user
.id;
context.chat_server().do_send(SendUserRoomMessage {
op: UserOperation::EditPrivateMessage,
response: res,
local_recipient_id,
recipient_id,
websocket_id: None,
});
@ -210,19 +181,19 @@ where
{
let to_and_cc = get_activity_to_and_cc(activity);
if to_and_cc.len() != 1 {
return Err(anyhow!("Private message can only be addressed to one person").into());
return Err(anyhow!("Private message can only be addressed to one user").into());
}
if to_and_cc.contains(&public()) {
return Err(anyhow!("Private message cant be public").into());
}
let person_id = activity
let user_id = activity
.actor()?
.to_owned()
.single_xsd_any_uri()
.context(location_info!())?;
check_is_apub_id_valid(&person_id)?;
// check that the sender is a person, not a community
get_or_fetch_and_upsert_person(&person_id, &context, request_counter).await?;
check_is_apub_id_valid(&user_id)?;
// check that the sender is a user, not a community
get_or_fetch_and_upsert_user(&user_id, &context, request_counter).await?;
Ok(())
}

View file

@ -2,7 +2,7 @@ use crate::{
activities::send::generate_activity_id,
activity_queue::{send_comment_mentions, send_to_community},
extensions::context::lemmy_context,
fetcher::person::get_or_fetch_and_upsert_person,
fetcher::user::get_or_fetch_and_upsert_user,
objects::ToApub,
ActorType,
ApubLikeableType,
@ -26,12 +26,12 @@ use activitystreams::{
};
use anyhow::anyhow;
use itertools::Itertools;
use lemmy_api_structs::{blocking, WebFingerResponse};
use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::source::{comment::Comment, community::Community, person::Person, post::Post};
use lemmy_db_schema::source::{comment::Comment, community::Community, post::Post, user::User_};
use lemmy_structs::{blocking, WebFingerResponse};
use lemmy_utils::{
request::{retry, RecvError},
settings::structs::Settings,
settings::Settings,
utils::{scrape_text_for_mentions, MentionData},
LemmyError,
};
@ -44,8 +44,8 @@ use url::Url;
#[async_trait::async_trait(?Send)]
impl ApubObjectType for Comment {
/// Send out information about a newly created comment, to the followers of the community and
/// mentioned persons.
async fn send_create(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
/// mentioned users.
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
@ -77,8 +77,8 @@ impl ApubObjectType for Comment {
}
/// Send out information about an edited post, to the followers of the community and mentioned
/// persons.
async fn send_update(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
/// users.
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let post_id = self.post_id;
@ -109,7 +109,7 @@ impl ApubObjectType for Comment {
Ok(())
}
async fn send_delete(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -135,7 +135,7 @@ impl ApubObjectType for Comment {
async fn send_undo_delete(
&self,
creator: &Person,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let post_id = self.post_id;
@ -173,7 +173,7 @@ impl ApubObjectType for Comment {
Ok(())
}
async fn send_remove(&self, mod_: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -197,11 +197,7 @@ impl ApubObjectType for Comment {
Ok(())
}
async fn send_undo_remove(
&self,
mod_: &Person,
context: &LemmyContext,
) -> Result<(), LemmyError> {
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -240,7 +236,7 @@ impl ApubObjectType for Comment {
#[async_trait::async_trait(?Send)]
impl ApubLikeableType for Comment {
async fn send_like(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -264,7 +260,7 @@ impl ApubLikeableType for Comment {
Ok(())
}
async fn send_dislike(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let post_id = self.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
@ -290,7 +286,7 @@ impl ApubLikeableType for Comment {
async fn send_undo_like(
&self,
creator: &Person,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let post_id = self.post_id;
@ -346,7 +342,7 @@ impl MentionsAndAddresses {
/// This takes a comment, and builds a list of to_addresses, inboxes,
/// and mention tags, so they know where to be sent to.
/// Addresses are the persons / addresses that go in the cc field.
/// Addresses are the users / addresses that go in the cc field.
async fn collect_non_local_mentions(
comment: &Comment,
community: &Community,
@ -360,7 +356,7 @@ async fn collect_non_local_mentions(
// Add the mention tag
let mut tags = Vec::new();
// Get the person IDs for any mentions
// Get the user IDs for any mentions
let mentions = scrape_text_for_mentions(&comment.content)
.into_iter()
// Filter only the non-local ones
@ -373,8 +369,8 @@ async fn collect_non_local_mentions(
debug!("mention actor_id: {}", actor_id);
addressed_ccs.push(actor_id.to_owned().to_string().parse()?);
let mention_person = get_or_fetch_and_upsert_person(&actor_id, context, &mut 0).await?;
inboxes.push(mention_person.get_shared_inbox_or_inbox_url());
let mention_user = get_or_fetch_and_upsert_user(&actor_id, context, &mut 0).await?;
inboxes.push(mention_user.get_shared_inbox_or_inbox_url());
let mut mention_tag = Mention::new();
mention_tag.set_href(actor_id).set_name(mention.full_name());
@ -391,12 +387,9 @@ async fn collect_non_local_mentions(
})
}
/// Returns the apub ID of the person this comment is responding to. Meaning, in case this is a
/// Returns the apub ID of the user this comment is responding to. Meaning, in case this is a
/// top-level comment, the creator of the post, otherwise the creator of the parent comment.
async fn get_comment_parent_creator(
pool: &DbPool,
comment: &Comment,
) -> Result<Person, LemmyError> {
async fn get_comment_parent_creator(pool: &DbPool, comment: &Comment) -> Result<User_, LemmyError> {
let parent_creator_id = if let Some(parent_comment_id) = comment.parent_id {
let parent_comment =
blocking(pool, move |conn| Comment::read(conn, parent_comment_id)).await??;
@ -406,10 +399,10 @@ async fn get_comment_parent_creator(
let parent_post = blocking(pool, move |conn| Post::read(conn, parent_post_id)).await??;
parent_post.creator_id
};
Ok(blocking(pool, move |conn| Person::read(conn, parent_creator_id)).await??)
Ok(blocking(pool, move |conn| User_::read(conn, parent_creator_id)).await??)
}
/// Turns a person id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// Turns a user id like `@name@example.com` into an apub ID, like `https://example.com/user/name`,
/// using webfinger.
async fn fetch_webfinger_url(mention: &MentionData, client: &Client) -> Result<Url, LemmyError> {
let fetch_url = format!(

View file

@ -3,8 +3,7 @@ use crate::{
activity_queue::{send_activity_single_dest, send_to_community_followers},
check_is_apub_id_valid,
extensions::context::lemmy_context,
fetcher::person::get_or_fetch_and_upsert_person,
insert_activity,
fetcher::user::get_or_fetch_and_upsert_user,
ActorType,
};
use activitystreams::{
@ -24,11 +23,11 @@ use activitystreams::{
};
use anyhow::Context;
use itertools::Itertools;
use lemmy_api_structs::blocking;
use lemmy_db_queries::DbPool;
use lemmy_db_schema::source::community::Community;
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use url::Url;
@ -71,7 +70,7 @@ impl ActorType for Community {
unimplemented!()
}
/// As a local community, accept the follow request from a remote person.
/// As a local community, accept the follow request from a remote user.
async fn send_accept_follow(
&self,
follow: Follow,
@ -81,7 +80,7 @@ impl ActorType for Community {
.actor()?
.as_single_xsd_any_uri()
.context(location_info!())?;
let person = get_or_fetch_and_upsert_person(actor_uri, context, &mut 0).await?;
let user = get_or_fetch_and_upsert_user(actor_uri, context, &mut 0).await?;
let mut accept = Accept::new(
self.actor_id.to_owned().into_inner(),
@ -90,9 +89,9 @@ impl ActorType for Community {
accept
.set_many_contexts(lemmy_context()?)
.set_id(generate_activity_id(AcceptType::Accept)?)
.set_to(person.actor_id());
.set_to(user.actor_id());
send_activity_single_dest(accept, self, person.inbox_url.into(), context).await?;
send_activity_single_dest(accept, self, user.inbox_url.into(), context).await?;
Ok(())
}
@ -165,20 +164,11 @@ impl ActorType for Community {
/// Wraps an activity sent to the community in an announce, and then sends the announce to all
/// community followers.
///
/// If we are announcing a local activity, it hasn't been stored in the database yet, and we need
/// to do it here, so that it can be fetched by ID. Remote activities are inserted into DB in the
/// inbox.
async fn send_announce(
&self,
activity: AnyBase,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let inner_id = activity.id().context(location_info!())?;
if inner_id.domain() == Some(&Settings::get().get_hostname_without_port()?) {
insert_activity(inner_id, activity.clone(), true, false, context.pool()).await?;
}
let mut announce = Announce::new(self.actor_id.to_owned().into_inner(), activity);
announce
.set_many_contexts(lemmy_context()?)

View file

@ -1,12 +1,12 @@
use lemmy_utils::settings::structs::Settings;
use lemmy_utils::settings::Settings;
use url::{ParseError, Url};
use uuid::Uuid;
pub(crate) mod comment;
pub(crate) mod community;
pub(crate) mod person;
pub(crate) mod post;
pub(crate) mod private_message;
pub(crate) mod user;
/// Generate a unique ID for an activity, in the format:
/// `http(s)://example.com/receive/create/202daf0a-1489-45df-8d2e-c8a3173fed36`

View file

@ -21,16 +21,16 @@ use activitystreams::{
prelude::*,
public,
};
use lemmy_api_structs::blocking;
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{community::Community, person::Person, post::Post};
use lemmy_db_schema::source::{community::Community, post::Post, user::User_};
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
#[async_trait::async_trait(?Send)]
impl ApubObjectType for Post {
/// Send out information about a newly created post, to the followers of the community.
async fn send_create(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
@ -54,7 +54,7 @@ impl ApubObjectType for Post {
}
/// Send out information about an edited post, to the followers of the community.
async fn send_update(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let page = self.to_apub(context.pool()).await?;
let community_id = self.community_id;
@ -77,7 +77,7 @@ impl ApubObjectType for Post {
Ok(())
}
async fn send_delete(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
@ -100,7 +100,7 @@ impl ApubObjectType for Post {
async fn send_undo_delete(
&self,
creator: &Person,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let community_id = self.community_id;
@ -134,7 +134,7 @@ impl ApubObjectType for Post {
Ok(())
}
async fn send_remove(&self, mod_: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
@ -155,11 +155,7 @@ impl ApubObjectType for Post {
Ok(())
}
async fn send_undo_remove(
&self,
mod_: &Person,
context: &LemmyContext,
) -> Result<(), LemmyError> {
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
@ -194,7 +190,7 @@ impl ApubObjectType for Post {
#[async_trait::async_trait(?Send)]
impl ApubLikeableType for Post {
async fn send_like(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
@ -215,7 +211,7 @@ impl ApubLikeableType for Post {
Ok(())
}
async fn send_dislike(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let community_id = self.community_id;
let community = blocking(context.pool(), move |conn| {
Community::read(conn, community_id)
@ -238,7 +234,7 @@ impl ApubLikeableType for Post {
async fn send_undo_like(
&self,
creator: &Person,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let community_id = self.community_id;

View file

@ -16,21 +16,20 @@ use activitystreams::{
},
prelude::*,
};
use lemmy_api_structs::blocking;
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::{person::Person, private_message::PrivateMessage};
use lemmy_db_schema::source::{private_message::PrivateMessage, user::User_};
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
#[async_trait::async_trait(?Send)]
impl ApubObjectType for PrivateMessage {
/// Send out information about a newly created private message
async fn send_create(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let recipient_id = self.recipient_id;
let recipient =
blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut create = Create::new(
creator.actor_id.to_owned().into_inner(),
@ -47,12 +46,11 @@ impl ApubObjectType for PrivateMessage {
}
/// Send out information about an edited private message, to the followers of the community.
async fn send_update(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let note = self.to_apub(context.pool()).await?;
let recipient_id = self.recipient_id;
let recipient =
blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut update = Update::new(
creator.actor_id.to_owned().into_inner(),
@ -67,10 +65,9 @@ impl ApubObjectType for PrivateMessage {
Ok(())
}
async fn send_delete(&self, creator: &Person, context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError> {
let recipient_id = self.recipient_id;
let recipient =
blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut delete = Delete::new(
creator.actor_id.to_owned().into_inner(),
@ -87,12 +84,11 @@ impl ApubObjectType for PrivateMessage {
async fn send_undo_delete(
&self,
creator: &Person,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let recipient_id = self.recipient_id;
let recipient =
blocking(context.pool(), move |conn| Person::read(conn, recipient_id)).await??;
let recipient = blocking(context.pool(), move |conn| User_::read(conn, recipient_id)).await??;
let mut delete = Delete::new(
creator.actor_id.to_owned().into_inner(),
@ -117,13 +113,13 @@ impl ApubObjectType for PrivateMessage {
Ok(())
}
async fn send_remove(&self, _mod_: &Person, _context: &LemmyContext) -> Result<(), LemmyError> {
async fn send_remove(&self, _mod_: &User_, _context: &LemmyContext) -> Result<(), LemmyError> {
unimplemented!()
}
async fn send_undo_remove(
&self,
_mod_: &Person,
_mod_: &User_,
_context: &LemmyContext,
) -> Result<(), LemmyError> {
unimplemented!()

View file

@ -13,18 +13,18 @@ use activitystreams::{
base::{AnyBase, BaseExt, ExtendsExt},
object::ObjectExt,
};
use lemmy_api_structs::blocking;
use lemmy_db_queries::{ApubObject, DbPool, Followable};
use lemmy_db_schema::source::{
community::{Community, CommunityFollower, CommunityFollowerForm},
person::Person,
user::User_,
};
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use url::Url;
#[async_trait::async_trait(?Send)]
impl ActorType for Person {
impl ActorType for User_ {
fn is_local(&self) -> bool {
self.local
}
@ -48,7 +48,7 @@ impl ActorType for Person {
.into()
}
/// As a given local person, send out a follow request to a remote community.
/// As a given local user, send out a follow request to a remote community.
async fn send_follow(
&self,
follow_actor_id: &Url,
@ -62,7 +62,7 @@ impl ActorType for Person {
let community_follower_form = CommunityFollowerForm {
community_id: community.id,
person_id: self.id,
user_id: self.id,
pending: true,
};
blocking(&context.pool(), move |conn| {

View file

@ -21,8 +21,8 @@ use background_jobs::{
};
use itertools::Itertools;
use lemmy_db_queries::DbPool;
use lemmy_db_schema::source::{community::Community, person::Person};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_db_schema::source::{community::Community, user::User_};
use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use log::{debug, warn};
use reqwest::Client;
@ -88,7 +88,7 @@ where
.await?
.iter()
.unique()
.filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname()))
.filter(|inbox| inbox.host_str() != Some(&Settings::get().hostname))
.filter(|inbox| check_is_apub_id_valid(inbox).is_ok())
.map(|inbox| inbox.to_owned())
.collect();
@ -112,7 +112,7 @@ where
Ok(())
}
/// Sends an activity from a local person to a remote community.
/// Sends an activity from a local user to a remote community.
///
/// * `activity` the activity to send
/// * `creator` the creator of the activity
@ -120,7 +120,7 @@ where
///
pub(crate) async fn send_to_community<T, Kind>(
activity: T,
creator: &Person,
creator: &User_,
community: &Community,
context: &LemmyContext,
) -> Result<(), LemmyError>
@ -157,13 +157,13 @@ where
Ok(())
}
/// Sends notification to any persons mentioned in a comment
/// Sends notification to any users mentioned in a comment
///
/// * `creator` person who created the comment
/// * `mentions` list of inboxes of persons which are mentioned in the comment
/// * `creator` user who created the comment
/// * `mentions` list of inboxes of users which are mentioned in the comment
/// * `activity` either a `Create/Note` or `Update/Note`
pub(crate) async fn send_comment_mentions<T, Kind>(
creator: &Person,
creator: &User_,
mentions: Vec<Url>,
activity: T,
context: &LemmyContext,
@ -215,7 +215,7 @@ where
Kind: Serialize,
<T as Extends<Kind>>::Error: From<serde_json::Error> + Send + Sync + 'static,
{
if !Settings::get().federation().enabled || inboxes.is_empty() {
if !Settings::get().federation.enabled || inboxes.is_empty() {
return Ok(());
}
@ -223,7 +223,7 @@ where
let hostname = Settings::get().get_hostname_without_port()?;
let inboxes: Vec<&Url> = inboxes
.iter()
.filter(|i| i.domain().expect("valid inbox url") != hostname)
.filter(|i| i.domain().unwrap() != hostname)
.collect();
let activity = activity.into_any_base()?;

View file

@ -6,11 +6,12 @@ pub(crate) fn lemmy_context() -> Result<Vec<AnyBase>, LemmyError> {
let context_ext = AnyBase::from_arbitrary_json(json!(
{
"sc": "http://schema.org#",
"category": "sc:category",
"sensitive": "as:sensitive",
"stickied": "as:stickied",
"comments_enabled": {
"kind": "sc:Boolean",
"id": "pt:commentsEnabled"
"kind": "sc:Boolean",
"id": "pt:commentsEnabled"
}
}))?;
Ok(vec![AnyBase::from(context()), context_ext])

View file

@ -1,19 +1,41 @@
use activitystreams::unparsed::UnparsedMutExt;
use activitystreams_ext::UnparsedExtension;
use diesel::PgConnection;
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::category::Category;
use lemmy_utils::LemmyError;
use serde::{Deserialize, Serialize};
/// Activitystreams extension to allow (de)serializing additional Community field
/// Activitystreams extension to allow (de)serializing additional Community fields `category` and
/// `sensitive` (called 'nsfw' in Lemmy).
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupExtension {
pub category: Option<GroupCategory>,
pub sensitive: Option<bool>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupCategory {
// Using a string because that's how Peertube does it.
pub identifier: String,
pub name: String,
}
impl GroupExtension {
pub fn new(sensitive: bool) -> Result<GroupExtension, LemmyError> {
pub fn new(
conn: &PgConnection,
category_id: i32,
sensitive: bool,
) -> Result<GroupExtension, LemmyError> {
let category = Category::read(conn, category_id)?;
let group_category = GroupCategory {
identifier: category_id.to_string(),
name: category.name,
};
Ok(GroupExtension {
category: Some(group_category),
sensitive: Some(sensitive),
})
}
@ -27,11 +49,13 @@ where
fn try_from_unparsed(unparsed_mut: &mut U) -> Result<Self, Self::Error> {
Ok(GroupExtension {
category: unparsed_mut.remove("category")?,
sensitive: unparsed_mut.remove("sensitive")?,
})
}
fn try_into_unparsed(self, unparsed_mut: &mut U) -> Result<(), Self::Error> {
unparsed_mut.insert("category", self.category)?;
unparsed_mut.insert("sensitive", self.sensitive)?;
Ok(())
}

View file

@ -95,7 +95,7 @@ pub(crate) fn verify_signature(
}
}
/// Extension for actor public key, which is needed on person and community for HTTP signatures.
/// Extension for actor public key, which is needed on user and community for HTTP signatures.
///
/// Taken from: https://docs.rs/activitystreams/0.5.0-alpha.17/activitystreams/ext/index.html
#[derive(Clone, Debug, Deserialize, Serialize)]

View file

@ -1,11 +1,11 @@
use crate::{
fetcher::{
fetch::fetch_remote_object,
get_or_fetch_and_upsert_person,
get_or_fetch_and_upsert_user,
is_deleted,
should_refetch_actor,
},
inbox::person_inbox::receive_announce,
inbox::user_inbox::receive_announce,
objects::FromApub,
GroupExt,
};
@ -16,9 +16,9 @@ use activitystreams::{
};
use anyhow::Context;
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{source::community::Community_, ApubObject, Joinable};
use lemmy_db_schema::source::community::{Community, CommunityModerator, CommunityModeratorForm};
use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug;
@ -92,7 +92,7 @@ async fn fetch_remote_community(
let mut creator_and_moderators = Vec::new();
for uri in creator_and_moderator_uris {
let c_or_m = get_or_fetch_and_upsert_person(uri, context, recursion_counter).await?;
let c_or_m = get_or_fetch_and_upsert_user(uri, context, recursion_counter).await?;
creator_and_moderators.push(c_or_m);
}
@ -104,7 +104,7 @@ async fn fetch_remote_community(
for mod_ in creator_and_moderators {
let community_moderator_form = CommunityModeratorForm {
community_id,
person_id: mod_.id,
user_id: mod_.id,
};
CommunityModerator::join(conn, &community_moderator_form)?;

View file

@ -1,14 +1,14 @@
pub(crate) mod community;
mod fetch;
pub(crate) mod objects;
pub(crate) mod person;
pub mod search;
pub(crate) mod user;
use crate::{
fetcher::{
community::get_or_fetch_and_upsert_community,
fetch::FetchError,
person::get_or_fetch_and_upsert_person,
user::get_or_fetch_and_upsert_user,
},
ActorType,
};
@ -37,8 +37,8 @@ where
false
}
/// Get a remote actor from its apub ID (either a person or a community). Thin wrapper around
/// `get_or_fetch_and_upsert_person()` and `get_or_fetch_and_upsert_community()`.
/// Get a remote actor from its apub ID (either a user or a community). Thin wrapper around
/// `get_or_fetch_and_upsert_user()` and `get_or_fetch_and_upsert_community()`.
///
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
/// Otherwise it is fetched from the remote instance, stored and returned.
@ -50,7 +50,7 @@ pub(crate) async fn get_or_fetch_and_upsert_actor(
let community = get_or_fetch_and_upsert_community(apub_id, context, recursion_counter).await;
let actor: Box<dyn ActorType> = match community {
Ok(c) => Box::new(c),
Err(_) => Box::new(get_or_fetch_and_upsert_person(apub_id, context, recursion_counter).await?),
Err(_) => Box::new(get_or_fetch_and_upsert_user(apub_id, context, recursion_counter).await?),
};
Ok(actor)
}

View file

@ -1,9 +1,9 @@
use crate::{fetcher::fetch::fetch_remote_object, objects::FromApub, NoteExt, PageExt};
use anyhow::anyhow;
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{ApubObject, Crud};
use lemmy_db_schema::source::{comment::Comment, post::Post};
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use log::debug;

View file

@ -2,7 +2,7 @@ use crate::{
fetcher::{
fetch::fetch_remote_object,
get_or_fetch_and_upsert_community,
get_or_fetch_and_upsert_person,
get_or_fetch_and_upsert_user,
is_deleted,
},
find_object_by_id,
@ -15,34 +15,34 @@ use crate::{
};
use activitystreams::base::BaseExt;
use anyhow::{anyhow, Context};
use lemmy_api_structs::{blocking, site::SearchResponse};
use lemmy_db_queries::{
source::{
comment::Comment_,
community::Community_,
person::Person_,
post::Post_,
private_message::PrivateMessage_,
user::User,
},
SearchType,
};
use lemmy_db_schema::source::{
comment::Comment,
community::Community,
person::Person,
post::Post,
private_message::PrivateMessage,
user::User_,
};
use lemmy_db_views::{comment_view::CommentView, post_view::PostView};
use lemmy_db_views_actor::{community_view::CommunityView, person_view::PersonViewSafe};
use lemmy_utils::{settings::structs::Settings, LemmyError};
use lemmy_db_views_actor::{community_view::CommunityView, user_view::UserViewSafe};
use lemmy_structs::{blocking, site::SearchResponse};
use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url;
/// The types of ActivityPub objects that can be fetched directly by searching for their ID.
#[derive(serde::Deserialize, Debug)]
#[serde(untagged)]
#[derive(serde::Deserialize, Debug)]
enum SearchAcceptedObjects {
Person(Box<PersonExt>),
Group(Box<GroupExt>),
@ -66,7 +66,7 @@ pub async fn search_by_apub_id(
debug!("Search for {}", query);
let split = query.split('@').collect::<Vec<&str>>();
// Person type will look like ['', username, instance]
// User type will look like ['', username, instance]
// Community will look like [!community, instance]
let (name, instance) = if split.len() == 3 {
(format!("/u/{}", split[1]), split[2])
@ -122,13 +122,13 @@ async fn build_response(
match fetch_response {
SearchAcceptedObjects::Person(p) => {
let person_uri = p.inner.id(domain)?.context("person has no id")?;
let user_uri = p.inner.id(domain)?.context("person has no id")?;
let person = get_or_fetch_and_upsert_person(&person_uri, context, recursion_counter).await?;
let user = get_or_fetch_and_upsert_user(&user_uri, context, recursion_counter).await?;
response.users = vec![
blocking(context.pool(), move |conn| {
PersonViewSafe::read(conn, person.id)
UserViewSafe::read(conn, user.id)
})
.await??,
];
@ -182,10 +182,10 @@ async fn delete_object_locally(query_url: &Url, context: &LemmyContext) -> Resul
})
.await??;
}
Object::Person(u) => {
Object::User(u) => {
// TODO: implement update_deleted() for user, move it to ApubObject trait
blocking(context.pool(), move |conn| {
Person::delete_account(conn, u.id)
User_::delete_account(conn, u.id)
})
.await??;
}

View file

@ -5,68 +5,66 @@ use crate::{
};
use anyhow::anyhow;
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{source::person::Person_, ApubObject};
use lemmy_db_schema::source::person::Person;
use lemmy_db_queries::{source::user::User, ApubObject};
use lemmy_db_schema::source::user::User_;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use log::debug;
use url::Url;
/// Get a person from its apub ID.
/// Get a user from its apub ID.
///
/// If it exists locally and `!should_refetch_actor()`, it is returned directly from the database.
/// Otherwise it is fetched from the remote instance, stored and returned.
pub(crate) async fn get_or_fetch_and_upsert_person(
pub(crate) async fn get_or_fetch_and_upsert_user(
apub_id: &Url,
context: &LemmyContext,
recursion_counter: &mut i32,
) -> Result<Person, LemmyError> {
) -> Result<User_, LemmyError> {
let apub_id_owned = apub_id.to_owned();
let person = blocking(context.pool(), move |conn| {
Person::read_from_apub_id(conn, &apub_id_owned.into())
let user = blocking(context.pool(), move |conn| {
User_::read_from_apub_id(conn, &apub_id_owned.into())
})
.await?;
match person {
match user {
// If its older than a day, re-fetch it
Ok(u) if !u.local && should_refetch_actor(u.last_refreshed_at) => {
debug!("Fetching and updating from remote person: {}", apub_id);
debug!("Fetching and updating from remote user: {}", apub_id);
let person =
fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await;
if is_deleted(&person) {
// TODO: use Person::update_deleted() once implemented
// TODO: use User_::update_deleted() once implemented
blocking(context.pool(), move |conn| {
Person::delete_account(conn, u.id)
User_::delete_account(conn, u.id)
})
.await??;
return Err(anyhow!("Person was deleted by remote instance").into());
return Err(anyhow!("User was deleted by remote instance").into());
} else if person.is_err() {
return Ok(u);
}
let person =
Person::from_apub(&person?, context, apub_id.to_owned(), recursion_counter).await?;
let user = User_::from_apub(&person?, context, apub_id.to_owned(), recursion_counter).await?;
let person_id = person.id;
let user_id = user.id;
blocking(context.pool(), move |conn| {
Person::mark_as_updated(conn, person_id)
User_::mark_as_updated(conn, user_id)
})
.await??;
Ok(person)
Ok(user)
}
Ok(u) => Ok(u),
Err(NotFound {}) => {
debug!("Fetching and creating remote person: {}", apub_id);
debug!("Fetching and creating remote user: {}", apub_id);
let person =
fetch_remote_object::<PersonExt>(context.client(), apub_id, recursion_counter).await?;
let person =
Person::from_apub(&person, context, apub_id.to_owned(), recursion_counter).await?;
let user = User_::from_apub(&person, context, apub_id.to_owned(), recursion_counter).await?;
Ok(person)
Ok(user)
}
Err(e) => Err(e.into()),
}

View file

@ -4,9 +4,9 @@ use crate::{
};
use actix_web::{body::Body, web, web::Path, HttpResponse};
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::Crud;
use lemmy_db_schema::{source::comment::Comment, CommentId};
use lemmy_db_schema::source::comment::Comment;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;
@ -21,7 +21,7 @@ pub async fn get_apub_comment(
info: Path<CommentQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let id = CommentId(info.comment_id.parse::<i32>()?);
let id = info.comment_id.parse::<i32>()?;
let comment = blocking(context.pool(), move |conn| Comment::read(conn, id)).await??;
if !comment.local {
return Err(NotFound.into());

View file

@ -9,10 +9,10 @@ use activitystreams::{
collection::{CollectionExt, OrderedCollection, UnorderedCollection},
};
use actix_web::{body::Body, web, HttpResponse};
use lemmy_api_structs::blocking;
use lemmy_db_queries::source::{activity::Activity_, community::Community_};
use lemmy_db_schema::source::{activity::Activity, community::Community};
use lemmy_db_views_actor::community_follower_view::CommunityFollowerView;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;

View file

@ -1,18 +1,17 @@
use crate::APUB_JSON_CONTENT_TYPE;
use actix_web::{body::Body, web, HttpResponse};
use http::StatusCode;
use lemmy_api_structs::blocking;
use lemmy_db_queries::source::activity::Activity_;
use lemmy_db_schema::source::activity::Activity;
use lemmy_utils::{settings::structs::Settings, LemmyError};
use lemmy_structs::blocking;
use lemmy_utils::{settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
use url::Url;
pub mod comment;
pub mod community;
pub mod person;
pub mod post;
pub mod user;
/// Convert the data to json and turn it into an HTTP Response with the correct ActivityPub
/// headers.
@ -47,13 +46,12 @@ pub async fn get_activity(
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let settings = Settings::get();
let activity_id = Url::parse(&format!(
let activity_id = format!(
"{}/activities/{}/{}",
settings.get_protocol_and_hostname(),
info.type_,
info.id
))?
.into();
);
let activity = blocking(context.pool(), move |conn| {
Activity::read_from_apub_id(&conn, &activity_id)
})

View file

@ -4,9 +4,9 @@ use crate::{
};
use actix_web::{body::Body, web, HttpResponse};
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::Crud;
use lemmy_db_schema::{source::post::Post, PostId};
use lemmy_db_schema::source::post::Post;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;
@ -21,7 +21,7 @@ pub async fn get_apub_post(
info: web::Path<PostQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let id = PostId(info.post_id.parse::<i32>()?);
let id = info.post_id.parse::<i32>()?;
let post = blocking(context.pool(), move |conn| Post::read(conn, id)).await??;
if !post.local {
return Err(NotFound.into());

View file

@ -9,70 +9,70 @@ use activitystreams::{
collection::{CollectionExt, OrderedCollection},
};
use actix_web::{body::Body, web, HttpResponse};
use lemmy_api_structs::blocking;
use lemmy_db_queries::source::person::Person_;
use lemmy_db_schema::source::person::Person;
use lemmy_db_queries::source::user::User;
use lemmy_db_schema::source::user::User_;
use lemmy_structs::blocking;
use lemmy_utils::LemmyError;
use lemmy_websocket::LemmyContext;
use serde::Deserialize;
use url::Url;
#[derive(Deserialize)]
pub struct PersonQuery {
pub struct UserQuery {
user_name: String,
}
/// Return the ActivityPub json representation of a local person over HTTP.
pub async fn get_apub_person_http(
info: web::Path<PersonQuery>,
/// Return the ActivityPub json representation of a local user over HTTP.
pub async fn get_apub_user_http(
info: web::Path<UserQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let user_name = info.into_inner().user_name;
// TODO: this needs to be able to read deleted persons, so that it can send tombstones
let person = blocking(context.pool(), move |conn| {
Person::find_by_name(conn, &user_name)
// TODO: this needs to be able to read deleted users, so that it can send tombstones
let user = blocking(context.pool(), move |conn| {
User_::find_by_email_or_username(conn, &user_name)
})
.await??;
if !person.deleted {
let apub = person.to_apub(context.pool()).await?;
if !user.deleted {
let apub = user.to_apub(context.pool()).await?;
Ok(create_apub_response(&apub))
} else {
Ok(create_apub_tombstone_response(&person.to_tombstone()?))
Ok(create_apub_tombstone_response(&user.to_tombstone()?))
}
}
pub async fn get_apub_person_outbox(
info: web::Path<PersonQuery>,
pub async fn get_apub_user_outbox(
info: web::Path<UserQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let person = blocking(context.pool(), move |conn| {
Person::find_by_name(&conn, &info.user_name)
let user = blocking(context.pool(), move |conn| {
User_::read_from_name(&conn, &info.user_name)
})
.await??;
// TODO: populate the person outbox
// TODO: populate the user outbox
let mut collection = OrderedCollection::new();
collection
.set_many_items(Vec::<Url>::new())
.set_many_contexts(lemmy_context()?)
.set_id(person.get_outbox_url()?)
.set_id(user.get_outbox_url()?)
.set_total_items(0_u64);
Ok(create_apub_response(&collection))
}
pub async fn get_apub_person_inbox(
info: web::Path<PersonQuery>,
pub async fn get_apub_user_inbox(
info: web::Path<UserQuery>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse<Body>, LemmyError> {
let person = blocking(context.pool(), move |conn| {
Person::find_by_name(&conn, &info.user_name)
let user = blocking(context.pool(), move |conn| {
User_::read_from_name(&conn, &info.user_name)
})
.await??;
let mut collection = OrderedCollection::new();
collection
.set_id(format!("{}/inbox", person.actor_id.into_inner()).parse()?)
.set_id(format!("{}/inbox", user.actor_id.into_inner()).parse()?)
.set_many_contexts(lemmy_context()?);
Ok(create_apub_response(&collection))
}

View file

@ -26,16 +26,13 @@ use activitystreams::{
};
use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::{anyhow, Context};
use lemmy_api_structs::blocking;
use lemmy_db_queries::{source::community::Community_, ApubObject, DbPool, Followable};
use lemmy_db_schema::{
source::{
community::{Community, CommunityFollower, CommunityFollowerForm},
person::Person,
},
CommunityId,
use lemmy_db_schema::source::{
community::{Community, CommunityFollower, CommunityFollowerForm},
user::User_,
};
use lemmy_db_views_actor::community_person_ban_view::CommunityPersonBanView;
use lemmy_db_views_actor::community_user_ban_view::CommunityUserBanView;
use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::info;
@ -47,8 +44,8 @@ use url::Url;
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub enum CommunityValidTypes {
Follow, // follow request from a person
Undo, // unfollow from a person
Follow, // follow request from a user
Undo, // unfollow from a user
Create, // create post or comment
Update, // update post or comment
Like, // upvote post or comment
@ -116,21 +113,21 @@ pub(crate) async fn community_receive_message(
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<HttpResponse, LemmyError> {
// Only persons can send activities to the community, so we can get the actor as person
// Only users can send activities to the community, so we can get the actor as user
// unconditionally.
let actor_id = actor.actor_id();
let person = blocking(&context.pool(), move |conn| {
Person::read_from_apub_id(&conn, &actor_id.into())
let user = blocking(&context.pool(), move |conn| {
User_::read_from_apub_id(&conn, &actor_id.into())
})
.await??;
check_community_or_site_ban(&person, to_community.id, context.pool()).await?;
check_community_or_site_ban(&user, &to_community, context.pool()).await?;
let any_base = activity.clone().into_any_base()?;
let actor_url = actor.actor_id();
let activity_kind = activity.kind().context(location_info!())?;
let do_announce = match activity_kind {
CommunityValidTypes::Follow => {
handle_follow(any_base.clone(), person, &to_community, &context).await?;
handle_follow(any_base.clone(), user, &to_community, &context).await?;
false
}
CommunityValidTypes::Undo => {
@ -165,7 +162,7 @@ pub(crate) async fn community_receive_message(
}
CommunityValidTypes::Remove => {
// TODO: we dont support remote mods, so this is ignored for now
//receive_remove_for_community(context, any_base.clone(), &person_url).await?
//receive_remove_for_community(context, any_base.clone(), &user_url).await?
false
}
};
@ -181,20 +178,20 @@ pub(crate) async fn community_receive_message(
Ok(HttpResponse::Ok().finish())
}
/// Handle a follow request from a remote person, adding the person as follower and returning an
/// Handle a follow request from a remote user, adding the user as follower and returning an
/// Accept activity.
async fn handle_follow(
activity: AnyBase,
person: Person,
user: User_,
community: &Community,
context: &LemmyContext,
) -> Result<HttpResponse, LemmyError> {
let follow = Follow::from_any_base(activity)?.context(location_info!())?;
verify_activity_domains_valid(&follow, &person.actor_id(), false)?;
verify_activity_domains_valid(&follow, &user.actor_id(), false)?;
let community_follower_form = CommunityFollowerForm {
community_id: community.id,
person_id: person.id,
user_id: user.id,
pending: false,
};
@ -229,27 +226,27 @@ async fn handle_undo(
}
}
/// Handle `Undo/Follow` from a person, removing the person from followers list.
/// Handle `Undo/Follow` from a user, removing the user from followers list.
async fn handle_undo_follow(
activity: AnyBase,
person_url: Url,
user_url: Url,
community: &Community,
context: &LemmyContext,
) -> Result<(), LemmyError> {
let undo = Undo::from_any_base(activity)?.context(location_info!())?;
verify_activity_domains_valid(&undo, &person_url, true)?;
verify_activity_domains_valid(&undo, &user_url, true)?;
let object = undo.object().to_owned().one().context(location_info!())?;
let follow = Follow::from_any_base(object)?.context(location_info!())?;
verify_activity_domains_valid(&follow, &person_url, false)?;
verify_activity_domains_valid(&follow, &user_url, false)?;
let person = blocking(&context.pool(), move |conn| {
Person::read_from_apub_id(&conn, &person_url.into())
let user = blocking(&context.pool(), move |conn| {
User_::read_from_apub_id(&conn, &user_url.into())
})
.await??;
let community_follower_form = CommunityFollowerForm {
community_id: community.id,
person_id: person.id,
user_id: user.id,
pending: false,
};
@ -263,18 +260,18 @@ async fn handle_undo_follow(
}
pub(crate) async fn check_community_or_site_ban(
person: &Person,
community_id: CommunityId,
user: &User_,
community: &Community,
pool: &DbPool,
) -> Result<(), LemmyError> {
if person.banned {
return Err(anyhow!("Person is banned from site").into());
if user.banned {
return Err(anyhow!("User is banned from site").into());
}
let person_id = person.id;
let is_banned =
move |conn: &'_ _| CommunityPersonBanView::get(conn, person_id, community_id).is_ok();
let user_id = user.id;
let community_id = community.id;
let is_banned = move |conn: &'_ _| CommunityUserBanView::get(conn, user_id, community_id).is_ok();
if blocking(pool, is_banned).await? {
return Err(anyhow!("Person is banned from community").into());
return Err(anyhow!("User is banned from community").into());
}
Ok(())

View file

@ -12,23 +12,23 @@ use activitystreams::{
};
use actix_web::HttpRequest;
use anyhow::{anyhow, Context};
use lemmy_api_structs::blocking;
use lemmy_db_queries::{
source::{activity::Activity_, community::Community_},
ApubObject,
DbPool,
};
use lemmy_db_schema::source::{activity::Activity, community::Community, person::Person};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_db_schema::source::{activity::Activity, community::Community, user::User_};
use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::Serialize;
use std::fmt::Debug;
use url::Url;
pub mod community_inbox;
pub mod person_inbox;
mod receive_for_community;
pub mod shared_inbox;
pub mod user_inbox;
pub(crate) fn get_activity_id<T, Kind>(activity: &T, creator_uri: &Url) -> Result<Url, LemmyError>
where
@ -45,7 +45,7 @@ pub(crate) async fn is_activity_already_known(
pool: &DbPool,
activity_id: &Url,
) -> Result<bool, LemmyError> {
let activity_id = activity_id.to_owned().into();
let activity_id = activity_id.to_string();
let existing = blocking(pool, move |conn| {
Activity::read_from_apub_id(&conn, &activity_id)
})
@ -119,17 +119,17 @@ where
}
/// Returns true if `to_and_cc` contains at least one local user.
pub(crate) async fn is_addressed_to_local_person(
pub(crate) async fn is_addressed_to_local_user(
to_and_cc: &[Url],
pool: &DbPool,
) -> Result<bool, LemmyError> {
for url in to_and_cc {
let url = url.to_owned();
let person = blocking(&pool, move |conn| {
Person::read_from_apub_id(&conn, &url.into())
let user = blocking(&pool, move |conn| {
User_::read_from_apub_id(&conn, &url.into())
})
.await?;
if let Ok(u) = person {
if let Ok(u) = user {
if u.local {
return Ok(true);
}
@ -167,7 +167,7 @@ where
let id = activity.id_unchecked().context(location_info!())?;
let activity_domain = id.domain().context(location_info!())?;
if activity_domain == Settings::get().hostname() {
if activity_domain == Settings::get().hostname {
return Err(
anyhow!(
"Error: received activity which was sent by local instance: {:?}",

View file

@ -43,22 +43,15 @@ use activitystreams::{
};
use anyhow::Context;
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::Crud;
use lemmy_db_schema::source::site::Site;
use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use strum_macros::EnumString;
use url::Url;
#[derive(EnumString)]
enum PageOrNote {
Page,
Note,
}
/// This file is for post/comment activities received by the community, and for post/comment
/// activities announced by the community and received by the person.
/// activities announced by the community and received by the user.
/// A post or comment being created
pub(in crate::inbox) async fn receive_create_for_community(
@ -71,13 +64,9 @@ pub(in crate::inbox) async fn receive_create_for_community(
verify_activity_domains_valid(&create, &expected_domain, true)?;
is_addressed_to_public(&create)?;
let kind = create
.object()
.as_single_kind_str()
.and_then(|s| s.parse().ok());
match kind {
Some(PageOrNote::Page) => receive_create_post(create, context, request_counter).await,
Some(PageOrNote::Note) => receive_create_comment(create, context, request_counter).await,
match create.object().as_single_kind_str() {
Some("Page") => receive_create_post(create, context, request_counter).await,
Some("Note") => receive_create_comment(create, context, request_counter).await,
_ => receive_unhandled_activity(create),
}
}
@ -93,13 +82,9 @@ pub(in crate::inbox) async fn receive_update_for_community(
verify_activity_domains_valid(&update, &expected_domain, true)?;
is_addressed_to_public(&update)?;
let kind = update
.object()
.as_single_kind_str()
.and_then(|s| s.parse().ok());
match kind {
Some(PageOrNote::Page) => receive_update_post(update, context, request_counter).await,
Some(PageOrNote::Note) => receive_update_comment(update, context, request_counter).await,
match update.object().as_single_kind_str() {
Some("Page") => receive_update_post(update, context, request_counter).await,
Some("Note") => receive_update_comment(update, context, request_counter).await,
_ => receive_unhandled_activity(update),
}
}
@ -115,14 +100,11 @@ pub(in crate::inbox) async fn receive_like_for_community(
verify_activity_domains_valid(&like, &expected_domain, false)?;
is_addressed_to_public(&like)?;
let object_id = like
.object()
.as_single_xsd_any_uri()
.context(location_info!())?;
let object_id = get_like_object_id(&like)?;
match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
PostOrComment::Post(post) => receive_like_post(like, *post, context, request_counter).await,
PostOrComment::Post(post) => receive_like_post(like, post, context, request_counter).await,
PostOrComment::Comment(comment) => {
receive_like_comment(like, *comment, context, request_counter).await
receive_like_comment(like, comment, context, request_counter).await
}
}
}
@ -146,16 +128,13 @@ pub(in crate::inbox) async fn receive_dislike_for_community(
verify_activity_domains_valid(&dislike, &expected_domain, false)?;
is_addressed_to_public(&dislike)?;
let object_id = dislike
.object()
.as_single_xsd_any_uri()
.context(location_info!())?;
let object_id = get_like_object_id(&dislike)?;
match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
PostOrComment::Post(post) => {
receive_dislike_post(dislike, *post, context, request_counter).await
receive_dislike_post(dislike, post, context, request_counter).await
}
PostOrComment::Comment(comment) => {
receive_dislike_comment(dislike, *comment, context, request_counter).await
receive_dislike_comment(dislike, comment, context, request_counter).await
}
}
}
@ -177,8 +156,8 @@ pub(in crate::inbox) async fn receive_delete_for_community(
.context(location_info!())?;
match find_post_or_comment_by_id(context, object).await {
Ok(PostOrComment::Post(p)) => receive_delete_post(context, *p).await,
Ok(PostOrComment::Comment(c)) => receive_delete_comment(context, *c).await,
Ok(PostOrComment::Post(p)) => receive_delete_post(context, p).await,
Ok(PostOrComment::Comment(c)) => receive_delete_comment(context, c).await,
// if we dont have the object, no need to do anything
Err(_) => Ok(()),
}
@ -215,21 +194,13 @@ pub(in crate::inbox) async fn receive_remove_for_community(
remove.id(community_id.domain().context(location_info!())?)?;
match find_post_or_comment_by_id(context, object).await {
Ok(PostOrComment::Post(p)) => receive_remove_post(context, remove, *p).await,
Ok(PostOrComment::Comment(c)) => receive_remove_comment(context, remove, *c).await,
Ok(PostOrComment::Post(p)) => receive_remove_post(context, remove, p).await,
Ok(PostOrComment::Comment(c)) => receive_remove_comment(context, remove, c).await,
// if we dont have the object, no need to do anything
Err(_) => Ok(()),
}
}
#[derive(EnumString)]
enum UndoableActivities {
Delete,
Remove,
Like,
Dislike,
}
/// A post/comment action being reverted (either a delete, remove, upvote or downvote)
pub(in crate::inbox) async fn receive_undo_for_community(
context: &LemmyContext,
@ -241,18 +212,13 @@ pub(in crate::inbox) async fn receive_undo_for_community(
verify_activity_domains_valid(&undo, &expected_domain.to_owned(), true)?;
is_addressed_to_public(&undo)?;
use UndoableActivities::*;
match undo
.object()
.as_single_kind_str()
.and_then(|s| s.parse().ok())
{
Some(Delete) => receive_undo_delete_for_community(context, undo, expected_domain).await,
Some(Remove) => receive_undo_remove_for_community(context, undo, expected_domain).await,
Some(Like) => {
match undo.object().as_single_kind_str() {
Some("Delete") => receive_undo_delete_for_community(context, undo, expected_domain).await,
Some("Remove") => receive_undo_remove_for_community(context, undo, expected_domain).await,
Some("Like") => {
receive_undo_like_for_community(context, undo, expected_domain, request_counter).await
}
Some(Dislike) => {
Some("Dislike") => {
receive_undo_dislike_for_community(context, undo, expected_domain, request_counter).await
}
_ => receive_unhandled_activity(undo),
@ -276,8 +242,8 @@ pub(in crate::inbox) async fn receive_undo_delete_for_community(
.single_xsd_any_uri()
.context(location_info!())?;
match find_post_or_comment_by_id(context, object).await {
Ok(PostOrComment::Post(p)) => receive_undo_delete_post(context, *p).await,
Ok(PostOrComment::Comment(c)) => receive_undo_delete_comment(context, *c).await,
Ok(PostOrComment::Post(p)) => receive_undo_delete_post(context, p).await,
Ok(PostOrComment::Comment(c)) => receive_undo_delete_comment(context, c).await,
// if we dont have the object, no need to do anything
Err(_) => Ok(()),
}
@ -300,8 +266,8 @@ pub(in crate::inbox) async fn receive_undo_remove_for_community(
.single_xsd_any_uri()
.context(location_info!())?;
match find_post_or_comment_by_id(context, object).await {
Ok(PostOrComment::Post(p)) => receive_undo_remove_post(context, *p).await,
Ok(PostOrComment::Comment(c)) => receive_undo_remove_comment(context, *c).await,
Ok(PostOrComment::Post(p)) => receive_undo_remove_post(context, p).await,
Ok(PostOrComment::Comment(c)) => receive_undo_remove_comment(context, c).await,
// if we dont have the object, no need to do anything
Err(_) => Ok(()),
}
@ -319,16 +285,13 @@ pub(in crate::inbox) async fn receive_undo_like_for_community(
verify_activity_domains_valid(&like, &expected_domain, false)?;
is_addressed_to_public(&like)?;
let object_id = like
.object()
.as_single_xsd_any_uri()
.context(location_info!())?;
let object_id = get_like_object_id(&like)?;
match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
PostOrComment::Post(post) => {
receive_undo_like_post(&like, *post, context, request_counter).await
receive_undo_like_post(&like, post, context, request_counter).await
}
PostOrComment::Comment(comment) => {
receive_undo_like_comment(&like, *comment, context, request_counter).await
receive_undo_like_comment(&like, comment, context, request_counter).await
}
}
}
@ -345,16 +308,13 @@ pub(in crate::inbox) async fn receive_undo_dislike_for_community(
verify_activity_domains_valid(&dislike, &expected_domain, false)?;
is_addressed_to_public(&dislike)?;
let object_id = dislike
.object()
.as_single_xsd_any_uri()
.context(location_info!())?;
let object_id = get_like_object_id(&dislike)?;
match fetch_post_or_comment_by_id(&object_id, context, request_counter).await? {
PostOrComment::Post(post) => {
receive_undo_dislike_post(&dislike, *post, context, request_counter).await
receive_undo_dislike_post(&dislike, post, context, request_counter).await
}
PostOrComment::Comment(comment) => {
receive_undo_dislike_comment(&dislike, *comment, context, request_counter).await
receive_undo_dislike_comment(&dislike, comment, context, request_counter).await
}
}
}
@ -365,12 +325,35 @@ async fn fetch_post_or_comment_by_id(
request_counter: &mut i32,
) -> Result<PostOrComment, LemmyError> {
if let Ok(post) = get_or_fetch_and_insert_post(apub_id, context, request_counter).await {
return Ok(PostOrComment::Post(Box::new(post)));
return Ok(PostOrComment::Post(post));
}
if let Ok(comment) = get_or_fetch_and_insert_comment(apub_id, context, request_counter).await {
return Ok(PostOrComment::Comment(Box::new(comment)));
return Ok(PostOrComment::Comment(comment));
}
Err(NotFound.into())
}
fn get_like_object_id<Activity>(like_or_dislike: &Activity) -> Result<Url, LemmyError>
where
Activity: ActorAndObjectRefExt,
{
// TODO: For backwards compatibility with older Lemmy versions where like.object contains a full
// post/comment. This can be removed after some time, using
// `activity.oject().as_single_xsd_any_uri()` instead.
let object = like_or_dislike.object();
if let Some(xsd_uri) = object.as_single_xsd_any_uri() {
Ok(xsd_uri.to_owned())
} else {
Ok(
object
.to_owned()
.one()
.context(location_info!())?
.id()
.context(location_info!())?
.to_owned(),
)
}
}

View file

@ -7,17 +7,17 @@ use crate::{
inbox_verify_http_signature,
is_activity_already_known,
is_addressed_to_community_followers,
is_addressed_to_local_person,
person_inbox::{person_receive_message, PersonAcceptedActivities},
is_addressed_to_local_user,
user_inbox::{user_receive_message, UserAcceptedActivities},
},
insert_activity,
};
use activitystreams::{activity::ActorAndObject, prelude::*};
use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::Context;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{ApubObject, DbPool};
use lemmy_db_schema::source::community::Community;
use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::{Deserialize, Serialize};
@ -69,9 +69,9 @@ pub async fn shared_inbox(
let mut res: Option<HttpResponse> = None;
let to_and_cc = get_activity_to_and_cc(&activity);
// Handle community first, so in case the sender is banned by the community, it will error out.
// If we handled the person receive first, the activity would be inserted to the database before the
// If we handled the user receive first, the activity would be inserted to the database before the
// community could check for bans.
// Note that an activity can be addressed to a community and to a person (or multiple persons) at the
// Note that an activity can be addressed to a community and to a user (or multiple users) at the
// same time. In this case we still only handle it once, to avoid duplicate websocket
// notifications.
let community = extract_local_community_from_destinations(&to_and_cc, context.pool()).await?;
@ -88,13 +88,13 @@ pub async fn shared_inbox(
)
.await?,
);
} else if is_addressed_to_local_person(&to_and_cc, context.pool()).await? {
let person_activity = PersonAcceptedActivities::from_any_base(activity_any_base.clone())?
} else if is_addressed_to_local_user(&to_and_cc, context.pool()).await? {
let user_activity = UserAcceptedActivities::from_any_base(activity_any_base.clone())?
.context(location_info!())?;
// `to_person` is only used for follow activities (which we dont receive here), so no need to pass
// `to_user` is only used for follow activities (which we dont receive here), so no need to pass
// it in
person_receive_message(
person_activity,
user_receive_message(
user_activity,
None,
actor.as_ref(),
&context,
@ -105,11 +105,11 @@ pub async fn shared_inbox(
.await?
.is_some()
{
let person_activity = PersonAcceptedActivities::from_any_base(activity_any_base.clone())?
let user_activity = UserAcceptedActivities::from_any_base(activity_any_base.clone())?
.context(location_info!())?;
res = Some(
person_receive_message(
person_activity,
user_receive_message(
user_activity,
None,
actor.as_ref(),
&context,

View file

@ -25,7 +25,7 @@ use crate::{
inbox_verify_http_signature,
is_activity_already_known,
is_addressed_to_community_followers,
is_addressed_to_local_person,
is_addressed_to_local_user,
is_addressed_to_public,
receive_for_community::{
receive_create_for_community,
@ -48,25 +48,24 @@ use activitystreams::{
use actix_web::{web, HttpRequest, HttpResponse};
use anyhow::{anyhow, Context};
use diesel::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{source::person::Person_, ApubObject, Followable};
use lemmy_db_queries::{source::user::User, ApubObject, Followable};
use lemmy_db_schema::source::{
community::{Community, CommunityFollower},
person::Person,
private_message::PrivateMessage,
user::User_,
};
use lemmy_structs::blocking;
use lemmy_utils::{location_info, LemmyError};
use lemmy_websocket::LemmyContext;
use log::debug;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use strum_macros::EnumString;
use url::Url;
/// Allowed activities for person inbox.
/// Allowed activities for user inbox.
#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Deserialize, Serialize)]
#[serde(rename_all = "PascalCase")]
pub enum PersonValidTypes {
pub enum UserValidTypes {
Accept, // community accepted our follow request
Create, // create private message
Update, // edit private message
@ -76,12 +75,12 @@ pub enum PersonValidTypes {
Announce, // post, comment or vote in community
}
pub type PersonAcceptedActivities = ActorAndObject<PersonValidTypes>;
pub type UserAcceptedActivities = ActorAndObject<UserValidTypes>;
/// Handler for all incoming activities to person inboxes.
pub async fn person_inbox(
/// Handler for all incoming activities to user inboxes.
pub async fn user_inbox(
request: HttpRequest,
input: web::Json<PersonAcceptedActivities>,
input: web::Json<UserAcceptedActivities>,
path: web::Path<String>,
context: web::Data<LemmyContext>,
) -> Result<HttpResponse, LemmyError> {
@ -98,29 +97,29 @@ pub async fn person_inbox(
// Check if the activity is actually meant for us
let username = path.into_inner();
let person = blocking(&context.pool(), move |conn| {
Person::find_by_name(&conn, &username)
let user = blocking(&context.pool(), move |conn| {
User_::read_from_name(&conn, &username)
})
.await??;
let to_and_cc = get_activity_to_and_cc(&activity);
// TODO: we should also accept activities that are sent to community followers
if !to_and_cc.contains(&&person.actor_id()) {
return Err(anyhow!("Activity delivered to wrong person").into());
if !to_and_cc.contains(&&user.actor_id()) {
return Err(anyhow!("Activity delivered to wrong user").into());
}
assert_activity_not_local(&activity)?;
insert_activity(&activity_id, activity.clone(), false, true, context.pool()).await?;
debug!(
"Person {} received activity {:?} from {}",
person.name,
"User {} received activity {:?} from {}",
user.name,
&activity.id_unchecked(),
&actor.actor_id()
);
person_receive_message(
user_receive_message(
activity.clone(),
Some(person.clone()),
Some(user.clone()),
actor.as_ref(),
&context,
request_counter,
@ -129,43 +128,36 @@ pub async fn person_inbox(
}
/// Receives Accept/Follow, Announce, private messages and community (undo) remove, (undo) delete
pub(crate) async fn person_receive_message(
activity: PersonAcceptedActivities,
to_person: Option<Person>,
pub(crate) async fn user_receive_message(
activity: UserAcceptedActivities,
to_user: Option<User_>,
actor: &dyn ActorType,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<HttpResponse, LemmyError> {
is_for_person_inbox(context, &activity).await?;
is_for_user_inbox(context, &activity).await?;
let any_base = activity.clone().into_any_base()?;
let kind = activity.kind().context(location_info!())?;
let actor_url = actor.actor_id();
match kind {
PersonValidTypes::Accept => {
receive_accept(
&context,
any_base,
actor,
to_person.expect("person provided"),
request_counter,
)
.await?;
UserValidTypes::Accept => {
receive_accept(&context, any_base, actor, to_user.unwrap(), request_counter).await?;
}
PersonValidTypes::Announce => {
UserValidTypes::Announce => {
receive_announce(&context, any_base, actor, request_counter).await?
}
PersonValidTypes::Create => {
UserValidTypes::Create => {
receive_create(&context, any_base, actor_url, request_counter).await?
}
PersonValidTypes::Update => {
UserValidTypes::Update => {
receive_update(&context, any_base, actor_url, request_counter).await?
}
PersonValidTypes::Delete => {
UserValidTypes::Delete => {
receive_delete(context, any_base, &actor_url, request_counter).await?
}
PersonValidTypes::Undo => receive_undo(context, any_base, &actor_url, request_counter).await?,
PersonValidTypes::Remove => receive_remove_community(&context, any_base, &actor_url).await?,
UserValidTypes::Undo => receive_undo(context, any_base, &actor_url, request_counter).await?,
UserValidTypes::Remove => receive_remove_community(&context, any_base, &actor_url).await?,
};
// TODO: would be logical to move websocket notification code here
@ -173,16 +165,16 @@ pub(crate) async fn person_receive_message(
Ok(HttpResponse::Ok().finish())
}
/// Returns true if the activity is addressed directly to one or more local persons, or if it is
/// addressed to the followers collection of a remote community, and at least one local person follows
/// Returns true if the activity is addressed directly to one or more local users, or if it is
/// addressed to the followers collection of a remote community, and at least one local user follows
/// it.
async fn is_for_person_inbox(
async fn is_for_user_inbox(
context: &LemmyContext,
activity: &PersonAcceptedActivities,
activity: &UserAcceptedActivities,
) -> Result<(), LemmyError> {
let to_and_cc = get_activity_to_and_cc(activity);
// Check if it is addressed directly to any local person
if is_addressed_to_local_person(&to_and_cc, context.pool()).await? {
// Check if it is addressed directly to any local user
if is_addressed_to_local_user(&to_and_cc, context.pool()).await? {
return Ok(());
}
@ -205,7 +197,7 @@ async fn is_for_person_inbox(
}
}
Err(anyhow!("Not addressed for any local person").into())
Err(anyhow!("Not addressed for any local user").into())
}
/// Handle accepted follows.
@ -213,7 +205,7 @@ async fn receive_accept(
context: &LemmyContext,
activity: AnyBase,
actor: &dyn ActorType,
person: Person,
user: User_,
request_counter: &mut i32,
) -> Result<(), LemmyError> {
let accept = Accept::from_any_base(activity)?.context(location_info!())?;
@ -221,7 +213,7 @@ async fn receive_accept(
let object = accept.object().to_owned().one().context(location_info!())?;
let follow = Follow::from_any_base(object)?.context(location_info!())?;
verify_activity_domains_valid(&follow, &person.actor_id(), false)?;
verify_activity_domains_valid(&follow, &user.actor_id(), false)?;
let community_uri = accept
.actor()?
@ -233,27 +225,16 @@ async fn receive_accept(
get_or_fetch_and_upsert_community(&community_uri, context, request_counter).await?;
let community_id = community.id;
let person_id = person.id;
let user_id = user.id;
// This will throw an error if no follow was requested
blocking(&context.pool(), move |conn| {
CommunityFollower::follow_accepted(conn, community_id, person_id)
CommunityFollower::follow_accepted(conn, community_id, user_id)
})
.await??;
Ok(())
}
#[derive(EnumString)]
enum AnnouncableActivities {
Create,
Update,
Like,
Dislike,
Delete,
Remove,
Undo,
}
/// Takes an announce and passes the inner activity to the appropriate handler.
pub async fn receive_announce(
context: &LemmyContext,
@ -265,10 +246,7 @@ pub async fn receive_announce(
verify_activity_domains_valid(&announce, &actor.actor_id(), false)?;
is_addressed_to_public(&announce)?;
let kind = announce
.object()
.as_single_kind_str()
.and_then(|s| s.parse().ok());
let kind = announce.object().as_single_kind_str();
let inner_activity = announce
.object()
.to_owned()
@ -281,23 +259,22 @@ pub async fn receive_announce(
return Ok(());
}
use AnnouncableActivities::*;
match kind {
Some(Create) => {
Some("Create") => {
receive_create_for_community(context, inner_activity, &inner_id, request_counter).await
}
Some(Update) => {
Some("Update") => {
receive_update_for_community(context, inner_activity, &inner_id, request_counter).await
}
Some(Like) => {
Some("Like") => {
receive_like_for_community(context, inner_activity, &inner_id, request_counter).await
}
Some(Dislike) => {
Some("Dislike") => {
receive_dislike_for_community(context, inner_activity, &inner_id, request_counter).await
}
Some(Delete) => receive_delete_for_community(context, inner_activity, &inner_id).await,
Some(Remove) => receive_remove_for_community(context, inner_activity, &inner_id).await,
Some(Undo) => {
Some("Delete") => receive_delete_for_community(context, inner_activity, &inner_id).await,
Some("Remove") => receive_remove_for_community(context, inner_activity, &inner_id).await,
Some("Undo") => {
receive_undo_for_community(context, inner_activity, &inner_id, request_counter).await
}
_ => receive_unhandled_activity(inner_activity),

View file

@ -8,7 +8,6 @@ pub mod fetcher;
pub mod http;
pub mod inbox;
pub mod objects;
pub mod routes;
use crate::extensions::{
group_extensions::GroupExtension,
@ -24,20 +23,17 @@ use activitystreams::{
use activitystreams_ext::{Ext1, Ext2};
use anyhow::{anyhow, Context};
use diesel::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{source::activity::Activity_, ApubObject, DbPool};
use lemmy_db_schema::{
source::{
activity::Activity,
comment::Comment,
community::Community,
person::Person as DbPerson,
post::Post,
private_message::PrivateMessage,
},
DbUrl,
use lemmy_db_schema::source::{
activity::Activity,
comment::Comment,
community::Community,
post::Post,
private_message::PrivateMessage,
user::User_,
};
use lemmy_utils::{location_info, settings::structs::Settings, LemmyError};
use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, LemmyError};
use lemmy_websocket::LemmyContext;
use serde::Serialize;
use std::net::IpAddr;
@ -45,7 +41,7 @@ use url::{ParseError, Url};
/// Activitystreams type for community
type GroupExt = Ext2<ApActor<ApObject<Group>>, GroupExtension, PublicKeyExtension>;
/// Activitystreams type for person
/// Activitystreams type for user
type PersonExt = Ext1<ApActor<ApObject<Person>>, PublicKeyExtension>;
/// Activitystreams type for post
type PageExt = Ext1<ApObject<Page>, PageExtension>;
@ -67,7 +63,7 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
let domain = apub_id.domain().context(location_info!())?.to_string();
let local_instance = settings.get_hostname_without_port()?;
if !settings.federation().enabled {
if !settings.federation.enabled {
return if domain == local_instance {
Ok(())
} else {
@ -91,23 +87,22 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
return Err(anyhow!("invalid apub id scheme {}: {}", apub_id.scheme(), apub_id).into());
}
let allowed_instances = Settings::get().get_allowed_instances();
let mut allowed_instances = Settings::get().get_allowed_instances();
let blocked_instances = Settings::get().get_blocked_instances();
if allowed_instances.is_none() && blocked_instances.is_none() {
if allowed_instances.is_empty() && blocked_instances.is_empty() {
Ok(())
} else if let Some(mut allowed) = allowed_instances {
} else if !allowed_instances.is_empty() {
// need to allow this explicitly because apub receive might contain objects from our local
// instance. split is needed to remove the port in our federation test setup.
allowed.push(local_instance);
allowed_instances.push(local_instance);
if allowed.contains(&domain) {
if allowed_instances.contains(&domain) {
Ok(())
} else {
Err(anyhow!("{} not in federation allowlist", domain).into())
}
} else if let Some(blocked) = blocked_instances {
if blocked.contains(&domain) {
} else if !blocked_instances.is_empty() {
if blocked_instances.contains(&domain) {
Err(anyhow!("{} is in federation blocklist", domain).into())
} else {
Ok(())
@ -121,41 +116,27 @@ fn check_is_apub_id_valid(apub_id: &Url) -> Result<(), LemmyError> {
/// and actors in Lemmy.
#[async_trait::async_trait(?Send)]
pub trait ApubObjectType {
async fn send_create(&self, creator: &DbPerson, context: &LemmyContext)
-> Result<(), LemmyError>;
async fn send_update(&self, creator: &DbPerson, context: &LemmyContext)
-> Result<(), LemmyError>;
async fn send_delete(&self, creator: &DbPerson, context: &LemmyContext)
-> Result<(), LemmyError>;
async fn send_create(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_update(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_delete(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_delete(
&self,
creator: &DbPerson,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_remove(&self, mod_: &DbPerson, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_remove(
&self,
mod_: &DbPerson,
creator: &User_,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_remove(&self, mod_: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
}
#[async_trait::async_trait(?Send)]
pub trait ApubLikeableType {
async fn send_like(&self, creator: &DbPerson, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_dislike(
&self,
creator: &DbPerson,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_undo_like(
&self,
creator: &DbPerson,
context: &LemmyContext,
) -> Result<(), LemmyError>;
async fn send_like(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_dislike(&self, creator: &User_, context: &LemmyContext) -> Result<(), LemmyError>;
async fn send_undo_like(&self, creator: &User_, context: &LemmyContext)
-> Result<(), LemmyError>;
}
/// Common methods provided by ActivityPub actors (community and person). Not all methods are
/// Common methods provided by ActivityPub actors (community and user). Not all methods are
/// implemented by all actors.
#[async_trait::async_trait(?Send)]
pub trait ActorType {
@ -223,7 +204,7 @@ pub trait ActorType {
pub enum EndpointType {
Community,
Person,
User,
Post,
Comment,
PrivateMessage,
@ -233,10 +214,10 @@ pub enum EndpointType {
pub fn generate_apub_endpoint(
endpoint_type: EndpointType,
name: &str,
) -> Result<DbUrl, ParseError> {
) -> Result<lemmy_db_schema::Url, ParseError> {
let point = match endpoint_type {
EndpointType::Community => "c",
EndpointType::Person => "u",
EndpointType::User => "u",
EndpointType::Post => "post",
EndpointType::Comment => "comment",
EndpointType::PrivateMessage => "private_message",
@ -253,15 +234,21 @@ pub fn generate_apub_endpoint(
)
}
pub fn generate_followers_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
pub fn generate_followers_url(
actor_id: &lemmy_db_schema::Url,
) -> Result<lemmy_db_schema::Url, ParseError> {
Ok(Url::parse(&format!("{}/followers", actor_id))?.into())
}
pub fn generate_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, ParseError> {
pub fn generate_inbox_url(
actor_id: &lemmy_db_schema::Url,
) -> Result<lemmy_db_schema::Url, ParseError> {
Ok(Url::parse(&format!("{}/inbox", actor_id))?.into())
}
pub fn generate_shared_inbox_url(actor_id: &DbUrl) -> Result<DbUrl, LemmyError> {
pub fn generate_shared_inbox_url(
actor_id: &lemmy_db_schema::Url,
) -> Result<lemmy_db_schema::Url, LemmyError> {
let actor_id = actor_id.clone().into_inner();
let url = format!(
"{}://{}{}/inbox",
@ -288,7 +275,7 @@ pub(crate) async fn insert_activity<T>(
where
T: Serialize + std::fmt::Debug + Send + 'static,
{
let ap_id = ap_id.to_owned().into();
let ap_id = ap_id.to_string();
blocking(pool, move |conn| {
Activity::insert(conn, ap_id, &activity, local, sensitive)
})
@ -297,8 +284,8 @@ where
}
pub(crate) enum PostOrComment {
Comment(Box<Comment>),
Post(Box<Post>),
Comment(Comment),
Post(Post),
}
/// Tries to find a post or comment in the local database, without any network requests.
@ -314,7 +301,7 @@ pub(crate) async fn find_post_or_comment_by_id(
})
.await?;
if let Ok(p) = post {
return Ok(PostOrComment::Post(Box::new(p)));
return Ok(PostOrComment::Post(p));
}
let ap_id = apub_id.clone();
@ -323,18 +310,18 @@ pub(crate) async fn find_post_or_comment_by_id(
})
.await?;
if let Ok(c) = comment {
return Ok(PostOrComment::Comment(Box::new(c)));
return Ok(PostOrComment::Comment(c));
}
Err(NotFound.into())
}
pub(crate) enum Object {
Comment(Box<Comment>),
Post(Box<Post>),
Community(Box<Community>),
Person(Box<DbPerson>),
PrivateMessage(Box<PrivateMessage>),
Comment(Comment),
Post(Post),
Community(Community),
User(User_),
PrivateMessage(PrivateMessage),
}
pub(crate) async fn find_object_by_id(
@ -344,18 +331,18 @@ pub(crate) async fn find_object_by_id(
let ap_id = apub_id.clone();
if let Ok(pc) = find_post_or_comment_by_id(context, ap_id.to_owned()).await {
return Ok(match pc {
PostOrComment::Post(p) => Object::Post(Box::new(*p)),
PostOrComment::Comment(c) => Object::Comment(Box::new(*c)),
PostOrComment::Post(p) => Object::Post(p),
PostOrComment::Comment(c) => Object::Comment(c),
});
}
let ap_id = apub_id.clone();
let person = blocking(context.pool(), move |conn| {
DbPerson::read_from_apub_id(conn, &ap_id.into())
let user = blocking(context.pool(), move |conn| {
User_::read_from_apub_id(conn, &ap_id.into())
})
.await?;
if let Ok(u) = person {
return Ok(Object::Person(Box::new(u)));
if let Ok(u) = user {
return Ok(Object::User(u));
}
let ap_id = apub_id.clone();
@ -364,7 +351,7 @@ pub(crate) async fn find_object_by_id(
})
.await?;
if let Ok(c) = community {
return Ok(Object::Community(Box::new(c)));
return Ok(Object::Community(c));
}
let private_message = blocking(context.pool(), move |conn| {
@ -372,7 +359,7 @@ pub(crate) async fn find_object_by_id(
})
.await?;
if let Ok(pm) = private_message {
return Ok(Object::PrivateMessage(Box::new(pm)));
return Ok(Object::PrivateMessage(pm));
}
Err(NotFound.into())

View file

@ -6,7 +6,7 @@ use crate::{
check_object_for_community_or_site_ban,
create_tombstone,
get_object_from_apub,
get_or_fetch_and_upsert_person,
get_or_fetch_and_upsert_user,
get_source_markdown_value,
set_content_and_source,
FromApub,
@ -21,16 +21,14 @@ use activitystreams::{
public,
};
use anyhow::{anyhow, Context};
use lemmy_api_structs::blocking;
use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::{
source::{
comment::{Comment, CommentForm},
person::Person,
post::Post,
},
CommentId,
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::Community,
post::Post,
user::User_,
};
use lemmy_structs::blocking;
use lemmy_utils::{
location_info,
utils::{convert_datetime, remove_slurs},
@ -47,11 +45,14 @@ impl ToApub for Comment {
let mut comment = ApObject::new(Note::new());
let creator_id = self.creator_id;
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
let post_id = self.post_id;
let post = blocking(pool, move |conn| Post::read(conn, post_id)).await??;
let community_id = post.community_id;
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
// Add a vector containing some important info to the "in_reply_to" field
// [post_ap_id, Option(parent_comment_ap_id)]
let mut in_reply_to_vec = vec![post.ap_id.into_inner()];
@ -67,7 +68,7 @@ impl ToApub for Comment {
.set_many_contexts(lemmy_context()?)
.set_id(self.ap_id.to_owned().into_inner())
.set_published(convert_datetime(self.published))
.set_to(public())
.set_many_tos(vec![community.actor_id.into_inner(), public()])
.set_many_in_reply_tos(in_reply_to_vec)
.set_attributed_to(creator.actor_id.into_inner());
@ -103,13 +104,13 @@ impl FromApub for Comment {
expected_domain: Url,
request_counter: &mut i32,
) -> Result<Comment, LemmyError> {
check_object_for_community_or_site_ban(note, context, request_counter).await?;
let comment: Comment =
get_object_from_apub(note, context, expected_domain, request_counter).await?;
let post_id = comment.post_id;
let post = blocking(context.pool(), move |conn| Post::read(conn, post_id)).await??;
check_object_for_community_or_site_ban(note, post.community_id, context, request_counter)
.await?;
if post.locked {
// This is not very efficient because a comment gets inserted just to be deleted right
// afterwards, but it seems to be the easiest way to implement it.
@ -138,8 +139,7 @@ impl FromApubToForm<NoteExt> for CommentForm {
.as_single_xsd_any_uri()
.context(location_info!())?;
let creator =
get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
let creator = get_or_fetch_and_upsert_user(creator_actor_id, context, request_counter).await?;
let mut in_reply_tos = note
.in_reply_to()
@ -156,7 +156,7 @@ impl FromApubToForm<NoteExt> for CommentForm {
// The 2nd item, if it exists, is the parent comment apub_id
// For deeply nested comments, FromApub automatically gets called recursively
let parent_id: Option<CommentId> = match in_reply_tos.next() {
let parent_id: Option<i32> = match in_reply_tos.next() {
Some(parent_comment_uri) => {
let parent_comment_ap_id = &parent_comment_uri?;
let parent_comment =

View file

@ -1,6 +1,6 @@
use crate::{
extensions::{context::lemmy_context, group_extensions::GroupExtension},
fetcher::person::get_or_fetch_and_upsert_person,
fetcher::user::get_or_fetch_and_upsert_user,
objects::{
check_object_domain,
create_tombstone,
@ -22,13 +22,13 @@ use activitystreams::{
};
use activitystreams_ext::Ext2;
use anyhow::Context;
use lemmy_api_structs::blocking;
use lemmy_db_queries::DbPool;
use lemmy_db_schema::{
naive_now,
source::community::{Community, CommunityForm},
};
use lemmy_db_views_actor::community_moderator_view::CommunityModeratorView;
use lemmy_structs::blocking;
use lemmy_utils::{
location_info,
utils::{check_slurs, check_slurs_opt, convert_datetime},
@ -73,13 +73,13 @@ impl ToApub for Community {
if let Some(icon_url) = &self.icon {
let mut image = Image::new();
image.set_url::<Url>(icon_url.to_owned().into());
image.set_url(Url::parse(icon_url)?);
group.set_icon(image.into_any_base()?);
}
if let Some(banner_url) = &self.banner {
let mut image = Image::new();
image.set_url::<Url>(banner_url.to_owned().into());
image.set_url(Url::parse(banner_url)?);
group.set_image(image.into_any_base()?);
}
@ -93,9 +93,16 @@ impl ToApub for Community {
..Default::default()
});
let nsfw = self.nsfw;
let category_id = self.category_id;
let group_extension = blocking(pool, move |conn| {
GroupExtension::new(conn, category_id, nsfw)
})
.await??;
Ok(Ext2::new(
ap_actor,
GroupExtension::new(self.nsfw)?,
group_extension,
self.get_public_key_ext()?,
))
}
@ -143,7 +150,7 @@ impl FromApubToForm<GroupExt> for CommunityForm {
.as_xsd_any_uri()
.context(location_info!())?;
let creator = get_or_fetch_and_upsert_person(creator_uri, context, request_counter).await?;
let creator = get_or_fetch_and_upsert_user(creator_uri, context, request_counter).await?;
let name = group
.inner
.preferred_username()
@ -173,7 +180,7 @@ impl FromApubToForm<GroupExt> for CommunityForm {
.url()
.context(location_info!())?
.as_single_xsd_any_uri()
.map(|u| u.to_owned().into()),
.map(|u| u.to_string()),
),
None => None,
};
@ -185,7 +192,7 @@ impl FromApubToForm<GroupExt> for CommunityForm {
.url()
.context(location_info!())?
.as_single_xsd_any_uri()
.map(|u| u.to_owned().into()),
.map(|u| u.to_string()),
),
None => None,
};
@ -200,6 +207,13 @@ impl FromApubToForm<GroupExt> for CommunityForm {
name,
title,
description,
category_id: group
.ext_one
.category
.clone()
.map(|c| c.identifier.parse::<i32>().ok())
.flatten()
.unwrap_or(1),
creator_id: creator.id,
removed: None,
published: group.inner.published().map(|u| u.to_owned().naive_local()),

View file

@ -1,6 +1,6 @@
use crate::{
check_is_apub_id_valid,
fetcher::{community::get_or_fetch_and_upsert_community, person::get_or_fetch_and_upsert_person},
fetcher::{community::get_or_fetch_and_upsert_community, user::get_or_fetch_and_upsert_user},
inbox::community_inbox::check_community_or_site_ban,
};
use activitystreams::{
@ -12,23 +12,18 @@ use activitystreams::{
use anyhow::{anyhow, Context};
use chrono::NaiveDateTime;
use diesel::result::Error::NotFound;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{ApubObject, Crud, DbPool};
use lemmy_db_schema::{source::community::Community, CommunityId, DbUrl};
use lemmy_utils::{
location_info,
settings::structs::Settings,
utils::{convert_datetime, markdown_to_html},
LemmyError,
};
use lemmy_db_schema::source::community::Community;
use lemmy_structs::blocking;
use lemmy_utils::{location_info, settings::Settings, utils::convert_datetime, LemmyError};
use lemmy_websocket::LemmyContext;
use url::Url;
pub(crate) mod comment;
pub(crate) mod community;
pub(crate) mod person;
pub(crate) mod post;
pub(crate) mod private_message;
pub(crate) mod user;
/// Trait for converting an object or actor into the respective ActivityPub type.
#[async_trait::async_trait(?Send)]
@ -96,7 +91,7 @@ where
pub(in crate::objects) fn check_object_domain<T, Kind>(
apub: &T,
expected_domain: Url,
) -> Result<DbUrl, LemmyError>
) -> Result<lemmy_db_schema::Url, LemmyError>
where
T: Base + AsBase<Kind>,
{
@ -119,8 +114,11 @@ where
.set_media_type(mime_markdown()?);
object.set_source(source.into_any_base()?);
object.set_content(markdown_to_html(markdown_text));
object.set_media_type(mime_html()?);
// set `content` to markdown for compatibility with older Lemmy versions
// TODO: change this to HTML in a while
object.set_content(markdown_text);
object.set_media_type(mime_markdown()?);
//object.set_content(markdown_to_html(markdown_text));
Ok(())
}
@ -136,28 +134,32 @@ where
.flatten()
.map(|s| s.to_string());
if content.is_some() {
let source = object.source().context(location_info!())?;
let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
check_is_markdown(source.media_type())?;
let source_content = source
.content()
.map(|s| s.as_single_xsd_string())
.flatten()
.context(location_info!())?
.to_string();
return Ok(Some(source_content));
let source = object.source();
// updated lemmy version, read markdown from `source.content`
if let Some(source) = source {
let source = Object::<()>::from_any_base(source.to_owned())?.context(location_info!())?;
check_is_markdown(source.media_type())?;
let source_content = source
.content()
.map(|s| s.as_single_xsd_string())
.flatten()
.context(location_info!())?
.to_string();
return Ok(Some(source_content));
}
// older lemmy version, read markdown from `content`
// TODO: remove this after a while
else {
return Ok(content);
}
}
Ok(None)
}
fn mime_markdown() -> Result<Mime, FromStrError> {
pub(in crate::objects) fn mime_markdown() -> Result<Mime, FromStrError> {
"text/markdown".parse()
}
fn mime_html() -> Result<Mime, FromStrError> {
"text/html".parse()
}
pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), LemmyError> {
let mime = mime.context(location_info!())?;
if !mime.eq(&mime_markdown()?) {
@ -172,7 +174,7 @@ pub(in crate::objects) fn check_is_markdown(mime: Option<&Mime>) -> Result<(), L
/// Converts an ActivityPub object (eg `Note`) to a database object (eg `Comment`). If an object
/// with the same ActivityPub ID already exists in the database, it is returned directly. Otherwise
/// the apub object is parsed, inserted and returned.
pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm, IdType>(
pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm>(
from: &From,
context: &LemmyContext,
expected_domain: Url,
@ -180,14 +182,14 @@ pub(in crate::objects) async fn get_object_from_apub<From, Kind, To, ToForm, IdT
) -> Result<To, LemmyError>
where
From: BaseExt<Kind>,
To: ApubObject<ToForm> + Crud<ToForm, IdType> + Send + 'static,
To: ApubObject<ToForm> + Crud<ToForm> + Send + 'static,
ToForm: FromApubToForm<From> + Send + 'static,
{
let object_id = from.id_unchecked().context(location_info!())?.to_owned();
let domain = object_id.domain().context(location_info!())?;
// if its a local object, return it directly from the database
if Settings::get().hostname() == domain {
if Settings::get().hostname == domain {
let object = blocking(context.pool(), move |conn| {
To::read_from_apub_id(conn, &object_id.into())
})
@ -205,20 +207,20 @@ where
pub(in crate::objects) async fn check_object_for_community_or_site_ban<T, Kind>(
object: &T,
community_id: CommunityId,
context: &LemmyContext,
request_counter: &mut i32,
) -> Result<(), LemmyError>
where
T: ObjectExt<Kind>,
{
let person_id = object
let user_id = object
.attributed_to()
.context(location_info!())?
.as_single_xsd_any_uri()
.context(location_info!())?;
let person = get_or_fetch_and_upsert_person(person_id, context, request_counter).await?;
check_community_or_site_ban(&person, community_id, context.pool()).await
let user = get_or_fetch_and_upsert_user(user_id, context, request_counter).await?;
let community = get_to_community(object, context, request_counter).await?;
check_community_or_site_ban(&user, &community, context.pool()).await
}
pub(in crate::objects) async fn get_to_community<T, Kind>(

View file

@ -1,6 +1,6 @@
use crate::{
extensions::{context::lemmy_context, page_extension::PageExtension},
fetcher::person::get_or_fetch_and_upsert_person,
fetcher::user::get_or_fetch_and_upsert_user,
objects::{
check_object_domain,
check_object_for_community_or_site_ban,
@ -22,16 +22,13 @@ use activitystreams::{
};
use activitystreams_ext::Ext1;
use anyhow::Context;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::{
self,
source::{
community::Community,
person::Person,
post::{Post, PostForm},
},
use lemmy_db_schema::source::{
community::Community,
post::{Post, PostForm},
user::User_,
};
use lemmy_structs::blocking;
use lemmy_utils::{
location_info,
request::fetch_iframely_and_pictrs_data,
@ -50,7 +47,7 @@ impl ToApub for Post {
let mut page = ApObject::new(Page::new());
let creator_id = self.creator_id;
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
let community_id = self.community_id;
let community = blocking(pool, move |conn| Community::read(conn, community_id)).await??;
@ -61,9 +58,8 @@ impl ToApub for Post {
// https://git.asonix.dog/Aardwolf/activitystreams/issues/5
.set_many_contexts(lemmy_context()?)
.set_id(self.ap_id.to_owned().into_inner())
.set_name(self.name.to_owned())
// `summary` field for compatibility with lemmy v0.9.9 and older,
// TODO: remove this after some time
// Use summary field to be consistent with mastodon content warning.
// https://mastodon.xyz/@Louisa/103987265222901387.json
.set_summary(self.name.to_owned())
.set_published(convert_datetime(self.published))
.set_many_tos(vec![community.actor_id.into_inner(), public()])
@ -73,13 +69,16 @@ impl ToApub for Post {
set_content_and_source(&mut page, &body)?;
}
if let Some(url) = &self.url {
page.set_url::<Url>(url.to_owned().into());
// TODO: hacky code because we get self.url == Some("")
// https://github.com/LemmyNet/lemmy/issues/602
let url = self.url.as_ref().filter(|u| !u.is_empty());
if let Some(u) = url {
page.set_url(Url::parse(u)?);
}
if let Some(thumbnail_url) = &self.thumbnail_url {
let mut image = Image::new();
image.set_url::<Url>(thumbnail_url.to_owned().into());
image.set_url(Url::parse(thumbnail_url)?);
page.set_image(image.into_any_base()?);
}
@ -118,10 +117,8 @@ impl FromApub for Post {
expected_domain: Url,
request_counter: &mut i32,
) -> Result<Post, LemmyError> {
let post: Post = get_object_from_apub(page, context, expected_domain, request_counter).await?;
check_object_for_community_or_site_ban(page, post.community_id, context, request_counter)
.await?;
Ok(post)
check_object_for_community_or_site_ban(page, context, request_counter).await?;
get_object_from_apub(page, context, expected_domain, request_counter).await
}
}
@ -142,12 +139,11 @@ impl FromApubToForm<PageExt> for PostForm {
.as_single_xsd_any_uri()
.context(location_info!())?;
let creator =
get_or_fetch_and_upsert_person(creator_actor_id, context, request_counter).await?;
let creator = get_or_fetch_and_upsert_user(creator_actor_id, context, request_counter).await?;
let community = get_to_community(page, context, request_counter).await?;
let thumbnail_url: Option<Url> = match &page.inner.image() {
let thumbnail_url = match &page.inner.image() {
Some(any_image) => Image::from_any_base(
any_image
.to_owned()
@ -159,7 +155,7 @@ impl FromApubToForm<PageExt> for PostForm {
.url()
.context(location_info!())?
.as_single_xsd_any_uri()
.map(|url| url.to_owned()),
.map(|u| u.to_string()),
None => None,
};
let url = page
@ -167,22 +163,19 @@ impl FromApubToForm<PageExt> for PostForm {
.url()
.map(|u| u.as_single_xsd_any_uri())
.flatten()
.map(|u| u.to_owned());
.map(|s| s.to_string());
let (iframely_title, iframely_description, iframely_html, pictrs_thumbnail) =
if let Some(url) = &url {
fetch_iframely_and_pictrs_data(context.client(), Some(url)).await
fetch_iframely_and_pictrs_data(context.client(), Some(url.to_owned())).await
} else {
(None, None, None, thumbnail_url)
};
let name = page
.inner
.name()
.map(|s| s.map(|s2| s2.to_owned()))
// The following is for compatibility with lemmy v0.9.9 and older
// TODO: remove it after some time (along with the map above)
.or_else(|| page.inner.summary().map(|s| s.to_owned()))
.summary()
.as_ref()
.context(location_info!())?
.as_single_xsd_string()
.context(location_info!())?
@ -193,7 +186,7 @@ impl FromApubToForm<PageExt> for PostForm {
let body_slurs_removed = body.map(|b| remove_slurs(&b));
Ok(PostForm {
name,
url: url.map(|u| u.into()),
url,
body: body_slurs_removed,
creator_id: creator.id,
community_id: community.id,
@ -215,7 +208,7 @@ impl FromApubToForm<PageExt> for PostForm {
embed_title: iframely_title,
embed_description: iframely_description,
embed_html: iframely_html,
thumbnail_url: pictrs_thumbnail.map(|u| u.into()),
thumbnail_url: pictrs_thumbnail,
ap_id: Some(check_object_domain(page, expected_domain)?),
local: false,
})

View file

@ -1,7 +1,7 @@
use crate::{
check_is_apub_id_valid,
extensions::context::lemmy_context,
fetcher::person::get_or_fetch_and_upsert_person,
fetcher::user::get_or_fetch_and_upsert_user,
objects::{
check_object_domain,
create_tombstone,
@ -19,12 +19,12 @@ use activitystreams::{
prelude::*,
};
use anyhow::Context;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{Crud, DbPool};
use lemmy_db_schema::source::{
person::Person,
private_message::{PrivateMessage, PrivateMessageForm},
user::User_,
};
use lemmy_structs::blocking;
use lemmy_utils::{location_info, utils::convert_datetime, LemmyError};
use lemmy_websocket::LemmyContext;
use url::Url;
@ -37,10 +37,10 @@ impl ToApub for PrivateMessage {
let mut private_message = ApObject::new(Note::new());
let creator_id = self.creator_id;
let creator = blocking(pool, move |conn| Person::read(conn, creator_id)).await??;
let creator = blocking(pool, move |conn| User_::read(conn, creator_id)).await??;
let recipient_id = self.recipient_id;
let recipient = blocking(pool, move |conn| Person::read(conn, recipient_id)).await??;
let recipient = blocking(pool, move |conn| User_::read(conn, recipient_id)).await??;
private_message
.set_many_contexts(lemmy_context()?)
@ -97,8 +97,7 @@ impl FromApubToForm<NoteExt> for PrivateMessageForm {
.single_xsd_any_uri()
.context(location_info!())?;
let creator =
get_or_fetch_and_upsert_person(&creator_actor_id, context, request_counter).await?;
let creator = get_or_fetch_and_upsert_user(&creator_actor_id, context, request_counter).await?;
let recipient_actor_id = note
.to()
.context(location_info!())?
@ -106,7 +105,7 @@ impl FromApubToForm<NoteExt> for PrivateMessageForm {
.single_xsd_any_uri()
.context(location_info!())?;
let recipient =
get_or_fetch_and_upsert_person(&recipient_actor_id, context, request_counter).await?;
get_or_fetch_and_upsert_user(&recipient_actor_id, context, request_counter).await?;
let ap_id = note.id_unchecked().context(location_info!())?.to_string();
check_is_apub_id_valid(&Url::parse(&ap_id)?)?;

View file

@ -18,15 +18,15 @@ use activitystreams::{
};
use activitystreams_ext::Ext1;
use anyhow::Context;
use lemmy_api_structs::blocking;
use lemmy_db_queries::{ApubObject, DbPool};
use lemmy_db_schema::{
naive_now,
source::person::{Person as DbPerson, PersonForm},
source::user::{UserForm, User_},
};
use lemmy_structs::blocking;
use lemmy_utils::{
location_info,
settings::structs::Settings,
settings::Settings,
utils::{check_slurs, check_slurs_opt, convert_datetime},
LemmyError,
};
@ -34,7 +34,7 @@ use lemmy_websocket::LemmyContext;
use url::Url;
#[async_trait::async_trait(?Send)]
impl ToApub for DbPerson {
impl ToApub for User_ {
type ApubType = PersonExt;
async fn to_apub(&self, _pool: &DbPool) -> Result<PersonExt, LemmyError> {
@ -50,18 +50,21 @@ impl ToApub for DbPerson {
if let Some(avatar_url) = &self.avatar {
let mut image = Image::new();
image.set_url::<Url>(avatar_url.to_owned().into());
image.set_url(Url::parse(avatar_url)?);
person.set_icon(image.into_any_base()?);
}
if let Some(banner_url) = &self.banner {
let mut image = Image::new();
image.set_url::<Url>(banner_url.to_owned().into());
image.set_url(Url::parse(banner_url)?);
person.set_image(image.into_any_base()?);
}
if let Some(bio) = &self.bio {
set_content_and_source(&mut person, bio)?;
// Also set summary for compatibility with older Lemmy versions.
// TODO: remove this after a while.
person.set_summary(bio.to_owned());
}
if let Some(i) = self.preferred_username.to_owned() {
@ -85,7 +88,7 @@ impl ToApub for DbPerson {
}
#[async_trait::async_trait(?Send)]
impl FromApub for DbPerson {
impl FromApub for User_ {
type ApubType = PersonExt;
async fn from_apub(
@ -93,29 +96,26 @@ impl FromApub for DbPerson {
context: &LemmyContext,
expected_domain: Url,
request_counter: &mut i32,
) -> Result<DbPerson, LemmyError> {
let person_id = person.id_unchecked().context(location_info!())?.to_owned();
let domain = person_id.domain().context(location_info!())?;
if domain == Settings::get().hostname() {
let person = blocking(context.pool(), move |conn| {
DbPerson::read_from_apub_id(conn, &person_id.into())
) -> Result<User_, LemmyError> {
let user_id = person.id_unchecked().context(location_info!())?.to_owned();
let domain = user_id.domain().context(location_info!())?;
if domain == Settings::get().hostname {
let user = blocking(context.pool(), move |conn| {
User_::read_from_apub_id(conn, &user_id.into())
})
.await??;
Ok(person)
Ok(user)
} else {
let person_form =
PersonForm::from_apub(person, context, expected_domain, request_counter).await?;
let person = blocking(context.pool(), move |conn| {
DbPerson::upsert(conn, &person_form)
})
.await??;
Ok(person)
let user_form =
UserForm::from_apub(person, context, expected_domain, request_counter).await?;
let user = blocking(context.pool(), move |conn| User_::upsert(conn, &user_form)).await??;
Ok(user)
}
}
}
#[async_trait::async_trait(?Send)]
impl FromApubToForm<PersonExt> for PersonForm {
impl FromApubToForm<PersonExt> for UserForm {
async fn from_apub(
person: &PersonExt,
_context: &LemmyContext,
@ -129,7 +129,7 @@ impl FromApubToForm<PersonExt> for PersonForm {
.url()
.context(location_info!())?
.as_single_xsd_any_uri()
.map(|url| url.to_owned()),
.map(|u| u.to_string()),
),
None => None,
};
@ -142,7 +142,7 @@ impl FromApubToForm<PersonExt> for PersonForm {
.url()
.context(location_info!())?
.as_single_xsd_any_uri()
.map(|url| url.to_owned()),
.map(|u| u.to_string()),
),
None => None,
};
@ -170,20 +170,30 @@ impl FromApubToForm<PersonExt> for PersonForm {
check_slurs_opt(&preferred_username)?;
check_slurs_opt(&bio)?;
Ok(PersonForm {
Ok(UserForm {
name,
preferred_username: Some(preferred_username),
password_encrypted: "".to_string(),
admin: false,
banned: None,
deleted: None,
avatar: avatar.map(|o| o.map(|i| i.into())),
banner: banner.map(|o| o.map(|i| i.into())),
email: None,
avatar,
banner,
published: person.inner.published().map(|u| u.to_owned().naive_local()),
updated: person.updated().map(|u| u.to_owned().naive_local()),
show_nsfw: false,
theme: "".to_string(),
default_sort_type: 0,
default_listing_type: 0,
lang: "".to_string(),
show_avatars: false,
send_notifications_to_email: false,
matrix_user_id: None,
actor_id: Some(check_object_domain(person, expected_domain)?),
bio: Some(bio),
local: Some(false),
local: false,
private_key: None,
public_key: Some(Some(person.ext_one.public_key.to_owned().public_key_pem)),
public_key: Some(person.ext_one.public_key.to_owned().public_key_pem),
last_refreshed_at: Some(naive_now()),
inbox_url: Some(person.inner.inbox()?.to_owned().into()),
shared_inbox_url: Some(shared_inbox),

View file

@ -6,7 +6,6 @@ edition = "2018"
[lib]
name = "lemmy_db_queries"
path = "src/lib.rs"
doctest = false
[dependencies]
lemmy_utils = { path = "../utils" }
@ -20,10 +19,8 @@ strum = "0.20.0"
strum_macros = "0.20.1"
log = "0.4.14"
sha2 = "0.9.3"
url = { version = "2.2.1", features = ["serde"] }
url = { version = "2.2.0", features = ["serde"] }
lazy_static = "1.4.0"
regex = "1.4.3"
bcrypt = "0.9.0"
[dev-dependencies]
serial_test = "0.5.1"
diesel_json = "0.1.1"

View file

@ -1,12 +1,12 @@
use diesel::{result::Error, *};
use lemmy_db_schema::{schema::comment_aggregates, CommentId};
use lemmy_db_schema::schema::comment_aggregates;
use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
#[table_name = "comment_aggregates"]
pub struct CommentAggregates {
pub id: i32,
pub comment_id: CommentId,
pub comment_id: i32,
pub score: i64,
pub upvotes: i64,
pub downvotes: i64,
@ -14,7 +14,7 @@ pub struct CommentAggregates {
}
impl CommentAggregates {
pub fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
comment_aggregates::table
.filter(comment_aggregates::comment_id.eq(comment_id))
.first::<Self>(conn)
@ -28,32 +28,42 @@ mod tests {
establish_unpooled_connection,
Crud,
Likeable,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
community::{Community, CommunityForm},
person::{Person, PersonForm},
post::{Post, PostForm},
user::{UserForm, User_},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "thommy_comment_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -61,20 +71,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let another_person = PersonForm {
let another_user = UserForm {
name: "jerry_comment_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -82,13 +102,14 @@ mod tests {
shared_inbox_url: None,
};
let another_inserted_person = Person::create(&conn, &another_person).unwrap();
let another_inserted_user = User_::create(&conn, &another_user).unwrap();
let new_community = CommunityForm {
name: "TIL_comment_agg".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -112,7 +133,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -133,7 +154,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -149,7 +170,7 @@ mod tests {
let child_comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -166,7 +187,7 @@ mod tests {
let comment_like = CommentLikeForm {
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
score: 1,
};
@ -178,11 +199,11 @@ mod tests {
assert_eq!(1, comment_aggs_before_delete.upvotes);
assert_eq!(0, comment_aggs_before_delete.downvotes);
// Add a post dislike from the other person
// Add a post dislike from the other user
let comment_dislike = CommentLikeForm {
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: another_inserted_person.id,
user_id: another_inserted_user.id,
score: -1,
};
@ -195,7 +216,7 @@ mod tests {
assert_eq!(1, comment_aggs_after_dislike.downvotes);
// Remove the first comment like
CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
let after_like_remove = CommentAggregates::read(&conn, inserted_comment.id).unwrap();
assert_eq!(-1, after_like_remove.score);
assert_eq!(0, after_like_remove.upvotes);
@ -209,8 +230,8 @@ mod tests {
assert!(after_delete.is_err());
// This should delete all the associated rows, and fire triggers
Person::delete(&conn, another_inserted_person.id).unwrap();
let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(1, person_num_deleted);
User_::delete(&conn, another_inserted_user.id).unwrap();
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(1, user_num_deleted);
}
}

View file

@ -1,12 +1,12 @@
use diesel::{result::Error, *};
use lemmy_db_schema::{schema::community_aggregates, CommunityId};
use lemmy_db_schema::schema::community_aggregates;
use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
#[table_name = "community_aggregates"]
pub struct CommunityAggregates {
pub id: i32,
pub community_id: CommunityId,
pub community_id: i32,
pub subscribers: i64,
pub posts: i64,
pub comments: i64,
@ -18,7 +18,7 @@ pub struct CommunityAggregates {
}
impl CommunityAggregates {
pub fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
community_aggregates::table
.filter(community_aggregates::community_id.eq(community_id))
.first::<Self>(conn)
@ -32,32 +32,42 @@ mod tests {
establish_unpooled_connection,
Crud,
Followable,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::{Community, CommunityFollower, CommunityFollowerForm, CommunityForm},
person::{Person, PersonForm},
post::{Post, PostForm},
user::{UserForm, User_},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "thommy_community_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -65,20 +75,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let another_person = PersonForm {
let another_user = UserForm {
name: "jerry_community_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -86,13 +106,14 @@ mod tests {
shared_inbox_url: None,
};
let another_inserted_person = Person::create(&conn, &another_person).unwrap();
let another_inserted_user = User_::create(&conn, &another_user).unwrap();
let new_community = CommunityForm {
name: "TIL_community_agg".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -114,9 +135,10 @@ mod tests {
let another_community = CommunityForm {
name: "TIL_community_agg_2".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -136,25 +158,25 @@ mod tests {
let another_inserted_community = Community::create(&conn, &another_community).unwrap();
let first_person_follow = CommunityFollowerForm {
let first_user_follow = CommunityFollowerForm {
community_id: inserted_community.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
pending: false,
};
CommunityFollower::follow(&conn, &first_person_follow).unwrap();
CommunityFollower::follow(&conn, &first_user_follow).unwrap();
let second_person_follow = CommunityFollowerForm {
let second_user_follow = CommunityFollowerForm {
community_id: inserted_community.id,
person_id: another_inserted_person.id,
user_id: another_inserted_user.id,
pending: false,
};
CommunityFollower::follow(&conn, &second_person_follow).unwrap();
CommunityFollower::follow(&conn, &second_user_follow).unwrap();
let another_community_follow = CommunityFollowerForm {
community_id: another_inserted_community.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
pending: false,
};
@ -164,7 +186,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -185,7 +207,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -201,7 +223,7 @@ mod tests {
let child_comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -230,12 +252,12 @@ mod tests {
assert_eq!(0, another_community_aggs.comments);
// Unfollow test
CommunityFollower::unfollow(&conn, &second_person_follow).unwrap();
CommunityFollower::unfollow(&conn, &second_user_follow).unwrap();
let after_unfollow = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
assert_eq!(1, after_unfollow.subscribers);
// Follow again just for the later tests
CommunityFollower::follow(&conn, &second_person_follow).unwrap();
CommunityFollower::follow(&conn, &second_user_follow).unwrap();
let after_follow_again = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
assert_eq!(2, after_follow_again.subscribers);
@ -245,14 +267,14 @@ mod tests {
assert_eq!(0, after_parent_post_delete.comments);
assert_eq!(0, after_parent_post_delete.posts);
// Remove the 2nd person
Person::delete(&conn, another_inserted_person.id).unwrap();
let after_person_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
assert_eq!(1, after_person_delete.subscribers);
// Remove the 2nd user
User_::delete(&conn, another_inserted_user.id).unwrap();
let after_user_delete = CommunityAggregates::read(&conn, inserted_community.id).unwrap();
assert_eq!(1, after_user_delete.subscribers);
// This should delete all the associated rows, and fire triggers
let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(1, person_num_deleted);
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(1, user_num_deleted);
// Should be none found, since the creator was deleted
let after_delete = CommunityAggregates::read(&conn, inserted_community.id);

View file

@ -1,5 +1,5 @@
pub mod comment_aggregates;
pub mod community_aggregates;
pub mod person_aggregates;
pub mod post_aggregates;
pub mod site_aggregates;
pub mod user_aggregates;

View file

@ -1,24 +1,23 @@
use diesel::{result::Error, *};
use lemmy_db_schema::{schema::post_aggregates, PostId};
use lemmy_db_schema::schema::post_aggregates;
use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
#[table_name = "post_aggregates"]
pub struct PostAggregates {
pub id: i32,
pub post_id: PostId,
pub post_id: i32,
pub comments: i64,
pub score: i64,
pub upvotes: i64,
pub downvotes: i64,
pub stickied: bool,
pub published: chrono::NaiveDateTime,
pub newest_comment_time_necro: chrono::NaiveDateTime, // A newest comment time, limited to 2 days, to prevent necrobumping
pub newest_comment_time: chrono::NaiveDateTime,
}
impl PostAggregates {
pub fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
pub fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
post_aggregates::table
.filter(post_aggregates::post_id.eq(post_id))
.first::<Self>(conn)
@ -32,32 +31,42 @@ mod tests {
establish_unpooled_connection,
Crud,
Likeable,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
person::{Person, PersonForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "thommy_community_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -65,20 +74,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let another_person = PersonForm {
let another_user = UserForm {
name: "jerry_community_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -86,13 +105,14 @@ mod tests {
shared_inbox_url: None,
};
let another_inserted_person = Person::create(&conn, &another_person).unwrap();
let another_inserted_user = User_::create(&conn, &another_user).unwrap();
let new_community = CommunityForm {
name: "TIL_community_agg".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -116,7 +136,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -137,7 +157,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -153,7 +173,7 @@ mod tests {
let child_comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -169,7 +189,7 @@ mod tests {
let post_like = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
score: 1,
};
@ -182,10 +202,10 @@ mod tests {
assert_eq!(1, post_aggs_before_delete.upvotes);
assert_eq!(0, post_aggs_before_delete.downvotes);
// Add a post dislike from the other person
// Add a post dislike from the other user
let post_dislike = PostLikeForm {
post_id: inserted_post.id,
person_id: another_inserted_person.id,
user_id: another_inserted_user.id,
score: -1,
};
@ -207,7 +227,7 @@ mod tests {
assert_eq!(1, after_comment_delete.downvotes);
// Remove the first post like
PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
let after_like_remove = PostAggregates::read(&conn, inserted_post.id).unwrap();
assert_eq!(0, after_like_remove.comments);
assert_eq!(-1, after_like_remove.score);
@ -215,9 +235,9 @@ mod tests {
assert_eq!(1, after_like_remove.downvotes);
// This should delete all the associated rows, and fire triggers
Person::delete(&conn, another_inserted_person.id).unwrap();
let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(1, person_num_deleted);
User_::delete(&conn, another_inserted_user.id).unwrap();
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(1, user_num_deleted);
// Should be none found, since the creator was deleted
let after_delete = PostAggregates::read(&conn, inserted_post.id);

View file

@ -25,33 +25,47 @@ impl SiteAggregates {
#[cfg(test)]
mod tests {
use crate::{aggregates::site_aggregates::SiteAggregates, establish_unpooled_connection, Crud};
use crate::{
aggregates::site_aggregates::SiteAggregates,
establish_unpooled_connection,
Crud,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm},
community::{Community, CommunityForm},
person::{Person, PersonForm},
post::{Post, PostForm},
site::{Site, SiteForm},
user::{UserForm, User_},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "thommy_site_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -59,14 +73,14 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let site_form = SiteForm {
name: "test_site".into(),
description: None,
icon: None,
banner: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
enable_downvotes: true,
open_registration: true,
enable_nsfw: true,
@ -77,9 +91,10 @@ mod tests {
let new_community = CommunityForm {
name: "TIL_site_agg".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -103,7 +118,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -126,7 +141,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -143,7 +158,7 @@ mod tests {
let child_comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -171,8 +186,8 @@ mod tests {
assert_eq!(0, site_aggregates_after_post_delete.comments);
// This shouuld delete all the associated rows, and fire triggers
let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(1, person_num_deleted);
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(1, user_num_deleted);
let after_delete = SiteAggregates::read(&conn);
assert!(after_delete.is_err());

View file

@ -1,22 +1,22 @@
use diesel::{result::Error, *};
use lemmy_db_schema::{schema::person_aggregates, PersonId};
use lemmy_db_schema::schema::user_aggregates;
use serde::Serialize;
#[derive(Queryable, Associations, Identifiable, PartialEq, Debug, Serialize, Clone)]
#[table_name = "person_aggregates"]
pub struct PersonAggregates {
#[table_name = "user_aggregates"]
pub struct UserAggregates {
pub id: i32,
pub person_id: PersonId,
pub user_id: i32,
pub post_count: i64,
pub post_score: i64,
pub comment_count: i64,
pub comment_score: i64,
}
impl PersonAggregates {
pub fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
person_aggregates::table
.filter(person_aggregates::person_id.eq(person_id))
impl UserAggregates {
pub fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
user_aggregates::table
.filter(user_aggregates::user_id.eq(user_id))
.first::<Self>(conn)
}
}
@ -24,36 +24,46 @@ impl PersonAggregates {
#[cfg(test)]
mod tests {
use crate::{
aggregates::person_aggregates::PersonAggregates,
aggregates::user_aggregates::UserAggregates,
establish_unpooled_connection,
Crud,
Likeable,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
comment::{Comment, CommentForm, CommentLike, CommentLikeForm},
community::{Community, CommunityForm},
person::{Person, PersonForm},
post::{Post, PostForm, PostLike, PostLikeForm},
user::{UserForm, User_},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "thommy_user_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -61,20 +71,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let another_person = PersonForm {
let another_user = UserForm {
name: "jerry_user_agg".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -82,13 +102,14 @@ mod tests {
shared_inbox_url: None,
};
let another_inserted_person = Person::create(&conn, &another_person).unwrap();
let another_inserted_user = User_::create(&conn, &another_user).unwrap();
let new_community = CommunityForm {
name: "TIL_site_agg".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -112,7 +133,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -133,7 +154,7 @@ mod tests {
let post_like = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
score: 1,
};
@ -141,7 +162,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -157,7 +178,7 @@ mod tests {
let mut comment_like = CommentLikeForm {
comment_id: inserted_comment.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
post_id: inserted_post.id,
score: 1,
};
@ -166,7 +187,7 @@ mod tests {
let mut child_comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -182,29 +203,28 @@ mod tests {
let child_comment_like = CommentLikeForm {
comment_id: inserted_child_comment.id,
person_id: another_inserted_person.id,
user_id: another_inserted_user.id,
post_id: inserted_post.id,
score: 1,
};
let _inserted_child_comment_like = CommentLike::like(&conn, &child_comment_like).unwrap();
let person_aggregates_before_delete =
PersonAggregates::read(&conn, inserted_person.id).unwrap();
let user_aggregates_before_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
assert_eq!(1, person_aggregates_before_delete.post_count);
assert_eq!(1, person_aggregates_before_delete.post_score);
assert_eq!(2, person_aggregates_before_delete.comment_count);
assert_eq!(2, person_aggregates_before_delete.comment_score);
assert_eq!(1, user_aggregates_before_delete.post_count);
assert_eq!(1, user_aggregates_before_delete.post_score);
assert_eq!(2, user_aggregates_before_delete.comment_count);
assert_eq!(2, user_aggregates_before_delete.comment_score);
// Remove a post like
PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
let after_post_like_remove = PersonAggregates::read(&conn, inserted_person.id).unwrap();
PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
let after_post_like_remove = UserAggregates::read(&conn, inserted_user.id).unwrap();
assert_eq!(0, after_post_like_remove.post_score);
// Remove a parent comment (the scores should also be removed)
Comment::delete(&conn, inserted_comment.id).unwrap();
let after_parent_comment_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
let after_parent_comment_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
assert_eq!(0, after_parent_comment_delete.comment_count);
assert_eq!(0, after_parent_comment_delete.comment_score);
@ -214,24 +234,24 @@ mod tests {
Comment::create(&conn, &child_comment_form).unwrap();
comment_like.comment_id = new_parent_comment.id;
CommentLike::like(&conn, &comment_like).unwrap();
let after_comment_add = PersonAggregates::read(&conn, inserted_person.id).unwrap();
let after_comment_add = UserAggregates::read(&conn, inserted_user.id).unwrap();
assert_eq!(2, after_comment_add.comment_count);
assert_eq!(1, after_comment_add.comment_score);
Post::delete(&conn, inserted_post.id).unwrap();
let after_post_delete = PersonAggregates::read(&conn, inserted_person.id).unwrap();
let after_post_delete = UserAggregates::read(&conn, inserted_user.id).unwrap();
assert_eq!(0, after_post_delete.comment_score);
assert_eq!(0, after_post_delete.comment_count);
assert_eq!(0, after_post_delete.post_score);
assert_eq!(0, after_post_delete.post_count);
// This should delete all the associated rows, and fire triggers
let person_num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(1, person_num_deleted);
Person::delete(&conn, another_inserted_person.id).unwrap();
let user_num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(1, user_num_deleted);
User_::delete(&conn, another_inserted_user.id).unwrap();
// Should be none found
let after_delete = PersonAggregates::read(&conn, inserted_person.id);
let after_delete = UserAggregates::read(&conn, inserted_user.id);
assert!(after_delete.is_err());
}
}

View file

@ -9,33 +9,28 @@ extern crate lazy_static;
#[macro_use]
extern crate diesel_migrations;
#[cfg(test)]
extern crate serial_test;
use diesel::{result::Error, *};
use lemmy_db_schema::{CommunityId, DbUrl, PersonId};
use lemmy_utils::ApiError;
use lemmy_db_schema::Url;
use regex::Regex;
use serde::{Deserialize, Serialize};
use std::{env, env::VarError};
use url::Url;
pub mod aggregates;
pub mod source;
pub type DbPool = diesel::r2d2::Pool<diesel::r2d2::ConnectionManager<diesel::PgConnection>>;
pub trait Crud<Form, IdType> {
fn create(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Crud<T> {
fn create(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn read(conn: &PgConnection, id: IdType) -> Result<Self, Error>
fn read(conn: &PgConnection, id: i32) -> Result<Self, Error>
where
Self: Sized;
fn update(conn: &PgConnection, id: IdType, form: &Form) -> Result<Self, Error>
fn update(conn: &PgConnection, id: i32, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn delete(_conn: &PgConnection, _id: IdType) -> Result<usize, Error>
fn delete(_conn: &PgConnection, _id: i32) -> Result<usize, Error>
where
Self: Sized,
{
@ -43,85 +38,81 @@ pub trait Crud<Form, IdType> {
}
}
pub trait Followable<Form> {
fn follow(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Followable<T> {
fn follow(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn follow_accepted(
conn: &PgConnection,
community_id: CommunityId,
person_id: PersonId,
) -> Result<Self, Error>
fn follow_accepted(conn: &PgConnection, community_id: i32, user_id: i32) -> Result<Self, Error>
where
Self: Sized;
fn unfollow(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn unfollow(conn: &PgConnection, form: &T) -> Result<usize, Error>
where
Self: Sized;
fn has_local_followers(conn: &PgConnection, community_id: CommunityId) -> Result<bool, Error>;
fn has_local_followers(conn: &PgConnection, community_id: i32) -> Result<bool, Error>;
}
pub trait Joinable<Form> {
fn join(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Joinable<T> {
fn join(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn leave(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn leave(conn: &PgConnection, form: &T) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Likeable<Form, IdType> {
fn like(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Likeable<T> {
fn like(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn remove(conn: &PgConnection, person_id: PersonId, item_id: IdType) -> Result<usize, Error>
fn remove(conn: &PgConnection, user_id: i32, item_id: i32) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Bannable<Form> {
fn ban(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Bannable<T> {
fn ban(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn unban(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn unban(conn: &PgConnection, form: &T) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Saveable<Form> {
fn save(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Saveable<T> {
fn save(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn unsave(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn unsave(conn: &PgConnection, form: &T) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Readable<Form> {
fn mark_as_read(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Readable<T> {
fn mark_as_read(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn mark_as_unread(conn: &PgConnection, form: &Form) -> Result<usize, Error>
fn mark_as_unread(conn: &PgConnection, form: &T) -> Result<usize, Error>
where
Self: Sized;
}
pub trait Reportable<Form> {
fn report(conn: &PgConnection, form: &Form) -> Result<Self, Error>
pub trait Reportable<T> {
fn report(conn: &PgConnection, form: &T) -> Result<Self, Error>
where
Self: Sized;
fn resolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
fn resolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
where
Self: Sized;
fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: PersonId) -> Result<usize, Error>
fn unresolve(conn: &PgConnection, report_id: i32, resolver_id: i32) -> Result<usize, Error>
where
Self: Sized;
}
pub trait ApubObject<Form> {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
pub trait ApubObject<T> {
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error>
where
Self: Sized;
fn upsert(conn: &PgConnection, user_form: &Form) -> Result<Self, Error>
fn upsert(conn: &PgConnection, user_form: &T) -> Result<Self, Error>
where
Self: Sized;
}
@ -174,7 +165,6 @@ pub enum SortType {
TopYear,
TopAll,
MostComments,
NewComments,
}
#[derive(EnumString, ToString, Debug, Serialize, Deserialize, Clone)]
@ -225,20 +215,6 @@ pub fn diesel_option_overwrite(opt: &Option<String>) -> Option<Option<String>> {
}
}
pub fn diesel_option_overwrite_to_url(
opt: &Option<String>,
) -> Result<Option<Option<DbUrl>>, ApiError> {
match opt.as_ref().map(|s| s.as_str()) {
// An empty string is an erase
Some("") => Ok(Some(None)),
Some(str_url) => match Url::parse(str_url) {
Ok(url) => Ok(Some(Some(url.into()))),
Err(_) => Err(ApiError::err("invalid_url")),
},
None => Ok(None),
}
}
embed_migrations!();
pub fn establish_unpooled_connection() -> PgConnection {
@ -251,14 +227,13 @@ pub fn establish_unpooled_connection() -> PgConnection {
};
let conn =
PgConnection::establish(&db_url).unwrap_or_else(|_| panic!("Error connecting to {}", db_url));
embedded_migrations::run(&conn).expect("load migrations");
embedded_migrations::run(&conn).unwrap();
conn
}
lazy_static! {
static ref EMAIL_REGEX: Regex =
Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$")
.expect("compile email regex");
Regex::new(r"^[a-zA-Z0-9.!#$%&*+/=?^_`{|}~-]+@[a-zA-Z0-9-]+(?:\.[a-zA-Z0-9-]+)*$").unwrap();
}
pub mod functions {
@ -271,7 +246,7 @@ pub mod functions {
#[cfg(test)]
mod tests {
use super::{fuzzy_search, *};
use super::fuzzy_search;
use crate::is_email_regex;
#[test]
@ -285,32 +260,4 @@ mod tests {
assert!(is_email_regex("gush@gmail.com"));
assert!(!is_email_regex("nada_neutho"));
}
#[test]
fn test_diesel_option_overwrite() {
assert_eq!(diesel_option_overwrite(&None), None);
assert_eq!(diesel_option_overwrite(&Some("".to_string())), Some(None));
assert_eq!(
diesel_option_overwrite(&Some("test".to_string())),
Some(Some("test".to_string()))
);
}
#[test]
fn test_diesel_option_overwrite_to_url() {
assert!(matches!(diesel_option_overwrite_to_url(&None), Ok(None)));
assert!(matches!(
diesel_option_overwrite_to_url(&Some("".to_string())),
Ok(Some(None))
));
assert!(matches!(
diesel_option_overwrite_to_url(&Some("invalid_url".to_string())),
Err(_)
));
let example_url = "https://example.com";
assert!(matches!(
diesel_option_overwrite_to_url(&Some(example_url.to_string())),
Ok(Some(Some(url))) if url == Url::parse(&example_url).unwrap().into()
));
}
}

View file

@ -1,6 +1,6 @@
use crate::Crud;
use diesel::{dsl::*, result::Error, sql_types::Text, *};
use lemmy_db_schema::{source::activity::*, DbUrl};
use lemmy_db_schema::{source::activity::*, Url};
use log::debug;
use serde::Serialize;
use serde_json::Value;
@ -9,7 +9,7 @@ use std::{
io::{Error as IoError, ErrorKind},
};
impl Crud<ActivityForm, i32> for Activity {
impl Crud<ActivityForm> for Activity {
fn read(conn: &PgConnection, activity_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::activity::dsl::*;
activity.find(activity_id).first::<Self>(conn)
@ -41,7 +41,7 @@ impl Crud<ActivityForm, i32> for Activity {
pub trait Activity_ {
fn insert<T>(
conn: &PgConnection,
ap_id: DbUrl,
ap_id: String,
data: &T,
local: bool,
sensitive: bool,
@ -49,20 +49,20 @@ pub trait Activity_ {
where
T: Serialize + Debug;
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error>;
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error>;
fn delete_olds(conn: &PgConnection) -> Result<usize, Error>;
/// Returns up to 20 activities of type `Announce/Create/Page` from the community
fn read_community_outbox(
conn: &PgConnection,
community_actor_id: &DbUrl,
community_actor_id: &Url,
) -> Result<Vec<Value>, Error>;
}
impl Activity_ for Activity {
fn insert<T>(
conn: &PgConnection,
ap_id: DbUrl,
ap_id: String,
data: &T,
local: bool,
sensitive: bool,
@ -88,7 +88,7 @@ impl Activity_ for Activity {
}
}
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Activity, Error> {
fn read_from_apub_id(conn: &PgConnection, object_id: &str) -> Result<Activity, Error> {
use lemmy_db_schema::schema::activity::dsl::*;
activity.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
@ -100,7 +100,7 @@ impl Activity_ for Activity {
fn read_community_outbox(
conn: &PgConnection,
community_actor_id: &DbUrl,
community_actor_id: &Url,
) -> Result<Vec<Value>, Error> {
use lemmy_db_schema::schema::activity::dsl::*;
let res: Vec<Value> = activity
@ -110,8 +110,7 @@ impl Activity_ for Activity {
.sql(" AND activity.data -> 'object' ->> 'type' = 'Create'")
.sql(" AND activity.data -> 'object' -> 'object' ->> 'type' = 'Page'")
.sql(" AND activity.data ->> 'actor' = ")
.bind::<Text, _>(community_actor_id)
.sql(" ORDER BY activity.published DESC"),
.bind::<Text, _>(community_actor_id),
)
.limit(20)
.get_results(conn)?;
@ -121,33 +120,45 @@ impl Activity_ for Activity {
#[cfg(test)]
mod tests {
use super::*;
use crate::{establish_unpooled_connection, source::activity::Activity_};
use crate::{
establish_unpooled_connection,
source::activity::Activity_,
Crud,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
activity::{Activity, ActivityForm},
person::{Person, PersonForm},
user::{UserForm, User_},
};
use serde_json::Value;
use serial_test::serial;
use url::Url;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let creator_form = PersonForm {
let creator_form = UserForm {
name: "activity_creator_pm".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -155,13 +166,10 @@ mod tests {
shared_inbox_url: None,
};
let inserted_creator = Person::create(&conn, &creator_form).unwrap();
let inserted_creator = User_::create(&conn, &creator_form).unwrap();
let ap_id: DbUrl = Url::parse(
"https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c",
)
.unwrap()
.into();
let ap_id =
"https://enterprise.lemmy.ml/activities/delete/f1b5d57c-80f8-4e03-a615-688d552e946c";
let test_json: Value = serde_json::from_str(
r#"{
"@context": "https://www.w3.org/ns/activitystreams",
@ -177,7 +185,7 @@ mod tests {
)
.unwrap();
let activity_form = ActivityForm {
ap_id: ap_id.clone(),
ap_id: ap_id.to_string(),
data: test_json.to_owned(),
local: true,
sensitive: false,
@ -187,7 +195,7 @@ mod tests {
let inserted_activity = Activity::create(&conn, &activity_form).unwrap();
let expected_activity = Activity {
ap_id: Some(ap_id.clone()),
ap_id: Some(ap_id.to_string()),
id: inserted_activity.id,
data: test_json,
local: true,
@ -197,8 +205,8 @@ mod tests {
};
let read_activity = Activity::read(&conn, inserted_activity.id).unwrap();
let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, &ap_id).unwrap();
Person::delete(&conn, inserted_creator.id).unwrap();
let read_activity_by_apub_id = Activity::read_from_apub_id(&conn, ap_id).unwrap();
User_::delete(&conn, inserted_creator.id).unwrap();
Activity::delete(&conn, inserted_activity.id).unwrap();
assert_eq!(expected_activity, read_activity);

View file

@ -0,0 +1,54 @@
use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{schema::category::dsl::*, source::category::*};
impl Crud<CategoryForm> for Category {
fn read(conn: &PgConnection, category_id: i32) -> Result<Self, Error> {
category.find(category_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, new_category: &CategoryForm) -> Result<Self, Error> {
insert_into(category)
.values(new_category)
.get_result::<Self>(conn)
}
fn update(
conn: &PgConnection,
category_id: i32,
new_category: &CategoryForm,
) -> Result<Self, Error> {
diesel::update(category.find(category_id))
.set(new_category)
.get_result::<Self>(conn)
}
}
pub trait Category_ {
fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error>;
}
impl Category_ for Category {
fn list_all(conn: &PgConnection) -> Result<Vec<Category>, Error> {
category.load::<Self>(conn)
}
}
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, source::category::Category_};
use lemmy_db_schema::source::category::Category;
#[test]
fn test_crud() {
let conn = establish_unpooled_connection();
let categories = Category::list_all(&conn).unwrap();
let expected_first_category = Category {
id: 1,
name: "Discussion".into(),
};
assert_eq!(expected_first_category, categories[0]);
}
}

View file

@ -10,54 +10,40 @@ use lemmy_db_schema::{
CommentSaved,
CommentSavedForm,
},
CommentId,
DbUrl,
PersonId,
Url,
};
pub trait Comment_ {
fn update_ap_id(
conn: &PgConnection,
comment_id: CommentId,
apub_id: DbUrl,
) -> Result<Comment, Error>;
fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: Url) -> Result<Comment, Error>;
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_creator_id: i32,
) -> Result<Vec<Comment>, Error>;
fn update_deleted(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
new_deleted: bool,
) -> Result<Comment, Error>;
fn update_removed(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
new_removed: bool,
) -> Result<Comment, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Comment>, Error>;
fn update_read(
conn: &PgConnection,
comment_id: CommentId,
new_read: bool,
) -> Result<Comment, Error>;
fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Comment, Error>;
fn update_content(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
new_content: &str,
) -> Result<Comment, Error>;
}
impl Comment_ for Comment {
fn update_ap_id(
conn: &PgConnection,
comment_id: CommentId,
apub_id: DbUrl,
) -> Result<Self, Error> {
fn update_ap_id(conn: &PgConnection, comment_id: i32, apub_id: Url) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
@ -65,10 +51,7 @@ impl Comment_ for Comment {
.get_result::<Self>(conn)
}
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.filter(creator_id.eq(for_creator_id)))
.set((
@ -81,7 +64,7 @@ impl Comment_ for Comment {
fn update_deleted(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
new_deleted: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
@ -92,7 +75,7 @@ impl Comment_ for Comment {
fn update_removed(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
new_removed: bool,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
@ -103,7 +86,7 @@ impl Comment_ for Comment {
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
@ -112,11 +95,7 @@ impl Comment_ for Comment {
.get_results::<Self>(conn)
}
fn update_read(
conn: &PgConnection,
comment_id: CommentId,
new_read: bool,
) -> Result<Self, Error> {
fn update_read(conn: &PgConnection, comment_id: i32, new_read: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::update(comment.find(comment_id))
.set(read.eq(new_read))
@ -125,7 +104,7 @@ impl Comment_ for Comment {
fn update_content(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
new_content: &str,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
@ -135,13 +114,13 @@ impl Comment_ for Comment {
}
}
impl Crud<CommentForm, CommentId> for Comment {
fn read(conn: &PgConnection, comment_id: CommentId) -> Result<Self, Error> {
impl Crud<CommentForm> for Comment {
fn read(conn: &PgConnection, comment_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
comment.find(comment_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, comment_id: CommentId) -> Result<usize, Error> {
fn delete(conn: &PgConnection, comment_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
diesel::delete(comment.find(comment_id)).execute(conn)
}
@ -155,7 +134,7 @@ impl Crud<CommentForm, CommentId> for Comment {
fn update(
conn: &PgConnection,
comment_id: CommentId,
comment_id: i32,
comment_form: &CommentForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
@ -166,7 +145,7 @@ impl Crud<CommentForm, CommentId> for Comment {
}
impl ApubObject<CommentForm> for Comment {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment::dsl::*;
comment.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
@ -182,26 +161,22 @@ impl ApubObject<CommentForm> for Comment {
}
}
impl Likeable<CommentLikeForm, CommentId> for CommentLike {
impl Likeable<CommentLikeForm> for CommentLike {
fn like(conn: &PgConnection, comment_like_form: &CommentLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::comment_like::dsl::*;
insert_into(comment_like)
.values(comment_like_form)
.on_conflict((comment_id, person_id))
.on_conflict((comment_id, user_id))
.do_update()
.set(comment_like_form)
.get_result::<Self>(conn)
}
fn remove(
conn: &PgConnection,
person_id: PersonId,
comment_id: CommentId,
) -> Result<usize, Error> {
fn remove(conn: &PgConnection, user_id: i32, comment_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::comment_like::dsl;
diesel::delete(
dsl::comment_like
.filter(dsl::comment_id.eq(comment_id))
.filter(dsl::person_id.eq(person_id)),
.filter(dsl::user_id.eq(user_id)),
)
.execute(conn)
}
@ -212,7 +187,7 @@ impl Saveable<CommentSavedForm> for CommentSaved {
use lemmy_db_schema::schema::comment_saved::dsl::*;
insert_into(comment_saved)
.values(comment_saved_form)
.on_conflict((comment_id, person_id))
.on_conflict((comment_id, user_id))
.do_update()
.set(comment_saved_form)
.get_result::<Self>(conn)
@ -222,7 +197,7 @@ impl Saveable<CommentSavedForm> for CommentSaved {
diesel::delete(
comment_saved
.filter(comment_id.eq(comment_saved_form.comment_id))
.filter(person_id.eq(comment_saved_form.person_id)),
.filter(user_id.eq(comment_saved_form.user_id)),
)
.execute(conn)
}
@ -230,32 +205,40 @@ impl Saveable<CommentSavedForm> for CommentSaved {
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, Crud, Likeable, Saveable};
use crate::{establish_unpooled_connection, Crud, Likeable, ListingType, Saveable, SortType};
use lemmy_db_schema::source::{
comment::*,
community::{Community, CommunityForm},
person::{Person, PersonForm},
post::*,
user::{UserForm, User_},
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "terry".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -263,13 +246,14 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let new_community = CommunityForm {
name: "test community".to_string(),
title: "nada".to_owned(),
description: None,
creator_id: inserted_person.id,
category_id: 1,
creator_id: inserted_user.id,
removed: None,
deleted: None,
updated: None,
@ -291,7 +275,7 @@ mod tests {
let new_post = PostForm {
name: "A test post".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
url: None,
body: None,
community_id: inserted_community.id,
@ -314,7 +298,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -331,7 +315,7 @@ mod tests {
let expected_comment = Comment {
id: inserted_comment.id,
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: false,
deleted: false,
@ -345,7 +329,7 @@ mod tests {
let child_comment_form = CommentForm {
content: "A child comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
parent_id: Some(inserted_comment.id),
removed: None,
@ -363,7 +347,7 @@ mod tests {
let comment_like_form = CommentLikeForm {
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
score: 1,
};
@ -373,7 +357,7 @@ mod tests {
id: inserted_comment_like.id,
comment_id: inserted_comment.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
published: inserted_comment_like.published,
score: 1,
};
@ -381,7 +365,7 @@ mod tests {
// Comment Saved
let comment_saved_form = CommentSavedForm {
comment_id: inserted_comment.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
};
let inserted_comment_saved = CommentSaved::save(&conn, &comment_saved_form).unwrap();
@ -389,19 +373,19 @@ mod tests {
let expected_comment_saved = CommentSaved {
id: inserted_comment_saved.id,
comment_id: inserted_comment.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
published: inserted_comment_saved.published,
};
let read_comment = Comment::read(&conn, inserted_comment.id).unwrap();
let updated_comment = Comment::update(&conn, inserted_comment.id, &comment_form).unwrap();
let like_removed = CommentLike::remove(&conn, inserted_person.id, inserted_comment.id).unwrap();
let like_removed = CommentLike::remove(&conn, inserted_user.id, inserted_comment.id).unwrap();
let saved_removed = CommentSaved::unsave(&conn, &comment_saved_form).unwrap();
let num_deleted = Comment::delete(&conn, inserted_comment.id).unwrap();
Comment::delete(&conn, inserted_child_comment.id).unwrap();
Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
Person::delete(&conn, inserted_person.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(expected_comment, read_comment);
assert_eq!(expected_comment, inserted_comment);

View file

@ -3,7 +3,6 @@ use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
naive_now,
source::comment_report::{CommentReport, CommentReportForm},
PersonId,
};
impl Reportable<CommentReportForm> for CommentReport {
@ -23,11 +22,7 @@ impl Reportable<CommentReportForm> for CommentReport {
/// * `conn` - the postgres connection
/// * `report_id` - the id of the report to resolve
/// * `by_resolver_id` - the id of the user resolving the report
fn resolve(
conn: &PgConnection,
report_id: i32,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::comment_report::dsl::*;
update(comment_report.find(report_id))
.set((
@ -43,11 +38,7 @@ impl Reportable<CommentReportForm> for CommentReport {
/// * `conn` - the postgres connection
/// * `report_id` - the id of the report to unresolve
/// * `by_resolver_id` - the id of the user unresolving the report
fn unresolve(
conn: &PgConnection,
report_id: i32,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::comment_report::dsl::*;
update(comment_report.find(report_id))
.set((

View file

@ -9,12 +9,10 @@ use lemmy_db_schema::{
CommunityForm,
CommunityModerator,
CommunityModeratorForm,
CommunityPersonBan,
CommunityPersonBanForm,
CommunityUserBan,
CommunityUserBanForm,
},
CommunityId,
DbUrl,
PersonId,
Url,
};
mod safe_type {
@ -26,6 +24,7 @@ mod safe_type {
name,
title,
description,
category_id,
creator_id,
removed,
published,
@ -46,6 +45,7 @@ mod safe_type {
name,
title,
description,
category_id,
creator_id,
removed,
published,
@ -61,13 +61,13 @@ mod safe_type {
}
}
impl Crud<CommunityForm, CommunityId> for Community {
fn read(conn: &PgConnection, community_id: CommunityId) -> Result<Self, Error> {
impl Crud<CommunityForm> for Community {
fn read(conn: &PgConnection, community_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community.find(community_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, community_id: CommunityId) -> Result<usize, Error> {
fn delete(conn: &PgConnection, community_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::community::dsl::*;
diesel::delete(community.find(community_id)).execute(conn)
}
@ -81,7 +81,7 @@ impl Crud<CommunityForm, CommunityId> for Community {
fn update(
conn: &PgConnection,
community_id: CommunityId,
community_id: i32,
new_community: &CommunityForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::community::dsl::*;
@ -92,7 +92,7 @@ impl Crud<CommunityForm, CommunityId> for Community {
}
impl ApubObject<CommunityForm> for Community {
fn read_from_apub_id(conn: &PgConnection, for_actor_id: &DbUrl) -> Result<Self, Error> {
fn read_from_apub_id(conn: &PgConnection, for_actor_id: &Url) -> Result<Self, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community
.filter(actor_id.eq(for_actor_id))
@ -114,29 +114,26 @@ pub trait Community_ {
fn read_from_name(conn: &PgConnection, community_name: &str) -> Result<Community, Error>;
fn update_deleted(
conn: &PgConnection,
community_id: CommunityId,
community_id: i32,
new_deleted: bool,
) -> Result<Community, Error>;
fn update_removed(
conn: &PgConnection,
community_id: CommunityId,
community_id: i32,
new_removed: bool,
) -> Result<Community, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Community>, Error>;
fn update_creator(
conn: &PgConnection,
community_id: CommunityId,
new_creator_id: PersonId,
community_id: i32,
new_creator_id: i32,
) -> Result<Community, Error>;
fn distinct_federated_communities(conn: &PgConnection) -> Result<Vec<String>, Error>;
fn read_from_followers_url(
conn: &PgConnection,
followers_url: &DbUrl,
) -> Result<Community, Error>;
fn read_from_followers_url(conn: &PgConnection, followers_url: &Url) -> Result<Community, Error>;
}
impl Community_ for Community {
@ -150,7 +147,7 @@ impl Community_ for Community {
fn update_deleted(
conn: &PgConnection,
community_id: CommunityId,
community_id: i32,
new_deleted: bool,
) -> Result<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
@ -161,7 +158,7 @@ impl Community_ for Community {
fn update_removed(
conn: &PgConnection,
community_id: CommunityId,
community_id: i32,
new_removed: bool,
) -> Result<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
@ -172,7 +169,7 @@ impl Community_ for Community {
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_creator_id: i32,
new_removed: bool,
) -> Result<Vec<Community>, Error> {
use lemmy_db_schema::schema::community::dsl::*;
@ -183,8 +180,8 @@ impl Community_ for Community {
fn update_creator(
conn: &PgConnection,
community_id: CommunityId,
new_creator_id: PersonId,
community_id: i32,
new_creator_id: i32,
) -> Result<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
diesel::update(community.find(community_id))
@ -199,7 +196,7 @@ impl Community_ for Community {
fn read_from_followers_url(
conn: &PgConnection,
followers_url_: &DbUrl,
followers_url_: &Url,
) -> Result<Community, Error> {
use lemmy_db_schema::schema::community::dsl::*;
community
@ -211,80 +208,74 @@ impl Community_ for Community {
impl Joinable<CommunityModeratorForm> for CommunityModerator {
fn join(
conn: &PgConnection,
community_moderator_form: &CommunityModeratorForm,
community_user_form: &CommunityModeratorForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::community_moderator::dsl::*;
insert_into(community_moderator)
.values(community_moderator_form)
.values(community_user_form)
.get_result::<Self>(conn)
}
fn leave(
conn: &PgConnection,
community_moderator_form: &CommunityModeratorForm,
community_user_form: &CommunityModeratorForm,
) -> Result<usize, Error> {
use lemmy_db_schema::schema::community_moderator::dsl::*;
diesel::delete(
community_moderator
.filter(community_id.eq(community_moderator_form.community_id))
.filter(person_id.eq(community_moderator_form.person_id)),
.filter(community_id.eq(community_user_form.community_id))
.filter(user_id.eq(community_user_form.user_id)),
)
.execute(conn)
}
}
pub trait CommunityModerator_ {
fn delete_for_community(
fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error>;
fn get_user_moderated_communities(
conn: &PgConnection,
for_community_id: CommunityId,
) -> Result<usize, Error>;
fn get_person_moderated_communities(
conn: &PgConnection,
for_person_id: PersonId,
) -> Result<Vec<CommunityId>, Error>;
for_user_id: i32,
) -> Result<Vec<i32>, Error>;
}
impl CommunityModerator_ for CommunityModerator {
fn delete_for_community(
conn: &PgConnection,
for_community_id: CommunityId,
) -> Result<usize, Error> {
fn delete_for_community(conn: &PgConnection, for_community_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::community_moderator::dsl::*;
diesel::delete(community_moderator.filter(community_id.eq(for_community_id))).execute(conn)
}
fn get_person_moderated_communities(
fn get_user_moderated_communities(
conn: &PgConnection,
for_person_id: PersonId,
) -> Result<Vec<CommunityId>, Error> {
for_user_id: i32,
) -> Result<Vec<i32>, Error> {
use lemmy_db_schema::schema::community_moderator::dsl::*;
community_moderator
.filter(person_id.eq(for_person_id))
.filter(user_id.eq(for_user_id))
.select(community_id)
.load::<CommunityId>(conn)
.load::<i32>(conn)
}
}
impl Bannable<CommunityPersonBanForm> for CommunityPersonBan {
impl Bannable<CommunityUserBanForm> for CommunityUserBan {
fn ban(
conn: &PgConnection,
community_person_ban_form: &CommunityPersonBanForm,
community_user_ban_form: &CommunityUserBanForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::community_person_ban::dsl::*;
insert_into(community_person_ban)
.values(community_person_ban_form)
use lemmy_db_schema::schema::community_user_ban::dsl::*;
insert_into(community_user_ban)
.values(community_user_ban_form)
.get_result::<Self>(conn)
}
fn unban(
conn: &PgConnection,
community_person_ban_form: &CommunityPersonBanForm,
community_user_ban_form: &CommunityUserBanForm,
) -> Result<usize, Error> {
use lemmy_db_schema::schema::community_person_ban::dsl::*;
use lemmy_db_schema::schema::community_user_ban::dsl::*;
diesel::delete(
community_person_ban
.filter(community_id.eq(community_person_ban_form.community_id))
.filter(person_id.eq(community_person_ban_form.person_id)),
community_user_ban
.filter(community_id.eq(community_user_ban_form.community_id))
.filter(user_id.eq(community_user_ban_form.user_id)),
)
.execute(conn)
}
@ -298,16 +289,12 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
use lemmy_db_schema::schema::community_follower::dsl::*;
insert_into(community_follower)
.values(community_follower_form)
.on_conflict((community_id, person_id))
.on_conflict((community_id, user_id))
.do_update()
.set(community_follower_form)
.get_result::<Self>(conn)
}
fn follow_accepted(
conn: &PgConnection,
community_id_: CommunityId,
person_id_: PersonId,
) -> Result<Self, Error>
fn follow_accepted(conn: &PgConnection, community_id_: i32, user_id_: i32) -> Result<Self, Error>
where
Self: Sized,
{
@ -315,7 +302,7 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
diesel::update(
community_follower
.filter(community_id.eq(community_id_))
.filter(person_id.eq(person_id_)),
.filter(user_id.eq(user_id_)),
)
.set(pending.eq(true))
.get_result::<Self>(conn)
@ -328,13 +315,13 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
diesel::delete(
community_follower
.filter(community_id.eq(&community_follower_form.community_id))
.filter(person_id.eq(&community_follower_form.person_id)),
.filter(user_id.eq(&community_follower_form.user_id)),
)
.execute(conn)
}
// TODO: this function name only makes sense if you call it with a remote community. for a local
// community, it will also return true if only remote followers exist
fn has_local_followers(conn: &PgConnection, community_id_: CommunityId) -> Result<bool, Error> {
fn has_local_followers(conn: &PgConnection, community_id_: i32) -> Result<bool, Error> {
use lemmy_db_schema::schema::community_follower::dsl::*;
diesel::select(exists(
community_follower.filter(community_id.eq(community_id_)),
@ -345,27 +332,43 @@ impl Followable<CommunityFollowerForm> for CommunityFollower {
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, Bannable, Crud, Followable, Joinable};
use lemmy_db_schema::source::{community::*, person::*};
use serial_test::serial;
use crate::{
establish_unpooled_connection,
Bannable,
Crud,
Followable,
Joinable,
ListingType,
SortType,
};
use lemmy_db_schema::source::{community::*, user::*};
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "bobbee".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -373,13 +376,14 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let new_community = CommunityForm {
name: "TIL".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: None,
deleted: None,
@ -401,10 +405,11 @@ mod tests {
let expected_community = Community {
id: inserted_community.id,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
name: "TIL".into(),
title: "nada".to_owned(),
description: None,
category_id: 1,
nsfw: false,
removed: false,
deleted: false,
@ -424,7 +429,7 @@ mod tests {
let community_follower_form = CommunityFollowerForm {
community_id: inserted_community.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
pending: false,
};
@ -434,56 +439,55 @@ mod tests {
let expected_community_follower = CommunityFollower {
id: inserted_community_follower.id,
community_id: inserted_community.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
pending: Some(false),
published: inserted_community_follower.published,
};
let community_moderator_form = CommunityModeratorForm {
let community_user_form = CommunityModeratorForm {
community_id: inserted_community.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
};
let inserted_community_moderator =
CommunityModerator::join(&conn, &community_moderator_form).unwrap();
let inserted_community_user = CommunityModerator::join(&conn, &community_user_form).unwrap();
let expected_community_moderator = CommunityModerator {
id: inserted_community_moderator.id,
let expected_community_user = CommunityModerator {
id: inserted_community_user.id,
community_id: inserted_community.id,
person_id: inserted_person.id,
published: inserted_community_moderator.published,
user_id: inserted_user.id,
published: inserted_community_user.published,
};
let community_person_ban_form = CommunityPersonBanForm {
let community_user_ban_form = CommunityUserBanForm {
community_id: inserted_community.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
};
let inserted_community_person_ban =
CommunityPersonBan::ban(&conn, &community_person_ban_form).unwrap();
let inserted_community_user_ban =
CommunityUserBan::ban(&conn, &community_user_ban_form).unwrap();
let expected_community_person_ban = CommunityPersonBan {
id: inserted_community_person_ban.id,
let expected_community_user_ban = CommunityUserBan {
id: inserted_community_user_ban.id,
community_id: inserted_community.id,
person_id: inserted_person.id,
published: inserted_community_person_ban.published,
user_id: inserted_user.id,
published: inserted_community_user_ban.published,
};
let read_community = Community::read(&conn, inserted_community.id).unwrap();
let updated_community =
Community::update(&conn, inserted_community.id, &new_community).unwrap();
let ignored_community = CommunityFollower::unfollow(&conn, &community_follower_form).unwrap();
let left_community = CommunityModerator::leave(&conn, &community_moderator_form).unwrap();
let unban = CommunityPersonBan::unban(&conn, &community_person_ban_form).unwrap();
let left_community = CommunityModerator::leave(&conn, &community_user_form).unwrap();
let unban = CommunityUserBan::unban(&conn, &community_user_ban_form).unwrap();
let num_deleted = Community::delete(&conn, inserted_community.id).unwrap();
Person::delete(&conn, inserted_person.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(expected_community, read_community);
assert_eq!(expected_community, inserted_community);
assert_eq!(expected_community, updated_community);
assert_eq!(expected_community_follower, inserted_community_follower);
assert_eq!(expected_community_moderator, inserted_community_moderator);
assert_eq!(expected_community_person_ban, inserted_community_person_ban);
assert_eq!(expected_community_user, inserted_community_user);
assert_eq!(expected_community_user_ban, inserted_community_user_ban);
assert_eq!(1, ignored_community);
assert_eq!(1, left_community);
assert_eq!(1, unban);

View file

@ -1,119 +0,0 @@
use crate::Crud;
use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
naive_now,
schema::local_user::dsl::*,
source::local_user::{LocalUser, LocalUserForm},
LocalUserId,
PersonId,
};
mod safe_settings_type {
use crate::ToSafeSettings;
use lemmy_db_schema::{schema::local_user::columns::*, source::local_user::LocalUser};
type Columns = (
id,
person_id,
email,
admin,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
matrix_user_id,
validator_time,
);
impl ToSafeSettings for LocalUser {
type SafeSettingsColumns = Columns;
/// Includes everything but the hashed password
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
(
id,
person_id,
email,
admin,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
matrix_user_id,
validator_time,
)
}
}
}
pub trait LocalUser_ {
fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<LocalUser, Error>;
fn update_password(
conn: &PgConnection,
local_user_id: LocalUserId,
new_password: &str,
) -> Result<LocalUser, Error>;
fn add_admin(conn: &PgConnection, person_id: PersonId, added: bool) -> Result<LocalUser, Error>;
}
impl LocalUser_ for LocalUser {
fn register(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
let mut edited_user = form.clone();
let password_hash =
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
edited_user.password_encrypted = password_hash;
Self::create(&conn, &edited_user)
}
fn update_password(
conn: &PgConnection,
local_user_id: LocalUserId,
new_password: &str,
) -> Result<Self, Error> {
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
diesel::update(local_user.find(local_user_id))
.set((
password_encrypted.eq(password_hash),
validator_time.eq(naive_now()),
))
.get_result::<Self>(conn)
}
fn add_admin(conn: &PgConnection, for_person_id: PersonId, added: bool) -> Result<Self, Error> {
diesel::update(local_user.filter(person_id.eq(for_person_id)))
.set(admin.eq(added))
.get_result::<Self>(conn)
}
}
impl Crud<LocalUserForm, LocalUserId> for LocalUser {
fn read(conn: &PgConnection, local_user_id: LocalUserId) -> Result<Self, Error> {
local_user.find(local_user_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, local_user_id: LocalUserId) -> Result<usize, Error> {
diesel::delete(local_user.find(local_user_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &LocalUserForm) -> Result<Self, Error> {
insert_into(local_user)
.values(form)
.get_result::<Self>(conn)
}
fn update(
conn: &PgConnection,
local_user_id: LocalUserId,
form: &LocalUserForm,
) -> Result<Self, Error> {
diesel::update(local_user.find(local_user_id))
.set(form)
.get_result::<Self>(conn)
}
}

View file

@ -1,13 +1,13 @@
pub mod activity;
pub mod category;
pub mod comment;
pub mod comment_report;
pub mod community;
pub mod local_user;
pub mod moderator;
pub mod password_reset_request;
pub mod person;
pub mod person_mention;
pub mod post;
pub mod post_report;
pub mod private_message;
pub mod site;
pub mod user;
pub mod user_mention;

View file

@ -2,7 +2,7 @@ use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::source::moderator::*;
impl Crud<ModRemovePostForm, i32> for ModRemovePost {
impl Crud<ModRemovePostForm> for ModRemovePost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_remove_post::dsl::*;
mod_remove_post.find(from_id).first::<Self>(conn)
@ -23,7 +23,7 @@ impl Crud<ModRemovePostForm, i32> for ModRemovePost {
}
}
impl Crud<ModLockPostForm, i32> for ModLockPost {
impl Crud<ModLockPostForm> for ModLockPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_lock_post::dsl::*;
mod_lock_post.find(from_id).first::<Self>(conn)
@ -44,7 +44,7 @@ impl Crud<ModLockPostForm, i32> for ModLockPost {
}
}
impl Crud<ModStickyPostForm, i32> for ModStickyPost {
impl Crud<ModStickyPostForm> for ModStickyPost {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_sticky_post::dsl::*;
mod_sticky_post.find(from_id).first::<Self>(conn)
@ -65,7 +65,7 @@ impl Crud<ModStickyPostForm, i32> for ModStickyPost {
}
}
impl Crud<ModRemoveCommentForm, i32> for ModRemoveComment {
impl Crud<ModRemoveCommentForm> for ModRemoveComment {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_remove_comment::dsl::*;
mod_remove_comment.find(from_id).first::<Self>(conn)
@ -86,7 +86,7 @@ impl Crud<ModRemoveCommentForm, i32> for ModRemoveComment {
}
}
impl Crud<ModRemoveCommunityForm, i32> for ModRemoveCommunity {
impl Crud<ModRemoveCommunityForm> for ModRemoveCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_remove_community::dsl::*;
mod_remove_community.find(from_id).first::<Self>(conn)
@ -111,7 +111,7 @@ impl Crud<ModRemoveCommunityForm, i32> for ModRemoveCommunity {
}
}
impl Crud<ModBanFromCommunityForm, i32> for ModBanFromCommunity {
impl Crud<ModBanFromCommunityForm> for ModBanFromCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_ban_from_community::dsl::*;
mod_ban_from_community.find(from_id).first::<Self>(conn)
@ -136,7 +136,7 @@ impl Crud<ModBanFromCommunityForm, i32> for ModBanFromCommunity {
}
}
impl Crud<ModBanForm, i32> for ModBan {
impl Crud<ModBanForm> for ModBan {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_ban::dsl::*;
mod_ban.find(from_id).first::<Self>(conn)
@ -155,7 +155,7 @@ impl Crud<ModBanForm, i32> for ModBan {
}
}
impl Crud<ModAddCommunityForm, i32> for ModAddCommunity {
impl Crud<ModAddCommunityForm> for ModAddCommunity {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_add_community::dsl::*;
mod_add_community.find(from_id).first::<Self>(conn)
@ -176,7 +176,7 @@ impl Crud<ModAddCommunityForm, i32> for ModAddCommunity {
}
}
impl Crud<ModAddForm, i32> for ModAdd {
impl Crud<ModAddForm> for ModAdd {
fn read(conn: &PgConnection, from_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::mod_add::dsl::*;
mod_add.find(from_id).first::<Self>(conn)
@ -197,28 +197,36 @@ impl Crud<ModAddForm, i32> for ModAdd {
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, Crud};
use lemmy_db_schema::source::{comment::*, community::*, moderator::*, person::*, post::*};
use serial_test::serial;
use crate::{establish_unpooled_connection, Crud, ListingType, SortType};
use lemmy_db_schema::source::{comment::*, community::*, moderator::*, post::*, user::*};
// use Crud;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_mod = PersonForm {
let new_mod = UserForm {
name: "the mod".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -226,20 +234,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_mod = Person::create(&conn, &new_mod).unwrap();
let inserted_mod = User_::create(&conn, &new_mod).unwrap();
let new_person = PersonForm {
let new_user = UserForm {
name: "jim2".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -247,13 +265,14 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let new_community = CommunityForm {
name: "mod_community".to_string(),
title: "nada".to_owned(),
description: None,
creator_id: inserted_person.id,
category_id: 1,
creator_id: inserted_user.id,
removed: None,
deleted: None,
updated: None,
@ -277,7 +296,7 @@ mod tests {
name: "A test post thweep".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -298,7 +317,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -316,7 +335,7 @@ mod tests {
// remove post
let mod_remove_post_form = ModRemovePostForm {
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
post_id: inserted_post.id,
reason: None,
removed: None,
@ -326,7 +345,7 @@ mod tests {
let expected_mod_remove_post = ModRemovePost {
id: inserted_mod_remove_post.id,
post_id: inserted_post.id,
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
reason: None,
removed: Some(true),
when_: inserted_mod_remove_post.when_,
@ -335,7 +354,7 @@ mod tests {
// lock post
let mod_lock_post_form = ModLockPostForm {
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
post_id: inserted_post.id,
locked: None,
};
@ -344,7 +363,7 @@ mod tests {
let expected_mod_lock_post = ModLockPost {
id: inserted_mod_lock_post.id,
post_id: inserted_post.id,
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
locked: Some(true),
when_: inserted_mod_lock_post.when_,
};
@ -352,7 +371,7 @@ mod tests {
// sticky post
let mod_sticky_post_form = ModStickyPostForm {
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
post_id: inserted_post.id,
stickied: None,
};
@ -361,7 +380,7 @@ mod tests {
let expected_mod_sticky_post = ModStickyPost {
id: inserted_mod_sticky_post.id,
post_id: inserted_post.id,
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
stickied: Some(true),
when_: inserted_mod_sticky_post.when_,
};
@ -369,7 +388,7 @@ mod tests {
// comment
let mod_remove_comment_form = ModRemoveCommentForm {
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
comment_id: inserted_comment.id,
reason: None,
removed: None,
@ -381,7 +400,7 @@ mod tests {
let expected_mod_remove_comment = ModRemoveComment {
id: inserted_mod_remove_comment.id,
comment_id: inserted_comment.id,
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
reason: None,
removed: Some(true),
when_: inserted_mod_remove_comment.when_,
@ -390,7 +409,7 @@ mod tests {
// community
let mod_remove_community_form = ModRemoveCommunityForm {
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
community_id: inserted_community.id,
reason: None,
removed: None,
@ -403,7 +422,7 @@ mod tests {
let expected_mod_remove_community = ModRemoveCommunity {
id: inserted_mod_remove_community.id,
community_id: inserted_community.id,
mod_person_id: inserted_mod.id,
mod_user_id: inserted_mod.id,
reason: None,
removed: Some(true),
expires: None,
@ -413,8 +432,8 @@ mod tests {
// ban from community
let mod_ban_from_community_form = ModBanFromCommunityForm {
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
community_id: inserted_community.id,
reason: None,
banned: None,
@ -427,8 +446,8 @@ mod tests {
let expected_mod_ban_from_community = ModBanFromCommunity {
id: inserted_mod_ban_from_community.id,
community_id: inserted_community.id,
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
reason: None,
banned: Some(true),
expires: None,
@ -438,8 +457,8 @@ mod tests {
// ban
let mod_ban_form = ModBanForm {
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
reason: None,
banned: None,
expires: None,
@ -448,8 +467,8 @@ mod tests {
let read_mod_ban = ModBan::read(&conn, inserted_mod_ban.id).unwrap();
let expected_mod_ban = ModBan {
id: inserted_mod_ban.id,
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
reason: None,
banned: Some(true),
expires: None,
@ -459,8 +478,8 @@ mod tests {
// mod add community
let mod_add_community_form = ModAddCommunityForm {
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
};
@ -471,8 +490,8 @@ mod tests {
let expected_mod_add_community = ModAddCommunity {
id: inserted_mod_add_community.id,
community_id: inserted_community.id,
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
removed: Some(false),
when_: inserted_mod_add_community.when_,
};
@ -480,16 +499,16 @@ mod tests {
// mod add
let mod_add_form = ModAddForm {
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
removed: None,
};
let inserted_mod_add = ModAdd::create(&conn, &mod_add_form).unwrap();
let read_mod_add = ModAdd::read(&conn, inserted_mod_add.id).unwrap();
let expected_mod_add = ModAdd {
id: inserted_mod_add.id,
mod_person_id: inserted_mod.id,
other_person_id: inserted_person.id,
mod_user_id: inserted_mod.id,
other_user_id: inserted_user.id,
removed: Some(false),
when_: inserted_mod_add.when_,
};
@ -497,8 +516,8 @@ mod tests {
Comment::delete(&conn, inserted_comment.id).unwrap();
Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
Person::delete(&conn, inserted_person.id).unwrap();
Person::delete(&conn, inserted_mod.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
User_::delete(&conn, inserted_mod.id).unwrap();
assert_eq!(expected_mod_remove_post, read_mod_remove_post);
assert_eq!(expected_mod_lock_post, read_mod_lock_post);

View file

@ -1,13 +1,9 @@
use crate::Crud;
use diesel::{dsl::*, result::Error, PgConnection, *};
use lemmy_db_schema::{
schema::password_reset_request::dsl::*,
source::password_reset_request::*,
LocalUserId,
};
use lemmy_db_schema::{schema::password_reset_request::dsl::*, source::password_reset_request::*};
use sha2::{Digest, Sha256};
impl Crud<PasswordResetRequestForm, i32> for PasswordResetRequest {
impl Crud<PasswordResetRequestForm> for PasswordResetRequest {
fn read(conn: &PgConnection, password_reset_request_id: i32) -> Result<Self, Error> {
password_reset_request
.find(password_reset_request_id)
@ -32,7 +28,7 @@ impl Crud<PasswordResetRequestForm, i32> for PasswordResetRequest {
pub trait PasswordResetRequest_ {
fn create_token(
conn: &PgConnection,
from_local_user_id: LocalUserId,
from_user_id: i32,
token: &str,
) -> Result<PasswordResetRequest, Error>;
fn read_from_token(conn: &PgConnection, token: &str) -> Result<PasswordResetRequest, Error>;
@ -41,7 +37,7 @@ pub trait PasswordResetRequest_ {
impl PasswordResetRequest_ for PasswordResetRequest {
fn create_token(
conn: &PgConnection,
from_local_user_id: LocalUserId,
from_user_id: i32,
token: &str,
) -> Result<PasswordResetRequest, Error> {
let mut hasher = Sha256::new();
@ -49,7 +45,7 @@ impl PasswordResetRequest_ for PasswordResetRequest {
let token_hash: String = bytes_to_hex(hasher.finalize().to_vec());
let form = PasswordResetRequestForm {
local_user_id: from_local_user_id,
user_id: from_user_id,
token_encrypted: token_hash,
};
@ -80,31 +76,37 @@ mod tests {
establish_unpooled_connection,
source::password_reset_request::PasswordResetRequest_,
Crud,
ListingType,
SortType,
};
use lemmy_db_schema::source::{
local_user::{LocalUser, LocalUserForm},
password_reset_request::PasswordResetRequest,
person::*,
};
use serial_test::serial;
use lemmy_db_schema::source::{password_reset_request::PasswordResetRequest, user::*};
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "thommy prw".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -112,40 +114,23 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let new_local_user = LocalUserForm {
person_id: inserted_person.id,
password_encrypted: "pass".to_string(),
email: None,
matrix_user_id: None,
admin: None,
show_nsfw: None,
theme: None,
default_sort_type: None,
default_listing_type: None,
lang: None,
show_avatars: None,
send_notifications_to_email: None,
};
let inserted_local_user = LocalUser::create(&conn, &new_local_user).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let token = "nope";
let token_encrypted_ = "ca3704aa0b06f5954c79ee837faa152d84d6b2d42838f0637a15eda8337dbdce";
let inserted_password_reset_request =
PasswordResetRequest::create_token(&conn, inserted_local_user.id, token).unwrap();
PasswordResetRequest::create_token(&conn, inserted_user.id, token).unwrap();
let expected_password_reset_request = PasswordResetRequest {
id: inserted_password_reset_request.id,
local_user_id: inserted_local_user.id,
user_id: inserted_user.id,
token_encrypted: token_encrypted_.to_string(),
published: inserted_password_reset_request.published,
};
let read_password_reset_request = PasswordResetRequest::read_from_token(&conn, token).unwrap();
let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(expected_password_reset_request, read_password_reset_request);
assert_eq!(

View file

@ -1,290 +0,0 @@
use crate::{ApubObject, Crud};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
naive_now,
schema::person::dsl::*,
source::person::{Person, PersonForm},
DbUrl,
PersonId,
};
mod safe_type {
use crate::ToSafe;
use lemmy_db_schema::{schema::person::columns::*, source::person::Person};
type Columns = (
id,
name,
preferred_username,
avatar,
banned,
published,
updated,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
);
impl ToSafe for Person {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
(
id,
name,
preferred_username,
avatar,
banned,
published,
updated,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
)
}
}
}
mod safe_type_alias_1 {
use crate::ToSafe;
use lemmy_db_schema::{schema::person_alias_1::columns::*, source::person::PersonAlias1};
type Columns = (
id,
name,
preferred_username,
avatar,
banned,
published,
updated,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
);
impl ToSafe for PersonAlias1 {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
(
id,
name,
preferred_username,
avatar,
banned,
published,
updated,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
)
}
}
}
mod safe_type_alias_2 {
use crate::ToSafe;
use lemmy_db_schema::{schema::person_alias_2::columns::*, source::person::PersonAlias2};
type Columns = (
id,
name,
preferred_username,
avatar,
banned,
published,
updated,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
);
impl ToSafe for PersonAlias2 {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
(
id,
name,
preferred_username,
avatar,
banned,
published,
updated,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
)
}
}
}
impl Crud<PersonForm, PersonId> for Person {
fn read(conn: &PgConnection, person_id: PersonId) -> Result<Self, Error> {
person
.filter(deleted.eq(false))
.find(person_id)
.first::<Self>(conn)
}
fn delete(conn: &PgConnection, person_id: PersonId) -> Result<usize, Error> {
diesel::delete(person.find(person_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &PersonForm) -> Result<Self, Error> {
insert_into(person).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, person_id: PersonId, form: &PersonForm) -> Result<Self, Error> {
diesel::update(person.find(person_id))
.set(form)
.get_result::<Self>(conn)
}
}
impl ApubObject<PersonForm> for Person {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
use lemmy_db_schema::schema::person::dsl::*;
person
.filter(deleted.eq(false))
.filter(actor_id.eq(object_id))
.first::<Self>(conn)
}
fn upsert(conn: &PgConnection, person_form: &PersonForm) -> Result<Person, Error> {
insert_into(person)
.values(person_form)
.on_conflict(actor_id)
.do_update()
.set(person_form)
.get_result::<Self>(conn)
}
}
pub trait Person_ {
fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Person, Error>;
fn find_by_name(conn: &PgConnection, name: &str) -> Result<Person, Error>;
fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error>;
}
impl Person_ for Person {
fn ban_person(conn: &PgConnection, person_id: PersonId, ban: bool) -> Result<Self, Error> {
diesel::update(person.find(person_id))
.set(banned.eq(ban))
.get_result::<Self>(conn)
}
fn find_by_name(conn: &PgConnection, from_name: &str) -> Result<Person, Error> {
person
.filter(deleted.eq(false))
.filter(local.eq(true))
.filter(name.ilike(from_name))
.first::<Person>(conn)
}
fn mark_as_updated(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
diesel::update(person.find(person_id))
.set((last_refreshed_at.eq(naive_now()),))
.get_result::<Self>(conn)
}
fn delete_account(conn: &PgConnection, person_id: PersonId) -> Result<Person, Error> {
use lemmy_db_schema::schema::local_user;
// Set the local user info to none
diesel::update(local_user::table.filter(local_user::person_id.eq(person_id)))
.set((
local_user::email.eq::<Option<String>>(None),
local_user::matrix_user_id.eq::<Option<String>>(None),
))
.execute(conn)?;
diesel::update(person.find(person_id))
.set((
preferred_username.eq::<Option<String>>(None),
bio.eq::<Option<String>>(None),
deleted.eq(true),
updated.eq(naive_now()),
))
.get_result::<Self>(conn)
}
}
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, source::person::*};
#[test]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
name: "holly".into(),
preferred_username: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
published: None,
updated: None,
actor_id: None,
bio: None,
local: None,
private_key: None,
public_key: None,
last_refreshed_at: None,
inbox_url: None,
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let expected_person = Person {
id: inserted_person.id,
name: "holly".into(),
preferred_username: None,
avatar: None,
banner: None,
banned: false,
deleted: false,
published: inserted_person.published,
updated: None,
actor_id: inserted_person.actor_id.to_owned(),
bio: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: inserted_person.published,
inbox_url: inserted_person.inbox_url.to_owned(),
shared_inbox_url: None,
};
let read_person = Person::read(&conn, inserted_person.id).unwrap();
let updated_person = Person::update(&conn, inserted_person.id, &new_person).unwrap();
let num_deleted = Person::delete(&conn, inserted_person.id).unwrap();
assert_eq!(expected_person, read_person);
assert_eq!(expected_person, inserted_person);
assert_eq!(expected_person, updated_person);
assert_eq!(1, num_deleted);
}
}

View file

@ -12,19 +12,16 @@ use lemmy_db_schema::{
PostSaved,
PostSavedForm,
},
CommunityId,
DbUrl,
PersonId,
PostId,
Url,
};
impl Crud<PostForm, PostId> for Post {
fn read(conn: &PgConnection, post_id: PostId) -> Result<Self, Error> {
impl Crud<PostForm> for Post {
fn read(conn: &PgConnection, post_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.find(post_id).first::<Self>(conn)
}
fn delete(conn: &PgConnection, post_id: PostId) -> Result<usize, Error> {
fn delete(conn: &PgConnection, post_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::delete(post.find(post_id)).execute(conn)
}
@ -34,7 +31,7 @@ impl Crud<PostForm, PostId> for Post {
insert_into(post).values(new_post).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, post_id: PostId, new_post: &PostForm) -> Result<Self, Error> {
fn update(conn: &PgConnection, post_id: i32, new_post: &PostForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(new_post)
@ -44,39 +41,24 @@ impl Crud<PostForm, PostId> for Post {
pub trait Post_ {
//fn read(conn: &PgConnection, post_id: i32) -> Result<Post, Error>;
fn list_for_community(
conn: &PgConnection,
the_community_id: CommunityId,
) -> Result<Vec<Post>, Error>;
fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Post, Error>;
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
) -> Result<Vec<Post>, Error>;
fn update_deleted(conn: &PgConnection, post_id: PostId, new_deleted: bool)
-> Result<Post, Error>;
fn update_removed(conn: &PgConnection, post_id: PostId, new_removed: bool)
-> Result<Post, Error>;
fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Post>, Error>;
fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: Url) -> Result<Post, Error>;
fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Post>, Error>;
fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Post, Error>;
fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Post, Error>;
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_community_id: Option<CommunityId>,
for_creator_id: i32,
for_community_id: Option<i32>,
new_removed: bool,
) -> Result<Vec<Post>, Error>;
fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Post, Error>;
fn update_stickied(
conn: &PgConnection,
post_id: PostId,
new_stickied: bool,
) -> Result<Post, Error>;
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool;
fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Post, Error>;
fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Post, Error>;
fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool;
}
impl Post_ for Post {
fn list_for_community(
conn: &PgConnection,
the_community_id: CommunityId,
) -> Result<Vec<Self>, Error> {
fn list_for_community(conn: &PgConnection, the_community_id: i32) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post
.filter(community_id.eq(the_community_id))
@ -86,7 +68,7 @@ impl Post_ for Post {
.load::<Self>(conn)
}
fn update_ap_id(conn: &PgConnection, post_id: PostId, apub_id: DbUrl) -> Result<Self, Error> {
fn update_ap_id(conn: &PgConnection, post_id: i32, apub_id: Url) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
@ -94,10 +76,7 @@ impl Post_ for Post {
.get_result::<Self>(conn)
}
fn permadelete_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
) -> Result<Vec<Self>, Error> {
fn permadelete_for_creator(conn: &PgConnection, for_creator_id: i32) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
let perma_deleted = "*Permananently Deleted*";
@ -114,22 +93,14 @@ impl Post_ for Post {
.get_results::<Self>(conn)
}
fn update_deleted(
conn: &PgConnection,
post_id: PostId,
new_deleted: bool,
) -> Result<Self, Error> {
fn update_deleted(conn: &PgConnection, post_id: i32, new_deleted: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((deleted.eq(new_deleted), updated.eq(naive_now())))
.get_result::<Self>(conn)
}
fn update_removed(
conn: &PgConnection,
post_id: PostId,
new_removed: bool,
) -> Result<Self, Error> {
fn update_removed(conn: &PgConnection, post_id: i32, new_removed: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set((removed.eq(new_removed), updated.eq(naive_now())))
@ -138,8 +109,8 @@ impl Post_ for Post {
fn update_removed_for_creator(
conn: &PgConnection,
for_creator_id: PersonId,
for_community_id: Option<CommunityId>,
for_creator_id: i32,
for_community_id: Option<i32>,
new_removed: bool,
) -> Result<Vec<Self>, Error> {
use lemmy_db_schema::schema::post::dsl::*;
@ -156,31 +127,27 @@ impl Post_ for Post {
.get_results::<Self>(conn)
}
fn update_locked(conn: &PgConnection, post_id: PostId, new_locked: bool) -> Result<Self, Error> {
fn update_locked(conn: &PgConnection, post_id: i32, new_locked: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(locked.eq(new_locked))
.get_result::<Self>(conn)
}
fn update_stickied(
conn: &PgConnection,
post_id: PostId,
new_stickied: bool,
) -> Result<Self, Error> {
fn update_stickied(conn: &PgConnection, post_id: i32, new_stickied: bool) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
diesel::update(post.find(post_id))
.set(stickied.eq(new_stickied))
.get_result::<Self>(conn)
}
fn is_post_creator(person_id: PersonId, post_creator_id: PersonId) -> bool {
person_id == post_creator_id
fn is_post_creator(user_id: i32, post_creator_id: i32) -> bool {
user_id == post_creator_id
}
}
impl ApubObject<PostForm> for Post {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error> {
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error> {
use lemmy_db_schema::schema::post::dsl::*;
post.filter(ap_id.eq(object_id)).first::<Self>(conn)
}
@ -196,22 +163,22 @@ impl ApubObject<PostForm> for Post {
}
}
impl Likeable<PostLikeForm, PostId> for PostLike {
impl Likeable<PostLikeForm> for PostLike {
fn like(conn: &PgConnection, post_like_form: &PostLikeForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::post_like::dsl::*;
insert_into(post_like)
.values(post_like_form)
.on_conflict((post_id, person_id))
.on_conflict((post_id, user_id))
.do_update()
.set(post_like_form)
.get_result::<Self>(conn)
}
fn remove(conn: &PgConnection, person_id: PersonId, post_id: PostId) -> Result<usize, Error> {
fn remove(conn: &PgConnection, user_id: i32, post_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_like::dsl;
diesel::delete(
dsl::post_like
.filter(dsl::post_id.eq(post_id))
.filter(dsl::person_id.eq(person_id)),
.filter(dsl::user_id.eq(user_id)),
)
.execute(conn)
}
@ -222,7 +189,7 @@ impl Saveable<PostSavedForm> for PostSaved {
use lemmy_db_schema::schema::post_saved::dsl::*;
insert_into(post_saved)
.values(post_saved_form)
.on_conflict((post_id, person_id))
.on_conflict((post_id, user_id))
.do_update()
.set(post_saved_form)
.get_result::<Self>(conn)
@ -232,7 +199,7 @@ impl Saveable<PostSavedForm> for PostSaved {
diesel::delete(
post_saved
.filter(post_id.eq(post_saved_form.post_id))
.filter(person_id.eq(post_saved_form.person_id)),
.filter(user_id.eq(post_saved_form.user_id)),
)
.execute(conn)
}
@ -251,7 +218,7 @@ impl Readable<PostReadForm> for PostRead {
diesel::delete(
post_read
.filter(post_id.eq(post_read_form.post_id))
.filter(person_id.eq(post_read_form.person_id)),
.filter(user_id.eq(post_read_form.user_id)),
)
.execute(conn)
}
@ -259,30 +226,38 @@ impl Readable<PostReadForm> for PostRead {
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, source::post::*};
use crate::{establish_unpooled_connection, source::post::*, ListingType, SortType};
use lemmy_db_schema::source::{
community::{Community, CommunityForm},
person::*,
user::*,
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "jim".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -290,13 +265,14 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let new_community = CommunityForm {
name: "test community_3".to_string(),
title: "nada".to_owned(),
description: None,
creator_id: inserted_person.id,
category_id: 1,
creator_id: inserted_user.id,
removed: None,
deleted: None,
updated: None,
@ -320,7 +296,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
removed: None,
deleted: None,
@ -344,7 +320,7 @@ mod tests {
name: "A test post".into(),
url: None,
body: None,
creator_id: inserted_person.id,
creator_id: inserted_user.id,
community_id: inserted_community.id,
published: inserted_post.published,
removed: false,
@ -364,7 +340,7 @@ mod tests {
// Post Like
let post_like_form = PostLikeForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
score: 1,
};
@ -373,7 +349,7 @@ mod tests {
let expected_post_like = PostLike {
id: inserted_post_like.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
published: inserted_post_like.published,
score: 1,
};
@ -381,7 +357,7 @@ mod tests {
// Post Save
let post_saved_form = PostSavedForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
};
let inserted_post_saved = PostSaved::save(&conn, &post_saved_form).unwrap();
@ -389,14 +365,14 @@ mod tests {
let expected_post_saved = PostSaved {
id: inserted_post_saved.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
published: inserted_post_saved.published,
};
// Post Read
let post_read_form = PostReadForm {
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
};
let inserted_post_read = PostRead::mark_as_read(&conn, &post_read_form).unwrap();
@ -404,18 +380,18 @@ mod tests {
let expected_post_read = PostRead {
id: inserted_post_read.id,
post_id: inserted_post.id,
person_id: inserted_person.id,
user_id: inserted_user.id,
published: inserted_post_read.published,
};
let read_post = Post::read(&conn, inserted_post.id).unwrap();
let updated_post = Post::update(&conn, inserted_post.id, &new_post).unwrap();
let like_removed = PostLike::remove(&conn, inserted_person.id, inserted_post.id).unwrap();
let like_removed = PostLike::remove(&conn, inserted_user.id, inserted_post.id).unwrap();
let saved_removed = PostSaved::unsave(&conn, &post_saved_form).unwrap();
let read_removed = PostRead::mark_as_unread(&conn, &post_read_form).unwrap();
let num_deleted = Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
Person::delete(&conn, inserted_person.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(expected_post, read_post);
assert_eq!(expected_post, inserted_post);

View file

@ -1,6 +1,6 @@
use crate::Reportable;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::post_report::*, PersonId};
use lemmy_db_schema::{naive_now, source::post_report::*};
impl Reportable<PostReportForm> for PostReport {
/// creates a post report and returns it
@ -19,11 +19,7 @@ impl Reportable<PostReportForm> for PostReport {
/// * `conn` - the postgres connection
/// * `report_id` - the id of the report to resolve
/// * `by_resolver_id` - the id of the user resolving the report
fn resolve(
conn: &PgConnection,
report_id: i32,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
fn resolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_report::dsl::*;
update(post_report.find(report_id))
.set((
@ -39,11 +35,7 @@ impl Reportable<PostReportForm> for PostReport {
/// * `conn` - the postgres connection
/// * `report_id` - the id of the report to unresolve
/// * `by_resolver_id` - the id of the user unresolving the report
fn unresolve(
conn: &PgConnection,
report_id: i32,
by_resolver_id: PersonId,
) -> Result<usize, Error> {
fn unresolve(conn: &PgConnection, report_id: i32, by_resolver_id: i32) -> Result<usize, Error> {
use lemmy_db_schema::schema::post_report::dsl::*;
update(post_report.find(report_id))
.set((

View file

@ -1,9 +1,9 @@
use crate::{ApubObject, Crud};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::private_message::*, DbUrl, PersonId, PrivateMessageId};
use lemmy_db_schema::{naive_now, source::private_message::*, Url};
impl Crud<PrivateMessageForm, PrivateMessageId> for PrivateMessage {
fn read(conn: &PgConnection, private_message_id: PrivateMessageId) -> Result<Self, Error> {
impl Crud<PrivateMessageForm> for PrivateMessage {
fn read(conn: &PgConnection, private_message_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
private_message.find(private_message_id).first::<Self>(conn)
}
@ -17,7 +17,7 @@ impl Crud<PrivateMessageForm, PrivateMessageId> for PrivateMessage {
fn update(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
private_message_form: &PrivateMessageForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
@ -28,7 +28,7 @@ impl Crud<PrivateMessageForm, PrivateMessageId> for PrivateMessage {
}
impl ApubObject<PrivateMessageForm> for PrivateMessage {
fn read_from_apub_id(conn: &PgConnection, object_id: &DbUrl) -> Result<Self, Error>
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error>
where
Self: Sized,
{
@ -52,35 +52,35 @@ impl ApubObject<PrivateMessageForm> for PrivateMessage {
pub trait PrivateMessage_ {
fn update_ap_id(
conn: &PgConnection,
private_message_id: PrivateMessageId,
apub_id: DbUrl,
private_message_id: i32,
apub_id: Url,
) -> Result<PrivateMessage, Error>;
fn update_content(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
new_content: &str,
) -> Result<PrivateMessage, Error>;
fn update_deleted(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
new_deleted: bool,
) -> Result<PrivateMessage, Error>;
fn update_read(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
new_read: bool,
) -> Result<PrivateMessage, Error>;
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: PersonId,
for_recipient_id: i32,
) -> Result<Vec<PrivateMessage>, Error>;
}
impl PrivateMessage_ for PrivateMessage {
fn update_ap_id(
conn: &PgConnection,
private_message_id: PrivateMessageId,
apub_id: DbUrl,
private_message_id: i32,
apub_id: Url,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
@ -91,7 +91,7 @@ impl PrivateMessage_ for PrivateMessage {
fn update_content(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
new_content: &str,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
@ -102,7 +102,7 @@ impl PrivateMessage_ for PrivateMessage {
fn update_deleted(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
new_deleted: bool,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
@ -113,7 +113,7 @@ impl PrivateMessage_ for PrivateMessage {
fn update_read(
conn: &PgConnection,
private_message_id: PrivateMessageId,
private_message_id: i32,
new_read: bool,
) -> Result<PrivateMessage, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
@ -124,7 +124,7 @@ impl PrivateMessage_ for PrivateMessage {
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: PersonId,
for_recipient_id: i32,
) -> Result<Vec<PrivateMessage>, Error> {
use lemmy_db_schema::schema::private_message::dsl::*;
diesel::update(
@ -139,27 +139,41 @@ impl PrivateMessage_ for PrivateMessage {
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, source::private_message::PrivateMessage_, Crud};
use lemmy_db_schema::source::{person::*, private_message::*};
use serial_test::serial;
use crate::{
establish_unpooled_connection,
source::private_message::PrivateMessage_,
Crud,
ListingType,
SortType,
};
use lemmy_db_schema::source::{private_message::*, user::*};
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let creator_form = PersonForm {
let creator_form = UserForm {
name: "creator_pm".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -167,20 +181,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_creator = Person::create(&conn, &creator_form).unwrap();
let inserted_creator = User_::create(&conn, &creator_form).unwrap();
let recipient_form = PersonForm {
let recipient_form = UserForm {
name: "recipient_pm".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -188,7 +212,7 @@ mod tests {
shared_inbox_url: None,
};
let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
let private_message_form = PrivateMessageForm {
content: "A test private message".into(),
@ -224,8 +248,8 @@ mod tests {
PrivateMessage::update_deleted(&conn, inserted_private_message.id, true).unwrap();
let marked_read_private_message =
PrivateMessage::update_read(&conn, inserted_private_message.id, true).unwrap();
Person::delete(&conn, inserted_creator.id).unwrap();
Person::delete(&conn, inserted_recipient.id).unwrap();
User_::delete(&conn, inserted_creator.id).unwrap();
User_::delete(&conn, inserted_recipient.id).unwrap();
assert_eq!(expected_private_message, read_private_message);
assert_eq!(expected_private_message, updated_private_message);

View file

@ -1,8 +1,8 @@
use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{naive_now, source::site::*, PersonId};
use lemmy_db_schema::{naive_now, source::site::*};
impl Crud<SiteForm, i32> for Site {
impl Crud<SiteForm> for Site {
fn read(conn: &PgConnection, _site_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::site::dsl::*;
site.first::<Self>(conn)
@ -26,12 +26,12 @@ impl Crud<SiteForm, i32> for Site {
}
pub trait Site_ {
fn transfer(conn: &PgConnection, new_creator_id: PersonId) -> Result<Site, Error>;
fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error>;
fn read_simple(conn: &PgConnection) -> Result<Site, Error>;
}
impl Site_ for Site {
fn transfer(conn: &PgConnection, new_creator_id: PersonId) -> Result<Site, Error> {
fn transfer(conn: &PgConnection, new_creator_id: i32) -> Result<Site, Error> {
use lemmy_db_schema::schema::site::dsl::*;
diesel::update(site.find(1))
.set((creator_id.eq(new_creator_id), updated.eq(naive_now())))

View file

@ -0,0 +1,458 @@
use crate::{is_email_regex, ApubObject, Crud, ToSafeSettings};
use bcrypt::{hash, DEFAULT_COST};
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{
naive_now,
schema::user_::dsl::*,
source::user::{UserForm, UserSafeSettings, User_},
Url,
};
use lemmy_utils::settings::Settings;
mod safe_type {
use crate::ToSafe;
use lemmy_db_schema::{schema::user_::columns::*, source::user::User_};
type Columns = (
id,
name,
preferred_username,
avatar,
admin,
banned,
published,
updated,
matrix_user_id,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
);
impl ToSafe for User_ {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
(
id,
name,
preferred_username,
avatar,
admin,
banned,
published,
updated,
matrix_user_id,
actor_id,
bio,
local,
banner,
deleted,
inbox_url,
shared_inbox_url,
)
}
}
}
mod safe_type_alias_1 {
use crate::ToSafe;
use lemmy_db_schema::{schema::user_alias_1::columns::*, source::user::UserAlias1};
type Columns = (
id,
name,
preferred_username,
avatar,
admin,
banned,
published,
updated,
matrix_user_id,
actor_id,
bio,
local,
banner,
deleted,
);
impl ToSafe for UserAlias1 {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
(
id,
name,
preferred_username,
avatar,
admin,
banned,
published,
updated,
matrix_user_id,
actor_id,
bio,
local,
banner,
deleted,
)
}
}
}
mod safe_type_alias_2 {
use crate::ToSafe;
use lemmy_db_schema::{schema::user_alias_2::columns::*, source::user::UserAlias2};
type Columns = (
id,
name,
preferred_username,
avatar,
admin,
banned,
published,
updated,
matrix_user_id,
actor_id,
bio,
local,
banner,
deleted,
);
impl ToSafe for UserAlias2 {
type SafeColumns = Columns;
fn safe_columns_tuple() -> Self::SafeColumns {
(
id,
name,
preferred_username,
avatar,
admin,
banned,
published,
updated,
matrix_user_id,
actor_id,
bio,
local,
banner,
deleted,
)
}
}
}
mod safe_settings_type {
use crate::ToSafeSettings;
use lemmy_db_schema::{schema::user_::columns::*, source::user::User_};
type Columns = (
id,
name,
preferred_username,
email,
avatar,
admin,
banned,
published,
updated,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
matrix_user_id,
actor_id,
bio,
local,
last_refreshed_at,
banner,
deleted,
);
impl ToSafeSettings for User_ {
type SafeSettingsColumns = Columns;
fn safe_settings_columns_tuple() -> Self::SafeSettingsColumns {
(
id,
name,
preferred_username,
email,
avatar,
admin,
banned,
published,
updated,
show_nsfw,
theme,
default_sort_type,
default_listing_type,
lang,
show_avatars,
send_notifications_to_email,
matrix_user_id,
actor_id,
bio,
local,
last_refreshed_at,
banner,
deleted,
)
}
}
}
pub trait UserSafeSettings_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<UserSafeSettings, Error>;
}
impl UserSafeSettings_ for UserSafeSettings {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
user_
.select(User_::safe_settings_columns_tuple())
.filter(deleted.eq(false))
.find(user_id)
.first::<Self>(conn)
}
}
impl Crud<UserForm> for User_ {
fn read(conn: &PgConnection, user_id: i32) -> Result<Self, Error> {
user_
.filter(deleted.eq(false))
.find(user_id)
.first::<Self>(conn)
}
fn delete(conn: &PgConnection, user_id: i32) -> Result<usize, Error> {
diesel::delete(user_.find(user_id)).execute(conn)
}
fn create(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
insert_into(user_).values(form).get_result::<Self>(conn)
}
fn update(conn: &PgConnection, user_id: i32, form: &UserForm) -> Result<Self, Error> {
diesel::update(user_.find(user_id))
.set(form)
.get_result::<Self>(conn)
}
}
impl ApubObject<UserForm> for User_ {
fn read_from_apub_id(conn: &PgConnection, object_id: &Url) -> Result<Self, Error> {
use lemmy_db_schema::schema::user_::dsl::*;
user_
.filter(deleted.eq(false))
.filter(actor_id.eq(object_id))
.first::<Self>(conn)
}
fn upsert(conn: &PgConnection, user_form: &UserForm) -> Result<User_, Error> {
insert_into(user_)
.values(user_form)
.on_conflict(actor_id)
.do_update()
.set(user_form)
.get_result::<Self>(conn)
}
}
pub trait User {
fn register(conn: &PgConnection, form: &UserForm) -> Result<User_, Error>;
fn update_password(conn: &PgConnection, user_id: i32, new_password: &str)
-> Result<User_, Error>;
fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<User_, Error>;
fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<User_, Error>;
fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<User_, Error>;
fn find_by_email_or_username(
conn: &PgConnection,
username_or_email: &str,
) -> Result<User_, Error>;
fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error>;
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error>;
fn get_profile_url(&self, hostname: &str) -> String;
fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error>;
fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error>;
}
impl User for User_ {
fn register(conn: &PgConnection, form: &UserForm) -> Result<Self, Error> {
let mut edited_user = form.clone();
let password_hash =
hash(&form.password_encrypted, DEFAULT_COST).expect("Couldn't hash password");
edited_user.password_encrypted = password_hash;
Self::create(&conn, &edited_user)
}
// TODO do more individual updates like these
fn update_password(conn: &PgConnection, user_id: i32, new_password: &str) -> Result<Self, Error> {
let password_hash = hash(new_password, DEFAULT_COST).expect("Couldn't hash password");
diesel::update(user_.find(user_id))
.set((
password_encrypted.eq(password_hash),
updated.eq(naive_now()),
))
.get_result::<Self>(conn)
}
fn read_from_name(conn: &PgConnection, from_user_name: &str) -> Result<Self, Error> {
user_
.filter(local.eq(true))
.filter(deleted.eq(false))
.filter(name.eq(from_user_name))
.first::<Self>(conn)
}
fn add_admin(conn: &PgConnection, user_id: i32, added: bool) -> Result<Self, Error> {
diesel::update(user_.find(user_id))
.set(admin.eq(added))
.get_result::<Self>(conn)
}
fn ban_user(conn: &PgConnection, user_id: i32, ban: bool) -> Result<Self, Error> {
diesel::update(user_.find(user_id))
.set(banned.eq(ban))
.get_result::<Self>(conn)
}
fn find_by_email_or_username(
conn: &PgConnection,
username_or_email: &str,
) -> Result<Self, Error> {
if is_email_regex(username_or_email) {
Self::find_by_email(conn, username_or_email)
} else {
Self::find_by_username(conn, username_or_email)
}
}
fn find_by_username(conn: &PgConnection, username: &str) -> Result<User_, Error> {
user_
.filter(deleted.eq(false))
.filter(local.eq(true))
.filter(name.ilike(username))
.first::<User_>(conn)
}
fn find_by_email(conn: &PgConnection, from_email: &str) -> Result<User_, Error> {
user_
.filter(deleted.eq(false))
.filter(local.eq(true))
.filter(email.eq(from_email))
.first::<User_>(conn)
}
fn get_profile_url(&self, hostname: &str) -> String {
format!(
"{}://{}/u/{}",
Settings::get().get_protocol_string(),
hostname,
self.name
)
}
fn mark_as_updated(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
diesel::update(user_.find(user_id))
.set((last_refreshed_at.eq(naive_now()),))
.get_result::<Self>(conn)
}
fn delete_account(conn: &PgConnection, user_id: i32) -> Result<User_, Error> {
diesel::update(user_.find(user_id))
.set((
preferred_username.eq::<Option<String>>(None),
email.eq::<Option<String>>(None),
matrix_user_id.eq::<Option<String>>(None),
bio.eq::<Option<String>>(None),
deleted.eq(true),
updated.eq(naive_now()),
))
.get_result::<Self>(conn)
}
}
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, source::user::*, ListingType, SortType};
#[test]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_user = UserForm {
name: "thommy".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
inbox_url: None,
shared_inbox_url: None,
};
let inserted_user = User_::create(&conn, &new_user).unwrap();
let expected_user = User_ {
id: inserted_user.id,
name: "thommy".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
admin: false,
banned: false,
published: inserted_user.published,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: inserted_user.actor_id.to_owned(),
bio: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: inserted_user.published,
deleted: false,
inbox_url: inserted_user.inbox_url.to_owned(),
shared_inbox_url: None,
};
let read_user = User_::read(&conn, inserted_user.id).unwrap();
let updated_user = User_::update(&conn, inserted_user.id, &new_user).unwrap();
let num_deleted = User_::delete(&conn, inserted_user.id).unwrap();
assert_eq!(expected_user, read_user);
assert_eq!(expected_user, inserted_user);
assert_eq!(expected_user, updated_user);
assert_eq!(1, num_deleted);
}
}

View file

@ -1,68 +1,68 @@
use crate::Crud;
use diesel::{dsl::*, result::Error, *};
use lemmy_db_schema::{source::person_mention::*, PersonId, PersonMentionId};
use lemmy_db_schema::source::user_mention::*;
impl Crud<PersonMentionForm, PersonMentionId> for PersonMention {
fn read(conn: &PgConnection, person_mention_id: PersonMentionId) -> Result<Self, Error> {
use lemmy_db_schema::schema::person_mention::dsl::*;
person_mention.find(person_mention_id).first::<Self>(conn)
impl Crud<UserMentionForm> for UserMention {
fn read(conn: &PgConnection, user_mention_id: i32) -> Result<Self, Error> {
use lemmy_db_schema::schema::user_mention::dsl::*;
user_mention.find(user_mention_id).first::<Self>(conn)
}
fn create(conn: &PgConnection, person_mention_form: &PersonMentionForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::person_mention::dsl::*;
fn create(conn: &PgConnection, user_mention_form: &UserMentionForm) -> Result<Self, Error> {
use lemmy_db_schema::schema::user_mention::dsl::*;
// since the return here isnt utilized, we dont need to do an update
// but get_result doesnt return the existing row here
insert_into(person_mention)
.values(person_mention_form)
insert_into(user_mention)
.values(user_mention_form)
.on_conflict((recipient_id, comment_id))
.do_update()
.set(person_mention_form)
.set(user_mention_form)
.get_result::<Self>(conn)
}
fn update(
conn: &PgConnection,
person_mention_id: PersonMentionId,
person_mention_form: &PersonMentionForm,
user_mention_id: i32,
user_mention_form: &UserMentionForm,
) -> Result<Self, Error> {
use lemmy_db_schema::schema::person_mention::dsl::*;
diesel::update(person_mention.find(person_mention_id))
.set(person_mention_form)
use lemmy_db_schema::schema::user_mention::dsl::*;
diesel::update(user_mention.find(user_mention_id))
.set(user_mention_form)
.get_result::<Self>(conn)
}
}
pub trait PersonMention_ {
pub trait UserMention_ {
fn update_read(
conn: &PgConnection,
person_mention_id: PersonMentionId,
user_mention_id: i32,
new_read: bool,
) -> Result<PersonMention, Error>;
) -> Result<UserMention, Error>;
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: PersonId,
) -> Result<Vec<PersonMention>, Error>;
for_recipient_id: i32,
) -> Result<Vec<UserMention>, Error>;
}
impl PersonMention_ for PersonMention {
impl UserMention_ for UserMention {
fn update_read(
conn: &PgConnection,
person_mention_id: PersonMentionId,
user_mention_id: i32,
new_read: bool,
) -> Result<PersonMention, Error> {
use lemmy_db_schema::schema::person_mention::dsl::*;
diesel::update(person_mention.find(person_mention_id))
) -> Result<UserMention, Error> {
use lemmy_db_schema::schema::user_mention::dsl::*;
diesel::update(user_mention.find(user_mention_id))
.set(read.eq(new_read))
.get_result::<Self>(conn)
}
fn mark_all_as_read(
conn: &PgConnection,
for_recipient_id: PersonId,
) -> Result<Vec<PersonMention>, Error> {
use lemmy_db_schema::schema::person_mention::dsl::*;
for_recipient_id: i32,
) -> Result<Vec<UserMention>, Error> {
use lemmy_db_schema::schema::user_mention::dsl::*;
diesel::update(
person_mention
user_mention
.filter(recipient_id.eq(for_recipient_id))
.filter(read.eq(false)),
)
@ -73,33 +73,41 @@ impl PersonMention_ for PersonMention {
#[cfg(test)]
mod tests {
use crate::{establish_unpooled_connection, Crud};
use crate::{establish_unpooled_connection, Crud, ListingType, SortType};
use lemmy_db_schema::source::{
comment::*,
community::{Community, CommunityForm},
person::*,
person_mention::*,
post::*,
user::*,
user_mention::*,
};
use serial_test::serial;
#[test]
#[serial]
fn test_crud() {
let conn = establish_unpooled_connection();
let new_person = PersonForm {
let new_user = UserForm {
name: "terrylake".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -107,20 +115,30 @@ mod tests {
shared_inbox_url: None,
};
let inserted_person = Person::create(&conn, &new_person).unwrap();
let inserted_user = User_::create(&conn, &new_user).unwrap();
let recipient_form = PersonForm {
let recipient_form = UserForm {
name: "terrylakes recipient".into(),
preferred_username: None,
password_encrypted: "nope".into(),
email: None,
matrix_user_id: None,
avatar: None,
banner: None,
banned: None,
deleted: None,
admin: false,
banned: Some(false),
published: None,
updated: None,
show_nsfw: false,
theme: "browser".into(),
default_sort_type: SortType::Hot as i16,
default_listing_type: ListingType::Subscribed as i16,
lang: "browser".into(),
show_avatars: true,
send_notifications_to_email: false,
actor_id: None,
bio: None,
local: None,
local: true,
private_key: None,
public_key: None,
last_refreshed_at: None,
@ -128,13 +146,14 @@ mod tests {
shared_inbox_url: None,
};
let inserted_recipient = Person::create(&conn, &recipient_form).unwrap();
let inserted_recipient = User_::create(&conn, &recipient_form).unwrap();
let new_community = CommunityForm {
name: "test community lake".to_string(),
title: "nada".to_owned(),
description: None,
creator_id: inserted_person.id,
category_id: 1,
creator_id: inserted_user.id,
removed: None,
deleted: None,
updated: None,
@ -156,7 +175,7 @@ mod tests {
let new_post = PostForm {
name: "A test post".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
url: None,
body: None,
community_id: inserted_community.id,
@ -179,7 +198,7 @@ mod tests {
let comment_form = CommentForm {
content: "A test comment".into(),
creator_id: inserted_person.id,
creator_id: inserted_user.id,
post_id: inserted_post.id,
removed: None,
deleted: None,
@ -193,15 +212,15 @@ mod tests {
let inserted_comment = Comment::create(&conn, &comment_form).unwrap();
let person_mention_form = PersonMentionForm {
let user_mention_form = UserMentionForm {
recipient_id: inserted_recipient.id,
comment_id: inserted_comment.id,
read: None,
};
let inserted_mention = PersonMention::create(&conn, &person_mention_form).unwrap();
let inserted_mention = UserMention::create(&conn, &user_mention_form).unwrap();
let expected_mention = PersonMention {
let expected_mention = UserMention {
id: inserted_mention.id,
recipient_id: inserted_mention.recipient_id,
comment_id: inserted_mention.comment_id,
@ -209,14 +228,14 @@ mod tests {
published: inserted_mention.published,
};
let read_mention = PersonMention::read(&conn, inserted_mention.id).unwrap();
let read_mention = UserMention::read(&conn, inserted_mention.id).unwrap();
let updated_mention =
PersonMention::update(&conn, inserted_mention.id, &person_mention_form).unwrap();
UserMention::update(&conn, inserted_mention.id, &user_mention_form).unwrap();
Comment::delete(&conn, inserted_comment.id).unwrap();
Post::delete(&conn, inserted_post.id).unwrap();
Community::delete(&conn, inserted_community.id).unwrap();
Person::delete(&conn, inserted_person.id).unwrap();
Person::delete(&conn, inserted_recipient.id).unwrap();
User_::delete(&conn, inserted_user.id).unwrap();
User_::delete(&conn, inserted_recipient.id).unwrap();
assert_eq!(expected_mention, read_mention);
assert_eq!(expected_mention, inserted_mention);

View file

@ -3,14 +3,10 @@ name = "lemmy_db_schema"
version = "0.1.0"
edition = "2018"
[lib]
doctest = false
[dependencies]
diesel = { version = "1.4.5", features = ["postgres","chrono","r2d2","serde_json"] }
chrono = { version = "0.4.19", features = ["serde"] }
serde = { version = "1.0.123", features = ["derive"] }
serde_json = { version = "1.0.61", features = ["preserve_order"] }
log = "0.4.14"
url = { version = "2.2.1", features = ["serde"] }
diesel-derive-newtype = "0.1"
url = { version = "2.2.0", features = ["serde"] }

View file

@ -1,9 +1,6 @@
#[macro_use]
extern crate diesel;
#[macro_use]
extern crate diesel_derive_newtype;
use chrono::NaiveDateTime;
use diesel::{
backend::Backend,
@ -11,62 +8,21 @@ use diesel::{
serialize::{Output, ToSql},
sql_types::Text,
};
use serde::{Deserialize, Serialize};
use serde::Serialize;
use std::{
fmt,
fmt::{Display, Formatter},
io::Write,
};
use url::Url;
pub mod schema;
pub mod source;
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct PostId(pub i32);
impl fmt::Display for PostId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct PersonId(pub i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct CommentId(pub i32);
impl fmt::Display for CommentId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct CommunityId(pub i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct LocalUserId(pub i32);
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct PrivateMessageId(i32);
impl fmt::Display for PrivateMessageId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
#[derive(Debug, Copy, Clone, Hash, Eq, PartialEq, Serialize, Deserialize, DieselNewType)]
pub struct PersonMentionId(i32);
#[repr(transparent)]
#[derive(Clone, PartialEq, Serialize, Deserialize, Debug, AsExpression, FromSqlRow)]
#[derive(Clone, PartialEq, Serialize, Debug, AsExpression, FromSqlRow)]
#[sql_type = "Text"]
pub struct DbUrl(Url);
pub struct Url(url::Url);
impl<DB: Backend> ToSql<Text, DB> for DbUrl
impl<DB: Backend> ToSql<Text, DB> for Url
where
String: ToSql<Text, DB>,
{
@ -75,37 +31,37 @@ where
}
}
impl<DB: Backend> FromSql<Text, DB> for DbUrl
impl<DB: Backend> FromSql<Text, DB> for Url
where
String: FromSql<Text, DB>,
{
fn from_sql(bytes: Option<&DB::RawValue>) -> diesel::deserialize::Result<Self> {
let str = String::from_sql(bytes)?;
Ok(DbUrl(Url::parse(&str)?))
Ok(Url(url::Url::parse(&str)?))
}
}
impl DbUrl {
pub fn into_inner(self) -> Url {
impl Url {
pub fn into_inner(self) -> url::Url {
self.0
}
}
impl Display for DbUrl {
impl Display for Url {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
self.to_owned().into_inner().fmt(f)
}
}
impl From<DbUrl> for Url {
fn from(url: DbUrl) -> Self {
impl From<Url> for url::Url {
fn from(url: Url) -> Self {
url.0
}
}
impl From<Url> for DbUrl {
fn from(url: Url) -> Self {
DbUrl(url)
impl From<url::Url> for Url {
fn from(url: url::Url) -> Self {
Url(url)
}
}

View file

@ -10,6 +10,13 @@ table! {
}
}
table! {
category (id) {
id -> Int4,
name -> Varchar,
}
}
table! {
comment (id) {
id -> Int4,
@ -41,7 +48,7 @@ table! {
table! {
comment_like (id) {
id -> Int4,
person_id -> Int4,
user_id -> Int4,
comment_id -> Int4,
post_id -> Int4,
score -> Int2,
@ -67,7 +74,7 @@ table! {
comment_saved (id) {
id -> Int4,
comment_id -> Int4,
person_id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
@ -78,6 +85,7 @@ table! {
name -> Varchar,
title -> Varchar,
description -> Nullable<Text>,
category_id -> Int4,
creator_id -> Int4,
removed -> Bool,
published -> Timestamp,
@ -89,11 +97,11 @@ table! {
private_key -> Nullable<Text>,
public_key -> Nullable<Text>,
last_refreshed_at -> Timestamp,
icon -> Nullable<Varchar>,
banner -> Nullable<Varchar>,
followers_url -> Varchar,
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
icon -> Nullable<Text>,
banner -> Nullable<Text>,
followers_url -> Text,
inbox_url -> Text,
shared_inbox_url -> Nullable<Text>,
}
}
@ -116,7 +124,7 @@ table! {
community_follower (id) {
id -> Int4,
community_id -> Int4,
person_id -> Int4,
user_id -> Int4,
published -> Timestamp,
pending -> Nullable<Bool>,
}
@ -126,44 +134,25 @@ table! {
community_moderator (id) {
id -> Int4,
community_id -> Int4,
person_id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
table! {
community_person_ban (id) {
community_user_ban (id) {
id -> Int4,
community_id -> Int4,
person_id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
table! {
local_user (id) {
id -> Int4,
person_id -> Int4,
password_encrypted -> Text,
email -> Nullable<Text>,
admin -> Bool,
show_nsfw -> Bool,
theme -> Varchar,
default_sort_type -> Int2,
default_listing_type -> Int2,
lang -> Varchar,
show_avatars -> Bool,
send_notifications_to_email -> Bool,
matrix_user_id -> Nullable<Text>,
validator_time -> Timestamp,
}
}
table! {
mod_add (id) {
id -> Int4,
mod_person_id -> Int4,
other_person_id -> Int4,
mod_user_id -> Int4,
other_user_id -> Int4,
removed -> Nullable<Bool>,
when_ -> Timestamp,
}
@ -172,8 +161,8 @@ table! {
table! {
mod_add_community (id) {
id -> Int4,
mod_person_id -> Int4,
other_person_id -> Int4,
mod_user_id -> Int4,
other_user_id -> Int4,
community_id -> Int4,
removed -> Nullable<Bool>,
when_ -> Timestamp,
@ -183,8 +172,8 @@ table! {
table! {
mod_ban (id) {
id -> Int4,
mod_person_id -> Int4,
other_person_id -> Int4,
mod_user_id -> Int4,
other_user_id -> Int4,
reason -> Nullable<Text>,
banned -> Nullable<Bool>,
expires -> Nullable<Timestamp>,
@ -195,8 +184,8 @@ table! {
table! {
mod_ban_from_community (id) {
id -> Int4,
mod_person_id -> Int4,
other_person_id -> Int4,
mod_user_id -> Int4,
other_user_id -> Int4,
community_id -> Int4,
reason -> Nullable<Text>,
banned -> Nullable<Bool>,
@ -208,7 +197,7 @@ table! {
table! {
mod_lock_post (id) {
id -> Int4,
mod_person_id -> Int4,
mod_user_id -> Int4,
post_id -> Int4,
locked -> Nullable<Bool>,
when_ -> Timestamp,
@ -218,7 +207,7 @@ table! {
table! {
mod_remove_comment (id) {
id -> Int4,
mod_person_id -> Int4,
mod_user_id -> Int4,
comment_id -> Int4,
reason -> Nullable<Text>,
removed -> Nullable<Bool>,
@ -229,7 +218,7 @@ table! {
table! {
mod_remove_community (id) {
id -> Int4,
mod_person_id -> Int4,
mod_user_id -> Int4,
community_id -> Int4,
reason -> Nullable<Text>,
removed -> Nullable<Bool>,
@ -241,7 +230,7 @@ table! {
table! {
mod_remove_post (id) {
id -> Int4,
mod_person_id -> Int4,
mod_user_id -> Int4,
post_id -> Int4,
reason -> Nullable<Text>,
removed -> Nullable<Bool>,
@ -252,7 +241,7 @@ table! {
table! {
mod_sticky_post (id) {
id -> Int4,
mod_person_id -> Int4,
mod_user_id -> Int4,
post_id -> Int4,
stickied -> Nullable<Bool>,
when_ -> Timestamp,
@ -262,60 +251,9 @@ table! {
table! {
password_reset_request (id) {
id -> Int4,
user_id -> Int4,
token_encrypted -> Text,
published -> Timestamp,
local_user_id -> Int4,
}
}
table! {
person (id) {
id -> Int4,
name -> Varchar,
preferred_username -> Nullable<Varchar>,
avatar -> Nullable<Varchar>,
banned -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
actor_id -> Varchar,
bio -> Nullable<Text>,
local -> Bool,
private_key -> Nullable<Text>,
public_key -> Nullable<Text>,
last_refreshed_at -> Timestamp,
banner -> Nullable<Varchar>,
deleted -> Bool,
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
}
}
table! {
person_aggregates (id) {
id -> Int4,
person_id -> Int4,
post_count -> Int8,
post_score -> Int8,
comment_count -> Int8,
comment_score -> Int8,
}
}
table! {
person_ban (id) {
id -> Int4,
person_id -> Int4,
published -> Timestamp,
}
}
table! {
person_mention (id) {
id -> Int4,
recipient_id -> Int4,
comment_id -> Int4,
read -> Bool,
published -> Timestamp,
}
}
@ -323,7 +261,7 @@ table! {
post (id) {
id -> Int4,
name -> Varchar,
url -> Nullable<Varchar>,
url -> Nullable<Text>,
body -> Nullable<Text>,
creator_id -> Int4,
community_id -> Int4,
@ -353,7 +291,6 @@ table! {
downvotes -> Int8,
stickied -> Bool,
published -> Timestamp,
newest_comment_time_necro -> Timestamp,
newest_comment_time -> Timestamp,
}
}
@ -362,7 +299,7 @@ table! {
post_like (id) {
id -> Int4,
post_id -> Int4,
person_id -> Int4,
user_id -> Int4,
score -> Int2,
published -> Timestamp,
}
@ -372,7 +309,7 @@ table! {
post_read (id) {
id -> Int4,
post_id -> Int4,
person_id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
@ -397,7 +334,7 @@ table! {
post_saved (id) {
id -> Int4,
post_id -> Int4,
person_id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
@ -428,8 +365,8 @@ table! {
enable_downvotes -> Bool,
open_registration -> Bool,
enable_nsfw -> Bool,
icon -> Nullable<Varchar>,
banner -> Nullable<Varchar>,
icon -> Nullable<Text>,
banner -> Nullable<Text>,
}
}
@ -448,6 +385,68 @@ table! {
}
}
table! {
user_ (id) {
id -> Int4,
name -> Varchar,
preferred_username -> Nullable<Varchar>,
password_encrypted -> Text,
email -> Nullable<Text>,
avatar -> Nullable<Text>,
admin -> Bool,
banned -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
show_nsfw -> Bool,
theme -> Varchar,
default_sort_type -> Int2,
default_listing_type -> Int2,
lang -> Varchar,
show_avatars -> Bool,
send_notifications_to_email -> Bool,
matrix_user_id -> Nullable<Text>,
actor_id -> Varchar,
bio -> Nullable<Text>,
local -> Bool,
private_key -> Nullable<Text>,
public_key -> Nullable<Text>,
last_refreshed_at -> Timestamp,
banner -> Nullable<Text>,
deleted -> Bool,
inbox_url -> Text,
shared_inbox_url -> Nullable<Text>,
}
}
table! {
user_aggregates (id) {
id -> Int4,
user_id -> Int4,
post_count -> Int8,
post_score -> Int8,
comment_count -> Int8,
comment_score -> Int8,
}
}
table! {
user_ban (id) {
id -> Int4,
user_id -> Int4,
published -> Timestamp,
}
}
table! {
user_mention (id) {
id -> Int4,
recipient_id -> Int4,
comment_id -> Int4,
read -> Bool,
published -> Timestamp,
}
}
// These are necessary since diesel doesn't have self joins / aliases
table! {
comment_alias_1 (id) {
@ -467,108 +466,127 @@ table! {
}
table! {
person_alias_1 (id) {
user_alias_1 (id) {
id -> Int4,
name -> Varchar,
preferred_username -> Nullable<Varchar>,
avatar -> Nullable<Varchar>,
password_encrypted -> Text,
email -> Nullable<Text>,
avatar -> Nullable<Text>,
admin -> Bool,
banned -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
show_nsfw -> Bool,
theme -> Varchar,
default_sort_type -> Int2,
default_listing_type -> Int2,
lang -> Varchar,
show_avatars -> Bool,
send_notifications_to_email -> Bool,
matrix_user_id -> Nullable<Text>,
actor_id -> Varchar,
bio -> Nullable<Text>,
local -> Bool,
private_key -> Nullable<Text>,
public_key -> Nullable<Text>,
last_refreshed_at -> Timestamp,
banner -> Nullable<Varchar>,
banner -> Nullable<Text>,
deleted -> Bool,
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
}
}
table! {
person_alias_2 (id) {
user_alias_2 (id) {
id -> Int4,
name -> Varchar,
preferred_username -> Nullable<Varchar>,
avatar -> Nullable<Varchar>,
password_encrypted -> Text,
email -> Nullable<Text>,
avatar -> Nullable<Text>,
admin -> Bool,
banned -> Bool,
published -> Timestamp,
updated -> Nullable<Timestamp>,
show_nsfw -> Bool,
theme -> Varchar,
default_sort_type -> Int2,
default_listing_type -> Int2,
lang -> Varchar,
show_avatars -> Bool,
send_notifications_to_email -> Bool,
matrix_user_id -> Nullable<Text>,
actor_id -> Varchar,
bio -> Nullable<Text>,
local -> Bool,
private_key -> Nullable<Text>,
public_key -> Nullable<Text>,
last_refreshed_at -> Timestamp,
banner -> Nullable<Varchar>,
banner -> Nullable<Text>,
deleted -> Bool,
inbox_url -> Varchar,
shared_inbox_url -> Nullable<Varchar>,
}
}
joinable!(comment_alias_1 -> person_alias_1 (creator_id));
joinable!(comment_alias_1 -> user_alias_1 (creator_id));
joinable!(comment -> comment_alias_1 (parent_id));
joinable!(person_mention -> person_alias_1 (recipient_id));
joinable!(post -> person_alias_1 (creator_id));
joinable!(comment -> person_alias_1 (creator_id));
joinable!(user_mention -> user_alias_1 (recipient_id));
joinable!(post -> user_alias_1 (creator_id));
joinable!(comment -> user_alias_1 (creator_id));
joinable!(post_report -> person_alias_2 (resolver_id));
joinable!(comment_report -> person_alias_2 (resolver_id));
joinable!(post_report -> user_alias_2 (resolver_id));
joinable!(comment_report -> user_alias_2 (resolver_id));
joinable!(comment -> person (creator_id));
joinable!(comment -> post (post_id));
joinable!(comment -> user_ (creator_id));
joinable!(comment_aggregates -> comment (comment_id));
joinable!(comment_like -> comment (comment_id));
joinable!(comment_like -> person (person_id));
joinable!(comment_like -> post (post_id));
joinable!(comment_like -> user_ (user_id));
joinable!(comment_report -> comment (comment_id));
joinable!(comment_saved -> comment (comment_id));
joinable!(comment_saved -> person (person_id));
joinable!(community -> person (creator_id));
joinable!(comment_saved -> user_ (user_id));
joinable!(community -> category (category_id));
joinable!(community -> user_ (creator_id));
joinable!(community_aggregates -> community (community_id));
joinable!(community_follower -> community (community_id));
joinable!(community_follower -> person (person_id));
joinable!(community_follower -> user_ (user_id));
joinable!(community_moderator -> community (community_id));
joinable!(community_moderator -> person (person_id));
joinable!(community_person_ban -> community (community_id));
joinable!(community_person_ban -> person (person_id));
joinable!(local_user -> person (person_id));
joinable!(community_moderator -> user_ (user_id));
joinable!(community_user_ban -> community (community_id));
joinable!(community_user_ban -> user_ (user_id));
joinable!(mod_add_community -> community (community_id));
joinable!(mod_ban_from_community -> community (community_id));
joinable!(mod_lock_post -> person (mod_person_id));
joinable!(mod_lock_post -> post (post_id));
joinable!(mod_lock_post -> user_ (mod_user_id));
joinable!(mod_remove_comment -> comment (comment_id));
joinable!(mod_remove_comment -> person (mod_person_id));
joinable!(mod_remove_comment -> user_ (mod_user_id));
joinable!(mod_remove_community -> community (community_id));
joinable!(mod_remove_community -> person (mod_person_id));
joinable!(mod_remove_post -> person (mod_person_id));
joinable!(mod_remove_community -> user_ (mod_user_id));
joinable!(mod_remove_post -> post (post_id));
joinable!(mod_sticky_post -> person (mod_person_id));
joinable!(mod_remove_post -> user_ (mod_user_id));
joinable!(mod_sticky_post -> post (post_id));
joinable!(password_reset_request -> local_user (local_user_id));
joinable!(person_aggregates -> person (person_id));
joinable!(person_ban -> person (person_id));
joinable!(person_mention -> comment (comment_id));
joinable!(person_mention -> person (recipient_id));
joinable!(mod_sticky_post -> user_ (mod_user_id));
joinable!(password_reset_request -> user_ (user_id));
joinable!(post -> community (community_id));
joinable!(post -> person (creator_id));
joinable!(post -> user_ (creator_id));
joinable!(post_aggregates -> post (post_id));
joinable!(post_like -> person (person_id));
joinable!(post_like -> post (post_id));
joinable!(post_read -> person (person_id));
joinable!(post_like -> user_ (user_id));
joinable!(post_read -> post (post_id));
joinable!(post_read -> user_ (user_id));
joinable!(post_report -> post (post_id));
joinable!(post_saved -> person (person_id));
joinable!(post_saved -> post (post_id));
joinable!(site -> person (creator_id));
joinable!(post_saved -> user_ (user_id));
joinable!(site -> user_ (creator_id));
joinable!(site_aggregates -> site (site_id));
joinable!(user_aggregates -> user_ (user_id));
joinable!(user_ban -> user_ (user_id));
joinable!(user_mention -> comment (comment_id));
joinable!(user_mention -> user_ (recipient_id));
allow_tables_to_appear_in_same_query!(
activity,
category,
comment,
comment_aggregates,
comment_like,
@ -578,8 +596,7 @@ allow_tables_to_appear_in_same_query!(
community_aggregates,
community_follower,
community_moderator,
community_person_ban,
local_user,
community_user_ban,
mod_add,
mod_add_community,
mod_ban,
@ -590,10 +607,6 @@ allow_tables_to_appear_in_same_query!(
mod_remove_post,
mod_sticky_post,
password_reset_request,
person,
person_aggregates,
person_ban,
person_mention,
post,
post_aggregates,
post_like,
@ -603,7 +616,11 @@ allow_tables_to_appear_in_same_query!(
private_message,
site,
site_aggregates,
user_,
user_aggregates,
user_ban,
user_mention,
comment_alias_1,
person_alias_1,
person_alias_2,
user_alias_1,
user_alias_2,
);

View file

@ -1,4 +1,4 @@
use crate::{schema::activity, DbUrl};
use crate::schema::activity;
use serde_json::Value;
use std::fmt::Debug;
@ -10,7 +10,7 @@ pub struct Activity {
pub local: bool,
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub ap_id: Option<DbUrl>,
pub ap_id: Option<String>,
pub sensitive: Option<bool>,
}
@ -20,6 +20,6 @@ pub struct ActivityForm {
pub data: Value,
pub local: bool,
pub updated: Option<chrono::NaiveDateTime>,
pub ap_id: DbUrl,
pub ap_id: String,
pub sensitive: bool,
}

View file

@ -0,0 +1,15 @@
use crate::schema::category;
use serde::Serialize;
#[derive(Queryable, Identifiable, PartialEq, Debug, Serialize, Clone)]
#[table_name = "category"]
pub struct Category {
pub id: i32,
pub name: String,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "category"]
pub struct CategoryForm {
pub name: String,
}

View file

@ -1,10 +1,7 @@
use crate::{
schema::{comment, comment_alias_1, comment_like, comment_saved},
source::post::Post,
CommentId,
DbUrl,
PersonId,
PostId,
Url,
};
use serde::Serialize;
@ -19,17 +16,17 @@ use serde::Serialize;
#[belongs_to(Post)]
#[table_name = "comment"]
pub struct Comment {
pub id: CommentId,
pub creator_id: PersonId,
pub post_id: PostId,
pub parent_id: Option<CommentId>,
pub id: i32,
pub creator_id: i32,
pub post_id: i32,
pub parent_id: Option<i32>,
pub content: String,
pub removed: bool,
pub read: bool, // Whether the recipient has read the comment or not
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub ap_id: DbUrl,
pub ap_id: Url,
pub local: bool,
}
@ -37,33 +34,33 @@ pub struct Comment {
#[belongs_to(Post)]
#[table_name = "comment_alias_1"]
pub struct CommentAlias1 {
pub id: CommentId,
pub creator_id: PersonId,
pub post_id: PostId,
pub parent_id: Option<CommentId>,
pub id: i32,
pub creator_id: i32,
pub post_id: i32,
pub parent_id: Option<i32>,
pub content: String,
pub removed: bool,
pub read: bool, // Whether the recipient has read the comment or not
pub published: chrono::NaiveDateTime,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: bool,
pub ap_id: DbUrl,
pub ap_id: Url,
pub local: bool,
}
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "comment"]
pub struct CommentForm {
pub creator_id: PersonId,
pub post_id: PostId,
pub parent_id: Option<CommentId>,
pub creator_id: i32,
pub post_id: i32,
pub parent_id: Option<i32>,
pub content: String,
pub removed: Option<bool>,
pub read: Option<bool>,
pub published: Option<chrono::NaiveDateTime>,
pub updated: Option<chrono::NaiveDateTime>,
pub deleted: Option<bool>,
pub ap_id: Option<DbUrl>,
pub ap_id: Option<Url>,
pub local: bool,
}
@ -72,9 +69,9 @@ pub struct CommentForm {
#[table_name = "comment_like"]
pub struct CommentLike {
pub id: i32,
pub person_id: PersonId,
pub comment_id: CommentId,
pub post_id: PostId, // TODO this is redundant
pub user_id: i32,
pub comment_id: i32,
pub post_id: i32, // TODO this is redundant
pub score: i16,
pub published: chrono::NaiveDateTime,
}
@ -82,9 +79,9 @@ pub struct CommentLike {
#[derive(Insertable, AsChangeset, Clone)]
#[table_name = "comment_like"]
pub struct CommentLikeForm {
pub person_id: PersonId,
pub comment_id: CommentId,
pub post_id: PostId, // TODO this is redundant
pub user_id: i32,
pub comment_id: i32,
pub post_id: i32, // TODO this is redundant
pub score: i16,
}
@ -93,14 +90,14 @@ pub struct CommentLikeForm {
#[table_name = "comment_saved"]
pub struct CommentSaved {
pub id: i32,
pub comment_id: CommentId,
pub person_id: PersonId,
pub comment_id: i32,
pub user_id: i32,
pub published: chrono::NaiveDateTime,
}
#[derive(Insertable, AsChangeset)]
#[table_name = "comment_saved"]
pub struct CommentSavedForm {
pub comment_id: CommentId,
pub person_id: PersonId,
pub comment_id: i32,
pub user_id: i32,
}

Some files were not shown because too many files have changed in this diff Show more