1
0

DirectMessageController.php 34 KB

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