1
0

DirectMessageController.php 34 KB

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