Merge pull request #208 from usbharu/feature/refactor-user-table

Feature/refactor user table
This commit is contained in:
usbharu 2023-12-12 18:33:52 +09:00 committed by GitHub
commit c699357e01
123 changed files with 1611 additions and 1025 deletions

View File

@ -154,6 +154,9 @@ performance:
UnnecessaryTemporaryInstantiation:
active: true
SpreadOperator:
active: false
potential-bugs:
CastToNullableType:
active: true

View File

@ -1,4 +1,4 @@
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Users
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Actors
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
@ -16,13 +16,13 @@ object AssertionUtil {
domain
}
val selectAll = Users.selectAll()
val selectAll = Actors.selectAll()
println(selectAll.fetchSize)
println(selectAll.toList().size)
selectAll.map { "@${it[Users.name]}@${it[Users.domain]}" }.forEach { println(it) }
selectAll.map { "@${it[Actors.name]}@${it[Actors.domain]}" }.forEach { println(it) }
return Users.select { Users.name eq username and (Users.domain eq s) }.empty().not()
return Actors.select { Actors.name eq username and (Actors.domain eq s) }.empty().not()
}
}

View File

@ -1,7 +1,7 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into actors (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (1730415786666758144, 'test-user', 'localhost', 'Im test user.', 'THis account is test user.',
'$2a$10$/mWC/n7nC7X3l9qCEOKnredxne2zewoqEsJWTOdlKfg2zXKJ0F9Em', 'http://localhost/users/test-user/inbox',
'http://localhost/users/test-user/inbox',
'http://localhost/users/test-user/outbox', 'http://localhost/users/test-user',
'-----BEGIN PUBLIC KEY-----
MIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEAi4mifRg6huAIn6DXk3Vn
@ -43,4 +43,8 @@ Ja15+ZWbOA4vJA9pOh3x4XM=
-----END PRIVATE KEY-----
', 1701398248417,
'http://localhost/users/test-user#pubkey', 'http://localhost/users/test-user/following',
'http://localhost/users/test-users/followers', null);
'http://localhost/users/test-users/followers', null, false);
insert into user_details (actor_id, password, auto_accept_followee_follow_request)
values ( 1730415786666758144
, '$2a$10$/mWC/n7nC7X3l9qCEOKnredxne2zewoqEsJWTOdlKfg2zXKJ0F9Em', true)

View File

@ -1,8 +1,8 @@
package mastodon.account
import dev.usbharu.hideout.SpringApplication
import dev.usbharu.hideout.core.infrastructure.exposedquery.ActorQueryServiceImpl
import dev.usbharu.hideout.core.infrastructure.exposedquery.FollowerQueryServiceImpl
import dev.usbharu.hideout.core.infrastructure.exposedquery.UserQueryServiceImpl
import kotlinx.coroutines.test.runTest
import org.assertj.core.api.Assertions.assertThat
import org.flywaydb.core.Flyway
@ -39,7 +39,7 @@ class AccountApiTest {
private lateinit var followerQueryServiceImpl: FollowerQueryServiceImpl
@Autowired
private lateinit var userQueryServiceImpl: UserQueryServiceImpl
private lateinit var userQueryServiceImpl: ActorQueryServiceImpl
@Autowired

View File

@ -2,7 +2,7 @@ package util
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.infrastructure.springframework.httpsignature.HttpSignatureUser
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.httpsignature.common.HttpHeaders
import dev.usbharu.httpsignature.common.HttpMethod
import dev.usbharu.httpsignature.common.HttpRequest
@ -14,7 +14,7 @@ import org.springframework.security.web.authentication.preauth.PreAuthenticatedA
import java.net.URL
class WithHttpSignatureSecurityContextFactory(
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val transaction: Transaction
) : WithSecurityContextFactory<WithHttpSignature> {
@ -28,7 +28,7 @@ class WithHttpSignatureSecurityContextFactory(
)
)
val httpSignatureUser = transaction.transaction {
val findByKeyId = userQueryService.findByKeyId(annotation.keyId)
val findByKeyId = actorQueryService.findByKeyId(annotation.keyId)
HttpSignatureUser(
findByKeyId.name,
findByKeyId.domain,

View File

@ -1,18 +1,16 @@
insert into "USERS" (id, name, domain, screen_name, description, password, inbox, outbox, url, public_key, private_key,
created_at, key_id, following, followers, instance)
insert into "actors" (id, name, domain, screen_name, description, inbox, outbox, url, public_key, private_key,
created_at, key_id, following, followers, instance, locked)
VALUES (3733363, 'follow-test-user-1', 'example.com', 'follow-test-user-1-name', '',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/follow-test-user-1/inbox',
'https://example.com/users/follow-test-user-1/outbox', 'https://example.com/users/follow-test-user-1',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/follow-test-user-1#pubkey', 'https://example.com/users/follow-test-user-1/following',
'https://example.com/users/follow-test-user-1/followers', null),
'https://example.com/users/follow-test-user-1/followers', null, false),
(37335363, 'follow-test-user-2', 'example.com', 'follow-test-user-2-name', '',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/follow-test-user-2/inbox',
'https://example.com/users/follow-test-user-2/outbox', 'https://example.com/users/follow-test-user-2',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/follow-test-user-2#pubkey', 'https://example.com/users/follow-test-user-2/following',
'https://example.com/users/follow-test-user-2/followers', null);
'https://example.com/users/follow-test-user-2/followers', null, false);

View File

@ -1,30 +1,25 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (8, 'test-user8', 'example.com', 'Im test-user8.', 'THis account is test-user8.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user8/inbox',
'https://example.com/users/test-user8/outbox', 'https://example.com/users/test-user8',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user8#pubkey', 'https://example.com/users/test-user8/following',
'https://example.com/users/test-user8/followers', null);
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
VALUES (9, 'test-user9', 'follower.example.com', 'Im test-user9.', 'THis account is test-user9.',
null,
'https://example.com/users/test-user8/followers', null, false),
(9, 'test-user9', 'follower.example.com', 'Im test-user9.', 'THis account is test-user9.',
'https://follower.example.com/users/test-user9/inbox',
'https://follower.example.com/users/test-user9/outbox', 'https://follower.example.com/users/test-user9',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
null, 12345678,
'https://follower.example.com/users/test-user9#pubkey',
'https://follower.example.com/users/test-user9/following',
'https://follower.example.com/users/test-user9/followers', null);
'https://follower.example.com/users/test-user9/followers', null, false);
insert into relationships (user_id, target_user_id, following, blocking, muting, follow_request,
insert into relationships (actor_id, target_actor_id, following, blocking, muting, follow_request,
ignore_follow_request)
VALUES (9, 8, true, false, false, false, false);
insert into POSTS (ID, USER_ID, OVERVIEW, TEXT, CREATED_AT, VISIBILITY, URL, REPLY_ID, REPOST_ID, SENSITIVE, AP_ID)
insert into POSTS (ID, ACTOR_ID, OVERVIEW, TEXT, CREATED_AT, VISIBILITY, URL, REPLY_ID, REPOST_ID, SENSITIVE, AP_ID)
VALUES (1239, 8, null, 'test post', 12345680, 2, 'https://example.com/users/test-user8/posts/1239', null, null, false,
'https://example.com/users/test-user8/posts/1239');

View File

@ -1,31 +1,26 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (4, 'test-user4', 'example.com', 'Im test user4.', 'THis account is test user4.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user4/inbox',
'https://example.com/users/test-user4/outbox', 'https://example.com/users/test-user4',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user4#pubkey', 'https://example.com/users/test-user4/following',
'https://example.com/users/test-user4/followers', null);
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
VALUES (5, 'test-user5', 'follower.example.com', 'Im test user5.', 'THis account is test user5.',
null,
'https://example.com/users/test-user4/followers', null, false),
(5, 'test-user5', 'follower.example.com', 'Im test user5.', 'THis account is test user5.',
'https://follower.example.com/users/test-user5/inbox',
'https://follower.example.com/users/test-user5/outbox', 'https://follower.example.com/users/test-user5',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
null, 12345678,
'https://follower.example.com/users/test-user5#pubkey',
'https://follower.example.com/users/test-user5/following',
'https://follower.example.com/users/test-user5/followers', null);
'https://follower.example.com/users/test-user5/followers', null, false);
insert into relationships (user_id, target_user_id, following, blocking, muting, follow_request,
insert into relationships (actor_id, target_actor_id, following, blocking, muting, follow_request,
ignore_follow_request)
VALUES (5, 4, true, false, false, false, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, "actor_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1237, 4, null, 'test post', 12345680, 0, 'https://example.com/users/test-user4/posts/1237', null, null, false,
'https://example.com/users/test-user4/posts/1237');

View File

@ -1,31 +1,26 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (6, 'test-user6', 'example.com', 'Im test-user6.', 'THis account is test-user6.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user6/inbox',
'https://example.com/users/test-user6/outbox', 'https://example.com/users/test-user6',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user6#pubkey', 'https://example.com/users/test-user6/following',
'https://example.com/users/test-user6/followers', null);
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
VALUES (7, 'test-user7', 'follower.example.com', 'Im test-user7.', 'THis account is test-user7.',
null,
'https://example.com/users/test-user6/followers', null, false),
(7, 'test-user7', 'follower.example.com', 'Im test-user7.', 'THis account is test-user7.',
'https://follower.example.com/users/test-user7/inbox',
'https://follower.example.com/users/test-user7/outbox', 'https://follower.example.com/users/test-user7',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
null, 12345678,
'https://follower.example.com/users/test-user7#pubkey',
'https://follower.example.com/users/test-user7/following',
'https://follower.example.com/users/test-user7/followers', null);
'https://follower.example.com/users/test-user7/followers', null, false);
insert into relationships (user_id, target_user_id, following, blocking, muting, follow_request,
insert into relationships (actor_id, target_actor_id, following, blocking, muting, follow_request,
ignore_follow_request)
VALUES (7, 6, true, false, false, false, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, "actor_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1238, 6, null, 'test post', 12345680, 1, 'https://example.com/users/test-user6/posts/1238', null, null, false,
'https://example.com/users/test-user6/posts/1238');

View File

@ -1,15 +1,14 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (11, 'test-user11', 'example.com', 'Im test-user11.', 'THis account is test-user11.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user11/inbox',
'https://example.com/users/test-user11/outbox', 'https://example.com/users/test-user11',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user11#pubkey', 'https://example.com/users/test-user11/following',
'https://example.com/users/test-user11/followers', null);
'https://example.com/users/test-user11/followers', null, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, actor_id, OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1242, 11, null, 'test post', 12345680, 0, 'https://example.com/users/test-user11/posts/1242', null, null, false,
'https://example.com/users/test-user11/posts/1242');

View File

@ -1,15 +1,14 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (10, 'test-user10', 'example.com', 'Im test-user10.', 'THis account is test-user10.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user10/inbox',
'https://example.com/users/test-user10/outbox', 'https://example.com/users/test-user10',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user10#pubkey', 'https://example.com/users/test-user10/following',
'https://example.com/users/test-user10/followers', null);
'https://example.com/users/test-user10/followers', null, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, actor_id, OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1240, 10, null, 'test post', 12345680, 0, 'https://example.com/users/test-user10/posts/1240', null, null, false,
'https://example.com/users/test-user10/posts/1240'),

View File

@ -1,15 +1,14 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (3, 'test-user3', 'example.com', 'Im test user3.', 'THis account is test user3.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user3/inbox',
'https://example.com/users/test-user3/outbox', 'https://example.com/users/test-user3',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user3#pubkey', 'https://example.com/users/test-user3/following',
'https://example.com/users/test-user3/followers', null);
'https://example.com/users/test-user3/followers', null, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, actor_id, OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1236, 3, null, 'test post', 12345680, 2, 'https://example.com/users/test-user3/posts/1236', null, null, false,
'https://example.com/users/test-user3/posts/1236')

View File

@ -1,14 +1,14 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into actors (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (1, 'test-user', 'example.com', 'Im test user.', 'THis account is test user.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8', 'https://example.com/users/test-user/inbox',
'https://example.com/users/test-user/inbox',
'https://example.com/users/test-user/outbox', 'https://example.com/users/test-user',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user#pubkey', 'https://example.com/users/test-user/following',
'https://example.com/users/test-users/followers', null);
'https://example.com/users/test-users/followers', null, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, actor_id, OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1234, 1, null, 'test post', 12345680, 0, 'https://example.com/users/test-user/posts/1234', null, null, false,
'https://example.com/users/test-user/posts/1234')

View File

@ -1,15 +1,14 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into actors (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (2, 'test-user2', 'example.com', 'Im test user2.', 'THis account is test user2.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user2/inbox',
'https://example.com/users/test-user2/outbox', 'https://example.com/users/test-user2',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user2#pubkey', 'https://example.com/users/test-user2/following',
'https://example.com/users/test-user2/followers', null);
'https://example.com/users/test-user2/followers', null, false);
insert into POSTS (ID, "USER_ID", OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
insert into POSTS (ID, actor_id, OVERVIEW, TEXT, "CREATED_AT", VISIBILITY, URL, "REPOST_ID", "REPLY_ID", SENSITIVE,
AP_ID)
VALUES (1235, 2, null, 'test post', 12345680, 1, 'https://example.com/users/test-user2/posts/1235', null, null, false,
'https://example.com/users/test-user2/posts/1235')

View File

@ -1,3 +1,3 @@
insert into posts (id, user_id, overview, text, created_at, visibility, url, repost_id, reply_id, sensitive, ap_id)
insert into posts (id, actor_id, overview, text, created_at, visibility, url, repost_id, reply_id, sensitive, ap_id)
VALUES (1, 1, null, 'hello', 1234455, 0, 'https://localhost/users/1/posts/1', null, null, false,
'https://users/1/posts/1');

View File

@ -1,9 +1,9 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (1, 'test-user', 'example.com', 'Im test user.', 'THis account is test user.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8', 'https://example.com/users/test-user/inbox',
'https://example.com/users/test-user/inbox',
'https://example.com/users/test-user/outbox', 'https://example.com/users/test-user',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user#pubkey', 'https://example.com/users/test-user/following',
'https://example.com/users/test-users/followers', null);
'https://example.com/users/test-users/followers', null, false);

View File

@ -1,10 +1,9 @@
insert into "USERS" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, PASSWORD, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE)
insert into "actors" (ID, NAME, DOMAIN, SCREEN_NAME, DESCRIPTION, INBOX, OUTBOX, URL, PUBLIC_KEY, PRIVATE_KEY,
CREATED_AT, KEY_ID, FOLLOWING, FOLLOWERS, INSTANCE, LOCKED)
VALUES (2, 'test-user2', 'example.com', 'Im test user.', 'THis account is test user.',
'5e884898da28047151d0e56f8dc6292773603d0d6aabbdd62a11ef721d1542d8',
'https://example.com/users/test-user2/inbox',
'https://example.com/users/test-user2/outbox', 'https://example.com/users/test-user2',
'-----BEGIN PUBLIC KEY-----...-----END PUBLIC KEY-----',
'-----BEGIN PRIVATE KEY-----...-----END PRIVATE KEY-----', 12345678,
'https://example.com/users/test-user2#pubkey', 'https://example.com/users/test-user2/following',
'https://example.com/users/test-user2s/followers', null);
'https://example.com/users/test-user2s/followers', null, false);

View File

@ -17,7 +17,8 @@ constructor(
var publicKey: Key,
var endpoints: Map<String, String> = emptyMap(),
var followers: String?,
var following: String?
var following: String?,
val manuallyApprovesFollowers: Boolean? = false
) : Object(add(type, "Person")), HasId, HasName {
@Suppress("CyclomaticComplexMethod", "CognitiveComplexMethod")
@ -40,6 +41,7 @@ constructor(
if (endpoints != other.endpoints) return false
if (followers != other.followers) return false
if (following != other.following) return false
if (manuallyApprovesFollowers != other.manuallyApprovesFollowers) return false
return true
}
@ -59,6 +61,26 @@ constructor(
result = 31 * result + endpoints.hashCode()
result = 31 * result + (followers?.hashCode() ?: 0)
result = 31 * result + (following?.hashCode() ?: 0)
result = 31 * result + manuallyApprovesFollowers.hashCode()
return result
}
override fun toString(): String {
return "Person(" +
"name='$name', " +
"id='$id', " +
"preferredUsername=$preferredUsername, " +
"summary=$summary, " +
"inbox='$inbox', " +
"outbox='$outbox', " +
"url='$url', " +
"icon=$icon, " +
"publicKey=$publicKey, " +
"endpoints=$endpoints, " +
"followers=$followers, " +
"following=$following, " +
"manuallyApprovesFollowers=$manuallyApprovesFollowers" +
")" +
" ${super.toString()}"
}
}

View File

@ -23,7 +23,7 @@ class NoteQueryServiceImpl(private val postRepository: PostRepository, private v
NoteQueryService {
override suspend fun findById(id: Long): Pair<Note, Post> {
return Posts
.leftJoin(Users)
.leftJoin(Actors)
.leftJoin(PostsMedia)
.leftJoin(Media)
.select { Posts.id eq id }
@ -35,7 +35,7 @@ class NoteQueryServiceImpl(private val postRepository: PostRepository, private v
override suspend fun findByApid(apId: String): Pair<Note, Post> {
return Posts
.leftJoin(Users)
.leftJoin(Actors)
.leftJoin(PostsMedia)
.leftJoin(Media)
.select { Posts.apId eq apId }
@ -61,11 +61,11 @@ class NoteQueryServiceImpl(private val postRepository: PostRepository, private v
val visibility1 =
visibility(
Visibility.values().first { visibility -> visibility.ordinal == this[Posts.visibility] },
this[Users.followers]
this[Actors.followers]
)
return Note(
id = this[Posts.apId],
attributedTo = this[Users.url],
attributedTo = this[Actors.url],
content = this[Posts.text],
published = Instant.ofEpochMilli(this[Posts.createdAt]).toString(),
to = visibility1.first,

View File

@ -4,20 +4,20 @@ import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.DeliverAcceptJob
import dev.usbharu.hideout.core.external.job.DeliverAcceptJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import org.springframework.stereotype.Service
@Service
class APDeliverAcceptJobProcessor(
private val apRequestService: APRequestService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val deliverAcceptJob: DeliverAcceptJob,
private val transaction: Transaction
) :
JobProcessor<DeliverAcceptJobParam, DeliverAcceptJob> {
override suspend fun process(param: DeliverAcceptJobParam): Unit = transaction.transaction {
apRequestService.apPost(param.inbox, param.accept, userQueryService.findById(param.signer))
apRequestService.apPost(param.inbox, param.accept, actorQueryService.findById(param.signer))
}
override fun job(): DeliverAcceptJob = deliverAcceptJob

View File

@ -7,14 +7,14 @@ import dev.usbharu.hideout.activitypub.service.common.AbstractActivityPubProcess
import dev.usbharu.hideout.activitypub.service.common.ActivityPubProcessContext
import dev.usbharu.hideout.activitypub.service.common.ActivityType
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import org.springframework.stereotype.Service
@Service
class ApAcceptProcessor(
transaction: Transaction,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val relationshipService: RelationshipService
) :
AbstractActivityPubProcessor<Accept>(transaction) {
@ -32,8 +32,8 @@ class ApAcceptProcessor(
val userUrl = follow.apObject
val followerUrl = follow.actor
val user = userQueryService.findByUrl(userUrl)
val follower = userQueryService.findByUrl(followerUrl)
val user = actorQueryService.findByUrl(userUrl)
val follower = actorQueryService.findByUrl(followerUrl)
relationshipService.acceptFollowRequest(user.id, follower.id)
logger.debug("SUCCESS Follow from ${user.url} to ${follower.url}.")

View File

@ -2,14 +2,14 @@ package dev.usbharu.hideout.activitypub.service.activity.accept
import dev.usbharu.hideout.activitypub.domain.model.Accept
import dev.usbharu.hideout.activitypub.domain.model.Follow
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.external.job.DeliverAcceptJob
import dev.usbharu.hideout.core.external.job.DeliverAcceptJobParam
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import org.springframework.stereotype.Service
interface ApSendAcceptService {
suspend fun sendAcceptFollow(user: User, target: User)
suspend fun sendAcceptFollow(actor: Actor, target: Actor)
}
@Service
@ -17,17 +17,17 @@ class ApSendAcceptServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val deliverAcceptJob: DeliverAcceptJob
) : ApSendAcceptService {
override suspend fun sendAcceptFollow(user: User, target: User) {
override suspend fun sendAcceptFollow(actor: Actor, target: Actor) {
val deliverAcceptJobParam = DeliverAcceptJobParam(
Accept(
apObject = Follow(
apObject = user.url,
apObject = actor.url,
actor = target.url
),
actor = user.url
actor = actor.url
),
target.inbox,
user.id
actor.id
)
jobQueueParentService.scheduleTypeSafe(deliverAcceptJob, deliverAcceptJobParam)

View File

@ -2,7 +2,7 @@ package dev.usbharu.hideout.activitypub.service.activity.block
import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.user.UserRepository
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.external.job.DeliverBlockJob
import dev.usbharu.hideout.core.external.job.DeliverBlockJobParam
import dev.usbharu.hideout.core.service.job.JobProcessor
@ -14,12 +14,12 @@ import org.springframework.stereotype.Service
@Service
class APDeliverBlockJobProcessor(
private val apRequestService: APRequestService,
private val userRepository: UserRepository,
private val actorRepository: ActorRepository,
private val transaction: Transaction,
private val deliverBlockJob: DeliverBlockJob
) : JobProcessor<DeliverBlockJobParam, DeliverBlockJob> {
override suspend fun process(param: DeliverBlockJobParam): Unit = transaction.transaction {
val signer = userRepository.findById(param.signer)
val signer = actorRepository.findById(param.signer)
apRequestService.apPost(
param.inbox,
param.reject,

View File

@ -4,14 +4,14 @@ import dev.usbharu.hideout.activitypub.domain.model.Block
import dev.usbharu.hideout.activitypub.domain.model.Follow
import dev.usbharu.hideout.activitypub.domain.model.Reject
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.external.job.DeliverBlockJob
import dev.usbharu.hideout.core.external.job.DeliverBlockJobParam
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import org.springframework.stereotype.Service
interface APSendBlockService {
suspend fun sendBlock(user: User, target: User)
suspend fun sendBlock(actor: Actor, target: Actor)
}
@Service
@ -20,19 +20,19 @@ class ApSendBlockServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val deliverBlockJob: DeliverBlockJob
) : APSendBlockService {
override suspend fun sendBlock(user: User, target: User) {
override suspend fun sendBlock(actor: Actor, target: Actor) {
val blockJobParam = DeliverBlockJobParam(
user.id,
actor.id,
Block(
user.url,
"${applicationConfig.url}/block/${user.id}/${target.id}",
actor.url,
"${applicationConfig.url}/block/${actor.id}/${target.id}",
target.url
),
Reject(
user.url,
"${applicationConfig.url}/reject/${user.id}/${target.id}",
actor.url,
"${applicationConfig.url}/reject/${actor.id}/${target.id}",
Follow(
apObject = user.url,
apObject = actor.url,
actor = target.url
)
),

View File

@ -5,7 +5,7 @@ import dev.usbharu.hideout.activitypub.service.common.AbstractActivityPubProcess
import dev.usbharu.hideout.activitypub.service.common.ActivityPubProcessContext
import dev.usbharu.hideout.activitypub.service.common.ActivityType
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import org.springframework.stereotype.Service
@ -14,14 +14,14 @@ import org.springframework.stereotype.Service
*/
@Service
class BlockActivityPubProcessor(
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val relationshipService: RelationshipService,
transaction: Transaction
) :
AbstractActivityPubProcessor<Block>(transaction) {
override suspend fun internalProcess(activity: ActivityPubProcessContext<Block>) {
val user = userQueryService.findByUrl(activity.activity.actor)
val target = userQueryService.findByUrl(activity.activity.apObject)
val user = actorQueryService.findByUrl(activity.activity.actor)
val target = actorQueryService.findByUrl(activity.activity.apObject)
relationshipService.block(user.id, target.id)
}

View File

@ -6,8 +6,8 @@ import dev.usbharu.hideout.activitypub.query.NoteQueryService
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.external.job.DeliverPostJob
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.query.FollowerQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@ -17,7 +17,7 @@ class ApSendCreateServiceImpl(
private val followerQueryService: FollowerQueryService,
private val objectMapper: ObjectMapper,
private val jobQueueParentService: JobQueueParentService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val noteQueryService: NoteQueryService,
private val applicationConfig: ApplicationConfig
) : ApSendCreateService {
@ -25,11 +25,11 @@ class ApSendCreateServiceImpl(
logger.info("CREATE Create Local Note ${post.url}")
logger.debug("START Create Local Note ${post.url}")
logger.trace("{}", post)
val followers = followerQueryService.findFollowersById(post.userId)
val followers = followerQueryService.findFollowersById(post.actorId)
logger.debug("DELIVER Deliver Note Create ${followers.size} accounts.")
val userEntity = userQueryService.findById(post.userId)
val userEntity = actorQueryService.findById(post.actorId)
val note = noteQueryService.findById(post.id).first
val create = Create(
name = "Create Note",

View File

@ -7,7 +7,7 @@ import dev.usbharu.hideout.activitypub.service.objects.user.APUserService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.ReceiveFollowJob
import dev.usbharu.hideout.core.external.job.ReceiveFollowJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import org.slf4j.LoggerFactory
@ -16,7 +16,7 @@ import org.springframework.stereotype.Service
@Service
class APReceiveFollowJobProcessor(
private val transaction: Transaction,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val apUserService: APUserService,
private val objectMapper: ObjectMapper,
private val relationshipService: RelationshipService
@ -28,9 +28,9 @@ class APReceiveFollowJobProcessor(
logger.info("START Follow from: {} to {}", param.targetActor, param.actor)
val targetEntity = userQueryService.findByUrl(param.targetActor)
val targetEntity = actorQueryService.findByUrl(param.targetActor)
val followActorEntity =
userQueryService.findByUrl(follow.actor)
actorQueryService.findByUrl(follow.actor)
relationshipService.followRequest(followActorEntity.id, targetEntity.id)

View File

@ -15,10 +15,10 @@ class APSendFollowServiceImpl(
) : APSendFollowService {
override suspend fun sendFollow(sendFollowDto: SendFollowDto) {
val follow = Follow(
apObject = sendFollowDto.followTargetUserId.url,
actor = sendFollowDto.userId.url
apObject = sendFollowDto.followTargetActorId.url,
actor = sendFollowDto.actorId.url
)
apRequestService.apPost(sendFollowDto.followTargetUserId.inbox, follow, sendFollowDto.userId)
apRequestService.apPost(sendFollowDto.followTargetActorId.inbox, follow, sendFollowDto.actorId)
}
}

View File

@ -4,9 +4,9 @@ import com.fasterxml.jackson.databind.ObjectMapper
import dev.usbharu.hideout.core.domain.model.reaction.Reaction
import dev.usbharu.hideout.core.external.job.DeliverReactionJob
import dev.usbharu.hideout.core.external.job.DeliverRemoveReactionJob
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.query.FollowerQueryService
import dev.usbharu.hideout.core.query.PostQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.service.job.JobQueueParentService
import org.springframework.beans.factory.annotation.Qualifier
import org.springframework.stereotype.Service
@ -19,14 +19,14 @@ interface APReactionService {
@Service
class APReactionServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val followerQueryService: FollowerQueryService,
private val postQueryService: PostQueryService,
@Qualifier("activitypub") private val objectMapper: ObjectMapper
) : APReactionService {
override suspend fun reaction(like: Reaction) {
val followers = followerQueryService.findFollowersById(like.userId)
val user = userQueryService.findById(like.userId)
val followers = followerQueryService.findFollowersById(like.actorId)
val user = actorQueryService.findById(like.actorId)
val post =
postQueryService.findById(like.postId)
followers.forEach { follower ->
@ -41,8 +41,8 @@ class APReactionServiceImpl(
}
override suspend fun removeReaction(like: Reaction) {
val followers = followerQueryService.findFollowersById(like.userId)
val user = userQueryService.findById(like.userId)
val followers = followerQueryService.findFollowersById(like.actorId)
val user = actorQueryService.findById(like.actorId)
val post =
postQueryService.findById(like.postId)
followers.forEach { follower ->

View File

@ -6,19 +6,19 @@ import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.DeliverReactionJob
import dev.usbharu.hideout.core.external.job.DeliverReactionJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import org.springframework.stereotype.Service
@Service
class ApReactionJobProcessor(
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val apRequestService: APRequestService,
private val applicationConfig: ApplicationConfig,
private val transaction: Transaction
) : JobProcessor<DeliverReactionJobParam, DeliverReactionJob> {
override suspend fun process(param: DeliverReactionJobParam): Unit = transaction.transaction {
val signer = userQueryService.findByUrl(param.actor)
val signer = actorQueryService.findByUrl(param.actor)
apRequestService.apPost(
param.inbox,

View File

@ -9,14 +9,14 @@ import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.DeliverRemoveReactionJob
import dev.usbharu.hideout.core.external.job.DeliverRemoveReactionJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import org.springframework.stereotype.Service
import java.time.Instant
@Service
class ApRemoveReactionJobProcessor(
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val transaction: Transaction,
private val objectMapper: ObjectMapper,
private val apRequestService: APRequestService,
@ -25,7 +25,7 @@ class ApRemoveReactionJobProcessor(
override suspend fun process(param: DeliverRemoveReactionJobParam): Unit = transaction.transaction {
val like = objectMapper.readValue<Like>(param.like)
val signer = userQueryService.findByUrl(param.actor)
val signer = actorQueryService.findByUrl(param.actor)
apRequestService.apPost(
param.inbox,

View File

@ -4,20 +4,20 @@ import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.DeliverRejectJob
import dev.usbharu.hideout.core.external.job.DeliverRejectJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import org.springframework.stereotype.Component
@Component
class APDeliverRejectJobProcessor(
private val apRequestService: APRequestService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val deliverRejectJob: DeliverRejectJob,
private val transaction: Transaction
) :
JobProcessor<DeliverRejectJobParam, DeliverRejectJob> {
override suspend fun process(param: DeliverRejectJobParam): Unit = transaction.transaction {
apRequestService.apPost(param.inbox, param.reject, userQueryService.findById(param.signer))
apRequestService.apPost(param.inbox, param.reject, actorQueryService.findById(param.signer))
}
override fun job(): DeliverRejectJob = deliverRejectJob

View File

@ -6,14 +6,14 @@ import dev.usbharu.hideout.activitypub.service.common.AbstractActivityPubProcess
import dev.usbharu.hideout.activitypub.service.common.ActivityPubProcessContext
import dev.usbharu.hideout.activitypub.service.common.ActivityType
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import org.springframework.stereotype.Service
@Service
class ApRejectProcessor(
private val relationshipService: RelationshipService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
transaction: Transaction
) :
AbstractActivityPubProcessor<Reject>(transaction) {
@ -26,13 +26,13 @@ class ApRejectProcessor(
}
when (activityType) {
"Follow" -> {
val user = userQueryService.findByUrl(activity.activity.actor)
val user = actorQueryService.findByUrl(activity.activity.actor)
activity.activity.apObject as Follow
val actor = activity.activity.apObject.actor
val target = userQueryService.findByUrl(actor)
val target = actorQueryService.findByUrl(actor)
logger.debug("REJECT Follow user {} target {}", user.url, target.url)

View File

@ -1,7 +1,7 @@
package dev.usbharu.hideout.activitypub.service.activity.reject
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
interface ApSendRejectService {
suspend fun sendRejectFollow(user: User, target: User)
suspend fun sendRejectFollow(actor: Actor, target: Actor)
}

View File

@ -3,7 +3,7 @@ package dev.usbharu.hideout.activitypub.service.activity.reject
import dev.usbharu.hideout.activitypub.domain.model.Follow
import dev.usbharu.hideout.activitypub.domain.model.Reject
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.external.job.DeliverRejectJob
import dev.usbharu.hideout.core.external.job.DeliverRejectJobParam
import dev.usbharu.hideout.core.service.job.JobQueueParentService
@ -15,15 +15,15 @@ class ApSendRejectServiceImpl(
private val jobQueueParentService: JobQueueParentService,
private val deliverRejectJob: DeliverRejectJob
) : ApSendRejectService {
override suspend fun sendRejectFollow(user: User, target: User) {
override suspend fun sendRejectFollow(actor: Actor, target: Actor) {
val deliverRejectJobParam = DeliverRejectJobParam(
Reject(
user.url,
"${applicationConfig.url}/reject/${user.id}/${target.id}",
Follow(apObject = user.url, actor = target.url)
actor.url,
"${applicationConfig.url}/reject/${actor.id}/${target.id}",
Follow(apObject = actor.url, actor = target.url)
),
target.inbox,
user.id
actor.id
)
jobQueueParentService.scheduleTypeSafe(deliverRejectJob, deliverRejectJobParam)

View File

@ -4,7 +4,7 @@ import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.DeliverUndoJob
import dev.usbharu.hideout.core.external.job.DeliverUndoJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import org.springframework.stereotype.Service
@ -12,11 +12,11 @@ import org.springframework.stereotype.Service
class APDeliverUndoJobProcessor(
private val deliverUndoJob: DeliverUndoJob,
private val apRequestService: APRequestService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val transaction: Transaction
) : JobProcessor<DeliverUndoJobParam, DeliverUndoJob> {
override suspend fun process(param: DeliverUndoJobParam): Unit = transaction.transaction {
apRequestService.apPost(param.inbox, param.undo, userQueryService.findById(param.signer))
apRequestService.apPost(param.inbox, param.undo, actorQueryService.findById(param.signer))
}
override fun job(): DeliverUndoJob = deliverUndoJob

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.activitypub.service.activity.undo
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
interface APSendUndoService {
suspend fun sendUndoFollow(user: User, target: User)
suspend fun sendUndoBlock(user: User, target: User)
suspend fun sendUndoFollow(actor: Actor, target: Actor)
suspend fun sendUndoBlock(actor: Actor, target: Actor)
}

View File

@ -4,7 +4,7 @@ import dev.usbharu.hideout.activitypub.domain.model.Block
import dev.usbharu.hideout.activitypub.domain.model.Follow
import dev.usbharu.hideout.activitypub.domain.model.Undo
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.external.job.DeliverUndoJob
import dev.usbharu.hideout.core.external.job.DeliverUndoJobParam
import dev.usbharu.hideout.core.service.job.JobQueueParentService
@ -17,38 +17,38 @@ class APSendUndoServiceImpl(
private val deliverUndoJob: DeliverUndoJob,
private val applicationConfig: ApplicationConfig
) : APSendUndoService {
override suspend fun sendUndoFollow(user: User, target: User) {
override suspend fun sendUndoFollow(actor: Actor, target: Actor) {
val deliverUndoJobParam = DeliverUndoJobParam(
Undo(
actor = user.url,
id = "${applicationConfig.url}/undo/follow/${user.id}/${target.url}",
actor = actor.url,
id = "${applicationConfig.url}/undo/follow/${actor.id}/${target.url}",
apObject = Follow(
apObject = user.url,
apObject = actor.url,
actor = target.url
),
published = Instant.now().toString()
),
target.inbox,
user.id
actor.id
)
jobQueueParentService.scheduleTypeSafe(deliverUndoJob, deliverUndoJobParam)
}
override suspend fun sendUndoBlock(user: User, target: User) {
override suspend fun sendUndoBlock(actor: Actor, target: Actor) {
val deliverUndoJobParam = DeliverUndoJobParam(
Undo(
actor = user.url,
id = "${applicationConfig.url}/undo/block/${user.id}/${target.url}",
actor = actor.url,
id = "${applicationConfig.url}/undo/block/${actor.id}/${target.url}",
apObject = Block(
apObject = user.url,
apObject = actor.url,
actor = target.url,
id = "${applicationConfig.url}/block/${user.id}/${target.id}"
id = "${applicationConfig.url}/block/${actor.id}/${target.id}"
),
published = Instant.now().toString()
),
target.inbox,
user.id
actor.id
)
jobQueueParentService.scheduleTypeSafe(deliverUndoJob, deliverUndoJobParam)

View File

@ -10,7 +10,7 @@ import dev.usbharu.hideout.activitypub.service.common.ActivityPubProcessContext
import dev.usbharu.hideout.activitypub.service.common.ActivityType
import dev.usbharu.hideout.activitypub.service.objects.user.APUserService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import org.springframework.stereotype.Service
@ -18,7 +18,7 @@ import org.springframework.stereotype.Service
class APUndoProcessor(
transaction: Transaction,
private val apUserService: APUserService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val relationshipService: RelationshipService
) :
AbstractActivityPubProcessor<Undo>(transaction) {
@ -35,8 +35,8 @@ class APUndoProcessor(
val follow = undo.apObject as Follow
apUserService.fetchPerson(undo.actor, follow.apObject)
val follower = userQueryService.findByUrl(undo.actor)
val target = userQueryService.findByUrl(follow.apObject)
val follower = actorQueryService.findByUrl(undo.actor)
val target = actorQueryService.findByUrl(follow.apObject)
relationshipService.unfollow(follower.id, target.id)
return
@ -46,7 +46,7 @@ class APUndoProcessor(
val block = undo.apObject as Block
val blocker = apUserService.fetchPersonWithEntity(undo.actor, block.apObject).second
val target = userQueryService.findByUrl(block.apObject)
val target = actorQueryService.findByUrl(block.apObject)
relationshipService.unblock(blocker.id, target.id)
return
@ -65,7 +65,7 @@ class APUndoProcessor(
}
val accepter = apUserService.fetchPersonWithEntity(undo.actor, acceptObject).second
val target = userQueryService.findByUrl(acceptObject)
val target = actorQueryService.findByUrl(acceptObject)
relationshipService.rejectFollowRequest(accepter.id, target.id)
}

View File

@ -1,25 +1,25 @@
package dev.usbharu.hideout.activitypub.service.common
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
interface APRequestService {
suspend fun <R : Object> apGet(url: String, signer: User? = null, responseClass: Class<R>): R
suspend fun <R : Object> apGet(url: String, signer: Actor? = null, responseClass: Class<R>): R
suspend fun <T : Object, R : Object> apPost(
url: String,
body: T? = null,
signer: User? = null,
signer: Actor? = null,
responseClass: Class<R>
): R
suspend fun <T : Object> apPost(url: String, body: T? = null, signer: User? = null): String
suspend fun <T : Object> apPost(url: String, body: T? = null, signer: Actor? = null): String
}
suspend inline fun <reified R : Object> APRequestService.apGet(url: String, signer: User? = null): R =
suspend inline fun <reified R : Object> APRequestService.apGet(url: String, signer: Actor? = null): R =
apGet(url, signer, R::class.java)
suspend inline fun <T : Object, reified R : Object> APRequestService.apPost(
url: String,
body: T? = null,
signer: User? = null
signer: Actor? = null
): R = apPost(url, body, signer, R::class.java)

View File

@ -2,7 +2,7 @@ package dev.usbharu.hideout.activitypub.service.common
import com.fasterxml.jackson.databind.ObjectMapper
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.util.Base64Util
import dev.usbharu.hideout.util.HttpUtil.Activity
import dev.usbharu.hideout.util.RsaUtil
@ -33,7 +33,7 @@ class APRequestServiceImpl(
@Qualifier("http") private val dateTimeFormatter: DateTimeFormatter,
) : APRequestService {
override suspend fun <R : Object> apGet(url: String, signer: User?, responseClass: Class<R>): R {
override suspend fun <R : Object> apGet(url: String, signer: Actor?, responseClass: Class<R>): R {
logger.debug("START ActivityPub Request GET url: {}, signer: {}", url, signer?.url)
val date = dateTimeFormatter.format(ZonedDateTime.now(ZoneId.of("GMT")))
val u = URL(url)
@ -57,7 +57,7 @@ class APRequestServiceImpl(
private suspend fun apGetSign(
date: String,
u: URL,
signer: User,
signer: Actor,
url: String
): HttpResponse {
val headers = headers {
@ -100,14 +100,14 @@ class APRequestServiceImpl(
override suspend fun <T : Object, R : Object> apPost(
url: String,
body: T?,
signer: User?,
signer: Actor?,
responseClass: Class<R>
): R {
val bodyAsText = apPost(url, body, signer)
return objectMapper.readValue(bodyAsText, responseClass)
}
override suspend fun <T : Object> apPost(url: String, body: T?, signer: User?): String {
override suspend fun <T : Object> apPost(url: String, body: T?, signer: Actor?): String {
logger.debug("START ActivityPub Request POST url: {}, signer: {}", url, signer?.url)
val requestBody = addContextIfNotNull(body)
@ -166,7 +166,7 @@ class APRequestServiceImpl(
date: String,
u: URL,
digest: String,
signer: User,
signer: Actor,
requestBody: String?
): HttpResponse {
val headers = headers {

View File

@ -1,14 +1,14 @@
package dev.usbharu.hideout.activitypub.service.common
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
interface APResourceResolveService {
suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singer: User?): T
suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singer: Actor?): T
suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singerId: Long?): T
}
suspend inline fun <reified T : Object> APResourceResolveService.resolve(url: String, singer: User?): T =
suspend inline fun <reified T : Object> APResourceResolveService.resolve(url: String, singer: Actor?): T =
resolve(url, T::class.java, singer)
suspend inline fun <reified T : Object> APResourceResolveService.resolve(url: String, singerId: Long?): T =

View File

@ -1,8 +1,8 @@
package dev.usbharu.hideout.activitypub.service.common
import dev.usbharu.hideout.activitypub.domain.model.objects.Object
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.user.UserRepository
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.service.resource.CacheManager
import dev.usbharu.hideout.core.service.resource.ResolveResponse
import org.springframework.stereotype.Service
@ -11,7 +11,7 @@ import java.io.InputStream
@Service
class APResourceResolveServiceImpl(
private val apRequestService: APRequestService,
private val userRepository: UserRepository,
private val actorRepository: ActorRepository,
private val cacheManager: CacheManager
) :
APResourceResolveService {
@ -19,19 +19,19 @@ class APResourceResolveServiceImpl(
override suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singerId: Long?): T =
internalResolve(url, singerId, clazz)
override suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singer: User?): T =
override suspend fun <T : Object> resolve(url: String, clazz: Class<T>, singer: Actor?): T =
internalResolve(url, singer, clazz)
private suspend fun <T : Object> internalResolve(url: String, singerId: Long?, clazz: Class<T>): T {
val key = genCacheKey(url, singerId)
cacheManager.putCache(key) {
runResolve(url, singerId?.let { userRepository.findById(it) }, clazz)
runResolve(url, singerId?.let { actorRepository.findById(it) }, clazz)
}
return (cacheManager.getOrWait(key) as APResolveResponse<T>).objects
}
private suspend fun <T : Object> internalResolve(url: String, singer: User?, clazz: Class<T>): T {
private suspend fun <T : Object> internalResolve(url: String, singer: Actor?, clazz: Class<T>): T {
val key = genCacheKey(url, singer?.id)
cacheManager.putCache(key) {
runResolve(url, singer, clazz)
@ -39,7 +39,7 @@ class APResourceResolveServiceImpl(
return (cacheManager.getOrWait(key) as APResolveResponse<T>).objects
}
private suspend fun <T : Object> runResolve(url: String, singer: User?, clazz: Class<T>): ResolveResponse =
private suspend fun <T : Object> runResolve(url: String, singer: Actor?, clazz: Class<T>): ResolveResponse =
APResolveResponse(apRequestService.apGet(url, singer, clazz))
private fun genCacheKey(url: String, singerId: Long?): String {

View File

@ -10,7 +10,7 @@ import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.external.job.InboxJob
import dev.usbharu.hideout.core.external.job.InboxJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import dev.usbharu.hideout.util.RsaUtil
import dev.usbharu.httpsignature.common.HttpHeaders
@ -29,7 +29,7 @@ class InboxJobProcessor(
private val objectMapper: ObjectMapper,
private val signatureHeaderParser: SignatureHeaderParser,
private val signatureVerifier: HttpSignatureVerifier,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val apUserService: APUserService,
private val transaction: Transaction
) : JobProcessor<InboxJobParam, InboxJob> {
@ -50,7 +50,7 @@ class InboxJobProcessor(
val user = transaction.transaction {
try {
userQueryService.findByKeyId(signature.keyId)
actorQueryService.findByKeyId(signature.keyId)
} catch (_: FailedToGetResourcesException) {
apUserService.fetchPersonWithEntity(signature.keyId).second
}

View File

@ -140,7 +140,7 @@ class APNoteServiceImpl(
postService.createRemote(
postBuilder.of(
id = postRepository.generateId(),
userId = person.second.id,
actorId = person.second.id,
text = note.content,
createdAt = Instant.parse(note.published).toEpochMilli(),
visibility = visibility,

View File

@ -7,7 +7,7 @@ import dev.usbharu.hideout.activitypub.service.common.APRequestService
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.external.job.DeliverPostJob
import dev.usbharu.hideout.core.external.job.DeliverPostJobParam
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.job.JobProcessor
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@ -16,13 +16,13 @@ import org.springframework.stereotype.Service
class ApNoteJobProcessor(
private val transaction: Transaction,
private val objectMapper: ObjectMapper,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val apRequestService: APRequestService
) : JobProcessor<DeliverPostJobParam, DeliverPostJob> {
override suspend fun process(param: DeliverPostJobParam) {
val create = objectMapper.readValue<Create>(param.create)
transaction.transaction {
val signer = userQueryService.findByUrl(param.actor)
val signer = actorQueryService.findByUrl(param.actor)
logger.debug("CreateNoteJob: actor: {} create: {} inbox: {}", param.actor, create, param.inbox)

View File

@ -44,7 +44,7 @@ class NoteApApiServiceImpl(
return null
}
if (followerQueryService.alreadyFollow(findById.second.userId, userId)) {
if (followerQueryService.alreadyFollow(findById.second.actorId, userId)) {
return findById.first
}
return null

View File

@ -9,8 +9,8 @@ import dev.usbharu.hideout.activitypub.service.common.resolve
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.user.RemoteUserCreateDto
import dev.usbharu.hideout.core.service.user.UserService
import org.springframework.stereotype.Service
@ -28,13 +28,13 @@ interface APUserService {
*/
suspend fun fetchPerson(url: String, targetActor: String? = null): Person
suspend fun fetchPersonWithEntity(url: String, targetActor: String? = null): Pair<Person, User>
suspend fun fetchPersonWithEntity(url: String, targetActor: String? = null): Pair<Person, Actor>
}
@Service
class APUserServiceImpl(
private val userService: UserService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val transaction: Transaction,
private val applicationConfig: ApplicationConfig,
private val apResourceResolveService: APResourceResolveService
@ -43,7 +43,7 @@ class APUserServiceImpl(
override suspend fun getPersonByName(name: String): Person {
val userEntity = transaction.transaction {
userQueryService.findByNameAndDomain(name, applicationConfig.url.host)
actorQueryService.findByNameAndDomain(name, applicationConfig.url.host)
}
// TODO: JOINで書き直し
val userUrl = "${applicationConfig.url}/users/$name"
@ -68,7 +68,8 @@ class APUserServiceImpl(
),
endpoints = mapOf("sharedInbox" to "${applicationConfig.url}/inbox"),
followers = userEntity.followers,
following = userEntity.following
following = userEntity.following,
manuallyApprovesFollowers = userEntity.locked
)
}
@ -76,9 +77,9 @@ class APUserServiceImpl(
fetchPersonWithEntity(url, targetActor).first
@Transactional
override suspend fun fetchPersonWithEntity(url: String, targetActor: String?): Pair<Person, User> {
override suspend fun fetchPersonWithEntity(url: String, targetActor: String?): Pair<Person, Actor> {
return try {
val userEntity = userQueryService.findByUrl(url)
val userEntity = actorQueryService.findByUrl(url)
val id = userEntity.url
return entityToPerson(userEntity, id) to userEntity
} catch (ignore: FailedToGetResourcesException) {
@ -86,7 +87,7 @@ class APUserServiceImpl(
val id = person.id
try {
val userEntity = userQueryService.findByUrl(id)
val userEntity = actorQueryService.findByUrl(id)
return entityToPerson(userEntity, id) to userEntity
} catch (_: FailedToGetResourcesException) {
}
@ -104,21 +105,22 @@ class APUserServiceImpl(
keyId = person.publicKey.id,
following = person.following,
followers = person.followers,
sharedInbox = person.endpoints["sharedInbox"]
sharedInbox = person.endpoints["sharedInbox"],
locked = person.manuallyApprovesFollowers
)
)
}
}
private fun entityToPerson(
userEntity: User,
actorEntity: Actor,
id: String
) = Person(
type = emptyList(),
name = userEntity.name,
name = actorEntity.name,
id = id,
preferredUsername = userEntity.name,
summary = userEntity.description,
preferredUsername = actorEntity.name,
summary = actorEntity.description,
inbox = "$id/inbox",
outbox = "$id/outbox",
url = id,
@ -128,12 +130,13 @@ class APUserServiceImpl(
url = "$id/icon.jpg"
),
publicKey = Key(
id = userEntity.keyId,
id = actorEntity.keyId,
owner = id,
publicKeyPem = userEntity.publicKey
publicKeyPem = actorEntity.publicKey
),
endpoints = mapOf("sharedInbox" to "${applicationConfig.url}/inbox"),
followers = userEntity.followers,
following = userEntity.following
followers = actorEntity.followers,
following = actorEntity.following,
manuallyApprovesFollowers = actorEntity.locked
)
}

View File

@ -1,21 +1,21 @@
package dev.usbharu.hideout.activitypub.service.webfinger
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.query.ActorQueryService
import org.springframework.stereotype.Service
@Service
interface WebFingerApiService {
suspend fun findByNameAndDomain(name: String, domain: String): User
suspend fun findByNameAndDomain(name: String, domain: String): Actor
}
@Service
class WebFingerApiServiceImpl(private val transaction: Transaction, private val userQueryService: UserQueryService) :
class WebFingerApiServiceImpl(private val transaction: Transaction, private val actorQueryService: ActorQueryService) :
WebFingerApiService {
override suspend fun findByNameAndDomain(name: String, domain: String): User {
override suspend fun findByNameAndDomain(name: String, domain: String): Actor {
return transaction.transaction {
userQueryService.findByNameAndDomain(name, domain)
actorQueryService.findByNameAndDomain(name, domain)
}
}
}

View File

@ -12,7 +12,7 @@ import dev.usbharu.hideout.core.infrastructure.springframework.httpsignature.Htt
import dev.usbharu.hideout.core.infrastructure.springframework.httpsignature.HttpSignatureVerifierComposite
import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.UserDetailsImpl
import dev.usbharu.hideout.core.infrastructure.springframework.oauth2.UserDetailsServiceImpl
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.util.RsaUtil
import dev.usbharu.hideout.util.hasAnyScope
import dev.usbharu.httpsignature.sign.RsaSha256HttpSignatureSigner
@ -69,7 +69,7 @@ import java.util.*
class SecurityConfig {
@Autowired
private lateinit var userQueryService: UserQueryService
private lateinit var actorQueryService: ActorQueryService
@Bean
fun authenticationManager(authenticationConfiguration: AuthenticationConfiguration): AuthenticationManager? =
@ -135,7 +135,7 @@ class SecurityConfig {
val signatureHeaderParser = DefaultSignatureHeaderParser()
provider.setPreAuthenticatedUserDetailsService(
HttpSignatureUserDetailsService(
userQueryService,
actorQueryService,
HttpSignatureVerifierComposite(
mapOf(
"rsa-sha256" to RsaSha256HttpSignatureVerifier(

View File

@ -1,3 +1,3 @@
package dev.usbharu.hideout.core.domain.model.user
package dev.usbharu.hideout.core.domain.model.actor
data class Acct(val username: String, val domain: String? = null, val isRemote: Boolean = domain == null)

View File

@ -1,4 +1,4 @@
package dev.usbharu.hideout.core.domain.model.user
package dev.usbharu.hideout.core.domain.model.actor
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.config.CharacterLimit
@ -6,13 +6,12 @@ import org.slf4j.LoggerFactory
import org.springframework.stereotype.Component
import java.time.Instant
data class User private constructor(
data class Actor private constructor(
val id: Long,
val name: String,
val domain: String,
val screenName: String,
val description: String,
val password: String? = null,
val inbox: String,
val outbox: String,
val url: String,
@ -22,13 +21,9 @@ data class User private constructor(
val keyId: String,
val followers: String? = null,
val following: String? = null,
val instance: Long? = null
val instance: Long? = null,
val locked: Boolean
) {
override fun toString(): String =
"User(id=$id, name='$name', domain='$domain', screenName='$screenName', description='$description'," +
" password=$password, inbox='$inbox', outbox='$outbox', url='$url', publicKey='$publicKey', " +
"privateKey=$privateKey, createdAt=$createdAt, keyId='$keyId', followers=$followers," +
" following=$following, instance=$instance)"
@Component
class UserBuilder(private val characterLimit: CharacterLimit, private val applicationConfig: ApplicationConfig) {
@ -42,7 +37,6 @@ data class User private constructor(
domain: String,
screenName: String,
description: String,
password: String? = null,
inbox: String,
outbox: String,
url: String,
@ -52,8 +46,9 @@ data class User private constructor(
keyId: String,
following: String? = null,
followers: String? = null,
instance: Long? = null
): User {
instance: Long? = null,
locked: Boolean
): Actor {
// idは0未満ではいけない
require(id >= 0) { "id must be greater than or equal to 0." }
@ -97,7 +92,6 @@ data class User private constructor(
// ローカルユーザーはpasswordとprivateKeyをnullにしてはいけない
if (domain == applicationConfig.url.host) {
requireNotNull(password) { "password and privateKey must not be null for local users." }
requireNotNull(privateKey) { "password and privateKey must not be null for local users." }
}
@ -129,13 +123,12 @@ data class User private constructor(
"keyId must contain non-blank characters."
}
return User(
return Actor(
id = id,
name = limitedName,
domain = domain,
screenName = limitedScreenName,
description = limitedDescription,
password = password,
inbox = inbox,
outbox = outbox,
url = url,
@ -145,8 +138,30 @@ data class User private constructor(
keyId = keyId,
followers = followers,
following = following,
instance = instance
instance = instance,
locked
)
}
}
override fun toString(): String {
return "Actor(" +
"id=$id, " +
"name='$name', " +
"domain='$domain', " +
"screenName='$screenName', " +
"description='$description', " +
"inbox='$inbox', " +
"outbox='$outbox', " +
"url='$url', " +
"publicKey='$publicKey', " +
"privateKey=$privateKey, " +
"createdAt=$createdAt, " +
"keyId='$keyId', " +
"followers=$followers, " +
"following=$following, " +
"instance=$instance, " +
"locked=$locked" +
")"
}
}

View File

@ -0,0 +1,14 @@
package dev.usbharu.hideout.core.domain.model.actor
import org.springframework.stereotype.Repository
@Repository
interface ActorRepository {
suspend fun save(actor: Actor): Actor
suspend fun findById(id: Long): Actor?
suspend fun delete(id: Long)
suspend fun nextId(): Long
}

View File

@ -5,7 +5,7 @@ import org.springframework.stereotype.Component
data class Post private constructor(
val id: Long,
val userId: Long,
val actorId: Long,
val overview: String? = null,
val text: String,
val createdAt: Long,
@ -23,7 +23,7 @@ data class Post private constructor(
@Suppress("FunctionMinLength", "LongParameterList")
fun of(
id: Long,
userId: Long,
actorId: Long,
overview: String? = null,
text: String,
createdAt: Long,
@ -37,7 +37,7 @@ data class Post private constructor(
): Post {
require(id >= 0) { "id must be greater than or equal to 0." }
require(userId >= 0) { "userId must be greater than or equal to 0." }
require(actorId >= 0) { "actorId must be greater than or equal to 0." }
val limitedOverview = if ((overview?.length ?: 0) >= characterLimit.post.overview) {
overview?.substring(0, characterLimit.post.overview)
@ -61,7 +61,7 @@ data class Post private constructor(
return Post(
id = id,
userId = userId,
actorId = actorId,
overview = limitedOverview,
text = limitedText,
createdAt = createdAt,

View File

@ -1,3 +1,3 @@
package dev.usbharu.hideout.core.domain.model.reaction
data class Reaction(val id: Long, val emojiId: Long, val postId: Long, val userId: Long)
data class Reaction(val id: Long, val emojiId: Long, val postId: Long, val actorId: Long)

View File

@ -3,20 +3,20 @@ package dev.usbharu.hideout.core.domain.model.relationship
/**
* ユーザーとの関係を表します
*
* @property userId ユーザー
* @property targetUserId 相手ユーザー
* @property actorId ユーザー
* @property targetActorId 相手ユーザー
* @property following フォローしているか
* @property blocking ブロックしているか
* @property muting ミュートしているか
* @property followRequest フォローリクエストを送っているか
* @property ignoreFollowRequestFromTarget フォローリクエストを無視しているか
* @property ignoreFollowRequestToTarget フォローリクエストを無視しているか
*/
data class Relationship(
val userId: Long,
val targetUserId: Long,
val actorId: Long,
val targetActorId: Long,
val following: Boolean,
val blocking: Boolean,
val muting: Boolean,
val followRequest: Boolean,
val ignoreFollowRequestFromTarget: Boolean
val ignoreFollowRequestToTarget: Boolean
)

View File

@ -23,9 +23,9 @@ interface RelationshipRepository {
/**
* userIdとtargetUserIdで[Relationship]を取得します
*
* @param userId 取得するユーザーID
* @param targetUserId 対象ユーザーID
* @param actorId 取得するユーザーID
* @param targetActorId 対象ユーザーID
* @return 取得された[Relationship] 存在しない場合nullが返ります
*/
suspend fun findByUserIdAndTargetUserId(userId: Long, targetUserId: Long): Relationship?
suspend fun findByUserIdAndTargetUserId(actorId: Long, targetActorId: Long): Relationship?
}

View File

@ -1,6 +1,6 @@
package dev.usbharu.hideout.core.domain.model.relationship
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Users
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Actors
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
@ -12,32 +12,32 @@ class RelationshipRepositoryImpl : RelationshipRepository {
val singleOrNull =
Relationships
.select {
(Relationships.userId eq relationship.userId)
.and(Relationships.targetUserId eq relationship.targetUserId)
(Relationships.actorId eq relationship.actorId)
.and(Relationships.targetActorId eq relationship.targetActorId)
}
.singleOrNull()
if (singleOrNull == null) {
Relationships.insert {
it[userId] = relationship.userId
it[targetUserId] = relationship.targetUserId
it[actorId] = relationship.actorId
it[targetActorId] = relationship.targetActorId
it[following] = relationship.following
it[blocking] = relationship.blocking
it[muting] = relationship.muting
it[followRequest] = relationship.followRequest
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestFromTarget
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestToTarget
}
} else {
Relationships
.update({
(Relationships.userId eq relationship.userId)
.and(Relationships.targetUserId eq relationship.targetUserId)
(Relationships.actorId eq relationship.actorId)
.and(Relationships.targetActorId eq relationship.targetActorId)
}) {
it[following] = relationship.following
it[blocking] = relationship.blocking
it[muting] = relationship.muting
it[followRequest] = relationship.followRequest
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestFromTarget
it[ignoreFollowRequestFromTarget] = relationship.ignoreFollowRequestToTarget
}
}
return relationship
@ -45,33 +45,33 @@ class RelationshipRepositoryImpl : RelationshipRepository {
override suspend fun delete(relationship: Relationship) {
Relationships.deleteWhere {
(Relationships.userId eq relationship.userId)
.and(Relationships.targetUserId eq relationship.targetUserId)
(Relationships.actorId eq relationship.actorId)
.and(Relationships.targetActorId eq relationship.targetActorId)
}
}
override suspend fun findByUserIdAndTargetUserId(userId: Long, targetUserId: Long): Relationship? {
override suspend fun findByUserIdAndTargetUserId(actorId: Long, targetActorId: Long): Relationship? {
return Relationships.select {
(Relationships.userId eq userId)
.and(Relationships.targetUserId eq targetUserId)
(Relationships.actorId eq actorId)
.and(Relationships.targetActorId eq targetActorId)
}.singleOrNull()
?.toRelationships()
}
}
fun ResultRow.toRelationships(): Relationship = Relationship(
userId = this[Relationships.userId],
targetUserId = this[Relationships.targetUserId],
actorId = this[Relationships.actorId],
targetActorId = this[Relationships.targetActorId],
following = this[Relationships.following],
blocking = this[Relationships.blocking],
muting = this[Relationships.muting],
followRequest = this[Relationships.followRequest],
ignoreFollowRequestFromTarget = this[Relationships.ignoreFollowRequestFromTarget]
ignoreFollowRequestToTarget = this[Relationships.ignoreFollowRequestFromTarget]
)
object Relationships : LongIdTable("relationships") {
val userId = long("user_id").references(Users.id)
val targetUserId = long("target_user_id").references(Users.id)
val actorId = long("actor_id").references(Actors.id)
val targetActorId = long("target_actor_id").references(Actors.id)
val following = bool("following")
val blocking = bool("blocking")
val muting = bool("muting")
@ -79,6 +79,6 @@ object Relationships : LongIdTable("relationships") {
val ignoreFollowRequestFromTarget = bool("ignore_follow_request")
init {
uniqueIndex(userId, targetUserId)
uniqueIndex(actorId, targetActorId)
}
}

View File

@ -13,7 +13,7 @@ data class Timeline(
val userId: Long,
val timelineId: Long,
val postId: Long,
val postUserId: Long,
val postActorId: Long,
val createdAt: Long,
val replyId: Long?,
val repostId: Long?,

View File

@ -1,14 +0,0 @@
package dev.usbharu.hideout.core.domain.model.user
import org.springframework.stereotype.Repository
@Repository
interface UserRepository {
suspend fun save(user: User): User
suspend fun findById(id: Long): User?
suspend fun delete(id: Long)
suspend fun nextId(): Long
}

View File

@ -0,0 +1,7 @@
package dev.usbharu.hideout.core.domain.model.userdetails
data class UserDetail(
val actorId: Long,
val password: String,
val autoAcceptFolloweeFollowRequest: Boolean
)

View File

@ -0,0 +1,7 @@
package dev.usbharu.hideout.core.domain.model.userdetails
interface UserDetailRepository {
suspend fun save(userDetail: UserDetail): UserDetail
suspend fun delete(userDetail: UserDetail)
suspend fun findByActorId(actorId: Long): UserDetail?
}

View File

@ -12,7 +12,7 @@ class PostResultRowMapper(private val postBuilder: Post.PostBuilder) : ResultRow
override fun map(resultRow: ResultRow): Post {
return postBuilder.of(
id = resultRow[Posts.id],
userId = resultRow[Posts.userId],
actorId = resultRow[Posts.actorId],
overview = resultRow[Posts.overview],
text = resultRow[Posts.text],
createdAt = resultRow[Posts.createdAt],

View File

@ -2,11 +2,11 @@ package dev.usbharu.hideout.core.infrastructure.exposed
import dev.usbharu.hideout.application.infrastructure.exposed.QueryMapper
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import org.jetbrains.exposed.sql.Query
import org.springframework.stereotype.Component
@Component
class UserQueryMapper(private val userResultRowMapper: ResultRowMapper<User>) : QueryMapper<User> {
override fun map(query: Query): List<User> = query.map(userResultRowMapper::map)
class UserQueryMapper(private val actorResultRowMapper: ResultRowMapper<Actor>) : QueryMapper<Actor> {
override fun map(query: Query): List<Actor> = query.map(actorResultRowMapper::map)
}

View File

@ -1,32 +1,32 @@
package dev.usbharu.hideout.core.infrastructure.exposed
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Users
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Actors
import org.jetbrains.exposed.sql.ResultRow
import org.springframework.stereotype.Component
import java.time.Instant
@Component
class UserResultRowMapper(private val userBuilder: User.UserBuilder) : ResultRowMapper<User> {
override fun map(resultRow: ResultRow): User {
return userBuilder.of(
id = resultRow[Users.id],
name = resultRow[Users.name],
domain = resultRow[Users.domain],
screenName = resultRow[Users.screenName],
description = resultRow[Users.description],
password = resultRow[Users.password],
inbox = resultRow[Users.inbox],
outbox = resultRow[Users.outbox],
url = resultRow[Users.url],
publicKey = resultRow[Users.publicKey],
privateKey = resultRow[Users.privateKey],
createdAt = Instant.ofEpochMilli((resultRow[Users.createdAt])),
keyId = resultRow[Users.keyId],
followers = resultRow[Users.followers],
following = resultRow[Users.following],
instance = resultRow[Users.instance]
class UserResultRowMapper(private val actorBuilder: Actor.UserBuilder) : ResultRowMapper<Actor> {
override fun map(resultRow: ResultRow): Actor {
return actorBuilder.of(
id = resultRow[Actors.id],
name = resultRow[Actors.name],
domain = resultRow[Actors.domain],
screenName = resultRow[Actors.screenName],
description = resultRow[Actors.description],
inbox = resultRow[Actors.inbox],
outbox = resultRow[Actors.outbox],
url = resultRow[Actors.url],
publicKey = resultRow[Actors.publicKey],
privateKey = resultRow[Actors.privateKey],
createdAt = Instant.ofEpochMilli((resultRow[Actors.createdAt])),
keyId = resultRow[Actors.keyId],
followers = resultRow[Actors.followers],
following = resultRow[Actors.following],
instance = resultRow[Actors.instance],
locked = resultRow[Actors.locked]
)
}
}

View File

@ -0,0 +1,60 @@
package dev.usbharu.hideout.core.infrastructure.exposedquery
import dev.usbharu.hideout.application.infrastructure.exposed.QueryMapper
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Actors
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class ActorQueryServiceImpl(
private val actorResultRowMapper: ResultRowMapper<Actor>,
private val actorQueryMapper: QueryMapper<Actor>
) : ActorQueryService {
private val logger = LoggerFactory.getLogger(ActorQueryServiceImpl::class.java)
override suspend fun findAll(limit: Int, offset: Long): List<Actor> =
Actors.selectAll().limit(limit, offset).let(actorQueryMapper::map)
override suspend fun findById(id: Long): Actor = Actors.select { Actors.id eq id }
.singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }
.let(actorResultRowMapper::map)
override suspend fun findByName(name: String): List<Actor> =
Actors.select { Actors.name eq name }.let(actorQueryMapper::map)
override suspend fun findByNameAndDomain(name: String, domain: String): Actor =
Actors
.select { Actors.name eq name and (Actors.domain eq domain) }
.singleOr {
FailedToGetResourcesException("name: $name,domain: $domain is duplicate or does not exist.", it)
}
.let(actorResultRowMapper::map)
override suspend fun findByUrl(url: String): Actor {
logger.trace("findByUrl url: $url")
return Actors.select { Actors.url eq url }
.singleOr { FailedToGetResourcesException("url: $url is duplicate or does not exist.", it) }
.let(actorResultRowMapper::map)
}
override suspend fun findByIds(ids: List<Long>): List<Actor> =
Actors.select { Actors.id inList ids }.let(actorQueryMapper::map)
override suspend fun existByNameAndDomain(name: String, domain: String): Boolean =
Actors.select { Actors.name eq name and (Actors.domain eq domain) }.empty().not()
override suspend fun findByKeyId(keyId: String): Actor {
return Actors.select { Actors.keyId eq keyId }
.singleOr { FailedToGetResourcesException("keyId: $keyId is duplicate or does not exist.", it) }
.let(actorResultRowMapper::map)
}
}

View File

@ -1,24 +1,24 @@
package dev.usbharu.hideout.core.infrastructure.exposedquery
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.query.FollowerQueryService
import dev.usbharu.hideout.core.query.RelationshipQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import org.springframework.stereotype.Repository
@Repository
class FollowerQueryServiceImpl(
private val relationshipQueryService: RelationshipQueryService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val relationshipRepository: RelationshipRepository
) : FollowerQueryService {
override suspend fun findFollowersById(id: Long): List<User> {
return userQueryService.findByIds(
relationshipQueryService.findByTargetIdAndFollowing(id, true).map { it.userId }
override suspend fun findFollowersById(id: Long): List<Actor> {
return actorQueryService.findByIds(
relationshipQueryService.findByTargetIdAndFollowing(id, true).map { it.actorId }
)
}
override suspend fun alreadyFollow(userId: Long, followerId: Long): Boolean =
relationshipRepository.findByUserIdAndTargetUserId(followerId, userId)?.following ?: false
override suspend fun alreadyFollow(actorId: Long, followerId: Long): Boolean =
relationshipRepository.findByUserIdAndTargetUserId(followerId, actorId)?.following ?: false
}

View File

@ -6,44 +6,46 @@ import dev.usbharu.hideout.core.infrastructure.exposedrepository.Reactions
import dev.usbharu.hideout.core.infrastructure.exposedrepository.toReaction
import dev.usbharu.hideout.core.query.ReactionQueryService
import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.select
import org.springframework.stereotype.Repository
@Repository
class ReactionQueryServiceImpl : ReactionQueryService {
override suspend fun findByPostId(postId: Long, userId: Long?): List<Reaction> {
override suspend fun findByPostId(postId: Long, actorId: Long?): List<Reaction> {
return Reactions.select {
Reactions.postId.eq(postId)
}.map { it.toReaction() }
}
@Suppress("FunctionMaxLength")
override suspend fun findByPostIdAndUserIdAndEmojiId(postId: Long, userId: Long, emojiId: Long): Reaction {
override suspend fun findByPostIdAndActorIdAndEmojiId(postId: Long, actorId: Long, emojiId: Long): Reaction {
return Reactions
.select {
Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and(
Reactions.postId.eq(postId).and(Reactions.actorId.eq(actorId)).and(
Reactions.emojiId.eq(emojiId)
)
}
.singleOr {
FailedToGetResourcesException(
"postId: $postId,userId: $userId,emojiId: $emojiId is duplicate or does not exist.",
"postId: $postId,userId: $actorId,emojiId: $emojiId is duplicate or does not exist.",
it
)
}
.toReaction()
}
override suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean {
override suspend fun reactionAlreadyExist(postId: Long, actorId: Long, emojiId: Long): Boolean {
return Reactions.select {
Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)).and(
Reactions.postId.eq(postId).and(Reactions.actorId.eq(actorId)).and(
Reactions.emojiId.eq(emojiId)
)
}.empty().not()
}
override suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long) {
Reactions.deleteWhere { Reactions.postId.eq(postId).and(Reactions.userId.eq(userId)) }
override suspend fun deleteByPostIdAndActorId(postId: Long, actorId: Long) {
Reactions.deleteWhere { Reactions.postId.eq(postId).and(Reactions.actorId.eq(actorId)) }
}
}

View File

@ -5,12 +5,39 @@ import dev.usbharu.hideout.core.domain.model.relationship.Relationships
import dev.usbharu.hideout.core.domain.model.relationship.toRelationships
import dev.usbharu.hideout.core.query.RelationshipQueryService
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.andWhere
import org.jetbrains.exposed.sql.select
import org.springframework.stereotype.Service
@Service
class RelationshipQueryServiceImpl : RelationshipQueryService {
override suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List<Relationship> =
Relationships.select { Relationships.targetUserId eq targetId and (Relationships.following eq following) }
Relationships.select { Relationships.targetActorId eq targetId and (Relationships.following eq following) }
.map { it.toRelationships() }
override suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId: Long?,
sinceId: Long?,
limit: Int,
targetId: Long,
followRequest: Boolean,
ignoreFollowRequest: Boolean
): List<Relationship> {
val query = Relationships
.select {
Relationships.targetActorId.eq(targetId)
.and(Relationships.followRequest.eq(followRequest))
.and(Relationships.ignoreFollowRequestFromTarget.eq(ignoreFollowRequest))
}.limit(limit)
if (maxId != null) {
query.andWhere { Relationships.id greater maxId }
}
if (sinceId != null) {
query.andWhere { Relationships.id less sinceId }
}
return query.map { it.toRelationships() }
}
}

View File

@ -1,60 +0,0 @@
package dev.usbharu.hideout.core.infrastructure.exposedquery
import dev.usbharu.hideout.application.infrastructure.exposed.QueryMapper
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Users
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.and
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.selectAll
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Repository
@Repository
class UserQueryServiceImpl(
private val userResultRowMapper: ResultRowMapper<User>,
private val userQueryMapper: QueryMapper<User>
) : UserQueryService {
private val logger = LoggerFactory.getLogger(UserQueryServiceImpl::class.java)
override suspend fun findAll(limit: Int, offset: Long): List<User> =
Users.selectAll().limit(limit, offset).let(userQueryMapper::map)
override suspend fun findById(id: Long): User = Users.select { Users.id eq id }
.singleOr { FailedToGetResourcesException("id: $id is duplicate or does not exist.", it) }
.let(userResultRowMapper::map)
override suspend fun findByName(name: String): List<User> =
Users.select { Users.name eq name }.let(userQueryMapper::map)
override suspend fun findByNameAndDomain(name: String, domain: String): User =
Users
.select { Users.name eq name and (Users.domain eq domain) }
.singleOr {
FailedToGetResourcesException("name: $name,domain: $domain is duplicate or does not exist.", it)
}
.let(userResultRowMapper::map)
override suspend fun findByUrl(url: String): User {
logger.trace("findByUrl url: $url")
return Users.select { Users.url eq url }
.singleOr { FailedToGetResourcesException("url: $url is duplicate or does not exist.", it) }
.let(userResultRowMapper::map)
}
override suspend fun findByIds(ids: List<Long>): List<User> =
Users.select { Users.id inList ids }.let(userQueryMapper::map)
override suspend fun existByNameAndDomain(name: String, domain: String): Boolean =
Users.select { Users.name eq name and (Users.domain eq domain) }.empty().not()
override suspend fun findByKeyId(keyId: String): User {
return Users.select { Users.keyId eq keyId }
.singleOr { FailedToGetResourcesException("keyId: $keyId is duplicate or does not exist.", it) }
.let(userResultRowMapper::map)
}
}

View File

@ -0,0 +1,100 @@
package dev.usbharu.hideout.core.infrastructure.exposedrepository
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.application.service.id.IdGenerateService
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.springframework.stereotype.Repository
@Repository
class ActorRepositoryImpl(
private val idGenerateService: IdGenerateService,
private val actorResultRowMapper: ResultRowMapper<Actor>
) :
ActorRepository {
override suspend fun save(actor: Actor): Actor {
val singleOrNull = Actors.select { Actors.id eq actor.id }.empty()
if (singleOrNull) {
Actors.insert {
it[id] = actor.id
it[name] = actor.name
it[domain] = actor.domain
it[screenName] = actor.screenName
it[description] = actor.description
it[inbox] = actor.inbox
it[outbox] = actor.outbox
it[url] = actor.url
it[createdAt] = actor.createdAt.toEpochMilli()
it[publicKey] = actor.publicKey
it[privateKey] = actor.privateKey
it[keyId] = actor.keyId
it[following] = actor.following
it[followers] = actor.followers
it[instance] = actor.instance
it[locked] = actor.locked
}
} else {
Actors.update({ Actors.id eq actor.id }) {
it[name] = actor.name
it[domain] = actor.domain
it[screenName] = actor.screenName
it[description] = actor.description
it[inbox] = actor.inbox
it[outbox] = actor.outbox
it[url] = actor.url
it[createdAt] = actor.createdAt.toEpochMilli()
it[publicKey] = actor.publicKey
it[privateKey] = actor.privateKey
it[keyId] = actor.keyId
it[following] = actor.following
it[followers] = actor.followers
it[instance] = actor.instance
it[locked] = actor.locked
}
}
return actor
}
override suspend fun findById(id: Long): Actor? =
Actors.select { Actors.id eq id }.singleOrNull()?.let(actorResultRowMapper::map)
override suspend fun delete(id: Long) {
Actors.deleteWhere { Actors.id.eq(id) }
}
override suspend fun nextId(): Long = idGenerateService.generateId()
}
object Actors : Table("actors") {
val id: Column<Long> = long("id")
val name: Column<String> = varchar("name", length = 300)
val domain: Column<String> = varchar("domain", length = 1000)
val screenName: Column<String> = varchar("screen_name", length = 300)
val description: Column<String> = varchar(
"description",
length = 10000
)
val inbox: Column<String> = varchar("inbox", length = 1000).uniqueIndex()
val outbox: Column<String> = varchar("outbox", length = 1000).uniqueIndex()
val url: Column<String> = varchar("url", length = 1000).uniqueIndex()
val publicKey: Column<String> = varchar("public_key", length = 10000)
val privateKey: Column<String?> = varchar(
"private_key",
length = 10000
).nullable()
val createdAt: Column<Long> = long("created_at")
val keyId = varchar("key_id", length = 1000)
val following = varchar("following", length = 1000).nullable()
val followers = varchar("followers", length = 1000).nullable()
val instance = long("instance").references(Instance.id).nullable()
val locked = bool("locked")
override val primaryKey: PrimaryKey = PrimaryKey(id)
init {
uniqueIndex(name, domain)
}
}

View File

@ -22,7 +22,7 @@ class ExposedTimelineRepository(private val idGenerateService: IdGenerateService
it[userId] = timeline.userId
it[timelineId] = timeline.timelineId
it[postId] = timeline.postId
it[postUserId] = timeline.postUserId
it[postActorId] = timeline.postActorId
it[createdAt] = timeline.createdAt
it[replyId] = timeline.replyId
it[repostId] = timeline.repostId
@ -37,7 +37,7 @@ class ExposedTimelineRepository(private val idGenerateService: IdGenerateService
it[userId] = timeline.userId
it[timelineId] = timeline.timelineId
it[postId] = timeline.postId
it[postUserId] = timeline.postUserId
it[postActorId] = timeline.postActorId
it[createdAt] = timeline.createdAt
it[replyId] = timeline.replyId
it[repostId] = timeline.repostId
@ -57,7 +57,7 @@ class ExposedTimelineRepository(private val idGenerateService: IdGenerateService
this[Timelines.userId] = it.userId
this[Timelines.timelineId] = it.timelineId
this[Timelines.postId] = it.postId
this[Timelines.postUserId] = it.postUserId
this[Timelines.postActorId] = it.postActorId
this[Timelines.createdAt] = it.createdAt
this[Timelines.replyId] = it.replyId
this[Timelines.repostId] = it.repostId
@ -84,7 +84,7 @@ fun ResultRow.toTimeline(): Timeline {
userId = this[Timelines.userId],
timelineId = this[Timelines.timelineId],
postId = this[Timelines.postId],
postUserId = this[Timelines.postUserId],
postActorId = this[Timelines.postActorId],
createdAt = this[Timelines.createdAt],
replyId = this[Timelines.replyId],
repostId = this[Timelines.repostId],
@ -101,7 +101,7 @@ object Timelines : Table("timelines") {
val userId = long("user_id")
val timelineId = long("timeline_id")
val postId = long("post_id")
val postUserId = long("post_user_id")
val postActorId = long("post_actor_id")
val createdAt = long("created_at")
val replyId = long("reply_id").nullable()
val repostId = long("repost_id").nullable()

View File

@ -23,7 +23,7 @@ class PostRepositoryImpl(
if (singleOrNull == null) {
Posts.insert {
it[id] = post.id
it[userId] = post.userId
it[actorId] = post.actorId
it[overview] = post.overview
it[text] = post.text
it[createdAt] = post.createdAt
@ -47,7 +47,7 @@ class PostRepositoryImpl(
this[PostsMedia.mediaId] = it
}
Posts.update({ Posts.id eq post.id }) {
it[userId] = post.userId
it[actorId] = post.actorId
it[overview] = post.overview
it[text] = post.text
it[createdAt] = post.createdAt
@ -75,7 +75,7 @@ class PostRepositoryImpl(
object Posts : Table() {
val id: Column<Long> = long("id")
val userId: Column<Long> = long("user_id").references(Users.id)
val actorId: Column<Long> = long("actor_id").references(Actors.id)
val overview: Column<String?> = varchar("overview", 100).nullable()
val text: Column<String> = varchar("text", 3000)
val createdAt: Column<Long> = long("created_at")

View File

@ -21,13 +21,13 @@ class ReactionRepositoryImpl(
it[id] = reaction.id
it[emojiId] = reaction.emojiId
it[postId] = reaction.postId
it[userId] = reaction.userId
it[actorId] = reaction.actorId
}
} else {
Reactions.update({ Reactions.id eq reaction.id }) {
it[emojiId] = reaction.emojiId
it[postId] = reaction.postId
it[userId] = reaction.userId
it[actorId] = reaction.actorId
}
}
return reaction
@ -37,7 +37,7 @@ class ReactionRepositoryImpl(
Reactions.deleteWhere {
id.eq(reaction.id)
.and(postId.eq(reaction.postId))
.and(userId.eq(reaction.postId))
.and(actorId.eq(reaction.postId))
.and(emojiId.eq(reaction.emojiId))
}
return reaction
@ -49,16 +49,16 @@ fun ResultRow.toReaction(): Reaction {
this[Reactions.id].value,
this[Reactions.emojiId],
this[Reactions.postId],
this[Reactions.userId]
this[Reactions.actorId]
)
}
object Reactions : LongIdTable("reactions") {
val emojiId: Column<Long> = long("emoji_id")
val postId: Column<Long> = long("post_id").references(Posts.id)
val userId: Column<Long> = long("user_id").references(Users.id)
val actorId: Column<Long> = long("actor_id").references(Actors.id)
init {
uniqueIndex(emojiId, postId, userId)
uniqueIndex(emojiId, postId, actorId)
}
}

View File

@ -0,0 +1,54 @@
package dev.usbharu.hideout.core.infrastructure.exposedrepository
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
import org.jetbrains.exposed.dao.id.LongIdTable
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.jetbrains.exposed.sql.deleteWhere
import org.jetbrains.exposed.sql.insert
import org.jetbrains.exposed.sql.select
import org.jetbrains.exposed.sql.update
import org.springframework.stereotype.Repository
@Repository
class UserDetailRepositoryImpl : UserDetailRepository {
override suspend fun save(userDetail: UserDetail): UserDetail {
val singleOrNull = UserDetails.select { UserDetails.actorId eq userDetail.actorId }.singleOrNull()
if (singleOrNull == null) {
UserDetails.insert {
it[actorId] = userDetail.actorId
it[password] = userDetail.password
it[autoAcceptFolloweeFollowRequest] = userDetail.autoAcceptFolloweeFollowRequest
}
} else {
UserDetails.update({ UserDetails.actorId eq userDetail.actorId }) {
it[password] = userDetail.password
it[autoAcceptFolloweeFollowRequest] = userDetail.autoAcceptFolloweeFollowRequest
}
}
return userDetail
}
override suspend fun delete(userDetail: UserDetail) {
UserDetails.deleteWhere { UserDetails.actorId eq userDetail.actorId }
}
override suspend fun findByActorId(actorId: Long): UserDetail? {
return UserDetails
.select { UserDetails.actorId eq actorId }
.singleOrNull()
?.let {
UserDetail(
it[UserDetails.actorId],
it[UserDetails.password],
it[UserDetails.autoAcceptFolloweeFollowRequest]
)
}
}
}
object UserDetails : LongIdTable("user_details") {
val actorId = long("actor_id").references(Actors.id)
val password = varchar("password", 255)
val autoAcceptFolloweeFollowRequest = bool("auto_accept_followee_follow_request")
}

View File

@ -1,100 +0,0 @@
package dev.usbharu.hideout.core.infrastructure.exposedrepository
import dev.usbharu.hideout.application.infrastructure.exposed.ResultRowMapper
import dev.usbharu.hideout.application.service.id.IdGenerateService
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.user.UserRepository
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.springframework.stereotype.Repository
@Repository
class UserRepositoryImpl(
private val idGenerateService: IdGenerateService,
private val userResultRowMapper: ResultRowMapper<User>
) :
UserRepository {
override suspend fun save(user: User): User {
val singleOrNull = Users.select { Users.id eq user.id }.empty()
if (singleOrNull) {
Users.insert {
it[id] = user.id
it[name] = user.name
it[domain] = user.domain
it[screenName] = user.screenName
it[description] = user.description
it[password] = user.password
it[inbox] = user.inbox
it[outbox] = user.outbox
it[url] = user.url
it[createdAt] = user.createdAt.toEpochMilli()
it[publicKey] = user.publicKey
it[privateKey] = user.privateKey
it[keyId] = user.keyId
it[following] = user.following
it[followers] = user.followers
it[instance] = user.instance
}
} else {
Users.update({ Users.id eq user.id }) {
it[name] = user.name
it[domain] = user.domain
it[screenName] = user.screenName
it[description] = user.description
it[password] = user.password
it[inbox] = user.inbox
it[outbox] = user.outbox
it[url] = user.url
it[createdAt] = user.createdAt.toEpochMilli()
it[publicKey] = user.publicKey
it[privateKey] = user.privateKey
it[keyId] = user.keyId
it[following] = user.following
it[followers] = user.followers
it[instance] = user.instance
}
}
return user
}
override suspend fun findById(id: Long): User? =
Users.select { Users.id eq id }.singleOrNull()?.let(userResultRowMapper::map)
override suspend fun delete(id: Long) {
Users.deleteWhere { Users.id.eq(id) }
}
override suspend fun nextId(): Long = idGenerateService.generateId()
}
object Users : Table("users") {
val id: Column<Long> = long("id")
val name: Column<String> = varchar("name", length = 300)
val domain: Column<String> = varchar("domain", length = 1000)
val screenName: Column<String> = varchar("screen_name", length = 300)
val description: Column<String> = varchar(
"description",
length = 10000
)
val password: Column<String?> = varchar("password", length = 255).nullable()
val inbox: Column<String> = varchar("inbox", length = 1000).uniqueIndex()
val outbox: Column<String> = varchar("outbox", length = 1000).uniqueIndex()
val url: Column<String> = varchar("url", length = 1000).uniqueIndex()
val publicKey: Column<String> = varchar("public_key", length = 10000)
val privateKey: Column<String?> = varchar(
"private_key",
length = 10000
).nullable()
val createdAt: Column<Long> = long("created_at")
val keyId = varchar("key_id", length = 1000)
val following = varchar("following", length = 1000).nullable()
val followers = varchar("followers", length = 1000).nullable()
val instance = long("instance").references(Instance.id).nullable()
override val primaryKey: PrimaryKey = PrimaryKey(id)
init {
uniqueIndex(name, domain)
}
}

View File

@ -3,7 +3,7 @@ package dev.usbharu.hideout.core.infrastructure.springframework.httpsignature
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.exception.HttpSignatureVerifyException
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.util.RsaUtil
import dev.usbharu.httpsignature.common.HttpMethod
import dev.usbharu.httpsignature.common.HttpRequest
@ -20,7 +20,7 @@ import org.springframework.security.core.userdetails.UsernameNotFoundException
import org.springframework.security.web.authentication.preauth.PreAuthenticatedAuthenticationToken
class HttpSignatureUserDetailsService(
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val httpSignatureVerifier: HttpSignatureVerifier,
private val transaction: Transaction,
private val httpSignatureHeaderParser: SignatureHeaderParser
@ -35,7 +35,7 @@ class HttpSignatureUserDetailsService(
val keyId = token.principal as String
val findByKeyId = transaction.transaction {
try {
userQueryService.findByKeyId(keyId)
actorQueryService.findByKeyId(keyId)
} catch (e: FailedToGetResourcesException) {
throw UsernameNotFoundException("User not found", e)
}

View File

@ -2,7 +2,9 @@ package dev.usbharu.hideout.core.infrastructure.springframework.oauth2
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
import dev.usbharu.hideout.core.query.ActorQueryService
import kotlinx.coroutines.runBlocking
import org.springframework.security.core.userdetails.UserDetails
import org.springframework.security.core.userdetails.UserDetailsService
@ -11,8 +13,9 @@ import org.springframework.stereotype.Service
@Service
class UserDetailsServiceImpl(
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val applicationConfig: ApplicationConfig,
private val userDetailRepository: UserDetailRepository,
private val transaction: Transaction
) :
UserDetailsService {
@ -21,11 +24,17 @@ class UserDetailsServiceImpl(
throw UsernameNotFoundException("$username not found")
}
transaction.transaction {
val findById = userQueryService.findByNameAndDomain(username, applicationConfig.url.host)
val findById = try {
actorQueryService.findByNameAndDomain(username, applicationConfig.url.host)
} catch (e: FailedToGetResourcesException) {
throw UsernameNotFoundException("$username not found", e)
}
val userDetails = userDetailRepository.findByActorId(findById.id)
?: throw UsernameNotFoundException("${findById.id} not found.")
UserDetailsImpl(
id = findById.id,
username = findById.name,
password = findById.password,
password = userDetails.password,
enabled = true,
accountNonExpired = true,
credentialsNonExpired = true,

View File

@ -0,0 +1,16 @@
package dev.usbharu.hideout.core.query
import dev.usbharu.hideout.core.domain.model.actor.Actor
import org.springframework.stereotype.Repository
@Repository
interface ActorQueryService {
suspend fun findAll(limit: Int, offset: Long): List<Actor>
suspend fun findById(id: Long): Actor
suspend fun findByName(name: String): List<Actor>
suspend fun findByNameAndDomain(name: String, domain: String): Actor
suspend fun findByUrl(url: String): Actor
suspend fun findByIds(ids: List<Long>): List<Actor>
suspend fun existByNameAndDomain(name: String, domain: String): Boolean
suspend fun findByKeyId(keyId: String): Actor
}

View File

@ -1,9 +1,9 @@
package dev.usbharu.hideout.core.query
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
@Deprecated("Use RelationshipQueryService")
interface FollowerQueryService {
suspend fun findFollowersById(id: Long): List<User>
suspend fun alreadyFollow(userId: Long, followerId: Long): Boolean
suspend fun findFollowersById(id: Long): List<Actor>
suspend fun alreadyFollow(actorId: Long, followerId: Long): Boolean
}

View File

@ -5,12 +5,12 @@ import org.springframework.stereotype.Repository
@Repository
interface ReactionQueryService {
suspend fun findByPostId(postId: Long, userId: Long? = null): List<Reaction>
suspend fun findByPostId(postId: Long, actorId: Long? = null): List<Reaction>
@Suppress("FunctionMaxLength")
suspend fun findByPostIdAndUserIdAndEmojiId(postId: Long, userId: Long, emojiId: Long): Reaction
suspend fun findByPostIdAndActorIdAndEmojiId(postId: Long, actorId: Long, emojiId: Long): Reaction
suspend fun reactionAlreadyExist(postId: Long, userId: Long, emojiId: Long): Boolean
suspend fun reactionAlreadyExist(postId: Long, actorId: Long, emojiId: Long): Boolean
suspend fun deleteByPostIdAndUserId(postId: Long, userId: Long)
suspend fun deleteByPostIdAndActorId(postId: Long, actorId: Long)
}

View File

@ -5,4 +5,14 @@ import dev.usbharu.hideout.core.domain.model.relationship.Relationship
interface RelationshipQueryService {
suspend fun findByTargetIdAndFollowing(targetId: Long, following: Boolean): List<Relationship>
@Suppress("LongParameterList")
suspend fun findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId: Long?,
sinceId: Long?,
limit: Int,
targetId: Long,
followRequest: Boolean,
ignoreFollowRequest: Boolean
): List<Relationship>
}

View File

@ -1,16 +0,0 @@
package dev.usbharu.hideout.core.query
import dev.usbharu.hideout.core.domain.model.user.User
import org.springframework.stereotype.Repository
@Repository
interface UserQueryService {
suspend fun findAll(limit: Int, offset: Long): List<User>
suspend fun findById(id: Long): User
suspend fun findByName(name: String): List<User>
suspend fun findByNameAndDomain(name: String, domain: String): User
suspend fun findByUrl(url: String): User
suspend fun findByIds(ids: List<Long>): List<User>
suspend fun existByNameAndDomain(name: String, domain: String): Boolean
suspend fun findByKeyId(keyId: String): User
}

View File

@ -1,5 +1,5 @@
package dev.usbharu.hideout.core.service.follow
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
data class SendFollowDto(val userId: User, val followTargetUserId: User)
data class SendFollowDto(val actorId: Actor, val followTargetActorId: Actor)

View File

@ -2,9 +2,9 @@ package dev.usbharu.hideout.core.service.post
import dev.usbharu.hideout.activitypub.service.activity.create.ApSendCreateService
import dev.usbharu.hideout.core.domain.exception.UserNotFoundException
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.domain.model.post.PostRepository
import dev.usbharu.hideout.core.domain.model.user.UserRepository
import dev.usbharu.hideout.core.query.PostQueryService
import dev.usbharu.hideout.core.service.timeline.TimelineService
import org.jetbrains.exposed.exceptions.ExposedSQLException
@ -16,7 +16,7 @@ import java.time.Instant
@Service
class PostServiceImpl(
private val postRepository: PostRepository,
private val userRepository: UserRepository,
private val actorRepository: ActorRepository,
private val timelineService: TimelineService,
private val postQueryService: PostQueryService,
private val postBuilder: Post.PostBuilder,
@ -32,7 +32,7 @@ class PostServiceImpl(
}
override suspend fun createRemote(post: Post): Post {
logger.info("START Create Remote Post user: {}, remote url: {}", post.userId, post.apId)
logger.info("START Create Remote Post user: {}, remote url: {}", post.actorId, post.apId)
val createdPost = internalCreate(post, false)
logger.info("SUCCESS Create Remote Post url: {}", createdPost.url)
return createdPost
@ -55,11 +55,11 @@ class PostServiceImpl(
}
private suspend fun internalCreate(post: PostCreateDto, isLocal: Boolean): Post {
val user = userRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
val user = actorRepository.findById(post.userId) ?: throw UserNotFoundException("${post.userId} was not found")
val id = postRepository.generateId()
val createPost = postBuilder.of(
id = id,
userId = post.userId,
actorId = post.userId,
overview = post.overview,
text = post.text,
createdAt = Instant.now().toEpochMilli(),

View File

@ -4,7 +4,7 @@ import org.springframework.stereotype.Service
@Service
interface ReactionService {
suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long)
suspend fun sendReaction(name: String, userId: Long, postId: Long)
suspend fun removeReaction(userId: Long, postId: Long)
suspend fun receiveReaction(name: String, domain: String, actorId: Long, postId: Long)
suspend fun sendReaction(name: String, actorId: Long, postId: Long)
suspend fun removeReaction(actorId: Long, postId: Long)
}

View File

@ -16,34 +16,34 @@ class ReactionServiceImpl(
private val apReactionService: APReactionService,
private val reactionQueryService: ReactionQueryService
) : ReactionService {
override suspend fun receiveReaction(name: String, domain: String, userId: Long, postId: Long) {
if (reactionQueryService.reactionAlreadyExist(postId, userId, 0).not()) {
override suspend fun receiveReaction(name: String, domain: String, actorId: Long, postId: Long) {
if (reactionQueryService.reactionAlreadyExist(postId, actorId, 0).not()) {
try {
reactionRepository.save(
Reaction(reactionRepository.generateId(), 0, postId, userId)
Reaction(reactionRepository.generateId(), 0, postId, actorId)
)
} catch (_: ExposedSQLException) {
}
}
}
override suspend fun sendReaction(name: String, userId: Long, postId: Long) {
override suspend fun sendReaction(name: String, actorId: Long, postId: Long) {
try {
val findByPostIdAndUserIdAndEmojiId =
reactionQueryService.findByPostIdAndUserIdAndEmojiId(postId, userId, 0)
reactionQueryService.findByPostIdAndActorIdAndEmojiId(postId, actorId, 0)
apReactionService.removeReaction(findByPostIdAndUserIdAndEmojiId)
reactionRepository.delete(findByPostIdAndUserIdAndEmojiId)
} catch (_: FailedToGetResourcesException) {
}
val reaction = Reaction(reactionRepository.generateId(), 0, postId, userId)
val reaction = Reaction(reactionRepository.generateId(), 0, postId, actorId)
reactionRepository.save(reaction)
apReactionService.reaction(reaction)
}
override suspend fun removeReaction(userId: Long, postId: Long) {
override suspend fun removeReaction(actorId: Long, postId: Long) {
try {
val findByPostIdAndUserIdAndEmojiId =
reactionQueryService.findByPostIdAndUserIdAndEmojiId(postId, userId, 0)
reactionQueryService.findByPostIdAndActorIdAndEmojiId(postId, actorId, 0)
reactionRepository.delete(findByPostIdAndUserIdAndEmojiId)
apReactionService.removeReaction(findByPostIdAndUserIdAndEmojiId)
} catch (_: FailedToGetResourcesException) {

View File

@ -1,22 +1,22 @@
package dev.usbharu.hideout.core.service.relationship
interface RelationshipService {
suspend fun followRequest(userId: Long, targetId: Long)
suspend fun block(userId: Long, targetId: Long)
suspend fun followRequest(actorId: Long, targetId: Long)
suspend fun block(actorId: Long, targetId: Long)
/**
* フォローリクエストを承認します
* [userId][targetId]からのフォローリクエストを承認します
* [actorId][targetId]からのフォローリクエストを承認します
*
* @param userId 承認操作をするユーザー
* @param actorId 承認操作をするユーザー
* @param targetId 承認するフォローリクエストを送ってきたユーザー
* @param force 強制的にAcceptアクティビティを発行する
*/
suspend fun acceptFollowRequest(userId: Long, targetId: Long, force: Boolean = false)
suspend fun rejectFollowRequest(userId: Long, targetId: Long)
suspend fun ignoreFollowRequest(userId: Long, targetId: Long)
suspend fun unfollow(userId: Long, targetId: Long)
suspend fun unblock(userId: Long, targetId: Long)
suspend fun mute(userId: Long, targetId: Long)
suspend fun unmute(userId: Long, targetId: Long)
suspend fun acceptFollowRequest(actorId: Long, targetId: Long, force: Boolean = false)
suspend fun rejectFollowRequest(actorId: Long, targetId: Long)
suspend fun ignoreFollowRequest(actorId: Long, targetId: Long)
suspend fun unfollow(actorId: Long, targetId: Long)
suspend fun unblock(actorId: Long, targetId: Long)
suspend fun mute(actorId: Long, targetId: Long)
suspend fun unmute(actorId: Long, targetId: Long)
}

View File

@ -7,10 +7,10 @@ import dev.usbharu.hideout.activitypub.service.activity.reject.ApSendRejectServi
import dev.usbharu.hideout.activitypub.service.activity.undo.APSendUndoService
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.relationship.Relationship
import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.follow.SendFollowDto
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@ -18,7 +18,7 @@ import org.springframework.stereotype.Service
@Service
class RelationshipServiceImpl(
private val applicationConfig: ApplicationConfig,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val relationshipRepository: RelationshipRepository,
private val apSendFollowService: APSendFollowService,
private val apSendBlockService: APSendBlockService,
@ -26,48 +26,48 @@ class RelationshipServiceImpl(
private val apSendRejectService: ApSendRejectService,
private val apSendUndoService: APSendUndoService
) : RelationshipService {
override suspend fun followRequest(userId: Long, targetId: Long) {
logger.info("START Follow Request userId: {} targetId: {}", userId, targetId)
override suspend fun followRequest(actorId: Long, targetId: Long) {
logger.info("START Follow Request userId: {} targetId: {}", actorId, targetId)
val relationship =
relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)?.copy(followRequest = true)
relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)?.copy(followRequest = true)
?: Relationship(
userId = userId,
targetUserId = targetId,
actorId = actorId,
targetActorId = targetId,
following = false,
blocking = false,
muting = false,
followRequest = true,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, userId) ?: Relationship(
userId = targetId,
targetUserId = userId,
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId) ?: Relationship(
actorId = targetId,
targetActorId = actorId,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
if (inverseRelationship.blocking) {
logger.debug("FAILED Blocked by target. userId: {} targetId: {}", userId, targetId)
logger.debug("FAILED Blocked by target. userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.blocking) {
logger.debug("FAILED Blocking user. userId: {} targetId: {}", userId, targetId)
logger.debug("FAILED Blocking user. userId: {} targetId: {}", actorId, targetId)
return
}
if (inverseRelationship.ignoreFollowRequestFromTarget) {
logger.debug("SUCCESS Ignore Follow Request. userId: {} targetId: {}", userId, targetId)
if (relationship.ignoreFollowRequestToTarget) {
logger.debug("SUCCESS Ignore Follow Request. userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.following) {
logger.debug("SUCCESS User already follow. userId: {} targetId: {}", userId, targetId)
acceptFollowRequest(targetId, userId, true)
logger.debug("SUCCESS User already follow. userId: {} targetId: {}", actorId, targetId)
acceptFollowRequest(targetId, actorId, true)
return
}
@ -76,29 +76,31 @@ class RelationshipServiceImpl(
val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) {
val user = userQueryService.findById(userId)
val user = actorQueryService.findById(actorId)
apSendFollowService.sendFollow(SendFollowDto(user, remoteUser))
} else {
// TODO: フォロー許可制ユーザーを実装したら消す
acceptFollowRequest(targetId, userId)
val target = actorQueryService.findById(targetId)
if (target.locked.not()) {
acceptFollowRequest(targetId, actorId)
}
}
logger.info("SUCCESS Follow Request userId: {} targetId: {}", userId, targetId)
logger.info("SUCCESS Follow Request userId: {} targetId: {}", actorId, targetId)
}
override suspend fun block(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)
override suspend fun block(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)
?.copy(blocking = true, followRequest = false, following = false) ?: Relationship(
userId = userId,
targetUserId = targetId,
actorId = actorId,
targetActorId = targetId,
following = false,
blocking = true,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, userId)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId)
?.copy(followRequest = false, following = false)
relationshipRepository.save(relationship)
@ -109,47 +111,47 @@ class RelationshipServiceImpl(
val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) {
val user = userQueryService.findById(userId)
val user = actorQueryService.findById(actorId)
apSendBlockService.sendBlock(user, remoteUser)
}
}
override suspend fun acceptFollowRequest(userId: Long, targetId: Long, force: Boolean) {
logger.info("START Accept follow request userId: {} targetId: {}", userId, targetId)
override suspend fun acceptFollowRequest(actorId: Long, targetId: Long, force: Boolean) {
logger.info("START Accept follow request userId: {} targetId: {}", actorId, targetId)
val relationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, userId)
val relationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId) ?: Relationship(
userId = targetId,
targetUserId = userId,
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId) ?: Relationship(
actorId = targetId,
targetActorId = actorId,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
if (relationship == null) {
logger.warn("FAILED Follow Request Not Found. (Relationship) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Follow Request Not Found. (Relationship) userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.followRequest.not() && force.not()) {
logger.warn("FAILED Follow Request Not Found. (Follow Request) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Follow Request Not Found. (Follow Request) userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.blocking) {
logger.warn("FAILED Blocking user userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Blocking user userId: {} targetId: {}", actorId, targetId)
throw IllegalStateException(
"Cannot accept a follow request from a blocked user. userId: $userId targetId: $targetId"
"Cannot accept a follow request from a blocked user. userId: $actorId targetId: $targetId"
)
}
if (inverseRelationship.blocking) {
logger.warn("FAILED BLocked by user userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED BLocked by user userId: {} targetId: {}", actorId, targetId)
throw IllegalStateException(
"Cannot accept a follow request from a blocking user. userId: $userId targetId: $targetId"
"Cannot accept a follow request from a blocking user. userId: $actorId targetId: $targetId"
)
}
@ -160,21 +162,21 @@ class RelationshipServiceImpl(
val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) {
val user = userQueryService.findById(userId)
val user = actorQueryService.findById(actorId)
apSendAcceptService.sendAcceptFollow(user, remoteUser)
}
}
override suspend fun rejectFollowRequest(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, userId)
override suspend fun rejectFollowRequest(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId)
if (relationship == null) {
logger.warn("FAILED Follow Request Not Found. (Relationship) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Follow Request Not Found. (Relationship) userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.followRequest.not() && relationship.following.not()) {
logger.warn("FAILED Follow Request Not Found. (Follow Request) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Follow Request Not Found. (Follow Request) userId: {} targetId: {}", actorId, targetId)
return
}
@ -185,37 +187,37 @@ class RelationshipServiceImpl(
val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) {
val user = userQueryService.findById(userId)
val user = actorQueryService.findById(actorId)
apSendRejectService.sendRejectFollow(user, remoteUser)
}
}
override suspend fun ignoreFollowRequest(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)
?.copy(ignoreFollowRequestFromTarget = true)
override suspend fun ignoreFollowRequest(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, actorId)
?.copy(ignoreFollowRequestToTarget = true)
?: Relationship(
userId = userId,
targetUserId = targetId,
actorId = targetId,
targetActorId = actorId,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = true
ignoreFollowRequestToTarget = true
)
relationshipRepository.save(relationship)
}
override suspend fun unfollow(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)
override suspend fun unfollow(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)
if (relationship == null) {
logger.warn("FAILED Unfollow. (Relationship) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Unfollow. (Relationship) userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.following.not()) {
logger.warn("SUCCESS User already unfollow. userId: {} targetId: {}", userId, targetId)
logger.warn("SUCCESS User already unfollow. userId: {} targetId: {}", actorId, targetId)
return
}
@ -226,21 +228,21 @@ class RelationshipServiceImpl(
val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) {
val user = userQueryService.findById(userId)
val user = actorQueryService.findById(actorId)
apSendUndoService.sendUndoFollow(user, remoteUser)
}
}
override suspend fun unblock(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)
override suspend fun unblock(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)
if (relationship == null) {
logger.warn("FAILED Unblock. (Relationship) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Unblock. (Relationship) userId: {} targetId: {}", actorId, targetId)
return
}
if (relationship.blocking.not()) {
logger.warn("SUCCESS User is not blocking. userId: {] targetId: {}", userId, targetId)
logger.warn("SUCCESS User is not blocking. userId: {] targetId: {}", actorId, targetId)
return
}
@ -249,41 +251,41 @@ class RelationshipServiceImpl(
val remoteUser = isRemoteUser(targetId)
if (remoteUser != null) {
val user = userQueryService.findById(userId)
val user = actorQueryService.findById(actorId)
apSendUndoService.sendUndoBlock(user, remoteUser)
}
}
override suspend fun mute(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)?.copy(muting = true)
override suspend fun mute(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)?.copy(muting = true)
?: Relationship(
userId = userId,
targetUserId = targetId,
actorId = actorId,
targetActorId = targetId,
following = false,
blocking = false,
muting = true,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
relationshipRepository.save(relationship)
}
override suspend fun unmute(userId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userId, targetId)?.copy(muting = false)
override suspend fun unmute(actorId: Long, targetId: Long) {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(actorId, targetId)?.copy(muting = false)
if (relationship == null) {
logger.warn("FAILED Mute. (Relationship) userId: {} targetId: {}", userId, targetId)
logger.warn("FAILED Mute. (Relationship) userId: {} targetId: {}", actorId, targetId)
return
}
relationshipRepository.save(relationship)
}
private suspend fun isRemoteUser(userId: Long): User? {
private suspend fun isRemoteUser(userId: Long): Actor? {
logger.trace("isRemoteUser({})", userId)
val user = try {
userQueryService.findById(userId)
actorQueryService.findById(userId)
} catch (e: FailedToGetResourcesException) {
logger.warn("User not found.", e)
throw IllegalStateException("User not found.", e)

View File

@ -4,22 +4,22 @@ import dev.usbharu.hideout.core.domain.model.post.Post
import dev.usbharu.hideout.core.domain.model.post.Visibility
import dev.usbharu.hideout.core.domain.model.timeline.Timeline
import dev.usbharu.hideout.core.domain.model.timeline.TimelineRepository
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.query.FollowerQueryService
import dev.usbharu.hideout.core.query.UserQueryService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
@Service
class TimelineService(
private val followerQueryService: FollowerQueryService,
private val userQueryService: UserQueryService,
private val actorQueryService: ActorQueryService,
private val timelineRepository: TimelineRepository
) {
suspend fun publishTimeline(post: Post, isLocal: Boolean) {
val findFollowersById = followerQueryService.findFollowersById(post.userId).toMutableList()
val findFollowersById = followerQueryService.findFollowersById(post.actorId).toMutableList()
if (isLocal) {
// 自分自身も含める必要がある
val user = userQueryService.findById(post.userId)
val user = actorQueryService.findById(post.actorId)
findFollowersById.add(user)
}
val timelines = findFollowersById.map {
@ -28,7 +28,7 @@ class TimelineService(
userId = it.id,
timelineId = 0,
postId = post.id,
postUserId = post.userId,
postActorId = post.actorId,
createdAt = post.createdAt,
replyId = post.replyId,
repostId = post.repostId,
@ -46,7 +46,7 @@ class TimelineService(
userId = 0,
timelineId = 0,
postId = post.id,
postUserId = post.userId,
postActorId = post.actorId,
createdAt = post.createdAt,
replyId = post.replyId,
repostId = post.repostId,

View File

@ -12,5 +12,6 @@ data class RemoteUserCreateDto(
val keyId: String,
val followers: String?,
val following: String?,
val sharedInbox: String?
val sharedInbox: String?,
val locked: Boolean?
)

View File

@ -0,0 +1,12 @@
package dev.usbharu.hideout.core.service.user
import dev.usbharu.hideout.core.domain.model.media.Media
data class UpdateUserDto(
val screenName: String,
val description: String,
val avatarMedia: Media?,
val headerMedia: Media?,
val locked: Boolean,
val autoAcceptFolloweeFollowRequest: Boolean
)

View File

@ -1,6 +1,6 @@
package dev.usbharu.hideout.core.service.user
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.query.ActorQueryService
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder
import org.springframework.stereotype.Service
import java.security.*
@ -8,13 +8,13 @@ import java.util.*
@Service
class UserAuthServiceImpl(
val userQueryService: UserQueryService
val actorQueryService: ActorQueryService
) : UserAuthService {
override fun hash(password: String): String = BCryptPasswordEncoder().encode(password)
override suspend fun usernameAlreadyUse(username: String): Boolean {
userQueryService.findByName(username)
actorQueryService.findByName(username)
return true
}

View File

@ -1,6 +1,6 @@
package dev.usbharu.hideout.core.service.user
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.actor.Actor
import org.springframework.stereotype.Service
@Service
@ -8,7 +8,9 @@ interface UserService {
suspend fun usernameAlreadyUse(username: String): Boolean
suspend fun createLocalUser(user: UserCreateDto): User
suspend fun createLocalUser(user: UserCreateDto): Actor
suspend fun createRemoteUser(user: RemoteUserCreateDto): User
suspend fun createRemoteUser(user: RemoteUserCreateDto): Actor
suspend fun updateUser(userId: Long, updateUserDto: UpdateUserDto)
}

View File

@ -1,9 +1,11 @@
package dev.usbharu.hideout.core.service.user
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.model.user.User
import dev.usbharu.hideout.core.domain.model.user.UserRepository
import dev.usbharu.hideout.core.query.UserQueryService
import dev.usbharu.hideout.core.domain.model.actor.Actor
import dev.usbharu.hideout.core.domain.model.actor.ActorRepository
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetail
import dev.usbharu.hideout.core.domain.model.userdetails.UserDetailRepository
import dev.usbharu.hideout.core.query.ActorQueryService
import dev.usbharu.hideout.core.service.instance.InstanceService
import org.jetbrains.exposed.exceptions.ExposedSQLException
import org.slf4j.LoggerFactory
@ -13,32 +15,32 @@ import java.time.Instant
@Service
class UserServiceImpl(
private val userRepository: UserRepository,
private val actorRepository: ActorRepository,
private val userAuthService: UserAuthService,
private val userQueryService: UserQueryService,
private val userBuilder: User.UserBuilder,
private val actorQueryService: ActorQueryService,
private val actorBuilder: Actor.UserBuilder,
private val applicationConfig: ApplicationConfig,
private val instanceService: InstanceService
private val instanceService: InstanceService,
private val userDetailRepository: UserDetailRepository
) :
UserService {
override suspend fun usernameAlreadyUse(username: String): Boolean {
val findByNameAndDomain = userQueryService.findByNameAndDomain(username, applicationConfig.url.host)
val findByNameAndDomain = actorQueryService.findByNameAndDomain(username, applicationConfig.url.host)
return findByNameAndDomain != null
}
override suspend fun createLocalUser(user: UserCreateDto): User {
val nextId = userRepository.nextId()
override suspend fun createLocalUser(user: UserCreateDto): Actor {
val nextId = actorRepository.nextId()
val hashedPassword = userAuthService.hash(user.password)
val keyPair = userAuthService.generateKeyPair()
val userUrl = "${applicationConfig.url}/users/${user.name}"
val userEntity = userBuilder.of(
val userEntity = actorBuilder.of(
id = nextId,
name = user.name,
domain = applicationConfig.url.host,
screenName = user.screenName,
description = user.description,
password = hashedPassword,
inbox = "$userUrl/inbox",
outbox = "$userUrl/outbox",
url = userUrl,
@ -47,13 +49,16 @@ class UserServiceImpl(
createdAt = Instant.now(),
following = "$userUrl/following",
followers = "$userUrl/followers",
keyId = "$userUrl#pubkey"
keyId = "$userUrl#pubkey",
locked = false
)
return userRepository.save(userEntity)
val save = actorRepository.save(userEntity)
userDetailRepository.save(UserDetail(nextId, hashedPassword, true))
return save
}
@Transactional
override suspend fun createRemoteUser(user: RemoteUserCreateDto): User {
override suspend fun createRemoteUser(user: RemoteUserCreateDto): Actor {
logger.info("START Create New remote user. name: {} url: {}", user.name, user.url)
@Suppress("TooGenericExceptionCaught")
val instance = try {
@ -63,8 +68,8 @@ class UserServiceImpl(
null
}
val nextId = userRepository.nextId()
val userEntity = userBuilder.of(
val nextId = actorRepository.nextId()
val userEntity = actorBuilder.of(
id = nextId,
name = user.name,
domain = user.domain,
@ -78,18 +83,40 @@ class UserServiceImpl(
followers = user.followers,
following = user.following,
keyId = user.keyId,
instance = instance?.id
instance = instance?.id,
locked = user.locked ?: false
)
return try {
val save = userRepository.save(userEntity)
val save = actorRepository.save(userEntity)
logger.warn("SUCCESS Create New remote user. id: {} name: {} url: {}", userEntity.id, user.name, user.url)
save
} catch (_: ExposedSQLException) {
logger.warn("FAILED User already exists. name: {} url: {}", user.name, user.url)
userQueryService.findByUrl(user.url)
actorQueryService.findByUrl(user.url)
}
}
override suspend fun updateUser(userId: Long, updateUserDto: UpdateUserDto) {
val userDetail = userDetailRepository.findByActorId(userId)
?: throw IllegalArgumentException("userId: $userId was not found.")
val actor = actorRepository.findById(userId) ?: throw IllegalArgumentException("userId $userId was not found.")
actorRepository.save(
actor.copy(
screenName = updateUserDto.screenName,
description = updateUserDto.description,
locked = updateUserDto.locked
)
)
userDetailRepository.save(
userDetail.copy(
autoAcceptFolloweeFollowRequest = updateUserDto.autoAcceptFolloweeFollowRequest
)
)
}
companion object {
private val logger = LoggerFactory.getLogger(UserServiceImpl::class.java)
}

View File

@ -0,0 +1,107 @@
package dev.usbharu.hideout.mastodon.infrastructure.exposedquery
import dev.usbharu.hideout.application.config.ApplicationConfig
import dev.usbharu.hideout.core.domain.exception.FailedToGetResourcesException
import dev.usbharu.hideout.core.domain.model.relationship.Relationships
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Actors
import dev.usbharu.hideout.core.infrastructure.exposedrepository.Posts
import dev.usbharu.hideout.domain.mastodon.model.generated.Account
import dev.usbharu.hideout.mastodon.query.AccountQueryService
import dev.usbharu.hideout.util.singleOr
import org.jetbrains.exposed.sql.*
import org.jetbrains.exposed.sql.SqlExpressionBuilder.eq
import org.springframework.stereotype.Repository
import java.time.Instant
@Repository
class AccountQueryServiceImpl(private val applicationConfig: ApplicationConfig) : AccountQueryService {
override suspend fun findById(accountId: Long): Account {
val followingCount = Count(Relationships.actorId.eq(Actors.id), true).alias("following_count")
val followersCount = Count(Relationships.targetActorId.eq(Actors.id), true).alias("followers_count")
val postsCount = Posts.id.countDistinct().alias("posts_count")
val lastCreated = Posts.createdAt.max().alias("last_created")
val query = Actors
.join(Relationships, JoinType.LEFT) {
Actors.id eq Relationships.actorId or (Actors.id eq Relationships.targetActorId)
}
.leftJoin(Posts)
.slice(
followingCount,
followersCount,
*(Actors.realFields.toTypedArray()),
lastCreated,
postsCount
)
.select {
(Actors.id.eq(accountId)).and(
Relationships.following.eq(true).or(Relationships.following.isNull())
)
}
.groupBy(Actors.id)
return query
.singleOr { FailedToGetResourcesException("accountId: $accountId wad not exist or duplicate", it) }
.let { toAccount(it, followingCount, followersCount, postsCount, lastCreated) }
}
override suspend fun findByIds(accountIds: List<Long>): List<Account> {
val followingCount = Count(Relationships.actorId.eq(Actors.id), true).alias("following_count")
val followersCount = Count(Relationships.targetActorId.eq(Actors.id), true).alias("followers_count")
val postsCount = Posts.id.countDistinct().alias("posts_count")
val lastCreated = Posts.createdAt.max().alias("last_created")
val query = Actors
.join(Relationships, JoinType.LEFT) {
Actors.id eq Relationships.actorId or (Actors.id eq Relationships.targetActorId)
}
.leftJoin(Posts)
.slice(
followingCount,
followersCount,
*(Actors.realFields.toTypedArray()),
lastCreated,
postsCount
)
.select {
Actors.id.inList(accountIds)
.and(Relationships.following.eq(true).or(Relationships.following.isNull()))
}
.groupBy(Actors.id)
return query
.map { toAccount(it, followingCount, followersCount, postsCount, lastCreated) }
}
private fun toAccount(
resultRow: ResultRow,
followingCount: ExpressionAlias<Long>,
followersCount: ExpressionAlias<Long>,
postsCount: ExpressionAlias<Long>,
lastCreated: ExpressionAlias<Long?>
): Account {
val userUrl = "${applicationConfig.url}/users/${resultRow[Actors.id]}"
return Account(
id = resultRow[Actors.id].toString(),
username = resultRow[Actors.name],
acct = "${resultRow[Actors.name]}@${resultRow[Actors.domain]}",
url = resultRow[Actors.url],
displayName = resultRow[Actors.screenName],
note = resultRow[Actors.description],
avatar = userUrl + "/icon.jpg",
avatarStatic = userUrl + "/icon.jpg",
header = userUrl + "/header.jpg",
headerStatic = userUrl + "/header.jpg",
locked = resultRow[Actors.locked],
fields = emptyList(),
emojis = emptyList(),
bot = false,
group = false,
discoverable = true,
createdAt = Instant.ofEpochMilli(resultRow[Actors.createdAt]).toString(),
lastStatusAt = resultRow[lastCreated]?.let { Instant.ofEpochMilli(it).toString() },
statusesCount = resultRow[postsCount].toInt(),
followersCount = resultRow[followersCount].toInt(),
followingCount = resultRow[followingCount].toInt(),
)
}
}

View File

@ -24,7 +24,7 @@ class StatusQueryServiceImpl : StatusQueryService {
val mediaIdSet = mutableSetOf<Long>()
mediaIdSet.addAll(statusQueries.flatMap { it.mediaIds })
val postMap = Posts
.leftJoin(Users)
.leftJoin(Actors)
.select { Posts.id inList postIdSet }
.associate { it[Posts.id] to toStatus(it) }
val mediaMap = Media.select { Media.id inList mediaIdSet }
@ -57,9 +57,9 @@ class StatusQueryServiceImpl : StatusQueryService {
): List<Status> {
val query = Posts
.leftJoin(PostsMedia)
.leftJoin(Users)
.leftJoin(Actors)
.leftJoin(Media)
.select { Posts.userId eq accountId }.limit(20)
.select { Posts.actorId eq accountId }.limit(20)
if (maxId != null) {
query.andWhere { Posts.id eq maxId }
@ -120,7 +120,7 @@ class StatusQueryServiceImpl : StatusQueryService {
private suspend fun findByPostIdsWithMedia(ids: List<Long>): List<Status> {
val pairs = Posts
.leftJoin(PostsMedia)
.leftJoin(Users)
.leftJoin(Actors)
.leftJoin(Media)
.select { Posts.id inList ids }
.groupBy { it[Posts.id] }
@ -141,24 +141,24 @@ private fun toStatus(it: ResultRow) = Status(
uri = it[Posts.apId],
createdAt = Instant.ofEpochMilli(it[Posts.createdAt]).toString(),
account = Account(
id = it[Users.id].toString(),
username = it[Users.name],
acct = "${it[Users.name]}@${it[Users.domain]}",
url = it[Users.url],
displayName = it[Users.screenName],
note = it[Users.description],
avatar = it[Users.url] + "/icon.jpg",
avatarStatic = it[Users.url] + "/icon.jpg",
header = it[Users.url] + "/header.jpg",
headerStatic = it[Users.url] + "/header.jpg",
id = it[Actors.id].toString(),
username = it[Actors.name],
acct = "${it[Actors.name]}@${it[Actors.domain]}",
url = it[Actors.url],
displayName = it[Actors.screenName],
note = it[Actors.description],
avatar = it[Actors.url] + "/icon.jpg",
avatarStatic = it[Actors.url] + "/icon.jpg",
header = it[Actors.url] + "/header.jpg",
headerStatic = it[Actors.url] + "/header.jpg",
locked = false,
fields = emptyList(),
emojis = emptyList(),
bot = false,
group = false,
discoverable = true,
createdAt = Instant.ofEpochMilli(it[Users.createdAt]).toString(),
lastStatusAt = Instant.ofEpochMilli(it[Users.createdAt]).toString(),
createdAt = Instant.ofEpochMilli(it[Actors.createdAt]).toString(),
lastStatusAt = Instant.ofEpochMilli(it[Actors.createdAt]).toString(),
statusesCount = 0,
followersCount = 0,
followingCount = 0,

View File

@ -143,4 +143,47 @@ class MastodonAccountApiController(
return ResponseEntity.ok(removeFromFollowers)
}
override suspend fun apiV1AccountsUpdateCredentialsPatch(updateCredentials: UpdateCredentials?):
ResponseEntity<Account> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val removeFromFollowers = accountApiService.updateProfile(userid, updateCredentials)
return ResponseEntity.ok(removeFromFollowers)
}
override suspend fun apiV1FollowRequestsAccountIdAuthorizePost(accountId: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val acceptFollowRequest = accountApiService.acceptFollowRequest(userid, accountId.toLong())
return ResponseEntity.ok(acceptFollowRequest)
}
override suspend fun apiV1FollowRequestsAccountIdRejectPost(accountId: String): ResponseEntity<Relationship> {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val rejectFollowRequest = accountApiService.rejectFollowRequest(userid, accountId.toLong())
return ResponseEntity.ok(rejectFollowRequest)
}
override fun apiV1FollowRequestsGet(maxId: String?, sinceId: String?, limit: Int?): ResponseEntity<Flow<Account>> =
runBlocking {
val principal = SecurityContextHolder.getContext().getAuthentication().principal as Jwt
val userid = principal.getClaim<String>("uid").toLong()
val accountFlow =
accountApiService.followRequests(userid, maxId?.toLong(), sinceId?.toLong(), limit ?: 20, false)
.asFlow()
ResponseEntity.ok(accountFlow)
}
}

View File

@ -0,0 +1,8 @@
package dev.usbharu.hideout.mastodon.query
import dev.usbharu.hideout.domain.mastodon.model.generated.Account
interface AccountQueryService {
suspend fun findById(accountId: Long): Account
suspend fun findByIds(accountIds: List<Long>): List<Account>
}

View File

@ -2,16 +2,21 @@ package dev.usbharu.hideout.mastodon.service.account
import dev.usbharu.hideout.application.external.Transaction
import dev.usbharu.hideout.core.domain.model.relationship.RelationshipRepository
import dev.usbharu.hideout.core.query.RelationshipQueryService
import dev.usbharu.hideout.core.service.media.MediaService
import dev.usbharu.hideout.core.service.relationship.RelationshipService
import dev.usbharu.hideout.core.service.user.UpdateUserDto
import dev.usbharu.hideout.core.service.user.UserCreateDto
import dev.usbharu.hideout.core.service.user.UserService
import dev.usbharu.hideout.domain.mastodon.model.generated.*
import dev.usbharu.hideout.mastodon.interfaces.api.media.MediaRequest
import dev.usbharu.hideout.mastodon.query.StatusQueryService
import org.slf4j.LoggerFactory
import org.springframework.stereotype.Service
import kotlin.math.min
@Service
@Suppress("TooManyFunctions")
interface AccountApiService {
@Suppress("LongParameterList")
suspend fun accountsStatuses(
@ -45,6 +50,17 @@ interface AccountApiService {
suspend fun unblock(userid: Long, target: Long): Relationship
suspend fun unfollow(userid: Long, target: Long): Relationship
suspend fun removeFromFollowers(userid: Long, target: Long): Relationship
suspend fun updateProfile(userid: Long, updateCredentials: UpdateCredentials?): Account
suspend fun followRequests(
loginUser: Long,
maxId: Long?,
sinceId: Long?,
limit: Int = 20,
withIgnore: Boolean
): List<Account>
suspend fun acceptFollowRequest(loginUser: Long, target: Long): Relationship
suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship
}
@Service
@ -54,7 +70,9 @@ class AccountApiServiceImpl(
private val userService: UserService,
private val statusQueryService: StatusQueryService,
private val relationshipService: RelationshipService,
private val relationshipRepository: RelationshipRepository
private val relationshipRepository: RelationshipRepository,
private val mediaService: MediaService,
private val relationshipQueryService: RelationshipQueryService
) :
AccountApiService {
override suspend fun accountsStatuses(
@ -153,6 +171,82 @@ class AccountApiServiceImpl(
return@transaction fetchRelationship(userid, target)
}
override suspend fun updateProfile(userid: Long, updateCredentials: UpdateCredentials?): Account =
transaction.transaction {
val avatarMedia = if (updateCredentials?.avatar != null) {
mediaService.uploadLocalMedia(
MediaRequest(
updateCredentials.avatar,
null,
null,
null
)
)
} else {
null
}
val headerMedia = if (updateCredentials?.header != null) {
mediaService.uploadLocalMedia(
MediaRequest(
updateCredentials.header,
null,
null,
null
)
)
} else {
null
}
val account = accountService.findById(userid)
val updateUserDto = UpdateUserDto(
screenName = updateCredentials?.displayName ?: account.displayName,
description = updateCredentials?.note ?: account.note,
avatarMedia = avatarMedia,
headerMedia = headerMedia,
locked = updateCredentials?.locked ?: account.locked,
autoAcceptFolloweeFollowRequest = false
)
userService.updateUser(userid, updateUserDto)
accountService.findById(userid)
}
override suspend fun followRequests(
loginUser: Long,
maxId: Long?,
sinceId: Long?,
limit: Int,
withIgnore: Boolean
): List<Account> = transaction.transaction {
val actorIdList = relationshipQueryService
.findByTargetIdAndFollowRequestAndIgnoreFollowRequest(
maxId = maxId,
sinceId = sinceId,
limit = limit,
targetId = loginUser,
followRequest = true,
ignoreFollowRequest = withIgnore
)
.map { it.actorId }
return@transaction accountService.findByIds(actorIdList)
}
override suspend fun acceptFollowRequest(loginUser: Long, target: Long): Relationship = transaction.transaction {
relationshipService.acceptFollowRequest(loginUser, target)
return@transaction fetchRelationship(loginUser, target)
}
override suspend fun rejectFollowRequest(loginUser: Long, target: Long): Relationship = transaction.transaction {
relationshipService.rejectFollowRequest(loginUser, target)
return@transaction fetchRelationship(loginUser, target)
}
private fun from(account: Account): CredentialAccount {
return CredentialAccount(
id = account.id,
@ -180,10 +274,10 @@ class AccountApiServiceImpl(
suspendex = account.suspendex,
limited = account.limited,
followingCount = account.followingCount,
source = CredentialAccountSource(
source = AccountSource(
account.note,
account.fields,
CredentialAccountSource.Privacy.public,
AccountSource.Privacy.public,
false,
0
),
@ -194,24 +288,24 @@ class AccountApiServiceImpl(
private suspend fun fetchRelationship(userid: Long, targetId: Long): Relationship {
val relationship = relationshipRepository.findByUserIdAndTargetUserId(userid, targetId)
?: dev.usbharu.hideout.core.domain.model.relationship.Relationship(
userId = userid,
targetUserId = targetId,
actorId = userid,
targetActorId = targetId,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
val inverseRelationship = relationshipRepository.findByUserIdAndTargetUserId(targetId, userid)
?: dev.usbharu.hideout.core.domain.model.relationship.Relationship(
userId = targetId,
targetUserId = userid,
actorId = targetId,
targetActorId = userid,
following = false,
blocking = false,
muting = false,
followRequest = false,
ignoreFollowRequestFromTarget = false
ignoreFollowRequestToTarget = false
)
return Relationship(

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