Function createPost improvement

dorje

  • Krabi krabong: the wedge of martial arts. :P
  • Posts: 97
Function createPost improvement
« on May 12th, 2011, 06:41 AM »
Well, this is quite a small thing, but important (I think).

In smf there is the createPost function that creates a post and puts the post ID inside the first array passed as parameter. But it doesn't show the topic ID, useful in some cases.

There will be quite simple to modify the function not to return a boolean value, but 0 in case of errors and the topic ID if TRUE.

What do you think?

(off course, I supposed that createPost is still there... If not, delete that post! :ph34r:)
Krabi Krabong & programming :D

live627

  • Should five per cent appear too small / Be thankful I don't take it all / 'Cause I'm the taxman, yeah I'm the taxman
  • Posts: 1,670
Re: Function createPost improvement
« Reply #1, on May 12th, 2011, 07:09 AM »
I don't get why it would return the topic ID? Seems illogical that a function for creating a post would return a topic ID ESPECIALLY when its primary use is to make a new post. I think I'm repeating myself here...

A confident man keeps quiet.whereas a frightened man keeps talking, hiding his fear.

dorje

  • Krabi krabong: the wedge of martial arts. :P
  • Posts: 97
Re: Function createPost improvement
« Reply #2, on May 12th, 2011, 07:24 AM »
Quote from live627 on May 12th, 2011, 07:09 AM
I don't get why it would return the topic ID? Seems illogical that a function for creating a post would return a topic ID ESPECIALLY when its primary use is to make a new post. I think I'm repeating myself here...
The post ID is already stored in the first argument, the board ID is already known... So the only thing we miss is the topic ID. Why is important? Let me show an example (I've expereinced that a couple of days ago)...

I've just ended to implement a system in which a user inserts a new topic in a particular section using the custom form mod. The system than sends a PM (or mail), and I wanted to write the link of the newly started topic... But I miss the topic ID as argument to create that link, the only way is to do a query on smf_messages and select the topic ID. But that means another query, so I guess if using the return of the createPost function can be a good idea, as that change will not affect uses where we expect a boolean value. :)

Nao

  • Dadman with a boy
  • Posts: 16,079

dorje

  • Krabi krabong: the wedge of martial arts. :P
  • Posts: 97
Re: Function createPost improvement
« Reply #4, on May 12th, 2011, 09:21 AM »
Quote from Nao/Gilles on May 12th, 2011, 08:56 AM
Technically, that's just one extra query per topic created. Right? Isn't that negligible?
Nope. The function already stores the id in $topicOptions['id']. It just doesn't return it, but is already there...

Nao

  • Dadman with a boy
  • Posts: 16,079
Re: Function createPost improvement
« Reply #5, on May 12th, 2011, 10:14 AM »
I see what you mean, but is it useful to return it?
We might as well return $topicOptions entirely, then... Or whatever.

dorje

  • Krabi krabong: the wedge of martial arts. :P
  • Posts: 97
Re: Function createPost improvement
« Reply #6, on May 12th, 2011, 10:22 AM »
Quote from Nao/Gilles on May 12th, 2011, 10:14 AM
I see what you mean, but is it useful to return it?
Well, I found a situation in which it could be useful. My question is: the value is there so... Why return only true or false? If I return false in error case and an integer if everything went ok, all routines that uses that function will work perfectly in any case. :)
Quote from Nao/Gilles on May 12th, 2011, 10:14 AM
We might as well return $topicOptions entirely, then... Or whatever.
Also, but than I don't know if maybe that change will affect other calls to that function. :)


Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Function createPost improvement
« Reply #7, on May 12th, 2011, 10:29 AM »
You know that you pass everything in by reference, right?

Check $topicOptions['id'] on return -- even in SMF. It HAS to know the topic id in any case, if it's a reply, you provide it, even in a new topic, you have to have it to tie the new message to the new topic.

So, not sure what you're asking for that it doesn't already do...
When we unite against a common enemy that attacks our ethos, it nurtures group solidarity. Trolls are sensational, yes, but we keep everyone honest. | Game Memorial

Nao

  • Dadman with a boy
  • Posts: 16,079
Re: Function createPost improvement
« Reply #8, on May 12th, 2011, 10:31 AM »
I'm too lazy to look into the code right now :P
Still, I added a create_post_after hook in Wedge, and IIRC it has all of the options in it.

dorje

  • Krabi krabong: the wedge of martial arts. :P
  • Posts: 97
Re: Function createPost improvement
« Reply #9, on May 12th, 2011, 11:01 AM »
Quote from Arantor on May 12th, 2011, 10:29 AM
You know that you pass everything in by reference, right?

Check $topicOptions['id'] on return -- even in SMF. It HAS to know the topic id in any case, if it's a reply, you provide it, even in a new topic, you have to have it to tie the new message to the new topic.

So, not sure what you're asking for that it doesn't already do...
:wow:

You are right...

So, sorry for the topic and... Thank you Arantor! :)
Quote from Nao/Gilles on May 12th, 2011, 10:31 AM
Still, I added a create_post_after hook in Wedge
:yahoo: :cool:

Nao

  • Dadman with a boy
  • Posts: 16,079

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278
Re: Function createPost improvement
« Reply #11, on May 12th, 2011, 04:35 PM »
I can't remember off hand, but do I take it then that there are two hooks on post creation, one for new topics and one for any new posts? (There was a pre-existing on-new-topic hook that's been in SMF since forever)

Nao

  • Dadman with a boy
  • Posts: 16,079
Re: Function createPost improvement
« Reply #12, on May 12th, 2011, 06:07 PM »
Nope.
- post creation before (topics and posts alike.)
- post creation after
- post modification before
- post modification after

Arantor

  • As powerful as possible, as complex as necessary.
  • Posts: 14,278

Nao

  • Dadman with a boy
  • Posts: 16,079