DirectMessageController.php 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886
  1. <?php
  2. namespace App\Http\Controllers;
  3. use Auth, Cache;
  4. use Illuminate\Http\Request;
  5. use App\{
  6. DirectMessage,
  7. Media,
  8. Notification,
  9. Profile,
  10. Status,
  11. User,
  12. UserFilter,
  13. UserSetting
  14. };
  15. use App\Services\MediaPathService;
  16. use App\Services\MediaBlocklistService;
  17. use App\Jobs\StatusPipeline\NewStatusPipeline;
  18. use App\Jobs\StatusPipeline\StatusDelete;
  19. use Illuminate\Support\Str;
  20. use App\Util\ActivityPub\Helpers;
  21. use App\Services\AccountService;
  22. use App\Services\StatusService;
  23. use App\Services\WebfingerService;
  24. use App\Models\Conversation;
  25. use App\Jobs\DirectPipeline\DirectDeletePipeline;
  26. use App\Jobs\DirectPipeline\DirectDeliverPipeline;
  27. use App\Services\UserRoleService;
  28. class DirectMessageController extends Controller
  29. {
  30. public function __construct()
  31. {
  32. $this->middleware('auth');
  33. }
  34. public function browse(Request $request)
  35. {
  36. $this->validate($request, [
  37. 'a' => 'nullable|string|in:inbox,sent,filtered',
  38. 'page' => 'nullable|integer|min:1|max:99'
  39. ]);
  40. $user = $request->user();
  41. if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id)) {
  42. return [];
  43. }
  44. $profile = $user->profile_id;
  45. $action = $request->input('a', 'inbox');
  46. $page = $request->input('page');
  47. if(config('database.default') == 'pgsql') {
  48. if($action == 'inbox') {
  49. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  50. ->whereToId($profile)
  51. ->with(['author','status'])
  52. ->whereIsHidden(false)
  53. ->when($page, function($q, $page) {
  54. if($page > 1) {
  55. return $q->offset($page * 8 - 8);
  56. }
  57. })
  58. ->latest()
  59. ->get()
  60. ->unique('from_id')
  61. ->take(8)
  62. ->map(function($r) use($profile) {
  63. return $r->from_id !== $profile ? [
  64. 'id' => (string) $r->from_id,
  65. 'name' => $r->author->name,
  66. 'username' => $r->author->username,
  67. 'avatar' => $r->author->avatarUrl(),
  68. 'url' => $r->author->url(),
  69. 'isLocal' => (bool) !$r->author->domain,
  70. 'domain' => $r->author->domain,
  71. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  72. 'lastMessage' => $r->status->caption,
  73. 'messages' => []
  74. ] : [
  75. 'id' => (string) $r->to_id,
  76. 'name' => $r->recipient->name,
  77. 'username' => $r->recipient->username,
  78. 'avatar' => $r->recipient->avatarUrl(),
  79. 'url' => $r->recipient->url(),
  80. 'isLocal' => (bool) !$r->recipient->domain,
  81. 'domain' => $r->recipient->domain,
  82. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  83. 'lastMessage' => $r->status->caption,
  84. 'messages' => []
  85. ];
  86. })->values();
  87. }
  88. if($action == 'sent') {
  89. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  90. ->whereFromId($profile)
  91. ->with(['author','status'])
  92. ->orderBy('id', 'desc')
  93. ->when($page, function($q, $page) {
  94. if($page > 1) {
  95. return $q->offset($page * 8 - 8);
  96. }
  97. })
  98. ->get()
  99. ->unique('to_id')
  100. ->take(8)
  101. ->map(function($r) use($profile) {
  102. return $r->from_id !== $profile ? [
  103. 'id' => (string) $r->from_id,
  104. 'name' => $r->author->name,
  105. 'username' => $r->author->username,
  106. 'avatar' => $r->author->avatarUrl(),
  107. 'url' => $r->author->url(),
  108. 'isLocal' => (bool) !$r->author->domain,
  109. 'domain' => $r->author->domain,
  110. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  111. 'lastMessage' => $r->status->caption,
  112. 'messages' => []
  113. ] : [
  114. 'id' => (string) $r->to_id,
  115. 'name' => $r->recipient->name,
  116. 'username' => $r->recipient->username,
  117. 'avatar' => $r->recipient->avatarUrl(),
  118. 'url' => $r->recipient->url(),
  119. 'isLocal' => (bool) !$r->recipient->domain,
  120. 'domain' => $r->recipient->domain,
  121. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  122. 'lastMessage' => $r->status->caption,
  123. 'messages' => []
  124. ];
  125. });
  126. }
  127. if($action == 'filtered') {
  128. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  129. ->whereToId($profile)
  130. ->with(['author','status'])
  131. ->whereIsHidden(true)
  132. ->orderBy('id', 'desc')
  133. ->when($page, function($q, $page) {
  134. if($page > 1) {
  135. return $q->offset($page * 8 - 8);
  136. }
  137. })
  138. ->get()
  139. ->unique('from_id')
  140. ->take(8)
  141. ->map(function($r) use($profile) {
  142. return $r->from_id !== $profile ? [
  143. 'id' => (string) $r->from_id,
  144. 'name' => $r->author->name,
  145. 'username' => $r->author->username,
  146. 'avatar' => $r->author->avatarUrl(),
  147. 'url' => $r->author->url(),
  148. 'isLocal' => (bool) !$r->author->domain,
  149. 'domain' => $r->author->domain,
  150. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  151. 'lastMessage' => $r->status->caption,
  152. 'messages' => []
  153. ] : [
  154. 'id' => (string) $r->to_id,
  155. 'name' => $r->recipient->name,
  156. 'username' => $r->recipient->username,
  157. 'avatar' => $r->recipient->avatarUrl(),
  158. 'url' => $r->recipient->url(),
  159. 'isLocal' => (bool) !$r->recipient->domain,
  160. 'domain' => $r->recipient->domain,
  161. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  162. 'lastMessage' => $r->status->caption,
  163. 'messages' => []
  164. ];
  165. });
  166. }
  167. } elseif(config('database.default') == 'mysql') {
  168. if($action == 'inbox') {
  169. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  170. ->whereToId($profile)
  171. ->with(['author','status'])
  172. ->whereIsHidden(false)
  173. ->groupBy('from_id')
  174. ->latest()
  175. ->when($page, function($q, $page) {
  176. if($page > 1) {
  177. return $q->offset($page * 8 - 8);
  178. }
  179. })
  180. ->limit(8)
  181. ->get()
  182. ->map(function($r) use($profile) {
  183. return $r->from_id !== $profile ? [
  184. 'id' => (string) $r->from_id,
  185. 'name' => $r->author->name,
  186. 'username' => $r->author->username,
  187. 'avatar' => $r->author->avatarUrl(),
  188. 'url' => $r->author->url(),
  189. 'isLocal' => (bool) !$r->author->domain,
  190. 'domain' => $r->author->domain,
  191. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  192. 'lastMessage' => $r->status->caption,
  193. 'messages' => []
  194. ] : [
  195. 'id' => (string) $r->to_id,
  196. 'name' => $r->recipient->name,
  197. 'username' => $r->recipient->username,
  198. 'avatar' => $r->recipient->avatarUrl(),
  199. 'url' => $r->recipient->url(),
  200. 'isLocal' => (bool) !$r->recipient->domain,
  201. 'domain' => $r->recipient->domain,
  202. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  203. 'lastMessage' => $r->status->caption,
  204. 'messages' => []
  205. ];
  206. });
  207. }
  208. if($action == 'sent') {
  209. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  210. ->whereFromId($profile)
  211. ->with(['author','status'])
  212. ->groupBy('to_id')
  213. ->orderBy('createdAt', 'desc')
  214. ->when($page, function($q, $page) {
  215. if($page > 1) {
  216. return $q->offset($page * 8 - 8);
  217. }
  218. })
  219. ->limit(8)
  220. ->get()
  221. ->map(function($r) use($profile) {
  222. return $r->from_id !== $profile ? [
  223. 'id' => (string) $r->from_id,
  224. 'name' => $r->author->name,
  225. 'username' => $r->author->username,
  226. 'avatar' => $r->author->avatarUrl(),
  227. 'url' => $r->author->url(),
  228. 'isLocal' => (bool) !$r->author->domain,
  229. 'domain' => $r->author->domain,
  230. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  231. 'lastMessage' => $r->status->caption,
  232. 'messages' => []
  233. ] : [
  234. 'id' => (string) $r->to_id,
  235. 'name' => $r->recipient->name,
  236. 'username' => $r->recipient->username,
  237. 'avatar' => $r->recipient->avatarUrl(),
  238. 'url' => $r->recipient->url(),
  239. 'isLocal' => (bool) !$r->recipient->domain,
  240. 'domain' => $r->recipient->domain,
  241. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  242. 'lastMessage' => $r->status->caption,
  243. 'messages' => []
  244. ];
  245. });
  246. }
  247. if($action == 'filtered') {
  248. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  249. ->whereToId($profile)
  250. ->with(['author','status'])
  251. ->whereIsHidden(true)
  252. ->groupBy('from_id')
  253. ->orderBy('createdAt', 'desc')
  254. ->when($page, function($q, $page) {
  255. if($page > 1) {
  256. return $q->offset($page * 8 - 8);
  257. }
  258. })
  259. ->limit(8)
  260. ->get()
  261. ->map(function($r) use($profile) {
  262. return $r->from_id !== $profile ? [
  263. 'id' => (string) $r->from_id,
  264. 'name' => $r->author->name,
  265. 'username' => $r->author->username,
  266. 'avatar' => $r->author->avatarUrl(),
  267. 'url' => $r->author->url(),
  268. 'isLocal' => (bool) !$r->author->domain,
  269. 'domain' => $r->author->domain,
  270. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  271. 'lastMessage' => $r->status->caption,
  272. 'messages' => []
  273. ] : [
  274. 'id' => (string) $r->to_id,
  275. 'name' => $r->recipient->name,
  276. 'username' => $r->recipient->username,
  277. 'avatar' => $r->recipient->avatarUrl(),
  278. 'url' => $r->recipient->url(),
  279. 'isLocal' => (bool) !$r->recipient->domain,
  280. 'domain' => $r->recipient->domain,
  281. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  282. 'lastMessage' => $r->status->caption,
  283. 'messages' => []
  284. ];
  285. });
  286. }
  287. }
  288. return response()->json($dms->all());
  289. }
  290. public function create(Request $request)
  291. {
  292. $this->validate($request, [
  293. 'to_id' => 'required',
  294. 'message' => 'required|string|min:1|max:500',
  295. 'type' => 'required|in:text,emoji'
  296. ]);
  297. $user = $request->user();
  298. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  299. $profile = $user->profile;
  300. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  301. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  302. $msg = $request->input('message');
  303. if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  304. if($recipient->follows($profile) == true) {
  305. $hidden = false;
  306. } else {
  307. $hidden = true;
  308. }
  309. } else {
  310. $hidden = false;
  311. }
  312. $status = new Status;
  313. $status->profile_id = $profile->id;
  314. $status->caption = $msg;
  315. $status->rendered = $msg;
  316. $status->visibility = 'direct';
  317. $status->scope = 'direct';
  318. $status->in_reply_to_profile_id = $recipient->id;
  319. $status->save();
  320. $dm = new DirectMessage;
  321. $dm->to_id = $recipient->id;
  322. $dm->from_id = $profile->id;
  323. $dm->status_id = $status->id;
  324. $dm->is_hidden = $hidden;
  325. $dm->type = $request->input('type');
  326. $dm->save();
  327. Conversation::updateOrInsert(
  328. [
  329. 'to_id' => $recipient->id,
  330. 'from_id' => $profile->id
  331. ],
  332. [
  333. 'type' => $dm->type,
  334. 'status_id' => $status->id,
  335. 'dm_id' => $dm->id,
  336. 'is_hidden' => $hidden
  337. ]
  338. );
  339. if(filter_var($msg, FILTER_VALIDATE_URL)) {
  340. if(Helpers::validateUrl($msg)) {
  341. $dm->type = 'link';
  342. $dm->meta = [
  343. 'domain' => parse_url($msg, PHP_URL_HOST),
  344. 'local' => parse_url($msg, PHP_URL_HOST) ==
  345. parse_url(config('app.url'), PHP_URL_HOST)
  346. ];
  347. $dm->save();
  348. }
  349. }
  350. $nf = UserFilter::whereUserId($recipient->id)
  351. ->whereFilterableId($profile->id)
  352. ->whereFilterableType('App\Profile')
  353. ->whereFilterType('dm.mute')
  354. ->exists();
  355. if($recipient->domain == null && $hidden == false && !$nf) {
  356. $notification = new Notification();
  357. $notification->profile_id = $recipient->id;
  358. $notification->actor_id = $profile->id;
  359. $notification->action = 'dm';
  360. $notification->item_id = $dm->id;
  361. $notification->item_type = "App\DirectMessage";
  362. $notification->save();
  363. }
  364. if($recipient->domain) {
  365. $this->remoteDeliver($dm);
  366. }
  367. $res = [
  368. 'id' => (string) $dm->id,
  369. 'isAuthor' => $profile->id == $dm->from_id,
  370. 'reportId' => (string) $dm->status_id,
  371. 'hidden' => (bool) $dm->is_hidden,
  372. 'type' => $dm->type,
  373. 'text' => $dm->status->caption,
  374. 'media' => null,
  375. 'timeAgo' => $dm->created_at->diffForHumans(null,null,true),
  376. 'seen' => $dm->read_at != null,
  377. 'meta' => $dm->meta
  378. ];
  379. return response()->json($res);
  380. }
  381. public function thread(Request $request)
  382. {
  383. $this->validate($request, [
  384. 'pid' => 'required'
  385. ]);
  386. $user = $request->user();
  387. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  388. $uid = $user->profile_id;
  389. $pid = $request->input('pid');
  390. $max_id = $request->input('max_id');
  391. $min_id = $request->input('min_id');
  392. $r = Profile::findOrFail($pid);
  393. if($min_id) {
  394. $res = DirectMessage::select('*')
  395. ->where('id', '>', $min_id)
  396. ->where(function($q) use($pid,$uid) {
  397. return $q->where([['from_id',$pid],['to_id',$uid]
  398. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  399. })
  400. ->latest()
  401. ->take(8)
  402. ->get();
  403. } else if ($max_id) {
  404. $res = DirectMessage::select('*')
  405. ->where('id', '<', $max_id)
  406. ->where(function($q) use($pid,$uid) {
  407. return $q->where([['from_id',$pid],['to_id',$uid]
  408. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  409. })
  410. ->latest()
  411. ->take(8)
  412. ->get();
  413. } else {
  414. $res = DirectMessage::where(function($q) use($pid,$uid) {
  415. return $q->where([['from_id',$pid],['to_id',$uid]
  416. ])->orWhere([['from_id',$uid],['to_id',$pid]]);
  417. })
  418. ->latest()
  419. ->take(8)
  420. ->get();
  421. }
  422. $res = $res->filter(function($s) {
  423. return $s && $s->status;
  424. })
  425. ->map(function($s) use ($uid) {
  426. return [
  427. 'id' => (string) $s->id,
  428. 'hidden' => (bool) $s->is_hidden,
  429. 'isAuthor' => $uid == $s->from_id,
  430. 'type' => $s->type,
  431. 'text' => $s->status->caption,
  432. 'media' => $s->status->firstMedia() ? $s->status->firstMedia()->url() : null,
  433. 'timeAgo' => $s->created_at->diffForHumans(null,null,true),
  434. 'seen' => $s->read_at != null,
  435. 'reportId' => (string) $s->status_id,
  436. 'meta' => json_decode($s->meta,true)
  437. ];
  438. })
  439. ->values();
  440. $w = [
  441. 'id' => (string) $r->id,
  442. 'name' => $r->name,
  443. 'username' => $r->username,
  444. 'avatar' => $r->avatarUrl(),
  445. 'url' => $r->url(),
  446. 'muted' => UserFilter::whereUserId($uid)
  447. ->whereFilterableId($r->id)
  448. ->whereFilterableType('App\Profile')
  449. ->whereFilterType('dm.mute')
  450. ->first() ? true : false,
  451. 'isLocal' => (bool) !$r->domain,
  452. 'domain' => $r->domain,
  453. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  454. 'lastMessage' => '',
  455. 'messages' => $res
  456. ];
  457. return response()->json($w, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  458. }
  459. public function delete(Request $request)
  460. {
  461. $this->validate($request, [
  462. 'id' => 'required'
  463. ]);
  464. $sid = $request->input('id');
  465. $pid = $request->user()->profile_id;
  466. $dm = DirectMessage::whereFromId($pid)
  467. ->whereStatusId($sid)
  468. ->firstOrFail();
  469. $status = Status::whereProfileId($pid)
  470. ->findOrFail($dm->status_id);
  471. $recipient = AccountService::get($dm->to_id);
  472. if(!$recipient) {
  473. return response('', 422);
  474. }
  475. if($recipient['local'] == false) {
  476. $dmc = $dm;
  477. $this->remoteDelete($dmc);
  478. } else {
  479. StatusDelete::dispatch($status)->onQueue('high');
  480. }
  481. if(Conversation::whereStatusId($sid)->count()) {
  482. $latest = DirectMessage::where(['from_id' => $dm->from_id, 'to_id' => $dm->to_id])
  483. ->orWhere(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
  484. ->latest()
  485. ->first();
  486. if($latest->status_id == $sid) {
  487. Conversation::where(['to_id' => $dm->from_id, 'from_id' => $dm->to_id])
  488. ->update([
  489. 'updated_at' => $latest->updated_at,
  490. 'status_id' => $latest->status_id,
  491. 'type' => $latest->type,
  492. 'is_hidden' => false
  493. ]);
  494. Conversation::where(['to_id' => $dm->to_id, 'from_id' => $dm->from_id])
  495. ->update([
  496. 'updated_at' => $latest->updated_at,
  497. 'status_id' => $latest->status_id,
  498. 'type' => $latest->type,
  499. 'is_hidden' => false
  500. ]);
  501. } else {
  502. Conversation::where([
  503. 'status_id' => $sid,
  504. 'to_id' => $dm->from_id,
  505. 'from_id' => $dm->to_id
  506. ])->delete();
  507. Conversation::where([
  508. 'status_id' => $sid,
  509. 'from_id' => $dm->from_id,
  510. 'to_id' => $dm->to_id
  511. ])->delete();
  512. }
  513. }
  514. StatusService::del($status->id, true);
  515. $status->forceDeleteQuietly();
  516. return [200];
  517. }
  518. public function get(Request $request, $id)
  519. {
  520. $user = $request->user();
  521. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  522. $pid = $request->user()->profile_id;
  523. $dm = DirectMessage::whereStatusId($id)->firstOrFail();
  524. abort_if($pid !== $dm->to_id && $pid !== $dm->from_id, 404);
  525. return response()->json($dm, 200, [], JSON_PRETTY_PRINT|JSON_UNESCAPED_SLASHES);
  526. }
  527. public function mediaUpload(Request $request)
  528. {
  529. $this->validate($request, [
  530. 'file' => function() {
  531. return [
  532. 'required',
  533. 'mimetypes:' . config_cache('pixelfed.media_types'),
  534. 'max:' . config_cache('pixelfed.max_photo_size'),
  535. ];
  536. },
  537. 'to_id' => 'required'
  538. ]);
  539. $user = $request->user();
  540. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  541. $profile = $user->profile;
  542. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  543. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  544. if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  545. if($recipient->follows($profile) == true) {
  546. $hidden = false;
  547. } else {
  548. $hidden = true;
  549. }
  550. } else {
  551. $hidden = false;
  552. }
  553. if(config_cache('pixelfed.enforce_account_limit') == true) {
  554. $size = Cache::remember($user->storageUsedKey(), now()->addDays(3), function() use($user) {
  555. return Media::whereUserId($user->id)->sum('size') / 1000;
  556. });
  557. $limit = (int) config_cache('pixelfed.max_account_size');
  558. if ($size >= $limit) {
  559. abort(403, 'Account size limit reached.');
  560. }
  561. }
  562. $photo = $request->file('file');
  563. $mimes = explode(',', config_cache('pixelfed.media_types'));
  564. if(in_array($photo->getMimeType(), $mimes) == false) {
  565. abort(403, 'Invalid or unsupported mime type.');
  566. }
  567. $storagePath = MediaPathService::get($user, 2) . Str::random(8);
  568. $path = $photo->storePublicly($storagePath);
  569. $hash = \hash_file('sha256', $photo);
  570. abort_if(MediaBlocklistService::exists($hash) == true, 451);
  571. $status = new Status;
  572. $status->profile_id = $profile->id;
  573. $status->caption = null;
  574. $status->rendered = null;
  575. $status->visibility = 'direct';
  576. $status->scope = 'direct';
  577. $status->in_reply_to_profile_id = $recipient->id;
  578. $status->save();
  579. $media = new Media();
  580. $media->status_id = $status->id;
  581. $media->profile_id = $profile->id;
  582. $media->user_id = $user->id;
  583. $media->media_path = $path;
  584. $media->original_sha256 = $hash;
  585. $media->size = $photo->getSize();
  586. $media->mime = $photo->getMimeType();
  587. $media->caption = null;
  588. $media->filter_class = null;
  589. $media->filter_name = null;
  590. $media->save();
  591. $dm = new DirectMessage;
  592. $dm->to_id = $recipient->id;
  593. $dm->from_id = $profile->id;
  594. $dm->status_id = $status->id;
  595. $dm->type = array_first(explode('/', $media->mime)) == 'video' ? 'video' : 'photo';
  596. $dm->is_hidden = $hidden;
  597. $dm->save();
  598. Conversation::updateOrInsert(
  599. [
  600. 'to_id' => $recipient->id,
  601. 'from_id' => $profile->id
  602. ],
  603. [
  604. 'type' => $dm->type,
  605. 'status_id' => $status->id,
  606. 'dm_id' => $dm->id,
  607. 'is_hidden' => $hidden
  608. ]
  609. );
  610. if($recipient->domain) {
  611. $this->remoteDeliver($dm);
  612. }
  613. return [
  614. 'id' => $dm->id,
  615. 'reportId' => (string) $dm->status_id,
  616. 'type' => $dm->type,
  617. 'url' => $media->url()
  618. ];
  619. }
  620. public function composeLookup(Request $request)
  621. {
  622. $this->validate($request, [
  623. 'q' => 'required|string|min:2|max:50',
  624. 'remote' => 'nullable',
  625. ]);
  626. $user = $request->user();
  627. if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id)) {
  628. return [];
  629. }
  630. $q = $request->input('q');
  631. $r = $request->input('remote', false);
  632. if($r && !Str::of($q)->contains('.')) {
  633. return [];
  634. }
  635. if($r && Helpers::validateUrl($q)) {
  636. Helpers::profileFetch($q);
  637. }
  638. if(Str::of($q)->startsWith('@')) {
  639. if(strlen($q) < 3) {
  640. return [];
  641. }
  642. if(substr_count($q, '@') == 2) {
  643. WebfingerService::lookup($q);
  644. }
  645. $q = mb_substr($q, 1);
  646. }
  647. $blocked = UserFilter::whereFilterableType('App\Profile')
  648. ->whereFilterType('block')
  649. ->whereFilterableId($request->user()->profile_id)
  650. ->pluck('user_id');
  651. $blocked->push($request->user()->profile_id);
  652. $results = Profile::select('id','domain','username')
  653. ->whereNotIn('id', $blocked)
  654. ->where('username','like','%'.$q.'%')
  655. ->orderBy('domain')
  656. ->limit(8)
  657. ->get()
  658. ->map(function($r) {
  659. $acct = AccountService::get($r->id);
  660. return [
  661. 'local' => (bool) !$r->domain,
  662. 'id' => (string) $r->id,
  663. 'name' => $r->username,
  664. 'privacy' => true,
  665. 'avatar' => $r->avatarUrl(),
  666. 'account' => $acct
  667. ];
  668. });
  669. return $results;
  670. }
  671. public function read(Request $request)
  672. {
  673. $this->validate($request, [
  674. 'pid' => 'required',
  675. 'sid' => 'required'
  676. ]);
  677. $pid = $request->input('pid');
  678. $sid = $request->input('sid');
  679. $user = $request->user();
  680. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  681. $dms = DirectMessage::whereToId($request->user()->profile_id)
  682. ->whereFromId($pid)
  683. ->where('status_id', '>=', $sid)
  684. ->get();
  685. $now = now();
  686. foreach($dms as $dm) {
  687. $dm->read_at = $now;
  688. $dm->save();
  689. }
  690. return response()->json($dms->pluck('id'));
  691. }
  692. public function mute(Request $request)
  693. {
  694. $this->validate($request, [
  695. 'id' => 'required'
  696. ]);
  697. $user = $request->user();
  698. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  699. $fid = $request->input('id');
  700. $pid = $request->user()->profile_id;
  701. UserFilter::firstOrCreate(
  702. [
  703. 'user_id' => $pid,
  704. 'filterable_id' => $fid,
  705. 'filterable_type' => 'App\Profile',
  706. 'filter_type' => 'dm.mute'
  707. ]
  708. );
  709. return [200];
  710. }
  711. public function unmute(Request $request)
  712. {
  713. $this->validate($request, [
  714. 'id' => 'required'
  715. ]);
  716. $user = $request->user();
  717. abort_if($user->has_roles && !UserRoleService::can('can-direct-message', $user->id), 403, 'Invalid permissions for this action');
  718. $fid = $request->input('id');
  719. $pid = $request->user()->profile_id;
  720. $f = UserFilter::whereUserId($pid)
  721. ->whereFilterableId($fid)
  722. ->whereFilterableType('App\Profile')
  723. ->whereFilterType('dm.mute')
  724. ->firstOrFail();
  725. $f->delete();
  726. return [200];
  727. }
  728. public function remoteDeliver($dm)
  729. {
  730. $profile = $dm->author;
  731. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  732. $tags = [
  733. [
  734. 'type' => 'Mention',
  735. 'href' => $dm->recipient->permalink(),
  736. 'name' => $dm->recipient->emailUrl(),
  737. ]
  738. ];
  739. $body = [
  740. '@context' => [
  741. 'https://w3id.org/security/v1',
  742. 'https://www.w3.org/ns/activitystreams',
  743. ],
  744. 'id' => $dm->status->permalink(),
  745. 'type' => 'Create',
  746. 'actor' => $dm->status->profile->permalink(),
  747. 'published' => $dm->status->created_at->toAtomString(),
  748. 'to' => [$dm->recipient->permalink()],
  749. 'cc' => [],
  750. 'object' => [
  751. 'id' => $dm->status->url(),
  752. 'type' => 'Note',
  753. 'summary' => null,
  754. 'content' => $dm->status->rendered ?? $dm->status->caption,
  755. 'inReplyTo' => null,
  756. 'published' => $dm->status->created_at->toAtomString(),
  757. 'url' => $dm->status->url(),
  758. 'attributedTo' => $dm->status->profile->permalink(),
  759. 'to' => [$dm->recipient->permalink()],
  760. 'cc' => [],
  761. 'sensitive' => (bool) $dm->status->is_nsfw,
  762. 'attachment' => $dm->status->media()->orderBy('order')->get()->map(function ($media) {
  763. return [
  764. 'type' => $media->activityVerb(),
  765. 'mediaType' => $media->mime,
  766. 'url' => $media->url(),
  767. 'name' => $media->caption,
  768. ];
  769. })->toArray(),
  770. 'tag' => $tags,
  771. ]
  772. ];
  773. DirectDeliverPipeline::dispatch($profile, $url, $body)->onQueue('high');
  774. }
  775. public function remoteDelete($dm)
  776. {
  777. $profile = $dm->author;
  778. $url = $dm->recipient->sharedInbox ?? $dm->recipient->inbox_url;
  779. $body = [
  780. '@context' => [
  781. 'https://www.w3.org/ns/activitystreams',
  782. ],
  783. 'id' => $dm->status->permalink('#delete'),
  784. 'to' => [
  785. 'https://www.w3.org/ns/activitystreams#Public'
  786. ],
  787. 'type' => 'Delete',
  788. 'actor' => $dm->status->profile->permalink(),
  789. 'object' => [
  790. 'id' => $dm->status->url(),
  791. 'type' => 'Tombstone'
  792. ]
  793. ];
  794. DirectDeletePipeline::dispatch($profile, $url, $body)->onQueue('high');
  795. }
  796. }