DirectMessageController.php 23 KB

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