BxTimelineResponse.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. <?php defined('BX_DOL') or die('hack attempt');
  2. /**
  3. * Copyright (c) UNA, Inc - https://una.io
  4. * MIT License - https://opensource.org/licenses/MIT
  5. *
  6. * @defgroup Timeline Timeline
  7. * @ingroup UnaModules
  8. *
  9. * @{
  10. */
  11. class BxTimelineResponse extends BxBaseModNotificationsResponse
  12. {
  13. public function __construct()
  14. {
  15. parent::__construct();
  16. $this->_oModule = BxDolModule::getInstance('bx_timeline');
  17. }
  18. /**
  19. * Overwtire the method of parent class.
  20. *
  21. * @param BxDolAlerts $oAlert an instance of alert.
  22. */
  23. public function response($oAlert)
  24. {
  25. $CNF = &$this->_oModule->_oConfig->CNF;
  26. $sMethod = '_process' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  27. if(method_exists($this, $sMethod))
  28. return $this->$sMethod($oAlert);
  29. $iObjectAuthorId = $this->_getObjectOwnerId($oAlert->aExtras);
  30. $iObjectPrivacyView = $this->_getObjectPrivacyView($oAlert->aExtras);
  31. if($iObjectPrivacyView == BX_DOL_PG_HIDDEN)
  32. return;
  33. $aHandler = $this->_oModule->_oConfig->getHandlers($oAlert->sUnit . '_' . $oAlert->sAction);
  34. if(empty($aHandler) || !is_array($aHandler))
  35. return;
  36. $iSilentMode = $this->_oModule->getSilentMode($oAlert->aExtras);
  37. if(in_array($iSilentMode, array(BX_BASE_MOD_NTFS_SLTMODE_ABSOLUTE, BX_TIMELINE_SLTMODE_ABSOLUTE)))
  38. return;
  39. switch($aHandler['type']) {
  40. case BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT:
  41. $iOwnerId = abs($oAlert->iSender);
  42. if($iObjectPrivacyView < 0)
  43. $iOwnerId = abs($iObjectPrivacyView);
  44. $sContent = '';
  45. if(!empty($oAlert->aExtras) && is_array($oAlert->aExtras))
  46. $sContent = serialize(bx_process_input($oAlert->aExtras));
  47. $aEvent = array(
  48. 'owner_id' => $iOwnerId,
  49. 'type' => $oAlert->sUnit,
  50. 'action' => $oAlert->sAction,
  51. 'object_id' => $oAlert->iObject,
  52. 'object_owner_id' => $iObjectAuthorId,
  53. 'object_privacy_view' => $iObjectPrivacyView,
  54. 'content' => $sContent,
  55. 'title' => '',
  56. 'description' => ''
  57. );
  58. $sMethod = '_prepareEvent' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  59. if(method_exists($this, $sMethod))
  60. $this->$sMethod($oAlert, $aEvent);
  61. $iId = $this->_oModule->_oDb->insertEvent($aEvent);
  62. if(!empty($iId))
  63. $this->_oModule->onPost($iId);
  64. break;
  65. case BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE:
  66. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  67. $aEvent = $this->_oModule->_oDb->getEvents(array(
  68. 'browse' => 'descriptor',
  69. 'type' => $oAlert->sUnit,
  70. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  71. 'object_id' => $oAlert->iObject
  72. ));
  73. if(empty($aEvent) || !is_array($aEvent))
  74. break;
  75. $aParamsSet = array(
  76. 'content' => !empty($oAlert->aExtras) && is_array($oAlert->aExtras) ? serialize(bx_process_input($oAlert->aExtras)) : ''
  77. );
  78. if($iObjectPrivacyView > 0 && !empty($iObjectAuthorId) && $iObjectAuthorId == $oAlert->iSender)
  79. $aParamsSet = array_merge($aParamsSet, array(
  80. 'owner_id' => $oAlert->iSender,
  81. 'object_privacy_view' => $iObjectPrivacyView
  82. ));
  83. else if($iObjectPrivacyView < 0)
  84. $aParamsSet = array_merge($aParamsSet, array(
  85. 'owner_id' => abs($iObjectPrivacyView),
  86. 'object_privacy_view' => $iObjectPrivacyView
  87. ));
  88. $this->_oModule->_oDb->updateEvent($aParamsSet, array('id' => $aEvent[$CNF['FIELD_ID']]));
  89. //--- Delete item cache.
  90. $oCacheItem = $this->_oModule->getCacheItemObject();
  91. $oCacheItem->delData($this->_oModule->_oConfig->getCacheItemKey($aEvent['id']));
  92. //--- Delete item cache for repost.
  93. $aReposts = $this->_oModule->_oDb->getReposts($aEvent[$CNF['FIELD_ID']]);
  94. foreach($aReposts as $aRepost)
  95. $oCacheItem->delData($this->_oModule->_oConfig->getCacheItemKey($aRepost['event_id']));
  96. break;
  97. case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
  98. if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
  99. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'owner_id', 'value' => $oAlert->iObject));
  100. foreach($aEvents as $aEvent)
  101. $this->_oModule->deleteEvent($aEvent);
  102. if(!isset($oAlert->aExtras['delete_with_content']) || !$oAlert->aExtras['delete_with_content'])
  103. break;
  104. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'common_by_object', 'value' => $oAlert->iObject));
  105. foreach($aEvents as $aEvent)
  106. $this->_oModule->deleteEvent($aEvent);
  107. break;
  108. }
  109. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  110. $aEvent = $this->_oModule->_oDb->getEvents(array(
  111. 'browse' => 'descriptor',
  112. 'type' => $oAlert->sUnit,
  113. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  114. 'object_id' => $oAlert->iObject
  115. ));
  116. if(empty($aEvent) || !is_array($aEvent))
  117. break;
  118. $this->_oModule->deleteEvent($aEvent);
  119. break;
  120. }
  121. }
  122. protected function _prepareEventCommentAdded($oAlert, &$aEvent)
  123. {
  124. $aEvent = array_merge($aEvent, array(
  125. 'object_id' => $oAlert->aExtras['comment_uniq_id'],
  126. 'object_owner_id' => $oAlert->aExtras['comment_author_id'],
  127. ));
  128. }
  129. protected function _processSystemClearCache($oAlert)
  130. {
  131. if(!in_array($oAlert->aExtras['type'], array('all', 'custom')))
  132. return;
  133. $this->_clearCache();
  134. }
  135. protected function _processSystemEnable($oAlert)
  136. {
  137. $aModuleConfig = $oAlert->aExtras['config'];
  138. if(empty($aModuleConfig) || !is_array($aModuleConfig))
  139. return false;
  140. $sName = $aModuleConfig['name'];
  141. if(!bx_srv_ii('system', 'is_module_context', [$sName]))
  142. return false;
  143. return $this->_oModule->serviceFeedsMenuAdd($sName);
  144. }
  145. protected function _processSystemDisable($oAlert)
  146. {
  147. $aModuleConfig = $oAlert->aExtras['config'];
  148. if(empty($aModuleConfig) || !is_array($aModuleConfig))
  149. return false;
  150. $sName = $aModuleConfig['name'];
  151. if(!bx_srv_ii('system', 'is_module_context', [$sName]))
  152. return false;
  153. return $this->_oModule->serviceFeedsMenuDelete($sName);
  154. }
  155. protected function _processAccountConfirm($oAlert)
  156. {
  157. $this->_clearCache();
  158. }
  159. protected function _processAccountUnconfirm($oAlert)
  160. {
  161. $this->_clearCache();
  162. }
  163. protected function _processProfileApprove($oAlert)
  164. {
  165. $this->_clearCache();
  166. }
  167. protected function _processProfileDisapprove($oAlert)
  168. {
  169. $this->_clearCache();
  170. }
  171. protected function _processProfileActivate($oAlert)
  172. {
  173. $this->_clearCache();
  174. }
  175. protected function _processProfileSuspend($oAlert)
  176. {
  177. $this->_clearCache();
  178. }
  179. protected function _clearCache()
  180. {
  181. //--- Clear item cache.
  182. $this->_oModule->getCacheItemObject()->removeAllByPrefix($this->_oModule->_oConfig->getPrefix('cache_item'));
  183. }
  184. protected function _processBxTimelineVideosMp4Transcoded($oAlert)
  185. {
  186. $this->_onVideoTranscoded($oAlert->iObject, $oAlert->aExtras['ret']);
  187. }
  188. protected function _onVideoTranscoded($iMediaId, $bResult, $aParams = array())
  189. {
  190. $CNF = &$this->_oModule->_oConfig->CNF;
  191. if(!isset($CNF['FIELD_STATUS']))
  192. return;
  193. $aMedia = $this->_oModule->_oDb->getMediaById($CNF['FIELD_VIDEO'], $iMediaId);
  194. if(empty($aMedia) || !is_array($aMedia))
  195. return;
  196. $iContentId = (int)$aMedia['event_id'];
  197. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  198. if(empty($aContentInfo) || !is_array($aContentInfo))
  199. return;
  200. if(!isset($aContentInfo[$CNF['FIELD_STATUS']]) || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  201. return;
  202. $iNow = time();
  203. $bNotify = $iNow - $aContentInfo[$CNF['FIELD_ADDED']] > $this->_oModule->_oConfig->getDpnTime();
  204. $iSystemBotProfileId = (int)getParam('sys_profile_bot');
  205. $iAuthorProfileId = $aContentInfo[$CNF['FIELD_' . ((int)$aContentInfo[$CNF['FIELD_SYSTEM']] == 0 ? 'OBJECT_ID' : 'OWNER_ID')]];
  206. if(!$bResult) {
  207. if((int)$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'failed'), array($CNF['FIELD_ID'] => $iContentId)) > 0) {
  208. $this->_oModule->onFailed($iContentId);
  209. if($bNotify)
  210. bx_alert($this->_oModule->getName(), 'publish_failed', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  211. 'object_author_id' => $iAuthorProfileId,
  212. 'privacy_view' => BX_DOL_PG_ALL
  213. ));
  214. }
  215. return;
  216. }
  217. if(isset($CNF['FIELD_PUBLISHED']) && isset($aContentInfo[$CNF['FIELD_PUBLISHED']]) && $aContentInfo[$CNF['FIELD_PUBLISHED']] > $iNow)
  218. return;
  219. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  220. $aMediasToCheck = $this->_oModule->_oDb->getMedia($CNF['FIELD_VIDEO'], $iContentId);
  221. foreach($aMediasToCheck as $iMediaToCheck)
  222. if(!$oTranscoder->isFileReady($iMediaToCheck))
  223. return;
  224. if(!$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  225. return;
  226. $this->_oModule->onPublished($iContentId);
  227. if($bNotify)
  228. bx_alert($this->_oModule->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  229. 'object_author_id' => $iAuthorProfileId,
  230. 'privacy_view' => BX_DOL_PG_ALL
  231. ));
  232. }
  233. }
  234. /** @} */