BxTimelineResponse.php 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292
  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. 'object_cf' => $this->_getObjectCf($oAlert->aExtras),
  55. 'content' => $sContent,
  56. 'title' => '',
  57. 'description' => ''
  58. );
  59. $sMethod = '_prepareEvent' . bx_gen_method_name($oAlert->sUnit . '_' . $oAlert->sAction);
  60. if(method_exists($this, $sMethod))
  61. $this->$sMethod($oAlert, $aEvent);
  62. $iId = $this->_oModule->_oDb->insertEvent($aEvent);
  63. if(!empty($iId))
  64. $this->_oModule->onPost($iId);
  65. break;
  66. case BX_BASE_MOD_NTFS_HANDLER_TYPE_UPDATE:
  67. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  68. $aEvent = $this->_oModule->_oDb->getEvents(array(
  69. 'browse' => 'descriptor',
  70. 'type' => $oAlert->sUnit,
  71. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  72. 'object_id' => $oAlert->iObject
  73. ));
  74. if(empty($aEvent) || !is_array($aEvent))
  75. break;
  76. $aParamsSet = array(
  77. 'content' => !empty($oAlert->aExtras) && is_array($oAlert->aExtras) ? serialize(bx_process_input($oAlert->aExtras)) : ''
  78. );
  79. if($iObjectPrivacyView > 0 && !empty($iObjectAuthorId) && $iObjectAuthorId == $oAlert->iSender)
  80. $aParamsSet = array_merge($aParamsSet, array(
  81. 'owner_id' => $oAlert->iSender,
  82. 'object_privacy_view' => $iObjectPrivacyView
  83. ));
  84. else if($iObjectPrivacyView < 0)
  85. $aParamsSet = array_merge($aParamsSet, array(
  86. 'owner_id' => abs($iObjectPrivacyView),
  87. 'object_privacy_view' => $iObjectPrivacyView
  88. ));
  89. $this->_oModule->_oDb->updateEvent($aParamsSet, array('id' => $aEvent[$CNF['FIELD_ID']]));
  90. //--- Delete item cache.
  91. $oCacheItem = $this->_oModule->getCacheItemObject();
  92. $oCacheItem->delData($this->_oModule->_oConfig->getCacheItemKey($aEvent['id']));
  93. //--- Delete item cache for repost.
  94. $aReposts = $this->_oModule->_oDb->getReposts($aEvent[$CNF['FIELD_ID']]);
  95. foreach($aReposts as $aRepost)
  96. $oCacheItem->delData($this->_oModule->_oConfig->getCacheItemKey($aRepost['event_id']));
  97. break;
  98. case BX_BASE_MOD_NTFS_HANDLER_TYPE_DELETE:
  99. if($oAlert->sUnit == 'profile' && $oAlert->sAction == 'delete') {
  100. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'owner_id', 'value' => $oAlert->iObject));
  101. foreach($aEvents as $aEvent)
  102. $this->_oModule->deleteEvent($aEvent);
  103. if(!isset($oAlert->aExtras['delete_with_content']) || !$oAlert->aExtras['delete_with_content'])
  104. break;
  105. $aEvents = $this->_oModule->_oDb->getEvents(array('browse' => 'common_by_object', 'value' => $oAlert->iObject));
  106. foreach($aEvents as $aEvent)
  107. $this->_oModule->deleteEvent($aEvent);
  108. break;
  109. }
  110. $aHandlers = $this->_oModule->_oDb->getHandlers(array('type' => 'by_group_key_type', 'group' => $aHandler['group']));
  111. $aEvent = $this->_oModule->_oDb->getEvents(array(
  112. 'browse' => 'descriptor',
  113. 'type' => $oAlert->sUnit,
  114. 'action' => $aHandlers[BX_BASE_MOD_NTFS_HANDLER_TYPE_INSERT]['alert_action'],
  115. 'object_id' => $oAlert->iObject
  116. ));
  117. if(empty($aEvent) || !is_array($aEvent))
  118. break;
  119. $this->_oModule->deleteEvent($aEvent);
  120. break;
  121. }
  122. }
  123. protected function _prepareEventCommentAdded($oAlert, &$aEvent)
  124. {
  125. $aEvent = array_merge($aEvent, array(
  126. 'object_id' => $oAlert->aExtras['comment_uniq_id'],
  127. 'object_owner_id' => $oAlert->aExtras['comment_author_id'],
  128. ));
  129. }
  130. protected function _processSystemClearCache($oAlert)
  131. {
  132. if(!in_array($oAlert->aExtras['type'], array('all', 'custom')))
  133. return;
  134. $this->_clearCache();
  135. }
  136. protected function _processSystemEnable($oAlert)
  137. {
  138. $aModuleConfig = $oAlert->aExtras['config'];
  139. if(empty($aModuleConfig) || !is_array($aModuleConfig))
  140. return false;
  141. $sName = $aModuleConfig['name'];
  142. if(!bx_srv_ii('system', 'is_module_context', [$sName]))
  143. return false;
  144. return $this->_oModule->serviceFeedsMenuAdd($sName);
  145. }
  146. protected function _processSystemDisable($oAlert)
  147. {
  148. $aModuleConfig = $oAlert->aExtras['config'];
  149. if(empty($aModuleConfig) || !is_array($aModuleConfig))
  150. return false;
  151. $sName = $aModuleConfig['name'];
  152. if(!bx_srv_ii('system', 'is_module_context', [$sName]))
  153. return false;
  154. return $this->_oModule->serviceFeedsMenuDelete($sName);
  155. }
  156. protected function _processAccountConfirm($oAlert)
  157. {
  158. $this->_clearCache();
  159. }
  160. protected function _processAccountUnconfirm($oAlert)
  161. {
  162. $this->_clearCache();
  163. }
  164. protected function _processProfileApprove($oAlert)
  165. {
  166. $this->_clearCache();
  167. }
  168. protected function _processProfileDisapprove($oAlert)
  169. {
  170. $this->_clearCache();
  171. }
  172. protected function _processProfileActivate($oAlert)
  173. {
  174. $this->_clearCache();
  175. }
  176. protected function _processProfileSuspend($oAlert)
  177. {
  178. $this->_clearCache();
  179. }
  180. protected function _clearCache()
  181. {
  182. //--- Clear item cache.
  183. $this->_oModule->getCacheItemObject()->removeAllByPrefix($this->_oModule->_oConfig->getPrefix('cache_item'));
  184. }
  185. protected function _processBxTimelineVideosMp4Transcoded($oAlert)
  186. {
  187. $this->_onVideoTranscoded($oAlert->iObject, $oAlert->aExtras['ret']);
  188. }
  189. protected function _onVideoTranscoded($iMediaId, $bResult, $aParams = array())
  190. {
  191. $CNF = &$this->_oModule->_oConfig->CNF;
  192. if(!isset($CNF['FIELD_STATUS']))
  193. return;
  194. $aMedia = $this->_oModule->_oDb->getMediaById($CNF['FIELD_VIDEO'], $iMediaId);
  195. if(empty($aMedia) || !is_array($aMedia))
  196. return;
  197. $iContentId = (int)$aMedia['event_id'];
  198. $aContentInfo = $this->_oModule->_oDb->getContentInfoById($iContentId);
  199. if(empty($aContentInfo) || !is_array($aContentInfo))
  200. return;
  201. if(!isset($aContentInfo[$CNF['FIELD_STATUS']]) || $aContentInfo[$CNF['FIELD_STATUS']] != 'awaiting')
  202. return;
  203. $iNow = time();
  204. $bNotify = $iNow - $aContentInfo[$CNF['FIELD_ADDED']] > $this->_oModule->_oConfig->getDpnTime();
  205. $iSystemBotProfileId = (int)getParam('sys_profile_bot');
  206. $iAuthorProfileId = $aContentInfo[$CNF['FIELD_' . ((int)$aContentInfo[$CNF['FIELD_SYSTEM']] == 0 ? 'OBJECT_ID' : 'OWNER_ID')]];
  207. if(!$bResult) {
  208. if((int)$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'failed'), array($CNF['FIELD_ID'] => $iContentId)) > 0) {
  209. $this->_oModule->onFailed($iContentId);
  210. if($bNotify)
  211. bx_alert($this->_oModule->getName(), 'publish_failed', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  212. 'object_author_id' => $iAuthorProfileId,
  213. 'privacy_view' => BX_DOL_PG_ALL
  214. ));
  215. }
  216. return;
  217. }
  218. if(isset($CNF['FIELD_PUBLISHED']) && isset($aContentInfo[$CNF['FIELD_PUBLISHED']]) && $aContentInfo[$CNF['FIELD_PUBLISHED']] > $iNow)
  219. return;
  220. $oTranscoder = BxDolTranscoder::getObjectInstance($CNF['OBJECT_VIDEOS_TRANSCODERS']['mp4']);
  221. $aMediasToCheck = $this->_oModule->_oDb->getMedia($CNF['FIELD_VIDEO'], $iContentId);
  222. foreach($aMediasToCheck as $iMediaToCheck)
  223. if(!$oTranscoder->isFileReady($iMediaToCheck))
  224. return;
  225. if(!$this->_oModule->_oDb->updateEntriesBy(array($CNF['FIELD_STATUS'] => 'active'), array($CNF['FIELD_ID'] => $iContentId)))
  226. return;
  227. $this->_oModule->onPublished($iContentId);
  228. if($bNotify)
  229. bx_alert($this->_oModule->getName(), 'publish_succeeded', $aContentInfo[$CNF['FIELD_ID']], $iSystemBotProfileId, array(
  230. 'object_author_id' => $iAuthorProfileId,
  231. 'privacy_view' => BX_DOL_PG_ALL
  232. ));
  233. }
  234. }
  235. /** @} */