1
+ import takos from "../../../../../../util/takos.ts" ;
2
+ import userConfig from "../../../../../../models/userConfig.ts" ;
3
+ import remoteFriends from "../../../../../../models/remoteFriends.ts" ;
4
+ import friends from "../../../../../../models/friends.ts" ;
5
+ import requestAddFriend from "../../../../../../models/reqestAddFriend.ts" ;
6
+ import { load } from "$std/dotenv/mod.ts" ;
7
+ import users from "../../../../../../models/users.ts" ;
8
+ const env = await load ( ) ;
9
+ export const handler = {
10
+ async GET ( req : Request , ctx : any ) {
11
+ if ( ! ctx . state . data . loggedIn ) {
12
+ return new Response ( JSON . stringify ( { status : false , message : "Not Logged In" } ) ) ;
13
+ }
14
+ const userid = ctx . state . data . userid ;
15
+ let body ;
16
+ try {
17
+ body = await req . json ( ) ;
18
+ } catch ( e ) {
19
+ return new Response ( JSON . stringify ( { status : false , message : "Invalid request" } ) , {
20
+ headers : { "Content-Type" : "application/json" } ,
21
+ status : 400 ,
22
+ } ) ;
23
+ }
24
+ const { userName, csrftoken } = body ;
25
+ if ( typeof userName !== "string" ) {
26
+ return new Response ( JSON . stringify ( { status : false , message : "Invalid userName" } ) , {
27
+ headers : { "Content-Type" : "application/json" } ,
28
+ status : 400 ,
29
+ } ) ;
30
+ }
31
+ if ( typeof csrftoken !== "string" ) {
32
+ return new Response ( JSON . stringify ( { status : false , message : "Invalid csrftoken" } ) , {
33
+ headers : { "Content-Type" : "application/json" } ,
34
+ status : 400 ,
35
+ } ) ;
36
+ }
37
+ if ( takos . checkUserName ( userName ) === false ) {
38
+ return new Response ( JSON . stringify ( { status : false , message : "Invalid" } ) , {
39
+ headers : { "Content-Type" : "application/json" } ,
40
+ status : 400 ,
41
+ } ) ;
42
+ }
43
+ if ( await takos . checkCsrfToken ( csrftoken , userid ) === false ) {
44
+ return new Response ( JSON . stringify ( { status : false , message : "Invalid CSRF token" } ) , {
45
+ headers : { "Content-Type" : "application/json" } ,
46
+ status : 400 ,
47
+ } ) ;
48
+ }
49
+ const userDomain = takos . splitUserName ( userName ) . domain ;
50
+ if ( userDomain !== env [ "DOMAIN" ] ) {
51
+ //friendのuuidを取得
52
+ const response = await fetch ( `https://${ userDomain } /api/v2/server/information/users/uuid` , {
53
+ method : "POST" ,
54
+ headers : {
55
+ "Content-Type" : "application/json" ,
56
+ } ,
57
+ body : JSON . stringify ( { host : env [ "DOMAIN" ] , body : JSON . stringify ( { userName } ) , signature : takos . signData ( JSON . stringify ( { userName } ) , await takos . getPrivateKey ( ) ) } ) ,
58
+ } ) ;
59
+ const data = await response . json ( ) ;
60
+ if ( data . status === false ) {
61
+ return new Response ( JSON . stringify ( { status : false , message : "User not found" } ) , {
62
+ headers : { "Content-Type" : "application/json" } ,
63
+ status : 404 ,
64
+ } ) ;
65
+ }
66
+ const friendId = data . userName ;
67
+ //すでにリクエストを送っているか確認
68
+ const request = await requestAddFriend . findOne ( { userid } ) ;
69
+ if ( request == null ) {
70
+ return new Response ( JSON . stringify ( { status : false , message : "Already requested" } ) , {
71
+ headers : { "Content-Type" : "application/json" } ,
72
+ status : 400 ,
73
+ } ) ;
74
+ }
75
+ const isAleredyRequested = request . requestedUser . find ( ( user ) => user . userID === friendId ) ;
76
+ if ( isAleredyRequested ) {
77
+ return new Response ( JSON . stringify ( { status : false , message : "Already requested" } ) , {
78
+ headers : { "Content-Type" : "application/json" } ,
79
+ status : 400 ,
80
+ } ) ;
81
+ }
82
+ //すでに友達か確認
83
+ const friendData = await friends . findOne ( { user : userid } ) ;
84
+ if ( friendData == null ) {
85
+ return new Response ( JSON . stringify ( { status : false , message : "User not found" } ) , {
86
+ headers : { "Content-Type" : "application/json" } ,
87
+ status : 404 ,
88
+ } ) ;
89
+ }
90
+ const isFriend = friendData . friends . find ( ( friend ) => friend . userid === friendId ) ;
91
+ if ( isFriend ) {
92
+ return new Response ( JSON . stringify ( { status : false , message : "Already friend" } ) , {
93
+ headers : { "Content-Type" : "application/json" } ,
94
+ status : 400 ,
95
+ } ) ;
96
+ }
97
+ //リクエストを送る
98
+ const res = await fetch ( `https://${ userDomain } /api/v2/server/friends/request/friend` , {
99
+ method : "POST" ,
100
+ headers : {
101
+ "Content-Type" : "application/json" ,
102
+ } ,
103
+ body : JSON . stringify ( {
104
+ host : env [ "DOMAIN" ] ,
105
+ body : JSON . stringify ( { userid, friendid : friendId } ) ,
106
+ signature : takos . signData ( JSON . stringify ( { userid, friendid : friendId } ) , await takos . getPrivateKey ( ) ) ,
107
+ } ) ,
108
+ } ) ;
109
+ const resData = await res . json ( ) ;
110
+ if ( resData . status === false ) {
111
+ return new Response ( JSON . stringify ( { status : false , message : "Failed to send request" } ) , {
112
+ headers : { "Content-Type" : "application/json" } ,
113
+ status : 500 ,
114
+ } ) ;
115
+ }
116
+ //リクエストを送信したことを記録
117
+ await requestAddFriend . updateOne ( { userid } , { $push : { requestedUser : { userID : friendId } } } ) ;
118
+ return new Response ( JSON . stringify ( { status : true } ) , {
119
+ headers : { "Content-Type" : "application/json" } ,
120
+ } ) ;
121
+ } else {
122
+ const friendInfo = await users . findOne ( { userName : takos . splitUserName ( userName ) . userName } ) ;
123
+ if ( friendInfo === null ) {
124
+ return new Response ( JSON . stringify ( { status : false , message : "User not found" } ) , {
125
+ headers : { "Content-Type" : "application/json" } ,
126
+ status : 404 ,
127
+ } ) ;
128
+ }
129
+ const friendData = await friends . findOne ( { user : userid } ) ;
130
+ if ( friendData == null ) {
131
+ return new Response ( JSON . stringify ( { status : false , message : "User not found" } ) , {
132
+ headers : { "Content-Type" : "application/json" } ,
133
+ status : 404 ,
134
+ } ) ;
135
+ }
136
+ const isFriend = friendData . friends . find ( ( friend ) => friend . userid === friendInfo . uuid ) ;
137
+ if ( isFriend ) {
138
+ return new Response ( JSON . stringify ( { status : false , message : "Already friend" } ) , {
139
+ headers : { "Content-Type" : "application/json" } ,
140
+ status : 400 ,
141
+ } ) ;
142
+ }
143
+ if ( friendData === null ) {
144
+ return new Response ( JSON . stringify ( { status : false , message : "User not found" } ) , {
145
+ headers : { "Content-Type" : "application/json" } ,
146
+ status : 404 ,
147
+ } ) ;
148
+ }
149
+ const isRequested = await requestAddFriend . findOne ( { userid : friendInfo . uuid } ) ;
150
+ if ( isRequested == null ) {
151
+ return new Response ( JSON . stringify ( { status : false , message : "Already requested" } ) , {
152
+ headers : { "Content-Type" : "application/json" } ,
153
+ status : 400 ,
154
+ } ) ;
155
+ }
156
+ const isAleredyRequested = isRequested . friendRequester . find ( ( user ) => user . userID === userid ) ;
157
+ if ( isAleredyRequested ) {
158
+ return new Response ( JSON . stringify ( { status : false , message : "Already requested" } ) , {
159
+ headers : { "Content-Type" : "application/json" } ,
160
+ status : 400 ,
161
+ } ) ;
162
+ }
163
+ //リクエストを送る
164
+ await requestAddFriend . updateOne ( { userid : friendInfo . uuid } , { $push : { friendRequester : { userID : userid } } } ) ;
165
+ await requestAddFriend . updateOne ( { userid } , { $push : { requestedUser : { userID : friendInfo . uuid } } } ) ;
166
+ return new Response ( JSON . stringify ( { status : true } ) , {
167
+ headers : { "Content-Type" : "application/json" } ,
168
+ } ) ;
169
+ }
170
+ } ,
171
+ } ;
172
+ //{ host: string, body: string, signature: string }
0 commit comments