DirectMessageController.php 23 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860
  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. class DirectMessageController extends Controller
  25. {
  26. public function __construct()
  27. {
  28. $this->middleware('auth');
  29. }
  30. public function browse(Request $request)
  31. {
  32. $this->validate($request, [
  33. 'a' => 'nullable|string|in:inbox,sent,filtered',
  34. 'page' => 'nullable|integer|min:1|max:99'
  35. ]);
  36. $profile = $request->user()->profile_id;
  37. $action = $request->input('a', 'inbox');
  38. $page = $request->input('page');
  39. if(config('database.default') == 'pgsql') {
  40. if($action == 'inbox') {
  41. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  42. ->whereToId($profile)
  43. ->with(['author','status'])
  44. ->whereIsHidden(false)
  45. ->when($page, function($q, $page) {
  46. if($page > 1) {
  47. return $q->offset($page * 8 - 8);
  48. }
  49. })
  50. ->latest()
  51. ->get()
  52. ->unique('from_id')
  53. ->take(8)
  54. ->map(function($r) use($profile) {
  55. return $r->from_id !== $profile ? [
  56. 'id' => (string) $r->from_id,
  57. 'name' => $r->author->name,
  58. 'username' => $r->author->username,
  59. 'avatar' => $r->author->avatarUrl(),
  60. 'url' => $r->author->url(),
  61. 'isLocal' => (bool) !$r->author->domain,
  62. 'domain' => $r->author->domain,
  63. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  64. 'lastMessage' => $r->status->caption,
  65. 'messages' => []
  66. ] : [
  67. 'id' => (string) $r->to_id,
  68. 'name' => $r->recipient->name,
  69. 'username' => $r->recipient->username,
  70. 'avatar' => $r->recipient->avatarUrl(),
  71. 'url' => $r->recipient->url(),
  72. 'isLocal' => (bool) !$r->recipient->domain,
  73. 'domain' => $r->recipient->domain,
  74. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  75. 'lastMessage' => $r->status->caption,
  76. 'messages' => []
  77. ];
  78. })->values();
  79. }
  80. if($action == 'sent') {
  81. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  82. ->whereFromId($profile)
  83. ->with(['author','status'])
  84. ->orderBy('id', 'desc')
  85. ->when($page, function($q, $page) {
  86. if($page > 1) {
  87. return $q->offset($page * 8 - 8);
  88. }
  89. })
  90. ->get()
  91. ->unique('to_id')
  92. ->take(8)
  93. ->map(function($r) use($profile) {
  94. return $r->from_id !== $profile ? [
  95. 'id' => (string) $r->from_id,
  96. 'name' => $r->author->name,
  97. 'username' => $r->author->username,
  98. 'avatar' => $r->author->avatarUrl(),
  99. 'url' => $r->author->url(),
  100. 'isLocal' => (bool) !$r->author->domain,
  101. 'domain' => $r->author->domain,
  102. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  103. 'lastMessage' => $r->status->caption,
  104. 'messages' => []
  105. ] : [
  106. 'id' => (string) $r->to_id,
  107. 'name' => $r->recipient->name,
  108. 'username' => $r->recipient->username,
  109. 'avatar' => $r->recipient->avatarUrl(),
  110. 'url' => $r->recipient->url(),
  111. 'isLocal' => (bool) !$r->recipient->domain,
  112. 'domain' => $r->recipient->domain,
  113. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  114. 'lastMessage' => $r->status->caption,
  115. 'messages' => []
  116. ];
  117. });
  118. }
  119. if($action == 'filtered') {
  120. $dms = DirectMessage::select('id', 'type', 'to_id', 'from_id', 'id', 'status_id', 'is_hidden', 'meta', 'created_at', 'read_at')
  121. ->whereToId($profile)
  122. ->with(['author','status'])
  123. ->whereIsHidden(true)
  124. ->orderBy('id', 'desc')
  125. ->when($page, function($q, $page) {
  126. if($page > 1) {
  127. return $q->offset($page * 8 - 8);
  128. }
  129. })
  130. ->get()
  131. ->unique('from_id')
  132. ->take(8)
  133. ->map(function($r) use($profile) {
  134. return $r->from_id !== $profile ? [
  135. 'id' => (string) $r->from_id,
  136. 'name' => $r->author->name,
  137. 'username' => $r->author->username,
  138. 'avatar' => $r->author->avatarUrl(),
  139. 'url' => $r->author->url(),
  140. 'isLocal' => (bool) !$r->author->domain,
  141. 'domain' => $r->author->domain,
  142. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  143. 'lastMessage' => $r->status->caption,
  144. 'messages' => []
  145. ] : [
  146. 'id' => (string) $r->to_id,
  147. 'name' => $r->recipient->name,
  148. 'username' => $r->recipient->username,
  149. 'avatar' => $r->recipient->avatarUrl(),
  150. 'url' => $r->recipient->url(),
  151. 'isLocal' => (bool) !$r->recipient->domain,
  152. 'domain' => $r->recipient->domain,
  153. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  154. 'lastMessage' => $r->status->caption,
  155. 'messages' => []
  156. ];
  157. });
  158. }
  159. } elseif(config('database.default') == 'mysql') {
  160. if($action == 'inbox') {
  161. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  162. ->whereToId($profile)
  163. ->with(['author','status'])
  164. ->whereIsHidden(false)
  165. ->groupBy('from_id')
  166. ->latest()
  167. ->when($page, function($q, $page) {
  168. if($page > 1) {
  169. return $q->offset($page * 8 - 8);
  170. }
  171. })
  172. ->limit(8)
  173. ->get()
  174. ->map(function($r) use($profile) {
  175. return $r->from_id !== $profile ? [
  176. 'id' => (string) $r->from_id,
  177. 'name' => $r->author->name,
  178. 'username' => $r->author->username,
  179. 'avatar' => $r->author->avatarUrl(),
  180. 'url' => $r->author->url(),
  181. 'isLocal' => (bool) !$r->author->domain,
  182. 'domain' => $r->author->domain,
  183. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  184. 'lastMessage' => $r->status->caption,
  185. 'messages' => []
  186. ] : [
  187. 'id' => (string) $r->to_id,
  188. 'name' => $r->recipient->name,
  189. 'username' => $r->recipient->username,
  190. 'avatar' => $r->recipient->avatarUrl(),
  191. 'url' => $r->recipient->url(),
  192. 'isLocal' => (bool) !$r->recipient->domain,
  193. 'domain' => $r->recipient->domain,
  194. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  195. 'lastMessage' => $r->status->caption,
  196. 'messages' => []
  197. ];
  198. });
  199. }
  200. if($action == 'sent') {
  201. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  202. ->whereFromId($profile)
  203. ->with(['author','status'])
  204. ->groupBy('to_id')
  205. ->orderBy('createdAt', 'desc')
  206. ->when($page, function($q, $page) {
  207. if($page > 1) {
  208. return $q->offset($page * 8 - 8);
  209. }
  210. })
  211. ->limit(8)
  212. ->get()
  213. ->map(function($r) use($profile) {
  214. return $r->from_id !== $profile ? [
  215. 'id' => (string) $r->from_id,
  216. 'name' => $r->author->name,
  217. 'username' => $r->author->username,
  218. 'avatar' => $r->author->avatarUrl(),
  219. 'url' => $r->author->url(),
  220. 'isLocal' => (bool) !$r->author->domain,
  221. 'domain' => $r->author->domain,
  222. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  223. 'lastMessage' => $r->status->caption,
  224. 'messages' => []
  225. ] : [
  226. 'id' => (string) $r->to_id,
  227. 'name' => $r->recipient->name,
  228. 'username' => $r->recipient->username,
  229. 'avatar' => $r->recipient->avatarUrl(),
  230. 'url' => $r->recipient->url(),
  231. 'isLocal' => (bool) !$r->recipient->domain,
  232. 'domain' => $r->recipient->domain,
  233. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  234. 'lastMessage' => $r->status->caption,
  235. 'messages' => []
  236. ];
  237. });
  238. }
  239. if($action == 'filtered') {
  240. $dms = DirectMessage::selectRaw('*, max(created_at) as createdAt')
  241. ->whereToId($profile)
  242. ->with(['author','status'])
  243. ->whereIsHidden(true)
  244. ->groupBy('from_id')
  245. ->orderBy('createdAt', 'desc')
  246. ->when($page, function($q, $page) {
  247. if($page > 1) {
  248. return $q->offset($page * 8 - 8);
  249. }
  250. })
  251. ->limit(8)
  252. ->get()
  253. ->map(function($r) use($profile) {
  254. return $r->from_id !== $profile ? [
  255. 'id' => (string) $r->from_id,
  256. 'name' => $r->author->name,
  257. 'username' => $r->author->username,
  258. 'avatar' => $r->author->avatarUrl(),
  259. 'url' => $r->author->url(),
  260. 'isLocal' => (bool) !$r->author->domain,
  261. 'domain' => $r->author->domain,
  262. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  263. 'lastMessage' => $r->status->caption,
  264. 'messages' => []
  265. ] : [
  266. 'id' => (string) $r->to_id,
  267. 'name' => $r->recipient->name,
  268. 'username' => $r->recipient->username,
  269. 'avatar' => $r->recipient->avatarUrl(),
  270. 'url' => $r->recipient->url(),
  271. 'isLocal' => (bool) !$r->recipient->domain,
  272. 'domain' => $r->recipient->domain,
  273. 'timeAgo' => $r->created_at->diffForHumans(null, true, true),
  274. 'lastMessage' => $r->status->caption,
  275. 'messages' => []
  276. ];
  277. });
  278. }
  279. }
  280. return response()->json($dms->all());
  281. }
  282. public function create(Request $request)
  283. {
  284. $this->validate($request, [
  285. 'to_id' => 'required',
  286. 'message' => 'required|string|min:1|max:500',
  287. 'type' => 'required|in:text,emoji'
  288. ]);
  289. $profile = $request->user()->profile;
  290. $recipient = Profile::where('id', '!=', $profile->id)->findOrFail($request->input('to_id'));
  291. abort_if(in_array($profile->id, $recipient->blockedIds()->toArray()), 403);
  292. $msg = $request->input('message');
  293. if((!$recipient->domain && $recipient->user->settings->public_dm == false) || $recipient->is_private) {
  294. if($recipient->follows($profile) == true) {
  295. $hidden = false;
  296. } else {
  297. $hidden = true;
  298. }
  299. } else {
  300. $hidden = false;
  301. }
  302. $status = new Status;
  303. $status->profile_id = $profile->id;
  304. $status->caption = $msg;
  305. $status->rendered = $msg;
  306. $status->visibility = 'direct';
  307. $status->scope = 'direct';
  308. $status->in_reply_to_profile_id = $recipient->id;
  309. $status->save();
  310. $dm = new DirectMessage;
  311. $dm->to_id = $recipient->id;
  312. $dm->from_id = $profile->id;
  313. $dm->status_id = $status->id;
  314. $dm->is_hidden = $hidden;
  315. $dm->type = $request->input('type');
  316. $dm->save();
  317. Conversation::updateOrInsert(
  318. [
  319. 'to_id' => $recipient->id,
  320. 'from_id' => $profile->id
  321. ],
  322. [
  323. 'type' => $dm->type,
  324. 'status_id' => $status->id,
  325. 'dm_id' => $dm->id,
  326. 'is_hidden' => $hidden
  327. ]
  328. );
  329. if(filter_var($msg, FILTER_VALIDATE_URL)) {
  330. if(Helpers::validateUrl($msg)) {
  331. $dm->type = 'link';
  332. $dm->meta = [
  333. 'domain' => parse_url($msg, PHP_URL_HOST),
  334. 'local' => parse_url($msg, PHP_URL_HOST) ==
  335. parse_url(config('app.url'), PHP_URL_HOST)
  336. ];
  337. $dm->save();
  338. }
  339. }
  340. $nf = UserFilter::whereUserId($recipient->id)
  341. ->whereFilterableId($profile->id)
  342. ->whereFilterableType('App\Profile')
  343. ->whereFilterType('dm.mute')
  344. ->exists();
  345. if($recipient->domain == null && $hidden == false && !$nf) {
  346. $notification = new Notification();
  347. $notification->profile_id = $recipient->id;
  348. $notification->actor_id = $profile->id;
  349. $notification->action = 'dm';
  350. $notification->message = $dm->toText();
  351. $notification->rendered = $dm->toHtml();
  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. Helpers::sendSignedObject($profile, $url, $body);
  750. }
  751. public function remoteDelete($dm)
  752. {
  753. $profile = $dm->author;
  754. $url = $dm->recipient->sharedInbox ?? $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. Helpers::sendSignedObject($profile, $url, $body);
  771. }
  772. }